[2021] solved day 2

This commit is contained in:
Maciej Jur 2021-12-02 18:11:35 +01:00
parent efcea56f36
commit cde2675d82
2 changed files with 1043 additions and 0 deletions

1000
2021/.input/day02 Normal file

File diff suppressed because it is too large Load diff

43
2021/Python/day02.py Normal file
View file

@ -0,0 +1,43 @@
from functools import reduce
import re
from typing import Tuple
pattern = re.compile('^([a-z]+) ([0-9]+)$')
def load_input() -> list[Tuple[str, int]]:
with open('../.input/day02', 'r') as f:
return [
(match.group(1), int(match.group(2))) for match
in (pattern.search(line.strip()) for line in f.readlines())
]
def solve1() -> int:
numbers = load_input()
instructions = {
"forward": lambda arg, x, y: (x + arg, y),
"up": lambda arg, x, y: (x, y + arg),
"down": lambda arg, x, y: (x, y - arg),
}
x, y = reduce(lambda pos, item: instructions[item[0]](item[1], *pos), numbers, (0, 0))
return abs(x) * abs(y)
def solve2() -> int:
numbers = load_input()
instructions = {
"forward": lambda arg, x, y, a: (x + arg, y + a * arg, a),
"up": lambda arg, x, y, a: (x, y, a - arg),
"down": lambda arg, x, y, a: (x, y, a + arg),
}
x, y, _ = reduce(lambda pos, item: instructions[item[0]](item[1], *pos), numbers, (0, 0, 0))
return abs(x) * abs(y)
if __name__ == '__main__':
print(solve1())
print(solve2())