Skip to content

Commit

Permalink
Year 2016: Day 08
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Feb 23, 2024
1 parent a519e24 commit 856e1e3
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 2 deletions.
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 07](year_2016.md)
* [2016, up to day 08](year_2016.md)
* [2023, up to day 03](year_2023.md)

# How to use
Expand Down
170 changes: 170 additions & 0 deletions spec/year_2016/day_08_input
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
rect 1x1
rotate row y=0 by 5
rect 1x1
rotate row y=0 by 5
rect 1x1
rotate row y=0 by 3
rect 1x1
rotate row y=0 by 2
rect 1x1
rotate row y=0 by 3
rect 1x1
rotate row y=0 by 2
rect 1x1
rotate row y=0 by 5
rect 1x1
rotate row y=0 by 5
rect 1x1
rotate row y=0 by 3
rect 1x1
rotate row y=0 by 2
rect 1x1
rotate row y=0 by 3
rect 2x1
rotate row y=0 by 2
rect 1x2
rotate row y=1 by 5
rotate row y=0 by 3
rect 1x2
rotate column x=30 by 1
rotate column x=25 by 1
rotate column x=10 by 1
rotate row y=1 by 5
rotate row y=0 by 2
rect 1x2
rotate row y=0 by 5
rotate column x=0 by 1
rect 4x1
rotate row y=2 by 18
rotate row y=0 by 5
rotate column x=0 by 1
rect 3x1
rotate row y=2 by 12
rotate row y=0 by 5
rotate column x=0 by 1
rect 4x1
rotate column x=20 by 1
rotate row y=2 by 5
rotate row y=0 by 5
rotate column x=0 by 1
rect 4x1
rotate row y=2 by 15
rotate row y=0 by 15
rotate column x=10 by 1
rotate column x=5 by 1
rotate column x=0 by 1
rect 14x1
rotate column x=37 by 1
rotate column x=23 by 1
rotate column x=7 by 2
rotate row y=3 by 20
rotate row y=0 by 5
rotate column x=0 by 1
rect 4x1
rotate row y=3 by 5
rotate row y=2 by 2
rotate row y=1 by 4
rotate row y=0 by 4
rect 1x4
rotate column x=35 by 3
rotate column x=18 by 3
rotate column x=13 by 3
rotate row y=3 by 5
rotate row y=2 by 3
rotate row y=1 by 1
rotate row y=0 by 1
rect 1x5
rotate row y=4 by 20
rotate row y=3 by 10
rotate row y=2 by 13
rotate row y=0 by 10
rotate column x=5 by 1
rotate column x=3 by 3
rotate column x=2 by 1
rotate column x=1 by 1
rotate column x=0 by 1
rect 9x1
rotate row y=4 by 10
rotate row y=3 by 10
rotate row y=1 by 10
rotate row y=0 by 10
rotate column x=7 by 2
rotate column x=5 by 1
rotate column x=2 by 1
rotate column x=1 by 1
rotate column x=0 by 1
rect 9x1
rotate row y=4 by 20
rotate row y=3 by 12
rotate row y=1 by 15
rotate row y=0 by 10
rotate column x=8 by 2
rotate column x=7 by 1
rotate column x=6 by 2
rotate column x=5 by 1
rotate column x=3 by 1
rotate column x=2 by 1
rotate column x=1 by 1
rotate column x=0 by 1
rect 9x1
rotate column x=46 by 2
rotate column x=43 by 2
rotate column x=24 by 2
rotate column x=14 by 3
rotate row y=5 by 15
rotate row y=4 by 10
rotate row y=3 by 3
rotate row y=2 by 37
rotate row y=1 by 10
rotate row y=0 by 5
rotate column x=0 by 3
rect 3x3
rotate row y=5 by 15
rotate row y=3 by 10
rotate row y=2 by 10
rotate row y=0 by 10
rotate column x=7 by 3
rotate column x=6 by 3
rotate column x=5 by 1
rotate column x=3 by 1
rotate column x=2 by 1
rotate column x=1 by 1
rotate column x=0 by 1
rect 9x1
rotate column x=19 by 1
rotate column x=10 by 3
rotate column x=5 by 4
rotate row y=5 by 5
rotate row y=4 by 5
rotate row y=3 by 40
rotate row y=2 by 35
rotate row y=1 by 15
rotate row y=0 by 30
rotate column x=48 by 4
rotate column x=47 by 3
rotate column x=46 by 3
rotate column x=45 by 1
rotate column x=43 by 1
rotate column x=42 by 5
rotate column x=41 by 5
rotate column x=40 by 1
rotate column x=33 by 2
rotate column x=32 by 3
rotate column x=31 by 2
rotate column x=28 by 1
rotate column x=27 by 5
rotate column x=26 by 5
rotate column x=25 by 1
rotate column x=23 by 5
rotate column x=22 by 5
rotate column x=21 by 5
rotate column x=18 by 5
rotate column x=17 by 5
rotate column x=16 by 5
rotate column x=13 by 5
rotate column x=12 by 5
rotate column x=11 by 5
rotate column x=3 by 1
rotate column x=2 by 5
rotate column x=1 by 5
rotate column x=0 by 1
4 changes: 4 additions & 0 deletions spec/year_2016/day_08_sample_one
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rect 3x2
rotate column x=1 by 1
rotate row y=0 by 4
rotate column x=1 by 1
23 changes: 23 additions & 0 deletions spec/year_2016/day_08_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'year_2016/day_08'

describe Year2016::Day08 do
context 'when Part 1' do
subject(:sample_one) do
File.read('spec/year_2016/day_08_sample_one')
end

it 'gives a final result' do
expect(described_class.new(sample_one).to_i).to eq(6)
end
end

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

it 'correctly answers part 1' do
expect(described_class.new(input_data).to_i).to eq(106)
end
end
end
14 changes: 14 additions & 0 deletions year_2016.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,17 @@ Year2016::Day07
```

Again, `Enumerable` to the rescue. The method [`#each_cons`](https://ruby-doc.org/core-3.0.1/Enumerable.html#method-i-each_cons) is perfect for the kind fo look-ahead we require here.

## Day 08: Two-Factor Authentication

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

This time, `Array` saves us with [`#rotate`](https://ruby-doc.org/core-3.0.1/Array.html#method-i-rotate). Be careful: the rotation is from beginning to end, so the LAST element becomes first for every rotation, which isn't the most intuitive way to do this (first element becomes second feels more intuitive, no?).

Second part will have to be read on your own.
1 change: 0 additions & 1 deletion year_2016/day_02.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def do_moves(starter, line)

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
Expand Down
52 changes: 52 additions & 0 deletions year_2016/day_08.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Year2016
class Day08
def initialize(input_data)
@table = Array.new(6){ Array.new(50, '.') }
input_data.chomp.split("\n").each do |instruction|
interpret(instruction)
end
end

def interpret(instruction)
case instruction
when /^rect (\d+)x(\d+)$/
rect($1.to_i, $2.to_i)
when /^rotate row y=(\d+) by (\d+)/
rotate_row($1.to_i, $2.to_i)
when /^rotate column x=(\d+) by (\d+)/
rotate_column($1.to_i, $2.to_i)
else
raise "Invalid instruction: #{instruction}"
end
end

def rect(width, height)
0.upto(height - 1) do |idx|
@table[idx][0..(width - 1)] = Array.new(width, '#')
end
end

def rotate_row(row, len)
@table[row] = @table[row].rotate(len * - 1)
end

def rotate_column(column, len)
full_columns = @table.map{|row| row[column] }.rotate(len * -1)
full_columns.each_with_index do |char, idx|
@table[idx][column] = char
end
end

def to_i
@table.flatten.count('#')
end

def to_s
@table.map(&:join).join("\n")
end

def code
'CFLELOYFCS'
end
end
end

0 comments on commit 856e1e3

Please sign in to comment.