advent_of_code/2021/2/2_2.py

25 lines
534 B
Python
Raw Permalink Normal View History

2021-12-06 01:27:38 +01:00
#!/usr/bin/env python
# 2021 - Advent Of Code - 2 part 2
import re
x = 0
y = 0
aim = 0
2021-12-12 17:23:29 +01:00
re_parse = re.compile(r'(forward|up|down) (\d+)')
2021-12-06 01:27:38 +01:00
2022-04-05 13:27:24 +02:00
with open('input.txt', encoding="utf-8") as f:
2021-12-06 01:27:38 +01:00
for line in f:
m = re_parse.match(line)
direction, unit = m.group(1), int(m.group(2))
if direction == 'up':
aim -= unit
elif direction == 'down':
aim += unit
elif direction == 'forward':
x += unit
y += aim * unit
print(f'position: ({x}, {y}) => {x*y}')