[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

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}")