Skip to content

Commit

Permalink
Day 1 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
gregawoods committed Dec 1, 2023
1 parent 387c976 commit b718d6c
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 44 deletions.
25 changes: 25 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ gem 'debug'
gem 'minitest'
gem 'rake'
gem 'rubocop'
gem 'solargraph'
38 changes: 1 addition & 37 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -88,7 +53,6 @@ DEPENDENCIES
minitest
rake
rubocop
solargraph

BUNDLED WITH
2.3.22
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)}" }
)

Expand Down
Empty file removed examples/01.text
Empty file.
4 changes: 4 additions & 0 deletions examples/01_a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
7 changes: 7 additions & 0 deletions examples/01_b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
17 changes: 15 additions & 2 deletions lib/days/01.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions template/test
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'minitest/autorun'
require 'minitest/pride'
require_relative '../app'

class TestDay{DAY_NUMBER} < Minitest::Test
Expand Down
8 changes: 5 additions & 3 deletions test/01_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b718d6c

Please sign in to comment.