2021 day 2
This commit is contained in:
1000
2021/02/input
Normal file
1000
2021/02/input
Normal file
File diff suppressed because it is too large
Load Diff
25
2021/02/part1
Executable file
25
2021/02/part1
Executable file
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
depth = 0
|
||||
hPos = 0
|
||||
|
||||
with open('input') as f:
|
||||
window = []
|
||||
for line in f:
|
||||
cmd, numStr = line.split(" ", 2)
|
||||
num = int(numStr)
|
||||
|
||||
# match...case is python 3.10+
|
||||
if cmd == "forward":
|
||||
hPos = hPos + num
|
||||
elif cmd == "backward":
|
||||
hPos = hPos - num
|
||||
elif cmd == "up":
|
||||
depth = depth - num
|
||||
elif cmd == "down":
|
||||
depth = depth + num
|
||||
else:
|
||||
raise "unknown command: "+cmd
|
||||
|
||||
print(depth * hPos)
|
||||
|
27
2021/02/part2
Executable file
27
2021/02/part2
Executable file
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
depth = 0
|
||||
hPos = 0
|
||||
aim = 0
|
||||
|
||||
with open('input') as f:
|
||||
window = []
|
||||
for line in f:
|
||||
cmd, numStr = line.split(" ", 2)
|
||||
num = int(numStr)
|
||||
|
||||
# match...case is python 3.10+
|
||||
if cmd == "forward":
|
||||
hPos = hPos + num
|
||||
depth = depth + (aim*num)
|
||||
elif cmd == "backward":
|
||||
hPos = hPos - num
|
||||
elif cmd == "up":
|
||||
aim = aim - num
|
||||
elif cmd == "down":
|
||||
aim = aim + num
|
||||
else:
|
||||
raise "unknown command: "+cmd
|
||||
|
||||
print(depth * hPos)
|
||||
|
Reference in New Issue
Block a user