-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3_b.py
More file actions
29 lines (23 loc) · 765 Bytes
/
day3_b.py
File metadata and controls
29 lines (23 loc) · 765 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
import sys
input_file = "input/3.ex" if len(sys.argv) < 2 else sys.argv[1]
with open(input_file, "r") as f:
lines = f.readlines()
lines = [ l.strip() for l in lines ]
result = 0
num_batteries = 12
for line in lines:
active = [0] * num_batteries
active_index = [-1] * (num_batteries + 1)
for battery in range(num_batteries-1,-1,-1):
for i in range(active_index[battery]+1,len(line)-battery):
d = line[i]
n = int(d)
if n > active[battery]:
active[battery] = n
active_index[battery-1] = i
joltage = 0
for i in range(num_batteries):
joltage += active[i] * 10 ** i
print(list(reversed(active)), joltage)
result += joltage
print(result)