2016 - Day 2 (Bathroom Security)
View puzzle description on AdventOfCode.com
Part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
#Advent of Code
#2016 Day 2, Part 1
#Solution by James C. (https://github.com/JamesMCo)
import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
def solve(puzzle_input):
here = [1, 1]
buttons = [[1, 4, 7],
[2, 5, 8],
[3, 6, 9]]
code = ""
for l in puzzle_input:
if l == "": continue
for d in l:
if d == "U" and here[1] != 0:
here[1] -= 1
elif d == "D" and here[1] != 2:
here[1] += 1
elif d == "L" and here[0] != 0:
here[0] -= 1
elif d == "R" and here[0] != 2:
here[0] += 1
code += str(buttons[here[0]][here[1]])
return code
def main():
puzzle_input = util.read.as_lines()
code = solve(puzzle_input)
print("The code for the bathroom is " + str(code) + ".")
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
self.assertEqual(solve(["ULL",
"RRDDD",
"LURDL",
"UUUUD"]), "1985")
if __name__ == "__main__":
run(main)
Part 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
#Advent of Code
#2016 Day 2, Part 2
#Solution by James C. (https://github.com/JamesMCo)
import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
def solve(puzzle_input):
here = [0, 2]
buttons = [[None, None, 5, None, None],
[None, 2, 6, "A", None],
[1, 3, 7, "B", "D" ],
[None, 4, 8, "C", None],
[None, None, 9, None, None]]
code = ""
for l in puzzle_input:
if l == "": continue
for d in l:
if d == "U" and here[1] != 0 and buttons[here[0]][here[1] - 1] != None:
here[1] -= 1
elif d == "D" and here[1] != 4 and buttons[here[0]][here[1] + 1] != None:
here[1] += 1
elif d == "L" and here[0] != 0 and buttons[here[0] - 1][here[1]] != None:
here[0] -= 1
elif d == "R" and here[0] != 4 and buttons[here[0] + 1][here[1]] != None:
here[0] += 1
code += str(buttons[here[0]][here[1]])
return code
def main():
puzzle_input = util.read.as_lines()
code = solve(puzzle_input)
print("The code for the bathroom is " + str(code) + ".")
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
self.assertEqual(solve(["ULL",
"RRDDD",
"LURDL",
"UUUUD"]), "5DB3")
if __name__ == "__main__":
run(main)