2016-12-02 - Bathroom Security
(original .ipynb)puzzle_input_str = open("puzzle_input/day2.txt").read() test_input_str = """ULL RRDDD LURDL UUUUD""" def up(r, c, numpad): if not numpad[r-1][c].isspace(): return r-1, c return r, c def down(r, c, numpad): if not numpad[r+1][c].isspace(): return r+1, c return r, c def left(r, c, numpad): if not numpad[r][c-1].isspace(): return r, c-1 return r,c def right(r, c, numpad): if not numpad[r][c+1].isspace(): return r, c+1 return r, c dirs = { "U": up, "D": down, "L": left, "R": right } def trace(instructions, keypad, r, c): code = [] for instruction in instructions: for char in instruction: r, c = dirs[char](r, c, keypad) code.append(keypad[r][c]) return "".join(code) def part_one(input_str): instructions = input_str.split("\n") keypad = [ " ", " 123 ", " 456 ", " 789 ", " " ] return trace(instructions, keypad, 2, 2) assert "1985" == part_one(test_input_str) print("part one:", part_one(puzzle_input_str))
part one: 47978
def part_two(input_str): instructions = input_str.split("\n") keypad = [ " ", " 1 ", " 234 ", " 56789 ", " ABC ", " D ", " " ] return trace(instructions, keypad, 3, 1) assert "5DB3" == part_two(test_input_str) print("part two:", part_two(puzzle_input_str))
part two: 659AD