-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2.py
More file actions
executable file
·51 lines (35 loc) · 1015 Bytes
/
Copy pathday2.py
File metadata and controls
executable file
·51 lines (35 loc) · 1015 Bytes
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
from aocd import get_data
from aoc_utils import * # type: ignore
from itertools import pairwise
data = get_data(year=2024, day=2, block=True)
def safe(report):
increasing = True
decreasing = True
rule = True
for a, b in pairwise(report):
increasing &= a > b
decreasing &= a < b
delta = abs(a - b)
rule &= delta >= 1 and delta <= 3
return (increasing or decreasing) and rule
def part_one(data):
total = 0
reports = [ints(l) for l in data.splitlines()]
for report in reports:
total += safe(report)
return total
def part_two(data):
total = 0
reports = [ints(l) for l in data.splitlines()]
for report in reports:
if not safe(report):
for i in range(len(report)):
r = report[:i] + report[i + 1:]
if safe(r):
total += 1
break
else:
total += 1
return total
print(part_one(data))
print(part_two(data))