aoc2017/day14.py
Stefan Harmuth 75d0aa0fc4 day14
2022-11-30 11:20:02 +01:00

52 lines
1.3 KiB
Python

from tools.coordinate import Coordinate
from tools.grid import Grid
from day10 import knot_hash
from tools.aoc import AOCDay
from typing import Any
class Day(AOCDay):
inputs = [
[
(8108, "input14_test"),
(8316, "input14")
],
[
(1242, "input14_test"),
(1074, "input14")
]
]
def part1(self) -> Any:
key_start = self.getInput().strip() + '-'
count = 0
for x in range(128):
hash = knot_hash(key_start + str(x))
bin_hash = bin(int(hash, 16))[2:].zfill(128)
count += bin_hash.count('1')
return count
def part2(self) -> Any:
key_start = self.getInput().strip() + '-'
grid = Grid()
for y in range(128):
hash = knot_hash(key_start + str(y))
bin_hash = bin(int(hash, 16))[2:].zfill(128)
for x, c in enumerate(bin_hash):
grid.set(Coordinate(x, y), c == '1')
count = 0
ignore_list = []
for c in grid.getActiveCells():
if c not in ignore_list:
count += 1
ignore_list += grid.getActiveRegion(c)
return count
if __name__ == '__main__':
day = Day(2017, 14)
day.run(verbose=True)