This commit is contained in:
cupcakearmy 2021-12-02 11:56:55 +01:00
parent e47fd7e331
commit cbe08c579f
No known key found for this signature in database
GPG Key ID: 3235314B4D31232F
4 changed files with 1090 additions and 0 deletions

11
2021/02/README.md Normal file
View File

@ -0,0 +1,11 @@
# 02
Description
<details>
<summary>Solutions</summary>
<ol>
<li>1670340</li>
<li>1954293920</li>
</ol>
</details>

1001
2021/02/input.txt Normal file

File diff suppressed because it is too large Load Diff

72
2021/02/python/main.py Normal file
View File

@ -0,0 +1,72 @@
#!/usr/bin/env python
from os.path import join, dirname
# Day 01
# Common
def read_input(filename):
data = join(dirname(__file__), '..', filename)
with open(data) as f:
return f.read()
# 1
class submarine:
def __init__(self, with_aim=False):
self.distance = 0
self.depth = 0
self.aim = 0
self.with_aim = with_aim
def execute(self, instruction: str):
op, x = instruction.split(' ')
x = int(x)
if op == 'forward':
if self.with_aim:
self.distance += x
self.depth += x * self.aim
else:
self.distance += x
elif op == 'up':
if self.with_aim:
self.aim -= x
else:
self.depth -= x
elif op == 'down':
if self.with_aim:
self.aim += x
else:
self.depth += x
def execute_instructions(self, instructions: str):
for instruction in instructions.strip().split('\n'):
self.execute(instruction)
def get_current_position(self):
return self.distance * self.depth
test = read_input('test.txt')
sub = submarine()
sub.execute_instructions(test)
print(f"Test: {sub.get_current_position()}")
data = read_input('input.txt')
sub = submarine()
sub.execute_instructions(data)
print(f"Part 1: {sub.get_current_position()}")
# 2
sub = submarine(with_aim=True)
sub.execute_instructions(test)
print(f"Test: {sub.get_current_position()}")
sub = submarine(with_aim=True)
sub.execute_instructions(data)
print(f"Part 2: {sub.get_current_position()}")

6
2021/02/test.txt Normal file
View File

@ -0,0 +1,6 @@
forward 5
down 5
forward 8
up 3
down 8
forward 2