Skip to content

Commit

Permalink
Year 2015: Day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Feb 9, 2024
1 parent 6c769c9 commit 8436203
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Consistency is hard without proper goals to set a good (let's dare say "atomic")
I'm also adding notes that may be useful if you're learning Ruby.

Notes for solving:
* [2015, up to day 09](year_2015.md)
* [2015, up to day 10](year_2015.md)
* [2023, up to day 03](year_2023.md)

# How to use
Expand Down
29 changes: 29 additions & 0 deletions spec/year_2015/day_10_spec.rb
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
15 changes: 15 additions & 0 deletions year_2015.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,18 @@ If you're not fond of graph transversal, the best answer is often to start using
You can then optimize it, for instance instructing the algorithm to stop searching once it's on a path longer than a previously explored one.

As for finding the "longest path"...just imagine you're not looking for a "shortest" or "longest" path, but a "best" path, and change how that path is selected among others.

## Day 10

```
Year2015::Day10
Part 1
iterates each look-and-say
Results
correctly answers part 1
correctly answers part 2
```

This exercise is based on John H. Conway's [Look-and-say sequences](https://en.wikipedia.org/wiki/Look-and-say_sequence), you probably know him for the [Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life), but the mathematician provided us with lot of science.

Nothing really hard in this exercise, except complexity quickly running high, it becomes important to use the best algorithm to generate the next sequence.
30 changes: 30 additions & 0 deletions year_2015/day_10.rb
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

0 comments on commit 8436203

Please sign in to comment.