Skip to content

Commit d939b9f

Browse files
committed
Day 2: Bathroom Security (2nd)
1 parent 9572208 commit d939b9f

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/.session
22
/day*_input.txt
3+
/.ownership
34
*.gem
45
*.rbc
56
/.config

day02.rb

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,32 @@
3939
# So, in this example, the bathroom code is 1985.
4040
#
4141
# Your puzzle input is the instructions from the document you found at the front desk. What is the bathroom code?
42+
#
43+
# --- Part Two ---
44+
#
45+
# You finally arrive at the bathroom (it's a several minute walk from the lobby so visitors can
46+
# behold the many fancy conference rooms and water coolers on this floor) and go to punch in the
47+
# code. Much to your bladder's dismay, the keypad is not at all like you imagined it. Instead, you
48+
# are confronted with the result of hundreds of man-hours of bathroom-keypad-design meetings:
49+
#
50+
# 1
51+
# 2 3 4
52+
# 5 6 7 8 9
53+
# A B C
54+
# D
55+
# You still start at "5" and stop when you're at an edge, but given the same instructions as
56+
# above, the outcome is very different:
57+
#
58+
# You start at "5" and don't move at all (up and left are both edges), ending at 5.
59+
# Continuing from "5", you move right twice and down three times (through "6", "7", "B", "D",
60+
# "D"), ending at D.
61+
# Then, from "D", you move five more times (through "D", "B", "C", "C", "B"), ending at B.
62+
# Finally, after five more moves, you end at 3.
63+
# So, given the actual keypad layout, the code would be 5DB3.
64+
#
65+
# Using the same instructions in your puzzle input, what is the correct bathroom code?
66+
#
67+
# Although it hasn't changed, you can still get your puzzle input.
4268

4369
require_relative 'input'
4470

@@ -47,7 +73,7 @@
4773

4874
# puts "solving day #{day} from input\n#{input}"
4975

50-
move = {
76+
move_square = {
5177
'1' => { 'U' => '1', 'D' => '4', 'L' => '1', 'R' => '2' },
5278
'2' => { 'U' => '2', 'D' => '5', 'L' => '1', 'R' => '3' },
5379
'3' => { 'U' => '3', 'D' => '6', 'L' => '2', 'R' => '3' },
@@ -58,12 +84,34 @@
5884
'8' => { 'U' => '5', 'D' => '8', 'L' => '7', 'R' => '9' },
5985
'9' => { 'U' => '6', 'D' => '9', 'L' => '8', 'R' => '9' },
6086
}
87+
move_diamond = {
88+
'1' => { 'U' => '1', 'D' => '3', 'L' => '1', 'R' => '1' },
89+
'2' => { 'U' => '2', 'D' => '6', 'L' => '2', 'R' => '3' },
90+
'3' => { 'U' => '1', 'D' => '7', 'L' => '2', 'R' => '4' },
91+
'4' => { 'U' => '4', 'D' => '8', 'L' => '3', 'R' => '4' },
92+
'5' => { 'U' => '5', 'D' => '5', 'L' => '5', 'R' => '6' },
93+
'6' => { 'U' => '2', 'D' => 'A', 'L' => '5', 'R' => '7' },
94+
'7' => { 'U' => '3', 'D' => 'B', 'L' => '6', 'R' => '8' },
95+
'8' => { 'U' => '4', 'D' => 'C', 'L' => '7', 'R' => '9' },
96+
'9' => { 'U' => '9', 'D' => '9', 'L' => '8', 'R' => '9' },
97+
'A' => { 'U' => '6', 'D' => 'A', 'L' => 'A', 'R' => 'B' },
98+
'B' => { 'U' => '7', 'D' => 'D', 'L' => 'A', 'R' => 'C' },
99+
'C' => { 'U' => '8', 'D' => 'C', 'L' => 'B', 'R' => 'C' },
100+
'D' => { 'U' => 'B', 'D' => 'D', 'L' => 'D', 'R' => 'D' },
101+
}
61102

62-
button = '5'
103+
square = ''
104+
button_square = '5'
105+
diamond = ''
106+
button_diamond = '5'
63107
input.each_line do |line|
64108
line.chomp.each_char do |dir|
65-
button = move[button][dir]
109+
button_square = move_square[button_square][dir]
110+
button_diamond = move_diamond[button_diamond][dir]
66111
end
67-
print button
112+
square << button_square
113+
diamond << button_diamond
68114
end
69-
puts
115+
116+
puts "Square: #{square}"
117+
puts "Diamond: #{diamond}"

0 commit comments

Comments
 (0)