From f2cff93c38dd3b023a6a2c9febd804a4aba2a8bc Mon Sep 17 00:00:00 2001 From: Philip Corliss Date: Thu, 30 Nov 2023 23:50:46 -0600 Subject: [PATCH] 2023 Part 1 Complete --- 2023/01/Guardfile | 17 +++++++++++++++ 2023/01/README.md | 0 2023/01/run.rb | 8 +++++++ 2023/01/spec/trebuchet_spec.rb | 39 ++++++++++++++++++++++++++++++++++ 2023/01/trebuchet.rb | 32 ++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+) create mode 100644 2023/01/Guardfile create mode 100644 2023/01/README.md create mode 100755 2023/01/run.rb create mode 100644 2023/01/spec/trebuchet_spec.rb create mode 100644 2023/01/trebuchet.rb diff --git a/2023/01/Guardfile b/2023/01/Guardfile new file mode 100644 index 0000000..77abec9 --- /dev/null +++ b/2023/01/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/01/README.md b/2023/01/README.md new file mode 100644 index 0000000..e69de29 diff --git a/2023/01/run.rb b/2023/01/run.rb new file mode 100755 index 0000000..3591b22 --- /dev/null +++ b/2023/01/run.rb @@ -0,0 +1,8 @@ +#!/usr/bin/env ruby + +require_relative 'trebuchet' + +input = File.read('./input.txt') + +ad = Advent::Trebuchet.new(input) +puts "Sum: #{ad.sum}" diff --git a/2023/01/spec/trebuchet_spec.rb b/2023/01/spec/trebuchet_spec.rb new file mode 100644 index 0000000..dab66af --- /dev/null +++ b/2023/01/spec/trebuchet_spec.rb @@ -0,0 +1,39 @@ +require './trebuchet.rb' +require 'rspec' +require 'pry' + +describe Advent do + + let(:input) { + <<~EOS + 1abc2 + pqr3stu8vwx + a1b2c3d4e5f + treb7uchet + EOS + } + + describe Advent::Trebuchet do + let(:ad) { Advent::Trebuchet.new(input) } + + describe "#new" do + end + + describe "#nums" do + it "returns an array of numbers" do + expect(ad.nums).to eq([12,38,15,77]) + end + + it "only grabs a single digit" do + ad = Advent::Trebuchet.new("12abc2") + expect(ad.nums).to eq([12]) + end + end + + context "validation" do + it "validates part1 examples" do + expect(ad.sum).to eq(142) + end + end + end +end diff --git a/2023/01/trebuchet.rb b/2023/01/trebuchet.rb new file mode 100644 index 0000000..16e104a --- /dev/null +++ b/2023/01/trebuchet.rb @@ -0,0 +1,32 @@ +require 'set' +require '../lib/grid.rb' +require '../lib/ring.rb' + +module Advent + + class Trebuchet + attr_accessor :debug + + def initialize(input) + @debug = false + @lines = input.each_line.map(&:strip) + end + + def nums + @nums ||= @lines.map do |line| + numbers_in_string = line.scan(/\d/).map(&:to_i) + a = numbers_in_string.first + b = numbers_in_string.last + a * 10 + b + end + end + + def sum + nums.sum + end + + def debug! + @debug = true + end + end +end