Skip to content

Commit

Permalink
2023 Day 9 Part 1 Complete
Browse files Browse the repository at this point in the history
  • Loading branch information
pcorliss committed Dec 9, 2023
1 parent 58935c9 commit c264899
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
17 changes: 17 additions & 0 deletions 2023/09/Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#guard :shell do
# watch(%r{^*\.rb}) { `bundle exec rspec --force-color -f doc spec/ ` }
#end

guard 'rspec', cmd: 'bundle exec rspec --force-color -f doc spec/', :all_on_start => true do
watch(%r{^([^/]+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^spec/.*_spec\.rb$})
end

notification :tmux,
display_message: true,
timeout: 5, # in seconds
default_message_format: '%s >> %s',
success: "green",
failed: "red",
pending: "yellow",
default: "green"
Empty file added 2023/09/README.md
Empty file.
8 changes: 8 additions & 0 deletions 2023/09/run.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env ruby

require_relative 'sequences'

input = File.read('./input.txt')

ad = Advent::Sequences.new(input)
puts "Part 1 Sum: #{ad.sum_next_number}"
52 changes: 52 additions & 0 deletions 2023/09/sequences.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'set'
require '../lib/grid.rb'
require '../lib/ring.rb'

module Advent

class Sequences
attr_accessor :debug
attr_reader :sequences

def initialize(input)
@debug = false
@sequences = input.lines.map do |line|
line.split.map(&:to_i)
end
end

def debug!
@debug = true
end

def diffs(sequence)
sequence.each_cons(2).map do |a, b|
b - a
end
end

def next_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 << working.last.last + seq.last
end

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

def sum_next_number
@sequences.sum do |seq|
next_number(seq)
end
end
end
end
44 changes: 44 additions & 0 deletions 2023/09/spec/sequences_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require './sequences.rb'
require 'rspec'
require 'pry'

describe Advent do

let(:input) {
<<~EOS
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45
EOS
}

describe Advent::Sequences do
let(:ad) { Advent::Sequences.new(input) }

describe "#new" do
it "inits sequences" do
expect(ad.sequences).to eq([
[0, 3, 6, 9, 12, 15],
[1, 3, 6, 10, 15, 21],
[10, 13, 16, 21, 30, 45],
])
end
end

describe "#next_number" do
it "returns the next number in the sequence" do
# ad.debug!
expect(ad.next_number([0, 3, 6, 9, 12, 15])).to eq(18)
expect(ad.next_number([1, 3, 6, 10, 15, 21])).to eq(28)
expect(ad.next_number([10, 13, 16, 21, 30, 45])).to eq(68)
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
end
end
end

0 comments on commit c264899

Please sign in to comment.