-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday02.py
executable file
·55 lines (38 loc) · 1.02 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
52
53
54
55
#!/usr/bin/env python3
import sys
def safe(ints, allow_removal=False):
for i in range(len(ints) - 1):
if not 1 <= ints[i + 1] - ints[i] <= 3:
break
else:
return True
if not allow_removal:
return False
# Try removing ints[i]
rest = ints[i - 1:i] + ints[i + 1:]
for j in range(len(rest) - 1):
if not 1 <= rest[j + 1] - rest[j] <= 3:
if j > 0:
return False
break
else:
return True
# Try removing ints[i + 1]
rest = ints[i:i + 1] + ints[i + 2:]
for j in range(len(rest) - 1):
if not 1 <= rest[j + 1] - rest[j] <= 3:
return False
return True
# Open the first argument as input or use stdin if no arguments were given
fin = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin
n_safe = n_safe_with_removal = 0
for line in fin:
ints = list(map(int, line.split()))
if safe(ints) or safe(ints[::-1]):
n_safe += 1
continue
if safe(ints, True) or safe(ints[::-1], True):
n_safe_with_removal += 1
n_safe_with_removal += n_safe
print('Part 1:', n_safe)
print('Part 2:', n_safe_with_removal)