-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.py
51 lines (31 loc) · 1.06 KB
/
day02.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
45
46
47
48
49
50
51
import re
import numpy as np
from aocd import data, puzzle
def parse(raw: str):
return [list(map(int, re.split(r'\s', line))) for line in raw.splitlines()]
def is_safe(report):
deltas = [(l - r) for (l, r) in zip(report, report[1:])]
s = np.sign(deltas[0])
return all(0 < abs(d) < 4 and np.sign(d) == s for d in deltas)
def solve(reports, safe_fn):
return sum(+safe_fn(report) for report in reports)
def part1(raw: str):
return solve(parse(raw), is_safe)
def is_safe_dampened(report):
if is_safe(report):
return True
for i in range(len(report)):
if is_safe(report[:i] + report[i + 1:]):
return True
return False
def part2(raw: str):
return solve(parse(raw), is_safe_dampened)
def test_part1_ex():
for ex in puzzle.examples:
assert ex.answer_a == str(part1(ex.input_data))
def test_part2_ex():
for ex in puzzle.examples:
assert ex.answer_b == str(part2(ex.input_data) - 100)
if __name__ == '__main__':
puzzle.answer_a = part1(data)
puzzle.answer_b = part2(data)