-
Notifications
You must be signed in to change notification settings - Fork 2
/
Day2.py
52 lines (45 loc) · 1.6 KB
/
Day2.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
"""
It seems like the submarine can take a series of commands like forward 1, down 2, or up 3:
- forward X increases the horizontal position by X units.
- down X increases the depth by X units.
- up X decreases the depth by X units.
"""
inst = open("input_d2.txt").read().splitlines()
"""
Part1 : What do you get if you multiply your final horizontal position by your final depth?
"""
print("====PART1====")
horiz = 0
depth = 0
for i in range(len(inst)):
x = inst[i].split(" ")[0]
if x =="forward" :
horiz += int(inst[i].split(" ")[1])
if x =="up" :
depth -= int(inst[i].split(" ")[1])
if x =="down" :
depth += int(inst[i].split(" ")[1])
print("horizontal position * depth :",depth*horiz)
"""
In addition to horizontal position and depth, you'll also need to track a third value, aim, which also starts at 0. The commands also mean something entirely different than you first thought:
- down X increases your aim by X units.
- up X decreases your aim by X units.
- forward X does two things:
* It increases your horizontal position by X units.
* It increases your depth by your aim multiplied by X.
Part2 : What do you get if you multiply your final horizontal position by your final depth?
"""
print("====PART2====")
aim = 0
horiz = 0
depth = 0
for i in range(len(inst)):
x = inst[i].split(" ")[0]
if x =="forward" :
horiz += int(inst[i].split(" ")[1])
depth += int(inst[i].split(" ")[1])*aim
if x =="up" :
aim -= int(inst[i].split(" ")[1])
if x =="down" :
aim += int(inst[i].split(" ")[1])
print("horizontal position * depth :",depth*horiz)