-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
5 changed files
with
121 additions
and
0 deletions.
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
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.
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,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}" |
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,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 |
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,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 |