mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 12:50:32 +01:00
Implement 2019 day 16 part 1
This commit is contained in:
33
2019/aoc2019/day16.py
Normal file
33
2019/aoc2019/day16.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import math
|
||||
from typing import List, TextIO
|
||||
|
||||
import numpy # type: ignore
|
||||
|
||||
|
||||
def read_input(data: TextIO) -> List[int]:
|
||||
line = next(data).strip()
|
||||
|
||||
return [int(c) for c in line]
|
||||
|
||||
|
||||
def simulate(numbers: List[int]) -> str:
|
||||
numbers = numpy.array(numbers)
|
||||
pattern = numpy.array([0, 1, 0, -1], dtype=numpy.int)
|
||||
|
||||
matrix = numpy.zeros((len(numbers), len(numbers)), dtype=numpy.int)
|
||||
|
||||
for i in range(len(numbers)):
|
||||
base = numpy.repeat(pattern, i + 1)
|
||||
needed_repetitions = math.ceil((len(numbers) + 1) / len(base))
|
||||
matrix[i, :] = numpy.tile(base, needed_repetitions)[1:len(numbers) + 1]
|
||||
|
||||
for _ in range(100):
|
||||
numbers = numpy.abs(numpy.dot(matrix, numbers)) % 10
|
||||
|
||||
return ''.join(str(s) for s in numbers[:8])
|
||||
|
||||
|
||||
def part1(data: TextIO) -> str:
|
||||
numbers = read_input(data)
|
||||
|
||||
return simulate(numbers)
|
||||
15
2019/tests/day16.py
Normal file
15
2019/tests/day16.py
Normal file
@@ -0,0 +1,15 @@
|
||||
import pytest
|
||||
|
||||
from aoc2019.day16 import simulate
|
||||
|
||||
|
||||
@pytest.mark.parametrize('data,correct', [
|
||||
('80871224585914546619083218645595', '24176176'),
|
||||
('19617804207202209144916044189917', '73745418'),
|
||||
('69317163492948606335995924319873', '52432133'),
|
||||
])
|
||||
def test_sample_part1(data: str, correct: str):
|
||||
numbers = [int(c) for c in data]
|
||||
|
||||
assert simulate(numbers) == correct
|
||||
pass
|
||||
Reference in New Issue
Block a user