Skip to content

Commit

Permalink
2023 Day 9 Part 2 Completed
Browse files Browse the repository at this point in the history
  • Loading branch information
pcorliss committed Dec 9, 2023
1 parent c264899 commit ee0ecec
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
3 changes: 2 additions & 1 deletion 2023/09/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
input = File.read('./input.txt')

ad = Advent::Sequences.new(input)
puts "Part 1 Sum: #{ad.sum_next_number}"
puts "Part 1 Sum: #{ad.sum_next_number}"
puts "Part 2 Sum: #{ad.sum_prev_number}"
24 changes: 24 additions & 0 deletions 2023/09/sequences.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,34 @@ def next_number(sequence)
working.last.last
end

def prev_number(sequence)
working = [sequence.dup]
puts "Working A: #{working.inspect}" if @debug
until working.last.all?(&:zero?) do
working << diffs(working.last)
puts "Working B: #{working.inspect}" if @debug
end

while working.length > 1 do
puts "Working C: #{working.inspect}" if @debug
seq = working.pop
working.last.unshift(working.last.first - seq.first)
end

puts "Working D: #{working.inspect}" if @debug
working.last.first
end

def sum_next_number
@sequences.sum do |seq|
next_number(seq)
end
end

def sum_prev_number
@sequences.sum do |seq|
prev_number(seq)
end
end
end
end
14 changes: 13 additions & 1 deletion 2023/09/spec/sequences_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,23 @@
end
end

context "validation" do
describe "#prev_number" do
it "returns the next number in the sequence" do
# ad.debug!
expect(ad.prev_number([0, 3, 6, 9, 12, 15])).to eq(-3)
expect(ad.prev_number([1, 3, 6, 10, 15, 21])).to eq(0)
expect(ad.prev_number([10, 13, 16, 21, 30, 45])).to eq(5)
end
end

context "validation" do
it "returns the sum of the next number in the sequence for part 1" do
expect(ad.sum_next_number).to eq(114)
end

it "returns the sum of the prev number in the sequence for part 2" do
expect(ad.sum_prev_number).to eq(2)
end
end
end
end

0 comments on commit ee0ecec

Please sign in to comment.