Advent of Code 2021 - Day 2
Day 2 is behind me. I enjoyed it, even though it was still pretty easy (I believe that’s going to change soon).
Let’s take a look at the solution:
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
import fileinput
def parse():
return [l.split() for l in fileinput.input()]
def task1(input):
horizontal = 0
depth = 0
for cmd in input:
dir = cmd[0]
val = int(cmd[1])
if dir == 'forward':
horizontal += val
if dir == 'down':
depth += val
if dir == 'up':
depth -= val
return horizontal * depth
def task2(input):
horizontal = 0
depth = 0
aim = 0
for cmd in input:
dir = cmd[0]
val = int(cmd[1])
if dir == 'forward':
horizontal += val
depth += aim * val
if dir == 'down':
aim += val
if dir == 'up':
aim -= val
return horizontal * depth
input = parse()
print(task1(input))
print(task2(input))
The task was similar to a one from the last year. In the input we have a command value
tuples, hence the split in line #5. In the first part, we have a simple switch statement (and I just found out that it was recently added to python 3.10 in the match-case
form. That’s cool!). We simply modify two variables.
In the second part, we have the third variable, aim
and a tiny change of logic. down
and up
modify the aim and forward
modify both horizontal and depth variables.
That’s it!
Ranks: 3562/2408. I guess it took me some time to wake up ;)