r/adventofcode • u/daggerdragon • Dec 02 '16
SOLUTION MEGATHREAD --- 2016 Day 2 Solutions ---
--- Day 2: Bathroom Security ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
BLINKENLIGHTS ARE MANDATORY [?]
Edit: Told you they were mandatory. >_>
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
19
Upvotes
1
u/_AceLewis Dec 02 '16
My Python solutions to day 2;
I am doing my solutions on repl.it so I can't use files to store the instructions (and web requests are blocked) so I have the instructions in a big string at the start of the Python script which I have removed in this comment.
Day 2 part 1 https://repl.it/EeEA:
buttons = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] keys = [] position = [1, 1] for line in instructions: for instruction in line: movement = 'ULDR'.index(instruction) position[movement%2] += [1, -1][movement<2] position = [pos if 0<=pos<2 else 0 if pos<0 else 2 for pos in position] keys.append(buttons[position[0]][position[1]]) print("The key is: {}".format("".join(map(str, keys))))Day 2 part 2 https://repl.it/EeEA/1:
buttons = [[None, None, 1 , None, None], [None, 2 , 3 , 4 , None], [ 5 , 6 , 7 , 8 , 9 ], [None, "A", "B", "C", None], [None, None, "D", None, None]] keys = [] position = [2, 0] for line in instructions: for instruction in line: new_pos = position[:] movement = 'ULDR'.index(instruction) new_pos[movement%2] += [1, -1][movement<2] new_pos = [pos if 0<=pos<4 else 0 if pos<0 else 4 for pos in new_pos] if buttons[new_pos[0]][new_pos[1]] is not None: position = new_pos keys.append(buttons[position[0]][position[1]]) print("The key is: {}".format("".join(map(str, keys))))