forked from optiver/advent-of-code-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.py
44 lines (28 loc) · 907 Bytes
/
day9.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import numpy as np
from numpy import typing
def _get_input(input_data: list[str]) -> typing.NDArray:
return np.array(
[[int(number) for number in line.split()] for line in input_data],
dtype=np.int32
)
def part_one(input_data: list[str]):
input_data = _get_input(input_data)
state = input_data[:, -1]
while not np.all(input_data == 0):
input_data = np.diff(input_data)
state += input_data[:, -1]
answer = state.sum()
return answer
def part_two(input_data: list[str]):
input_data = _get_input(input_data)[:, ::-1]
state = input_data[:, -1]
while not np.all(input_data == 0):
input_data = np.diff(input_data)
state += input_data[:, -1]
answer = state.sum()
return answer
if __name__ == '__main__':
with open("day9.txt") as f:
data = f.readlines()
part_one(data)
part_two(data)