[py] Port 2016_02

This commit is contained in:
Joscha 2022-12-06 19:55:37 +01:00
parent bd2e910569
commit a3d046c572
3 changed files with 88 additions and 84 deletions

View file

@ -2,7 +2,7 @@ import sys
import argparse
from pathlib import Path
from .y2016 import d01
from .y2016 import d01, d02
from .y2017 import d01, d02, d03, d04, d05, d06, d07, d08, d09
from .y2018 import d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11
from .y2020 import d10
@ -11,6 +11,7 @@ from .y2022 import d01, d02, d03, d04, d05, d06
DAYS = {
"2016_01": y2016.d01.solve,
"2016_02": y2016.d02.solve,
"2017_01": y2017.d01.solve,
"2017_02": y2017.d02.solve,
"2017_03": y2017.d03.solve,

86
py/aoc/y2016/d02.py Normal file
View file

@ -0,0 +1,86 @@
def load_moves(inputstr):
moves = []
for line in inputstr.splitlines():
moves.append(line)
return moves
# PART 1
LAYOUT_1 = {
(0, 0): "1",
(1, 0): "2",
(2, 0): "3",
(0, 1): "4",
(1, 1): "5",
(2, 1): "6",
(0, 2): "7",
(1, 2): "8",
(2, 2): "9",
}
def pos_to_num(layout, pos):
return layout.get(pos)
def do_step(layout, pos, step):
x, y = pos
if step == "U":
y -= 1
elif step == "D":
y += 1
elif step == "L":
x -= 1
elif step == "R":
x += 1
newpos = (x, y)
if newpos in layout:
return newpos
else:
return pos
def do_move(layout, pos, steps):
for step in steps:
pos = do_step(layout, pos, step)
return pos
def enter(layout, moves):
pos = (1, 1)
numbers = []
for move in moves:
pos = do_move(layout, pos, move)
numbers.append(pos_to_num(layout, pos))
numbers = "".join(numbers)
return numbers
# PART 2
LAYOUT_2 = {
(2, 0): "1",
(1, 1): "2",
(2, 1): "3",
(3, 1): "4",
(0, 2): "5",
(1, 2): "6",
(2, 2): "7",
(3, 2): "8",
(4, 2): "9",
(1, 3): "A",
(2, 3): "B",
(3, 3): "C",
(2, 4): "D",
}
def solve(inputstr):
moves = load_moves(inputstr)
numbers = enter(LAYOUT_1, moves)
print(f"Part 1: {numbers}")
numbers_2 = enter(LAYOUT_2, moves)
print(f"Part 2: {numbers_2}")