-
Notifications
You must be signed in to change notification settings - Fork 0
/
02.py
48 lines (41 loc) · 1.12 KB
/
02.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
import numpy as np
def get_data():
file = "data/02.txt"
forward = []
down = []
up = []
with open(file, "r") as f:
for line in f:
line = line.strip()
if line.startswith("forward"):
forward.append(int(line[8:]))
down.append(0)
up.append(0)
elif line.startswith("down"):
down.append(int(line[5:]))
forward.append(0)
up.append(0)
else:
up.append(int(line[3:]))
down.append(0)
forward.append(0)
return forward, down, up
def first_solution():
forward, down, up = get_data()
f_pos = np.sum(forward)
d_pos = np.sum(down) - np.sum(up)
print(f_pos, d_pos, f_pos*d_pos)
if __name__ == "__main__":
f_data, d_data, u_data = get_data()
f_pos = 0
d_pos = 0
aim = 0
for f, d, u in zip(f_data, d_data, u_data):
if f != 0:
f_pos += f
d_pos += aim * f
elif d != 0:
aim += d
else:
aim -= u
print(f_pos, d_pos, f_pos * d_pos)