advent-of-code/2024-python/output/day_12.py
Anders Englöf Ytterström 1e807b5daf Solve 2024:13 p1-2 "Claw Contraption"
Initial version of the code tried to solve pt 1
using BFS, which took way too long even for the
test data.

After some fiddling with algebra with pen and paper,
I realized this 2 formulaes (using first example):

94 * b1 + 22 * b2 == 840
34 * b1 + 67 * b2 == 540

*b1 = button A presses
*b2 = button B presses

... could be rewritten to this single expression:

(94 + 34) * b1 + (22 + 67) * b2 = 840 * 540

I failed to remember the algebra for solving x than
y though, that I had to learn from the subreddit.
In the code, this is the ratio part.

Also, this solution using fractions is SICK.
https://www.reddit.com/r/adventofcode/comments/1hd4wda/comment/m1tz3nf/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
2025-01-05 00:06:18 +01:00

88 lines
2.4 KiB
Python

import re
from collections import Counter, defaultdict, deque
from heapq import heappop, heappush
from itertools import chain, combinations, compress, permutations
from output import ADJ, DD, D, ccw, cw, ints, matrix, mdbg, mhd, vdbg
def solve(data):
grid, H, W = matrix(data)
p1 = 0
seen = set()
for r, row in enumerate(grid):
for c, col in enumerate(row):
if (r, c) in seen:
continue
areas = 0
perimeters = 0
q = deque([(r, c)])
while q:
rc = q.popleft()
if rc in seen:
continue
seen.add(rc)
areas += 1
y, x = rc
for dy, dx in D:
if (0 <= y + dy < H and 0 <= x + dx < W) and grid[y + dy][
x + dx
] == col:
q.append((y + dy, x + dx))
else:
perimeters += 1
p1 += areas * perimeters
p2 = None
return p1, p2
if __name__ == "__main__":
import os
# use dummy data
inp = """
RRRRIICCFF
RRRRIICCCF
VVRRRCCFFF
VVRCCCJFFF
VVVVCJJCFE
VVIVCCJJEE
VVIIICJJEE
MIIIIIJJEE
MIIISIJEEE
MMMISSJEEE
""".strip()
"""
A region of R plants with price 12 * 18 = 216.
A region of I plants with price 4 * 8 = 32.
A region of C plants with price 14 * 28 = 392.
A region of F plants with price 10 * 18 = 180.
A region of V plants with price 13 * 20 = 260.
A region of J plants with price 11 * 20 = 220.
A region of C plants with price 1 * 4 = 4.
A region of E plants with price 13 * 18 = 234.
A region of I plants with price 14 * 22 = 308.
A region of M plants with price 5 * 12 = 60.
A region of S plants with price 3 * 8 = 24.
"""
# uncomment to instead use stdin
# import sys; inp = sys.stdin.read().strip()
# uncomment to use AoC provided puzzle input
with open("./input/12.txt", "r") as f:
inp = f.read().strip()
# uncomment to do initial data processing shared by part 1-2
p1, p2 = solve(inp)
print(p1)
os.system(f"echo {p1} | wl-copy")
# print(p2)
# os.system(f"echo {p2} | wl-copy")
# uncomment and replace 0 with actual output to refactor code
# and ensure nonbreaking changes
assert p1 == 1446042
# assert p2 == 0