Add day 2 solution
This commit is contained in:
40
day-02/day-02.py
Normal file
40
day-02/day-02.py
Normal file
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
def part_1(input):
|
||||
h_pos = 0
|
||||
depth = 0
|
||||
for line in input:
|
||||
step = line.split()
|
||||
if 'forward' == step[0]:
|
||||
h_pos += int(step[1])
|
||||
elif 'up' == step[0]:
|
||||
depth -= int(step[1])
|
||||
elif 'down' == step[0]:
|
||||
depth += int(step[1])
|
||||
result = h_pos * depth
|
||||
print("Part 1 result:", result)
|
||||
|
||||
|
||||
def part_2(input):
|
||||
h_pos = 0
|
||||
depth = 0
|
||||
aim = 0
|
||||
for line in input:
|
||||
step = line.split()
|
||||
if 'forward' == step[0]:
|
||||
h_pos += int(step[1])
|
||||
depth += aim * int(step[1])
|
||||
elif 'up' == step[0]:
|
||||
aim -= int(step[1])
|
||||
elif 'down' == step[0]:
|
||||
aim += int(step[1])
|
||||
result = h_pos * depth
|
||||
print("Part 2 result:", result)
|
||||
|
||||
|
||||
input = list()
|
||||
with open('input.txt') as fp:
|
||||
input = fp.readlines()
|
||||
|
||||
part_1(input)
|
||||
part_2(input)
|
||||
Reference in New Issue
Block a user