From b718d6c1c8efa9a244b0059803de0be85fc8cf12 Mon Sep 17 00:00:00 2001 From: Greg Woods Date: Fri, 1 Dec 2023 09:17:36 -0500 Subject: [PATCH] Day 1 solution --- .vscode/launch.json | 25 +++++++++++++++++++++++++ Gemfile | 1 - Gemfile.lock | 38 +------------------------------------- Rakefile | 2 +- examples/01.text | 0 examples/01_a.txt | 4 ++++ examples/01_b.txt | 7 +++++++ lib/days/01.rb | 17 +++++++++++++++-- template/test | 1 + test/01_test.rb | 8 +++++--- 10 files changed, 59 insertions(+), 44 deletions(-) create mode 100644 .vscode/launch.json delete mode 100644 examples/01.text create mode 100644 examples/01_a.txt create mode 100644 examples/01_b.txt diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..cba410a --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,25 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "ruby_lsp", + "name": "Debug script", + "request": "launch", + "program": "ruby ${file}" + }, + { + "type": "ruby_lsp", + "name": "Debug test", + "request": "launch", + "program": "ruby -Itest ${relativeFile}" + }, + { + "type": "ruby_lsp", + "name": "Attach debugger", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/Gemfile b/Gemfile index f2d469e..a9ac540 100644 --- a/Gemfile +++ b/Gemfile @@ -6,4 +6,3 @@ gem 'debug' gem 'minitest' gem 'rake' gem 'rubocop' -gem 'solargraph' diff --git a/Gemfile.lock b/Gemfile.lock index 053f98b..af9bbd0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,29 +2,16 @@ GEM remote: https://rubygems.org/ specs: ast (2.4.2) - backport (1.2.0) - benchmark (0.3.0) debug (1.8.0) irb (>= 1.5.0) reline (>= 0.3.1) - diff-lcs (1.5.0) - e2mmap (0.1.0) io-console (0.6.0) irb (1.9.1) rdoc reline (>= 0.3.8) - jaro_winkler (1.5.6) json (2.7.0) - kramdown (2.4.0) - rexml - kramdown-parser-gfm (1.1.0) - kramdown (~> 2.0) language_server-protocol (3.17.0.3) minitest (5.20.0) - nokogiri (1.15.5-arm64-darwin) - racc (~> 1.4) - nokogiri (1.15.5-x64-mingw-ucrt) - racc (~> 1.4) parallel (1.23.0) parser (3.2.2.4) ast (~> 2.4.1) @@ -34,14 +21,11 @@ GEM racc (1.7.3) rainbow (3.1.1) rake (13.1.0) - rbs (2.8.4) - rdoc (6.6.0) + rdoc (6.4.0) psych (>= 4.0.0) regexp_parser (2.8.2) reline (0.4.1) io-console (~> 0.5) - reverse_markdown (2.1.1) - nokogiri rexml (3.2.6) rubocop (1.58.0) json (~> 2.3) @@ -57,27 +41,8 @@ GEM rubocop-ast (1.30.0) parser (>= 3.2.1.0) ruby-progressbar (1.13.0) - solargraph (0.49.0) - backport (~> 1.2) - benchmark - bundler (~> 2.0) - diff-lcs (~> 1.4) - e2mmap - jaro_winkler (~> 1.5) - kramdown (~> 2.3) - kramdown-parser-gfm (~> 1.1) - parser (~> 3.0) - rbs (~> 2.0) - reverse_markdown (~> 2.0) - rubocop (~> 1.38) - thor (~> 1.0) - tilt (~> 2.0) - yard (~> 0.9, >= 0.9.24) stringio (3.1.0) - thor (1.3.0) - tilt (2.3.0) unicode-display_width (2.5.0) - yard (0.9.34) PLATFORMS arm64-darwin-21 @@ -88,7 +53,6 @@ DEPENDENCIES minitest rake rubocop - solargraph BUNDLED WITH 2.3.22 diff --git a/Rakefile b/Rakefile index a361f4b..7e4345f 100644 --- a/Rakefile +++ b/Rakefile @@ -48,7 +48,7 @@ namespace :download do puts "⬇️ Download day #{number} input" response = Net::HTTP.get_response( - URI("https://adventofcode.com/2022/day/#{number}/input"), + URI("https://adventofcode.com/2023/day/#{number}/input"), { 'Cookie' => "session=#{File.read(session_path)}" } ) diff --git a/examples/01.text b/examples/01.text deleted file mode 100644 index e69de29..0000000 diff --git a/examples/01_a.txt b/examples/01_a.txt new file mode 100644 index 0000000..7bbc69a --- /dev/null +++ b/examples/01_a.txt @@ -0,0 +1,4 @@ +1abc2 +pqr3stu8vwx +a1b2c3d4e5f +treb7uchet diff --git a/examples/01_b.txt b/examples/01_b.txt new file mode 100644 index 0000000..41aa89c --- /dev/null +++ b/examples/01_b.txt @@ -0,0 +1,7 @@ +two1nine +eightwothree +abcone2threexyz +xtwone3four +4nineeightseven2 +zoneight234 +7pqrstsixteen diff --git a/lib/days/01.rb b/lib/days/01.rb index 9fc4372..35dfbb7 100644 --- a/lib/days/01.rb +++ b/lib/days/01.rb @@ -1,11 +1,24 @@ class Day01 def part1(input) - 'TODO' + input.split("\n").map do |line| + nums = line.chars.grep(/\d/) + (nums.first + nums.last).to_i + end.sum end + DIGITS = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'].freeze + def part2(input) - 'TODO' + regex = Regexp.new("(?=(#{DIGITS.join('|')}|\\d))") + + input.split("\n").map do |line| + nums = line.scan(regex).flatten.map do |m| + DIGITS.include?(m) ? DIGITS.index(m) : m + end + + "#{nums.first}#{nums.last}".to_i + end.sum end end diff --git a/template/test b/template/test index 1ca589e..8b0ef62 100644 --- a/template/test +++ b/template/test @@ -1,4 +1,5 @@ require 'minitest/autorun' +require 'minitest/pride' require_relative '../app' class TestDay{DAY_NUMBER} < Minitest::Test diff --git a/test/01_test.rb b/test/01_test.rb index a0d58de..7ed6dca 100644 --- a/test/01_test.rb +++ b/test/01_test.rb @@ -1,17 +1,19 @@ require 'minitest/autorun' +require 'minitest/pride' require_relative '../app' class TestDay01 < Minitest::Test def setup - @data = File.read(File.join(APP_ROOT, 'examples', '01.txt')).rstrip + @data_a = File.read(File.join(APP_ROOT, 'examples', '01_a.txt')).rstrip + @data_b = File.read(File.join(APP_ROOT, 'examples', '01_b.txt')).rstrip @day = Day01.new end def test_part1 - assert_equal @day.part1(@data), '' + assert_equal @day.part1(@data_a), 142 end def test_part2 - assert_equal @day.part2(@data), '' + assert_equal @day.part2(@data_b), 281 end end