Skip to content

Commit

Permalink
Year 2016: Day 02
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Feb 21, 2024
1 parent b78bae9 commit 5eff3dd
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ I'm also adding notes that may be useful if you're learning Ruby.

Notes for solving:
* [2015, complete](year_2015.md)
* [2016, up to day 01](year_2016.md)
* [2016, up to day 02](year_2016.md)
* [2023, up to day 03](year_2023.md)

# How to use
Expand Down
5 changes: 5 additions & 0 deletions spec/year_2016/day_02_input
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
UULLULLUULLLURDLDUURRDRRLDURDULLRURDUDULLLUULURURLRDRRRRULDRUULLLLUUDURDULDRRDRUDLRRLDLUDLDDRURURUURRRDDDLLRUDURDULUULLRRULLRULDUDRDRLDLURURUDDUDLURUDUDURLURURRURLUDDRURRDLUURLLRURRDUDLULULUDULDLLRRRDLRDLDUDRDDDRRUURRRRRUURRDRRDLURDRRURDLLUULULLRURDLDDDRRLLRRUURULURUUDDLRRUDDRURUUDLRLRDLRURRRDULLDLRUDDUULRDULURUURDULUDLLRRLDDLRDLRUDRLDDRLRRRDURDULLRRRDRRLUURURDRRDRRLDLUDURURLDUURDRUDRDDRLDRRLDLURURULLUURUDUUDLRLL
LLLULLULDDULRLLURLLLRUUDDLRUULRLULLDLLRRDRLRLRLLDRUUURULDRDDLUDLLDUDULLLRLULLLRULDRDRUDLLRLRLLUDULRRRLDRUULDDULLDULULLUDUDLDRDURDLDLLDUDRRRDLUURRUURULLURLDURLRRLLDDUUULDRLUUDUDLURLULUDURRDRLLDDDDDRRULLRLDULULDDRUURRDLUDDDUDURDDRDRULULLLLUURDURUUUULUDLRURRULRDDRURURLLRLUUDUUURDLLDDLUDRLLLUDLLLLULRLURDRRRDUUDLLDLDDDURRDDRURUURDDRURRLDDDURDLLUURUUULRLUURRUDRLLDLURDUDRLULDLRLULULUDDLRDUDRUDLUULUULDURDRRRRLRULLUDRDDRDLDUDRDRRLDLLLLUDDLRULDLLDDUULDDRRULRRUURUDRDURLLLDDUUDRUUDLULLDR
UDUUULLDDDDLUDLDULRLRDLULLDDRULDURRLURRUDLRRUDURRDUDRRRUULRLLRLUDLDRRDUURDDRDRDUUUDUDLDLLRRLUURLUUUDDDUURLULURRLURRRDRDURURUDRLRUURUDRUDDDRDRDLDRDURDLDRRDUUDLLURLDDURRRLULDRDRLLRLLLRURLDURDRLDRUURRLDLDRLDDDRLDLRLDURURLLLLDDRDUDLRULULLRDDLLUDRDRRLUUULDRLDURURDUDURLLDRRDUULDUUDLLDDRUUULRRULDDUDRDRLRULUUDUURULLDLLURLRRLDDDLLDRRDDRLDDLURRUDURULUDLLLDUDDLDLDLRUDUDRDUDDLDDLDULURDDUDRRUUURLDUURULLRLULUURLLLLDUUDURUUDUULULDRULRLRDULDLLURDLRUUUDDURLLLLDUDRLUUDUDRRURURRDRDDRULDLRLURDLLRRDRUUUURLDRURDUUDLDURUDDLRDDDDURRLRLUDRRDDURDDRLDDLLRR
ULDRUDURUDULLUDUDURLDLLRRULRRULRUDLULLLDRULLDURUULDDURDUUDLRDRUDUDDLDRDLUULRRDLRUULULUUUDUUDDRDRLLULLRRDLRRLUDRLULLUUUUURRDURLLRURRULLLRLURRULRDUURRLDDRRDRLULDDRRDRLULLRDLRRURUDURULRLUDRUDLUDDDUDUDDUDLLRDLLDRURULUDRLRRULRDDDDDRLDLRRLUUDLUURRDURRDLDLDUDRLULLULRLDRDUDLRULLULLRLDDRURLLLRLDDDLLLRURDDDLLUDLDLRLUULLLRULDRRDUDLRRDDULRLLDUURLLLLLDRULDRLLLUURDURRULURLDDLRRUDULUURRLULRDRDDLULULRRURLDLRRRUDURURDURDULURULLRLDD
DURLRRRDRULDLULUDULUURURRLULUDLURURDDURULLRRUUDLRURLDLRUDULDLLRRULLLLRRLRUULDLDLLRDUDLLRLULRLLUUULULRDLDLRRURLUDDRRLUUDDRRUDDRRURLRRULLDDULLLURRULUDLRRRURRULRLLLRULLRRURDRLURULLDULRLLLULLRLRLLLDRRRRDDDDDDULUUDUDULRURDRUDRLUULURDURLURRDRRRRDRRLLLLUDLRRDURURLLULUDDLRLRLRRUURLLURLDUULLRRDURRULRULURLLLRLUURRULLLURDDDRURDUDDULLRULUUUDDRURUUDUURURRDRURDUDRLLRRULURUDLDURLDLRRRRLLUURRLULDDDUUUURUULDLDRLDUDULDRRULDRDULURRUURDU
38 changes: 38 additions & 0 deletions spec/year_2016/day_02_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'year_2016/day_02'

describe Year2016::Day02 do
subject(:sample) do
<<~HEREDOC
ULL
RRDDD
LURDL
UUUUD
HEREDOC
end

context 'when Part 1' do
it 'gives a final result' do
expect(described_class.new(sample, true).code).to eq('1985')
end
end

context 'when Part 2' do
it 'gives a final result' do
expect(described_class.new(sample).code).to eq('5DB3')
end
end

context 'when Results' do
subject(:input_data) do
File.read('spec/year_2016/day_02_input')
end

it 'correctly answers part 1' do
expect(described_class.new(input_data, true).code).to eq('45973')
end

it 'correctly answers part 2' do
expect(described_class.new(input_data).code).to eq('27CA4')
end
end
end
15 changes: 15 additions & 0 deletions year_2016.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,18 @@ Year2015::Day01
```

There really is nothing specifically hard here, it's all about knowing when to properly break out of a loop.

## Day 02: Bathroom Security

```
Year2016::Day02
when Part 1
gives a final result
when Part 2
gives a final result
when Results
correctly answers part 1
correctly answers part 2
```

While reviewing [another code solution for the previous day's exercise](https://github.com/rHermes/adventofcode/blob/master/2016/01/y2016_d01_p02.py), I was intrigued by a very interesting piece of code: the author had treated the turning of left and right into transition paths from one state to another. While the situation felt like overkill to me in that case, it proved to work perfectly for today's exercise.
69 changes: 69 additions & 0 deletions year_2016/day_02.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
class Year2016
class Day02
# Reminder:
# 1 2 3
# 4 5 6
# 7 8 9
BUTTONS_V1 = {
'1' => { 'R' => '2', 'D' => '4' },
'2' => { 'L' => '1', 'R' => '3', 'D' => '5' },
'3' => { 'L' => '2', 'D' => '6' },
'4' => { 'U' => '1', 'R' => '5', 'D' => '7' },
'5' => { 'U' => '2', 'L' => '4', 'R' => '6', 'D' => '8' },
'6' => { 'L' => '5', 'U' => '3', 'D' => '9' },
'7' => { 'R' => '8', 'U' => '4' },
'8' => { 'L' => '7', 'R' => '9', 'U' => '5' },
'9' => { 'L' => '8', 'U' => '6' }
}.freeze

# Reminder:
# 1
# 2 3 4
# 5 6 7 8 9
# A B C
# D
BUTTONS_V2 = {
'1' => { 'D' => '3' },

'2' => { 'R' => '3', 'D' => '6' },
'3' => { 'L' => '2', 'U' => '1', 'D' => '7', 'R' => '4' },
'4' => { 'L' => '3', 'D' => '8' },

'5' => { 'R' => '6' },
'6' => { 'U' => '2', 'L' => '5', 'R' => '7', 'D' => 'A' },
'7' => { 'U' => '3', 'L' => '6', 'R' => '8', 'D' => 'B' },
'8' => { 'U' => '4', 'L' => '7', 'R' => '9', 'D' => 'C' },
'9' => { 'L' => '8' },

'A' => { 'R' => 'B', 'U' => '6' },
'B' => { 'L' => 'A', 'U' => '7', 'D' => 'D', 'R' => 'C' },
'C' => { 'L' => 'B', 'U' => '8' },

'D' => { 'U' => 'B' }
}.freeze

def initialize(input_data, input_part_one = false)
@input = input_data
@version = input_part_one ? 1 : 2
end

def do_moves(starter, line)
curpos = starter
line.each_char do |direction|
nextpos = (@version == 1 ? BUTTONS_V1 : BUTTONS_V2)[curpos][direction]

curpos = nextpos if nextpos
end
curpos
end

def code
@code ||= @input.split("\n").each_with_object([]) do |line, code|
# puts "=> #{code[-1] || '?'} => #{line}"
result = do_moves(code[-1] || '5', line)

code << result
end.join
end
end
end

0 comments on commit 5eff3dd

Please sign in to comment.