Compare commits
2 Commits
0ca6d110e3
...
63765cedbe
| Author | SHA1 | Date | |
|---|---|---|---|
| 63765cedbe | |||
| fbe12ad205 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1 @@
|
|||||||
*/__pycache__/*
|
**/__pycache__/*
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ def taskB (data) -> int:
|
|||||||
gameScoreB = 0
|
gameScoreB = 0
|
||||||
for report in data:
|
for report in data:
|
||||||
reportData = [int(data) for data in report.split(' ')]
|
reportData = [int(data) for data in report.split(' ')]
|
||||||
gameScoreB = gameScoreB +any([isCorrect(reportData[:i] + reportData[i + 1:]) for i in range(len(reportData))])
|
gameScoreB = gameScoreB + any([isCorrect(reportData[:i] + reportData[i + 1:]) for i in range(len(reportData))])
|
||||||
return gameScoreB
|
return gameScoreB
|
||||||
|
|
||||||
def task(task: int,data) -> int:
|
def task(task: int,data) -> int:
|
||||||
|
|||||||
@@ -17,10 +17,34 @@ filename = getFilename2Data(aocDay)
|
|||||||
def checkNextChar(inputChar):
|
def checkNextChar(inputChar):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def countXMAS2(data):
|
def countXMAS3(data):
|
||||||
#ret = re2.findall("M.M\n.A.\nS.S", data, rotate=True)
|
#ret = re2.findall("M.M\n.A.\nS.S", data, rotate=True)
|
||||||
|
|
||||||
|
|
||||||
|
for i in range(n):
|
||||||
|
for j in range(n):
|
||||||
|
if word_search[i][j] != 'A':
|
||||||
|
continue
|
||||||
|
if not is_inbounds(i+1, j+1):
|
||||||
|
continue
|
||||||
|
if not is_inbounds(i+1, j-1):
|
||||||
|
continue
|
||||||
|
if not is_inbounds(i-1, j+1):
|
||||||
|
continue
|
||||||
|
if not is_inbounds(i-1, j-1):
|
||||||
|
continue
|
||||||
|
if not (word_search[i-1][j-1], word_search[i+1][j+1]) in (('M', 'S'), ('S', 'M')):
|
||||||
|
continue
|
||||||
|
if not (word_search[i+1][j-1], word_search[i-1][j+1]) in (('M', 'S'), ('S', 'M')):
|
||||||
|
continue
|
||||||
|
result += 1
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
def countXMAS2(dataMatrix,searchWord):
|
||||||
|
for x in range(len(dataMatrix)):
|
||||||
|
for y in range(len(dataMatrix[x])):
|
||||||
|
if dataMatrix[x][y] == searchWord[1:2]:
|
||||||
|
|
||||||
|
|
||||||
def countXMAS(dataMatrix,searchWord):
|
def countXMAS(dataMatrix,searchWord):
|
||||||
|
|||||||
44
2024/day04a.py
Normal file
44
2024/day04a.py
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
word_search = []
|
||||||
|
with open('/home/bandeira/git.bandeira/aoc/2024/day04/input.day04', 'r') as f:
|
||||||
|
for line in f.readlines():
|
||||||
|
word_search.append(list(line.strip()))
|
||||||
|
n = len(word_search)
|
||||||
|
|
||||||
|
def is_inbounds(i, j):
|
||||||
|
return 0 <= i < n and 0 <= j < n
|
||||||
|
|
||||||
|
# part 1
|
||||||
|
result = 0
|
||||||
|
for i in range(n):
|
||||||
|
for j in range(n):
|
||||||
|
if word_search[i][j] != 'X':
|
||||||
|
continue
|
||||||
|
for di in [-1, 0, 1]:
|
||||||
|
for dj in [-1, 0, 1]:
|
||||||
|
if (di, dj) == (0, 0):
|
||||||
|
continue
|
||||||
|
if is_inbounds(i+3*di, j+3*dj):
|
||||||
|
if ''.join(word_search[i+k*di][j+k*dj] for k in range(4)) == 'XMAS':
|
||||||
|
result += 1
|
||||||
|
print(result)
|
||||||
|
|
||||||
|
# part 2
|
||||||
|
result = 0
|
||||||
|
for i in range(n):
|
||||||
|
for j in range(n):
|
||||||
|
if word_search[i][j] != 'A':
|
||||||
|
continue
|
||||||
|
if not is_inbounds(i+1, j+1):
|
||||||
|
continue
|
||||||
|
if not is_inbounds(i+1, j-1):
|
||||||
|
continue
|
||||||
|
if not is_inbounds(i-1, j+1):
|
||||||
|
continue
|
||||||
|
if not is_inbounds(i-1, j-1):
|
||||||
|
continue
|
||||||
|
if not (word_search[i-1][j-1], word_search[i+1][j+1]) in (('M', 'S'), ('S', 'M')):
|
||||||
|
continue
|
||||||
|
if not (word_search[i+1][j-1], word_search[i-1][j+1]) in (('M', 'S'), ('S', 'M')):
|
||||||
|
continue
|
||||||
|
result += 1
|
||||||
|
print(result)
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
import sys,time
|
import sys,time
|
||||||
from helpingFunctions import *
|
from helpingFunctions import *
|
||||||
|
|
||||||
setSampleMode(False)
|
setSampleMode(True)
|
||||||
|
|
||||||
aocDay = identifyDay(sys.argv[0])
|
aocDay = identifyDay(sys.argv[0])
|
||||||
aocYear = identifyYear(sys.argv[0])
|
aocYear = identifyYear(sys.argv[0])
|
||||||
@@ -14,8 +14,61 @@ aocYear = identifyYear(sys.argv[0])
|
|||||||
path = getPath2Data(aocDay,aocYear)
|
path = getPath2Data(aocDay,aocYear)
|
||||||
filename = getFilename2Data(aocDay)
|
filename = getFilename2Data(aocDay)
|
||||||
|
|
||||||
|
def extractData(data):
|
||||||
|
pageOrdRule=[]
|
||||||
|
updQueue=[]
|
||||||
|
dataBreak = False
|
||||||
|
for dataLine in data:
|
||||||
|
if len(dataLine) == 0:
|
||||||
|
dataBreak = True
|
||||||
|
elif dataBreak == False:
|
||||||
|
x,y = dataLine.split("|")
|
||||||
|
pageOrdRule.append([int(x),int(y)])
|
||||||
|
elif dataBreak== True:
|
||||||
|
updQueueEntry=[]
|
||||||
|
for dataInput in dataLine.split(','):
|
||||||
|
updQueueEntry.append(int(dataInput))
|
||||||
|
updQueue.append(updQueueEntry)
|
||||||
|
return [pageOrdRule, updQueue]
|
||||||
|
|
||||||
|
def findPageOrdRules(pageOrdRule,inputValue):
|
||||||
|
pageRules=[]
|
||||||
|
for pageRule in pageOrdRule:
|
||||||
|
if pageRule[0]==inputValue or pageRule[1]==inputValue:
|
||||||
|
pageRules.append(pageRule)
|
||||||
|
return pageRules
|
||||||
|
|
||||||
|
def checkRule(queueValue,queueValueNext,pageRules):
|
||||||
|
ret= False
|
||||||
|
for pageRule in pageRules:
|
||||||
|
if queueValue == pageRule[0] and queueValueNext == pageRule[1]:
|
||||||
|
ret = True
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
def validateQueue(queueList,pageRules,queuePos):
|
||||||
|
ret = False
|
||||||
|
|
||||||
|
queueValue = queueList[queuePos]
|
||||||
|
queueValueNext = queueList[queuePos+1]
|
||||||
|
|
||||||
|
for i in range(queuePos+1,len(queueList)):
|
||||||
|
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def getOrderedQueues(pageOrdRule, updQueue):
|
||||||
|
orderedQueues=[]
|
||||||
|
for queueList in updQueue:
|
||||||
|
for queuePos in range(len(queueList)-1):
|
||||||
|
pageRules = findPageOrdRules(pageOrdRule,queueList[queuePos])
|
||||||
|
validQueue = validateQueueValue(queueList,pageRules,queuePos)
|
||||||
|
return 0
|
||||||
|
|
||||||
def taskA (data) -> int:
|
def taskA (data) -> int:
|
||||||
gameScoreA = 0
|
gameScoreA = 0
|
||||||
|
pageOrdRule, updQueue = extractData(data)
|
||||||
|
res = getOrderedQueues(pageOrdRule, updQueue)
|
||||||
return gameScoreA
|
return gameScoreA
|
||||||
|
|
||||||
def taskB (data) -> int:
|
def taskB (data) -> int:
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,28 @@
|
|||||||
|
47|53
|
||||||
|
97|13
|
||||||
|
97|61
|
||||||
|
97|47
|
||||||
|
75|29
|
||||||
|
61|13
|
||||||
|
75|53
|
||||||
|
29|13
|
||||||
|
97|29
|
||||||
|
53|29
|
||||||
|
61|53
|
||||||
|
97|53
|
||||||
|
61|29
|
||||||
|
47|13
|
||||||
|
75|47
|
||||||
|
97|75
|
||||||
|
47|61
|
||||||
|
75|61
|
||||||
|
47|29
|
||||||
|
75|13
|
||||||
|
53|13
|
||||||
|
|
||||||
|
75,47,61,53,29
|
||||||
|
97,61,53,29,13
|
||||||
|
75,29,13
|
||||||
|
75,97,47,61,53
|
||||||
|
61,13,29
|
||||||
|
97,13,75,29,47
|
||||||
20
2024/day05a.py
Normal file
20
2024/day05a.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
input = [line.strip() for line in open('/home/bandeira/git.bandeira/aoc/2024/day05/input.day05') if line.strip()]
|
||||||
|
|
||||||
|
rules = defaultdict(set)
|
||||||
|
for line in input:
|
||||||
|
if '|' in line:
|
||||||
|
a, b = map(int, line.split('|'))
|
||||||
|
rules[a].add(b)
|
||||||
|
|
||||||
|
updates = [list(map(int, line.split(','))) for line in input if ',' in line]
|
||||||
|
|
||||||
|
is_correct = lambda u: all(not (set(u[:i]) & rules[u[i]]) for i in range(len(u)))
|
||||||
|
|
||||||
|
ans1 = sum(u[len(u) // 2] for u in updates if is_correct(u))
|
||||||
|
|
||||||
|
ans2 = sum(next(n for n in update if len(rules[n] & set(update)) == len(update) // 2)
|
||||||
|
for update in updates if not is_correct(update))
|
||||||
|
|
||||||
|
print(f"{ans1}\n{ans2}")
|
||||||
@@ -13,13 +13,82 @@ aocYear = identifyYear(sys.argv[0])
|
|||||||
|
|
||||||
path = getPath2Data(aocDay,aocYear)
|
path = getPath2Data(aocDay,aocYear)
|
||||||
filename = getFilename2Data(aocDay)
|
filename = getFilename2Data(aocDay)
|
||||||
|
|
||||||
|
posGuard=(0,0)
|
||||||
|
mapGrid={}
|
||||||
|
|
||||||
|
def createMapGrid(data):
|
||||||
|
posY=0
|
||||||
|
global posGuard
|
||||||
|
global mapGrid
|
||||||
|
for dataLine in data:
|
||||||
|
posX = 0
|
||||||
|
for dataPoint in dataLine:
|
||||||
|
if dataPoint == '^':
|
||||||
|
dataPoint = '.'
|
||||||
|
posGuard=(posX,posY)
|
||||||
|
mapGrid[posX,posY] = dataPoint
|
||||||
|
posX += 1
|
||||||
|
posY += 1
|
||||||
|
return (posX,posY)
|
||||||
|
|
||||||
|
def detectNoCollition(startPoint,moveDirection):
|
||||||
|
try:
|
||||||
|
if moveDirection == 'N' and mapGrid[startPoint[0],startPoint[1]-1] == '#': return True
|
||||||
|
elif moveDirection == 'S' and mapGrid[startPoint[0],startPoint[1]+1] == '#': return True
|
||||||
|
elif moveDirection == 'W' and mapGrid[startPoint[0]-1,startPoint[1]] == '#': return True
|
||||||
|
elif moveDirection == 'O' and mapGrid[startPoint[0]+1,startPoint[1]] == '#': return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def rotateGuard(moveDirection):
|
||||||
|
if (moveDirection == 'N'):
|
||||||
|
return 'O'
|
||||||
|
elif (moveDirection == 'O'):
|
||||||
|
return 'S'
|
||||||
|
elif (moveDirection == 'S'):
|
||||||
|
return 'W'
|
||||||
|
elif (moveDirection == 'W'):
|
||||||
|
return 'N'
|
||||||
|
|
||||||
|
def moveGuard(startPoint,moveDirection):
|
||||||
|
if detectNoCollition(startPoint,moveDirection):
|
||||||
|
moveDirection = rotateGuard(moveDirection)
|
||||||
|
global mapGrid
|
||||||
|
mapGrid[startPoint[0],startPoint[1]] = '*'
|
||||||
|
if moveDirection == 'N':
|
||||||
|
newPoint=(startPoint[0],startPoint[1]-1)
|
||||||
|
elif moveDirection == 'S':
|
||||||
|
newPoint=(startPoint[0],startPoint[1]+1)
|
||||||
|
elif moveDirection == 'W':
|
||||||
|
newPoint=(startPoint[0]-1,startPoint[1])
|
||||||
|
elif moveDirection == 'O':
|
||||||
|
newPoint=(startPoint[0]+1,startPoint[1])
|
||||||
|
return (newPoint,moveDirection)
|
||||||
|
|
||||||
def taskA (data) -> int:
|
def taskA (data) -> int:
|
||||||
|
global posGuard
|
||||||
|
global mapGrid
|
||||||
gameScoreA = 0
|
gameScoreA = 0
|
||||||
|
posX,posY = createMapGrid(data)
|
||||||
|
moveDirection='N'
|
||||||
|
|
||||||
|
while True:
|
||||||
|
posGuard,moveDirection = moveGuard(posGuard,moveDirection)
|
||||||
|
if posGuard[0]>posX or posGuard[0]<0 or posGuard[1]>posY or posGuard[1]<0:
|
||||||
|
break
|
||||||
|
mapGridList = list(mapGrid.values())
|
||||||
|
gameScoreA = mapGridList.count('*')-1
|
||||||
return gameScoreA
|
return gameScoreA
|
||||||
|
|
||||||
def taskB (data) -> int:
|
def taskB (data) -> int:
|
||||||
|
global posGuard
|
||||||
|
global mapGrid
|
||||||
gameScoreB = 0
|
gameScoreB = 0
|
||||||
|
posX,posY = createMapGrid(data)
|
||||||
|
moveDirection='N'
|
||||||
return gameScoreB
|
return gameScoreB
|
||||||
|
|
||||||
def task(task: int,data) -> int:
|
def task(task: int,data) -> int:
|
||||||
|
|||||||
@@ -0,0 +1,130 @@
|
|||||||
|
.....#..#................#...#.....#.......................................................#.............................##.......
|
||||||
|
......................#..............................................................#..............#..........................#..
|
||||||
|
.........#........................#.....................................................#..............#...........#........##....
|
||||||
|
..........#......................#.....#...#............#..........................#.....#........................................
|
||||||
|
#....................................................................................................................#............
|
||||||
|
.#....#......................#.......................#...............................#...#.....#...................#........#.....
|
||||||
|
..#..#.......................##........#...............................................................#........#.........#.......
|
||||||
|
..............................................................#...#.........#..#..................................................
|
||||||
|
.............####..................................#................#..#.....................................#.....#..............
|
||||||
|
..........#................#................................................#........................#.....#......................
|
||||||
|
..............................................#.........#.....................#..................#.......................#........
|
||||||
|
.........................................##..#.................................#.....#............................................
|
||||||
|
............#..#...#...............................#.#.....#...............#...............................#..............#.#.#..#
|
||||||
|
.................#..........#..#....#.....................#......................................##............#..................
|
||||||
|
.......#....#.....................#......##...................#..............#.................#........#..#......#...............
|
||||||
|
...............................................#.............#....................................................................
|
||||||
|
..............#..............................................##..................#..........................#............#........
|
||||||
|
................#.......................#..#............................................................#..........#..#.........#.
|
||||||
|
.#.......................###............#.........#.................................................#.............................
|
||||||
|
........#......................................................................................#.....#.......#...#................
|
||||||
|
#.#.........................#..#.............................................#....#........................#......................
|
||||||
|
.......#.....#...............................#.....#......................#.................................#.....#...#...........
|
||||||
|
................................................................................................#............#....................
|
||||||
|
.......#......#...............#...#............................#................................#................................#
|
||||||
|
.#.....................................................................................................................#......#...
|
||||||
|
...........#..........................................#........#...................#..............................................
|
||||||
|
....#......................................................................................#.....##.#...................#.........
|
||||||
|
...#..........................................................................##.......................................#.....#....
|
||||||
|
#........#................#...............#.........#..#.#..............##..............#........#............#....#.....#........
|
||||||
|
......#......................................#...............#....#..#...........................#................................
|
||||||
|
#..............................................................................#............#...#..#....................#......#..
|
||||||
|
.......#.........................................#.........#.......#....................................#.........................
|
||||||
|
......#......#.............................................#.....................#...........#.............#..#...................
|
||||||
|
#.......................#........#............................................##..............................#...................
|
||||||
|
..........................................................................#........................................#....#......#..
|
||||||
|
..........#..#...............................................#............................#.#..............................#......
|
||||||
|
....#...........#..#...................................#................................................................#.........
|
||||||
|
..............#..........#...............#....#.....................................#.......................#.....................
|
||||||
|
.............#........#....#.........#...............#.........#..................................................................
|
||||||
|
.#......#...................#.......#...........................................................................#.............#...
|
||||||
|
.............##...........#.........................................#......................#.......................#..............
|
||||||
|
.....#................#.....#...................##.......#......#...........#.......#............................................#
|
||||||
|
.#..#.............................................................#..............................................#................
|
||||||
|
...#......#............................#..........................................................................................
|
||||||
|
...................................................................................#..............................................
|
||||||
|
#...................................#...................##..................#...............................................#.....
|
||||||
|
................#.....#...........................#...............................................................................
|
||||||
|
.................................#............#........................................................#.............#............
|
||||||
|
..................#......#........................................................#..^.....#......................................
|
||||||
|
..#.....#.................................................#........#.........................................................#..#.
|
||||||
|
................#..............................#....................................................#.............................
|
||||||
|
................#....#..........................................#............................#....................................
|
||||||
|
...............#.......................................#.....................................#..............................#.....
|
||||||
|
..................................................#............#..............#........##.#.....................................#.
|
||||||
|
.#..................................#......#..#............#.................#.............#......#..#.......................#....
|
||||||
|
..................#..............................#..........................#..........................##............#.......#....
|
||||||
|
.........#..................................................................#...................#..........................#......
|
||||||
|
................#....................................#............#............................................................#..
|
||||||
|
..##..........#.#..#........................#....#.................................#.............................................#
|
||||||
|
............................................#.........................................................#..#...................#...#
|
||||||
|
.......................................#......................................................................#...................
|
||||||
|
....#.......................#...#.........................................................................#................#......
|
||||||
|
...........#...........#............#.....#...#...................#........................................#.................##...
|
||||||
|
....#...........................................................................................#.................................
|
||||||
|
..............................................................................#.#.............................................#...
|
||||||
|
.........#..#..............................................#......................................................................
|
||||||
|
#...................#........#............#.......#.....................#................#.#.#............#..................#....
|
||||||
|
.......................#................#.....#...........#.....................#...........................#..........#.........#
|
||||||
|
........................................................................#.#.....................#.................................
|
||||||
|
...#.................................................#..........................#........#.......................#................
|
||||||
|
............................#...........#.......#.................................#.##...........#............................#...
|
||||||
|
...##.....................................#.........................#...........#...................................#.............
|
||||||
|
#.#..#................................#.........#..#.#..........#......................#............#......................#......
|
||||||
|
....#.............................................................#...............................................................
|
||||||
|
...............#.#......#........................................................................#.........................#......
|
||||||
|
................#.....#...#...............#.........................................#....#...........................#.......#....
|
||||||
|
...............#.........................................................................#........................................
|
||||||
|
..........................#..##...................................................................................................
|
||||||
|
.##....................#..#.....................#..............................................#........................#.........
|
||||||
|
............................................................................................#............#........................
|
||||||
|
#...................#...#......................#...............#............#....................................#................
|
||||||
|
...........................#........#......................................................................#......................
|
||||||
|
................................#...#...#.#.......#.................#.......#................##...................................
|
||||||
|
........................#.......................#.................................................................................
|
||||||
|
#...#...............#.........................................................................#.......................#...........
|
||||||
|
.........#...................#...............................................#...................##..............................#
|
||||||
|
.....#............................................................................................................................
|
||||||
|
.#.......#...............................................................#.......................#................#..........#....
|
||||||
|
....................................#..........#......#...........................................................#.#.#..#...#....
|
||||||
|
......................#..............................................##..................#........................................
|
||||||
|
....#......#...................#................#..............................................................................#..
|
||||||
|
...........#.................##.#...................................................#..#................#.........................
|
||||||
|
......#.....#......#...#........#.....................................#....#...........#................#..........#..............
|
||||||
|
#.......##.....#........#...........................................#...#..........#.............#........#.......#...............
|
||||||
|
......................#......#...................#.........................#........................#..................#...#......
|
||||||
|
.....#...........#...#.........................##.......#.....................................#...................................
|
||||||
|
....................................................................#.............................................................
|
||||||
|
........#...............#.........................................................................................................
|
||||||
|
.......................#.........................#..........................................#.#....#..#....#......................
|
||||||
|
........................##............................................................#.....#.........#....#......#..#............
|
||||||
|
........................#........#..................#................#.#.##............#..........................................
|
||||||
|
.......................................................#.........................#...............##...................##....#...#.
|
||||||
|
........#...#.............#...........#........#......................#...#.........#..#..................#.#............#........
|
||||||
|
#.#....................#..........................................#...................................................#........#..
|
||||||
|
...............................................#............#.....#.#...........#........#.#....................................#.
|
||||||
|
......#..........................................#............................................................#..........#...#...#
|
||||||
|
....................#......................................#....#..............................#....................#.............
|
||||||
|
......................#.#.....#......#....#..#....................................................................................
|
||||||
|
.....#..........................................#.........#...................................#................#.......#..........
|
||||||
|
........##....................................................................#...................................#.............#.
|
||||||
|
#.........................................#..#....#................#.#.....#....................#............................#....
|
||||||
|
#....#..#..........#..................#......................#....................................................................
|
||||||
|
...................#..#....#...................#...........................#.#..#............#......#................#.......#.#..
|
||||||
|
...............#......................................................#......#..#..............#.....................#............
|
||||||
|
............#.....................#.........#....#...............#.......#...#...............#.......................##.#.........
|
||||||
|
....#............#..................#....#.......................#..............................................#...#.............
|
||||||
|
..........................................#....#..........#..#....................#......#......................#...#.............
|
||||||
|
............#..........#...........#......#..#......................................................................#.............
|
||||||
|
...#.................#.............................................................#..............................................
|
||||||
|
.....#.#.........#....................................................#..................#...............#........................
|
||||||
|
..#......................##.....#.........#..............#..#............#..#............................................#........
|
||||||
|
...................#.....#.........#........#....................#..................................#................#............
|
||||||
|
....#.............................#.............................#.......................#.......................#.....#...........
|
||||||
|
...#......#....#...........#............................#.....#........#......#...................................................
|
||||||
|
........#..........#............................#..#..#.......................................#....#.##...........................
|
||||||
|
.......#......##........#.................#.............................................................#....#........#...........
|
||||||
|
.....................#............................................#.#......#.....##....#........#.................................
|
||||||
|
...........#.....................##.#....#..#.....................................................................................
|
||||||
|
....................#......#................................#.....................................................#.......#..#....
|
||||||
|
.....................................................#.........#.......................................#.....##..#................
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
....#.....
|
||||||
|
.........#
|
||||||
|
..........
|
||||||
|
..#.......
|
||||||
|
.......#..
|
||||||
|
..........
|
||||||
|
.#..^.....
|
||||||
|
........#.
|
||||||
|
#.........
|
||||||
|
......#...
|
||||||
56
2024/day06a.py
Normal file
56
2024/day06a.py
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
with open('/home/bandeira/git.bandeira/aoc/2024/day06/sample.day06', 'r') as f:
|
||||||
|
input_grid = f.read().split('\n')
|
||||||
|
|
||||||
|
UP, LEFT, DOWN, RIGHT = 1, 2, 3, 4
|
||||||
|
rows = len(input_grid)
|
||||||
|
cols = len(input_grid[0])
|
||||||
|
obstacles = set()
|
||||||
|
for i, r in enumerate(input_grid):
|
||||||
|
for j, c in enumerate(r):
|
||||||
|
if c == '#':
|
||||||
|
obstacles.add((i,j))
|
||||||
|
elif c == '^':
|
||||||
|
guard_start = (i, j, UP)
|
||||||
|
|
||||||
|
total = 0
|
||||||
|
for r in range(rows):
|
||||||
|
for c in range(cols):
|
||||||
|
if (r,c) in obstacles:
|
||||||
|
continue
|
||||||
|
|
||||||
|
obstacles.add((r,c))
|
||||||
|
visited_states = {guard_start}
|
||||||
|
guard = guard_start
|
||||||
|
while True:
|
||||||
|
if guard[2] == UP:
|
||||||
|
if (guard[0] - 1, guard[1]) in obstacles:
|
||||||
|
guard = (guard[0], guard[1], RIGHT)
|
||||||
|
else:
|
||||||
|
guard = (guard[0] - 1, guard[1], guard[2])
|
||||||
|
elif guard[2] == RIGHT:
|
||||||
|
if (guard[0], guard[1] + 1) in obstacles:
|
||||||
|
guard = (guard[0], guard[1], DOWN)
|
||||||
|
else:
|
||||||
|
guard = (guard[0], guard[1] + 1, guard[2])
|
||||||
|
elif guard[2] == DOWN:
|
||||||
|
if (guard[0] + 1, guard[1]) in obstacles:
|
||||||
|
guard = (guard[0], guard[1], LEFT)
|
||||||
|
else:
|
||||||
|
guard = (guard[0] + 1, guard[1], guard[2])
|
||||||
|
else:
|
||||||
|
if (guard[0], guard[1] - 1) in obstacles:
|
||||||
|
guard = (guard[0], guard[1], UP)
|
||||||
|
else:
|
||||||
|
guard = (guard[0], guard[1] - 1, guard[2])
|
||||||
|
|
||||||
|
if guard[0] < 0 or guard[0] >= rows or guard[1] < 0 \
|
||||||
|
or guard[1] >= cols:
|
||||||
|
obstacles.remove((r,c))
|
||||||
|
break
|
||||||
|
elif guard in visited_states:
|
||||||
|
total += 1
|
||||||
|
obstacles.remove((r,c))
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
visited_states.add(guard)
|
||||||
|
print(total)
|
||||||
Reference in New Issue
Block a user