-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_2.py
84 lines (64 loc) · 1.97 KB
/
day_2.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from utils import read_input
from itertools import pairwise
def map_fn(line):
"""Splits each line of input by whitespace and maps
each value to integer.
Example line:
7 6 4 2 1
"""
return [int(number) for number in line.split()]
def calc_sign(a, b):
"""Returns:
0 if a == b
-1 if a < b
1 if a > b
"""
diff = a - b
if diff == 0:
return 0
return diff / abs(diff)
def is_report_safe(report):
"""Checks if a given list of numbers satisfies
following rules:
1. Numbers are either increasing or decreasing consistently
2. Difference between two consecutive numbers is 0 < number < 4
"""
sign = calc_sign(report[0], report[1])
for prev, next in pairwise(report):
diff = abs(prev - next)
if diff <= 0 or diff > 3:
return False
if sign != calc_sign(prev, next):
return False
return True
def check_report_with_dampener(report):
"""Checks if a given list of numbers satisfies following rules:
1. Numbers are either increasing or decreasing consistently
2. Difference between two consecutive numbers is 0 < number < 4
If it fails a rule, it retries by removing one number each time.
If any of the shorter lists satisfies the rules, returns True.
"""
if is_report_safe(report):
return True
for idx, _ in enumerate(report):
if is_report_safe(report[:idx] + report[idx + 1 :]):
return True
return False
def part_1():
reports = read_input(2, map_fn)
valid_reports = 0
for report in reports:
if is_report_safe(report):
valid_reports += 1
print(f"Part 1: {valid_reports}")
assert valid_reports == 549
def part_2():
reports = read_input(2, map_fn)
valid_reports = 0
for report in reports:
if check_report_with_dampener(report):
valid_reports += 1
print(f"Part 2: {valid_reports}")
assert valid_reports == 589
part_1()
part_2()