Roderic Day 5 vuotta sitten
vanhempi
commit
8299f793eb
2 muutettua tiedostoa jossa 56 lisäystä ja 0 poistoa
  1. +11
    -0
      toolkit.py
  2. +45
    -0
      y2019/p17.py

+ 11
- 0
toolkit.py Näytä tiedosto

@@ -1,3 +1,6 @@
import collections


def render(grid, brush):
if isinstance(brush, str):
brush = {i: c for i, c in enumerate(brush)}
@@ -26,3 +29,11 @@ def bsearch(fn, goal, lo, hi):
assert goal <= b, 'higher bound too low'

return lo


def read_image(text):
grid = collections.defaultdict(str)
for y, line in enumerate(text.splitlines()):
for x, cell in enumerate(line):
grid[complex(x, y)] = cell
return grid

+ 45
- 0
y2019/p17.py Näytä tiedosto

@@ -0,0 +1,45 @@
import re
import sys

from intcode import compute, parse

from toolkit import read_image


# part1
text = sys.stdin.read()
snapshot = ''.join(chr(val) for val in compute(text, 0))
grid = read_image(snapshot)
alignment_parameters = [
(pos.real, pos.imag)
for pos, val in grid.items()
if {grid.get(pos + d) for d in [0, 1, -1, 1j, -1j]} == {'#'}
]
print(int(sum(a * b for a, b in alignment_parameters)))

# find path
pos = {v: k for k, v in grid.items()}['^']
ori = -1j
path = []
while {grid[pos + ori * rot] for rot in [1, 1j, -1j]} != {'.'}:
if grid[pos + ori] == '#':
pos += ori
path[-1] += 1
else:
for c, rot in [('R', 1j), ('L', -1j)]:
if grid[pos + ori * rot] == '#':
ori *= rot
path += [c, 0]
full_path = ','.join(str(c) for c in path) + ','

# compress
regex = r'^(.{2,21})(?:\1)*(.{2,21})(?:\1|\2)*(.{2,21})(?:\1|\2|\3)*$'
A, B, C = [group.strip(',') for group in re.match(regex, full_path).groups()]
MAIN = full_path.replace(A, 'A').replace(B, 'B').replace(C, 'C').strip(',')

# part2
mem = parse(text)
mem[0] += 1
code = ('\n'.join([MAIN, A, B, C, 'n']) + '\n').encode()
output = ''.join(chr(val) for val in compute(mem, iter(code)))
print(ord(output[-1]))

Loading…
Peruuta
Tallenna