From c264899ee099414d5da88ece51dd6cbde47d8ce6 Mon Sep 17 00:00:00 2001 From: Philip Corliss Date: Fri, 8 Dec 2023 23:39:45 -0600 Subject: [PATCH] 2023 Day 9 Part 1 Complete --- 2023/09/Guardfile | 17 +++++++++++ 2023/09/README.md | 0 2023/09/run.rb | 8 ++++++ 2023/09/sequences.rb | 52 ++++++++++++++++++++++++++++++++++ 2023/09/spec/sequences_spec.rb | 44 ++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+) create mode 100644 2023/09/Guardfile create mode 100644 2023/09/README.md create mode 100755 2023/09/run.rb create mode 100644 2023/09/sequences.rb create mode 100644 2023/09/spec/sequences_spec.rb diff --git a/2023/09/Guardfile b/2023/09/Guardfile new file mode 100644 index 0000000..77abec9 --- /dev/null +++ b/2023/09/Guardfile @@ -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" diff --git a/2023/09/README.md b/2023/09/README.md new file mode 100644 index 0000000..e69de29 diff --git a/2023/09/run.rb b/2023/09/run.rb new file mode 100755 index 0000000..61c116e --- /dev/null +++ b/2023/09/run.rb @@ -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}" \ No newline at end of file diff --git a/2023/09/sequences.rb b/2023/09/sequences.rb new file mode 100644 index 0000000..a95deea --- /dev/null +++ b/2023/09/sequences.rb @@ -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 diff --git a/2023/09/spec/sequences_spec.rb b/2023/09/spec/sequences_spec.rb new file mode 100644 index 0000000..5c2286a --- /dev/null +++ b/2023/09/spec/sequences_spec.rb @@ -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