Day 2, Year 2021: Dive!
First read the problem description.
class Submarine():
= 0
horizontal = 0
depth = 0
aim
= False
use_aim
def __init__(self, use_aim=False):
self.use_aim = use_aim
def down(self, n):
if self.use_aim:
self.aim += n
else:
self.depth += n
def up(self, n):
if self.use_aim:
self.aim -= n
else:
self.depth -= n
def forward(self, n):
self.horizontal += n
if self.use_aim:
self.depth += n * self.aim
import import_ipynb
import helper
= helper.open_file('2021_2.txt')
f
= Submarine()
s = Submarine(use_aim=True)
sa
for l in f:
= l.split(' ')
d, n = None
f if d == 'forward':
= Submarine.forward
f elif d == 'up':
= Submarine.up
f elif d == 'down':
= Submarine.down
f
int(n))
f(s, int(n))
f(sa,
print(s.horizontal * s.depth)
print(sa.horizontal * sa.depth)
1694130
1698850445
Source code of the solution(s):