Added 2021 (python)
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
32
2021/02/prog.py
Normal file
32
2021/02/prog.py
Normal file
@ -0,0 +1,32 @@
|
||||
def get_input(sample = False, part = 1):
|
||||
with open(f'sample_p{part}' if sample else 'input', 'r') as f:
|
||||
tmp = [line.strip().split(' ') for line in f.readlines()]
|
||||
return [[e[0], int(e[1])] for e in tmp]
|
||||
|
||||
|
||||
def result(inp, part = 1):
|
||||
hor = dep = 0
|
||||
if part == 1:
|
||||
for [cmd, n] in inp:
|
||||
if cmd == "forward":
|
||||
hor += n
|
||||
elif cmd == "down":
|
||||
dep += n
|
||||
elif cmd == "up":
|
||||
dep -= n
|
||||
else:
|
||||
aim = 0
|
||||
for [cmd, n] in inp:
|
||||
if cmd == "forward":
|
||||
hor += n
|
||||
dep += n * aim
|
||||
elif cmd == "down":
|
||||
aim += n
|
||||
elif cmd == "up":
|
||||
aim -= n
|
||||
# print(f"{cmd} {n}")
|
||||
# print(f"\t{aim = }, {hor = }, {dep = }")
|
||||
return hor * dep
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(result(get_input(), part = 2))
|
6
2021/02/sample_p1
Normal file
6
2021/02/sample_p1
Normal file
@ -0,0 +1,6 @@
|
||||
forward 5
|
||||
down 5
|
||||
forward 8
|
||||
up 3
|
||||
down 8
|
||||
forward 2
|
10
2021/02/test.py
Normal file
10
2021/02/test.py
Normal file
@ -0,0 +1,10 @@
|
||||
from prog import *
|
||||
import unittest
|
||||
|
||||
class Tests(unittest.TestCase):
|
||||
def test_part1(self):
|
||||
self.assertEqual(result(get_input(True)), 150)
|
||||
|
||||
def test_part2(self):
|
||||
self.assertEqual(result(get_input(True), 2), 900)
|
||||
|
Reference in New Issue
Block a user