-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c769c9
commit 8436203
Showing
4 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'year_2015/day_10' | ||
|
||
describe Year2015::Day10 do | ||
context 'Part 1' do | ||
subject do | ||
Year2015::Day10.new('1') | ||
end | ||
|
||
it 'iterates each look-and-say' do | ||
%w(1 11 21 1211 111221 312211).each_with_index do |result, i| | ||
expect(subject.iterations(i)).to eq(result) | ||
end | ||
end | ||
end | ||
|
||
context 'Results' do | ||
subject do | ||
Year2015::Day10.new('1113222113') | ||
end | ||
|
||
it 'correctly answers part 1' do | ||
expect(subject.to_i(40)).to eq(252594) | ||
end | ||
|
||
it 'correctly answers part 2' do | ||
expect(subject.to_i(50)).to eq(3579328) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class Year2015 | ||
class Day10 | ||
def iterations(desired_iteration = -1) | ||
look_and_say until @inner_iterations.length > desired_iteration | ||
@inner_iterations[desired_iteration] | ||
end | ||
|
||
def look_and_say | ||
starter = @inner_iterations.last | ||
next_iteration = starter.each_char.with_object([starter[0], 0]) do |char, memo| | ||
if char == memo[-2] | ||
memo[-1] += 1 | ||
next memo | ||
end | ||
memo.push(char, 1) | ||
memo | ||
end.each_slice(2).map(&:reverse).flatten.map(&:to_s).join | ||
|
||
@inner_iterations.push(next_iteration) | ||
end | ||
|
||
def initialize(input_data) | ||
@inner_iterations = [input_data] | ||
end | ||
|
||
def to_i(desired_iteration = -1) | ||
iterations(desired_iteration).length | ||
end | ||
end | ||
end |