diff options
author | Brian Chu <brianmchu42@gmail.com> | 2022-02-21 00:11:06 -0800 |
---|---|---|
committer | Brian Chu <brianmchu42@gmail.com> | 2022-02-21 00:11:06 -0800 |
commit | 0a4fe70d367f0bf1a78602af600052f58a377c34 (patch) | |
tree | 69c2bcd2b54b48968fd52728aaf0e21ca154784c /day21.py | |
parent | 1e2642d8793e6a4fb6cba16cd651d5fdca3e4581 (diff) | |
download | AdventOfCode2017-0a4fe70d367f0bf1a78602af600052f58a377c34.tar.gz |
solutions to day 25 main
Diffstat (limited to 'day21.py')
-rw-r--r-- | day21.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/day21.py b/day21.py new file mode 100644 index 0000000..5904366 --- /dev/null +++ b/day21.py @@ -0,0 +1,47 @@ +import numpy as np + + +with open('day21.txt') as f: + instructions = f.readlines() + +mappings = {} +start = '.#./..#/###' + + +def translate_to_np(s): + return np.array([[c == '#' for c in l] + for l in s.split('/')]) + +for line in instructions: + k, v = map(translate_to_np, line.strip().split(' => ')) + for a in (k, np.fliplr(k)): + for r in range(4): + mappings[np.rot90(a, r).tobytes()] = v + + +def enhance(grid): + size = len(grid) + by = 2 if size % 2 == 0 else 3 + resize = lambda x: x * (by+1) // by + new_size = resize(size) + solution = np.empty((new_size, new_size), dtype=bool) + squares = range(0, size, by) + new_squares = range(0, new_size, by+1) + + for i, ni in zip(squares, new_squares): + for j, nj in zip(squares, new_squares): + square = grid[i:i+by, j:j+by] + enhanced = mappings[square.tobytes()] + solution[ni:ni+by+1, nj:nj+by+1] = enhanced + return solution + +def solve(part): + grid = translate_to_np(start) + iterations = 5 if part == 1 else 18 + for _ in range(iterations): + grid = enhance(grid) + return int(grid.sum()) + + +print(solve(part=1)) +print(solve(part=2)) |