From d1403d0e2567f3b580b6b26d302dd324fac1a2c9 Mon Sep 17 00:00:00 2001 From: Lachlan Meyer Date: Mon, 16 Dec 2024 18:08:25 +1100 Subject: [PATCH] 15-2 --- 2024/15/15-2.py | 111 ++++++++++++++++++++++++++++++++++++ 2024/15/testinputsmall2.txt | 9 +++ 2 files changed, 120 insertions(+) create mode 100644 2024/15/15-2.py create mode 100644 2024/15/testinputsmall2.txt diff --git a/2024/15/15-2.py b/2024/15/15-2.py new file mode 100644 index 0000000..44419f1 --- /dev/null +++ b/2024/15/15-2.py @@ -0,0 +1,111 @@ +import functools + +map = {} +moves = [] +currentPosition = startPosition = (-1, -1) +width = height = 0 +total = 0 + +def calcGpsCoords(): + global total + for i in range(height): + for j in range(width): + if map[i,j] == '[': + total += i * 100 + j + +def printMap(): + output = "" + for i in range(height): + line = "" + for j in range(width): + line += map[i,j] + output += line + "\n" + print(output) + +@functools.cache +def getDirectionCoords(direction): + match direction: + case '^': + return 0, -1 + case 'v': + return 0, 1 + case '<': + return -1, 0 + case '>': + return 1, 0 + case _: + raise Exception("bruh") + +def tryMoveInDirection(startX, startY, direction, allowMove = False): + global currentPosition + # see if there's something in front, try move that (recursively) + # if there's a wall in front, return False + # move up if all clear, return True + + char = map[startY, startX] + + dirX, dirY = getDirectionCoords(direction) + nextX, nextY = startX + dirX, startY + dirY + nextSpot = map[nextY, nextX] + if nextSpot == '#': + return False + + if nextSpot == '[': + # it's the left of a box, try move it and the right side and all subsequent boxes + if dirX == 0: + if not (tryMoveInDirection(nextX, nextY, direction, allowMove) and tryMoveInDirection(nextX+1, nextY, direction, allowMove)): + return False + elif not tryMoveInDirection(nextX, nextY, direction, allowMove): + return False + elif nextSpot == ']': + # it's the right of a box, try move it and the left side and all subsequent boxes + if dirX == 0: + if not (tryMoveInDirection(nextX, nextY, direction, allowMove) and tryMoveInDirection(nextX-1, nextY, direction, allowMove)): + return False + elif not tryMoveInDirection(nextX, nextY, direction, allowMove): + return False + + # the robot and box(es) could move without hitting a wall + if allowMove: + map[nextY, nextX] = char + map[startY, startX] = '.' + currentPosition = (nextX, nextY) + return True + + + +with open("2024/15/input.txt") as f: + writingMap = True + for i, line in enumerate(f): + line = line.strip() + if i == 0: + height = int(len(line)) + width = height * 2 + + if len(line) == 0: + writingMap = False + length = len(line) + for j, char in enumerate(line): + if writingMap: + if char == '@': + currentPosition = startPosition = (j*2, i) + map[i,j*2] = '@' + map[i,j*2+1] = '.' + elif char == 'O': + map[i,j*2] = '[' + map[i,j*2+1] = ']' + else: + map[i,j*2] = char + map[i,j*2+1] = char + + else: + moves.append(char) + + printMap() + + for direction in moves: + if tryMoveInDirection(currentPosition[0], currentPosition[1], direction): + tryMoveInDirection(currentPosition[0], currentPosition[1], direction, True) + + calcGpsCoords() + print(total) \ No newline at end of file diff --git a/2024/15/testinputsmall2.txt b/2024/15/testinputsmall2.txt new file mode 100644 index 0000000..dfe6a69 --- /dev/null +++ b/2024/15/testinputsmall2.txt @@ -0,0 +1,9 @@ +####### +#...#.# +#.....# +#..OO@# +#..O..# +#.....# +####### + +