2021-12-02 18:11:35 +01:00
|
|
|
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__':
|
2021-12-03 16:31:30 +01:00
|
|
|
print(solve1()) # 2215080
|
|
|
|
print(solve2()) # 1864715580
|