Add solution to problem 4
This commit is contained in:
71
Problem4_1.py
Normal file
71
Problem4_1.py
Normal file
@@ -0,0 +1,71 @@
|
||||
def checkRolls(x: int, y: int) :
|
||||
rolls = 0
|
||||
#!Check row above
|
||||
if y > 0 :
|
||||
#Check above left
|
||||
if x > 0 and inputGrid[y - 1][x - 1] == "@" :
|
||||
rolls += 1
|
||||
#Check directly above
|
||||
if inputGrid[y - 1][x] == "@" :
|
||||
rolls += 1
|
||||
#Check above right
|
||||
if x < len(inputGrid[y - 1]) - 1 and inputGrid[y - 1][x + 1] == "@" :
|
||||
rolls += 1
|
||||
#!Check same row
|
||||
#Check left
|
||||
if x > 0 and inputGrid[y][x - 1] == "@" :
|
||||
rolls += 1
|
||||
#Check right
|
||||
if x < len(inputGrid[y]) - 1 and inputGrid[y][x + 1] == "@":
|
||||
rolls += 1
|
||||
#!Check row below
|
||||
if y < len(inputGrid) - 1 :
|
||||
#Check below left
|
||||
if x > 0 and inputGrid[y + 1][x - 1] == "@" :
|
||||
rolls += 1
|
||||
#Check directly below
|
||||
if inputGrid[y + 1][x] == "@" :
|
||||
rolls += 1
|
||||
#Check below right
|
||||
if x < len(inputGrid[y]) - 1 and inputGrid[y + 1][x + 1] == "@" :
|
||||
rolls += 1
|
||||
return rolls
|
||||
|
||||
|
||||
testInput = [
|
||||
"..@@.@@@@.",
|
||||
"@@@.@.@.@@",
|
||||
"@@@@@.@.@@",
|
||||
"@.@@@@..@.",
|
||||
"@@.@@@@.@@",
|
||||
".@@@@@@@.@",
|
||||
".@.@.@.@@@",
|
||||
"@.@@@.@@@@",
|
||||
".@@@@@@@@.",
|
||||
"@.@.@@@.@."
|
||||
]
|
||||
|
||||
def readFile() :
|
||||
ary = []
|
||||
with open("files/Problem4.txt", "r") as file :
|
||||
for line in file :
|
||||
ary.append(line.replace("\n", ""))
|
||||
return ary
|
||||
|
||||
|
||||
#inputGrid = testInput
|
||||
inputGrid = readFile()
|
||||
accessibleRoles = 0
|
||||
x = 0
|
||||
y = 0
|
||||
for y in range(len(inputGrid)) :
|
||||
for x in range(len(inputGrid[y])) :
|
||||
if inputGrid[y][x] == "@" :
|
||||
rollsAround = checkRolls(x, y)
|
||||
#print(f"roll at ({x}, {y}) with {rollsAround} rolls around")
|
||||
if rollsAround < 4 :
|
||||
accessibleRoles += 1
|
||||
#print(f"Accessible roll at ({x}, {y}) with {rollsAround} rolls around")
|
||||
print(f"Total accessible roles = {accessibleRoles}")
|
||||
|
||||
#Total accessible roles = 1445
|
||||
Reference in New Issue
Block a user