-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday05.py
executable file
·63 lines (53 loc) · 1.15 KB
/
day05.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
#!/usr/bin/env python3
def parse(filename):
with open(filename) as f:
lines = [int(x.strip()) for x in f.readlines()]
return lines
def part1(jumps):
jmax = len(jumps)
current_idx = 0
old_idx = 0
steps = 0
while True:
old_idx = current_idx
if current_idx >= jmax:
break
current_idx += jumps[current_idx]
jumps[old_idx] += 1
steps += 1
return steps
def part2(jumps):
jmax = len(jumps)
current_idx = 0
old_idx = 0
steps = 0
while True:
old_idx = current_idx
if current_idx >= jmax:
break
current_idx += jumps[current_idx]
if jumps[old_idx] >= 3:
jumps[old_idx] -= 1
else:
jumps[old_idx] += 1
steps += 1
return steps
jumps = parse("../input_small.txt")
p1 = part1(jumps)
print("Expect to see: 5")
print(p1)
## --
jumps = parse("../input_small.txt")
p2 = part2(jumps)
print("Expect to see: 10")
print(p2)
print("\n")
jumps = parse("../input.txt")
p1 = part1(jumps)
print("Part1:")
print(p1)
## --
jumps = parse("../input.txt")
p2 = part2(jumps)
print("Part2:")
print(p2)