commit 636dd73a95666d2dd37cb94905aa895c7bf00113 (patch)
parent 35efed10bdc5ea2ccdfbdb5a114d67b1e1e4af82
Author: Alex Karle <alex@alexkarle.com>
Date: Mon, 16 Dec 2024 03:03:28 +0100
2024: Add half of day 15
Its not that I can't do part 2.. I just kinda don't want to
Diffstat:
A | 2024/15/1.py | | | 89 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | 2024/15/2.py | | | 104 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 193 insertions(+), 0 deletions(-)
diff --git a/2024/15/1.py b/2024/15/1.py
@@ -0,0 +1,89 @@
+#!/usr/bin/env python3
+import sys
+import os
+
+def parse():
+ G = []
+ moves = []
+ rob = None
+ for l in sys.stdin:
+ if l[0] == "#":
+ G.append([c for c in l.strip()])
+ if l[0] in {">", "<", "^", "v"}:
+ moves += [c for c in l.strip()]
+ for i in range(len(G)):
+ for j in range(len(G[0])):
+ if G[i][j] == "@":
+ rob = (i, j)
+ return G, moves, rob
+
+def debug(msg):
+ if os.getenv("DEBUG"):
+ print(msg)
+
+def pprint(G):
+ for row in G:
+ debug("".join(row))
+
+velocity = {
+ "^": (-1, 0),
+ ">": (0, 1),
+ "v": (1, 0),
+ "<": (0, -1),
+}
+
+def get(G, pos):
+ if pos[0] < 0 or pos[0] > len(G) or pos[1] < 0 or pos[1] > len(G[0]):
+ return None
+ return G[pos[0]][pos[1]]
+
+def step(pos, move):
+ dy, dx = velocity[move]
+ return (pos[0] + dy, pos[1] + dx)
+
+def hasopen(G, move, rob):
+ curr = step(rob, move)
+ while get(G, curr):
+ if get(G, curr) == ".":
+ return True
+ if get(G, curr) == "#":
+ return False
+ curr = step(curr, move)
+ return False
+
+def move(G, move, rob):
+ dy, dx = velocity[move]
+ if not hasopen(G, move, rob):
+ return rob
+
+ # we have an open spot to shift into
+ curr = rob
+ cval = get(G, curr)
+ while True:
+ next = step(curr, move)
+ nval = get(G, next)
+ G[next[0]][next[1]] = cval
+ cval = nval
+ curr = next
+ if nval == ".":
+ break
+ G[rob[0]][rob[1]] = "."
+ return step(rob, move)
+
+def score(G):
+ s = 0
+ for i in range(len(G)):
+ for j in range(len(G[0])):
+ if G[i][j] == "O":
+ s += 100 * i + j
+ return s
+
+if __name__ == '__main__':
+ G, moves, rob = parse()
+ pprint(G)
+ for m in moves:
+ debug(f"Move {m}:")
+ rob = move(G, m, rob)
+ pprint(G)
+ debug("")
+ print(score(G))
diff --git a/2024/15/2.py b/2024/15/2.py
@@ -0,0 +1,104 @@
+#!/usr/bin/env python3
+import sys
+import os
+
+def dupe(c):
+ if c == "#":
+ return ["#", "#"]
+ if c == "O":
+ return ["[", "]"]
+ if c == "@":
+ return ["@", "."]
+ if c == ".":
+ return [".", "."]
+ else:
+ raise RuntimeException(f"Unknown char {c}")
+
+def parse():
+ G = []
+ moves = []
+ rob = None
+ for l in sys.stdin:
+ if l[0] == "#":
+ row = []
+ for c in l.strip():
+ row += dupe(c)
+ G.append(row)
+ if l[0] in {">", "<", "^", "v"}:
+ moves += [c for c in l.strip()]
+ for i in range(len(G)):
+ for j in range(len(G[0])):
+ if G[i][j] == "@":
+ rob = (i, j)
+ return G, moves, rob
+
+def debug(msg):
+ if os.getenv("DEBUG"):
+ print(msg)
+
+def pprint(G):
+ for row in G:
+ debug("".join(row))
+
+velocity = {
+ "^": (-1, 0),
+ ">": (0, 1),
+ "v": (1, 0),
+ "<": (0, -1),
+}
+
+def get(G, pos):
+ if pos[0] < 0 or pos[0] > len(G) or pos[1] < 0 or pos[1] > len(G[0]):
+ return None
+ return G[pos[0]][pos[1]]
+
+def step(pos, move):
+ dy, dx = velocity[move]
+ return (pos[0] + dy, pos[1] + dx)
+
+def hasopen(G, move, rob):
+ curr = step(rob, move)
+ while get(G, curr):
+ if get(G, curr) == ".":
+ return True
+ if get(G, curr) == "#":
+ return False
+ curr = step(curr, move)
+ return False
+
+def move(G, move, rob):
+ dy, dx = velocity[move]
+ if not hasopen(G, move, rob):
+ return rob
+
+ # we have an open spot to shift into
+ curr = rob
+ cval = get(G, curr)
+ while True:
+ next = step(curr, move)
+ nval = get(G, next)
+ G[next[0]][next[1]] = cval
+ cval = nval
+ curr = next
+ if nval == ".":
+ break
+ G[rob[0]][rob[1]] = "."
+ return step(rob, move)
+
+def score(G):
+ s = 0
+ for i in range(len(G)):
+ for j in range(len(G[0])):
+ if G[i][j] == "O":
+ s += 100 * i + j
+ return s
+
+if __name__ == '__main__':
+ G, moves, rob = parse()
+ pprint(G)
+ for m in moves:
+ debug(f"Move {m}:")
+ rob = move(G, m, rob)
+ pprint(G)
+ debug("")
+ print(score(G))