Add day 1

main
Guillaume Dott 2023-12-02 00:25:40 +01:00
commit 98c4be3aeb
12 changed files with 1269 additions and 0 deletions

1
.env-template 100644
View File

@ -0,0 +1 @@
AOC_COOKIE=AOC_COOKIE

1
.gitignore vendored 100644
View File

@ -0,0 +1 @@
.env

7
Gemfile 100644
View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem "aoc_rb"

44
Gemfile.lock 100644
View File

@ -0,0 +1,44 @@
GEM
remote: https://rubygems.org/
specs:
aoc_rb (0.2.5)
dotenv (~> 2.7.6)
httparty (~> 0.20.0)
nokogiri (~> 1.13.6)
rspec (~> 3.0)
thor (~> 1.2.1)
diff-lcs (1.5.0)
dotenv (2.7.6)
httparty (0.20.0)
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
mime-types (3.4.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2023.0218.1)
multi_xml (0.6.0)
nokogiri (1.13.10-x86_64-linux)
racc (~> 1.4)
racc (1.6.2)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.6)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.1)
thor (1.2.1)
PLATFORMS
x86_64-linux
DEPENDENCIES
aoc_rb
BUNDLED WITH
2.3.7

35
bin/aoc 100644
View File

@ -0,0 +1,35 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
#
# This file was generated by Bundler.
#
# The application 'aoc' is installed as part of a gem, and
# this file is here to facilitate running it.
#
# THIS IS THE BIN FILE
require "pathname"
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)
bundle_binstub = File.expand_path("../bundle", __FILE__)
if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end
$LOAD_PATH.unshift File.expand_path("../..", __FILE__)
require "rubygems"
require "bundler/setup"
# load Gem.bin_path("aoc_rb", "aoc")
require 'aoc_rb/app'

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,25 @@
## --- Day 1: Trebuchet?! ---
Something is wrong with global snow production, and you've been selected to take a look. The Elves have even given you a map; on it, they've used stars to mark the top fifty locations that are likely to be having problems.
You've been doing this long enough to know that to restore snow operations, you need to check all **fifty stars** by December 25th.
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants **one star**. Good luck!
You try to ask why they can't just use a weather machine ("not powerful enough") and where they're even sending you ("the sky") and why your map looks mostly blank ("you sure ask a lot of questions") and hang on did you just say the sky ("of course, where do you think snow comes from") when you realize that the Elves are already loading you into a trebuchet ("please hold still, we need to strap you in").
As they're making the final adjustments, they discover that their calibration document (your puzzle input) has been **amended** by a very young Elf who was apparently just excited to show off her art skills. Consequently, the Elves are having trouble reading the values on the document.
The newly-improved calibration document consists of lines of text; each line originally contained a specific **calibration value** that the Elves now need to recover. On each line, the calibration value can be found by combining the **first digit** and the **last digit** (in that order) to form a single **two-digit number**.
For example:
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
In this example, the calibration values of these four lines are ``12``, ``38``, ``15``, and ``77``. Adding these together produces ``142``.
Consider your entire calibration document. **What is the sum of all of the calibration values?**

View File

@ -0,0 +1,18 @@
## --- Part Two ---
Your calculation isn't quite right. It looks like some of the digits are actually **spelled out with letters**: ``one``, ``two``, ``three``, ``four``, ``five``, ``six``, ``seven``, ``eight``, and ``nine`` **also** count as valid "digits".
Equipped with this new information, you now need to find the real first and last digit on each line. For example:
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
In this example, the calibration values are ``29``, ``83``, ``13``, ``24``, ``42``, ``14``, and ``76``. Adding these together produces ``281``.
**What is the sum of all of the calibration values?**

View File

@ -0,0 +1,44 @@
# frozen_string_literal: true
module Year2023
class Day01 < Solution
# @input is available if you need the raw data input
# Call `data` to access either an array of the parsed data, or a single record for a 1-line input file
NUMBERS = {
'one' => 1,
'two' => 2,
'three' => 3,
'four' => 4,
'five' => 5,
'six' => 6,
'seven' => 7,
'eight' => 8,
'nine' => 9
}
def part_1
data.map do |line|
digits = line.gsub(/[^0-9]/, '')
"#{digits[0]}#{digits[-1]}".to_i
end.sum
end
def part_2
data.map do |line|
matches = line.scan(/(?=([0-9]|#{NUMBERS.keys.join('|')}))/).flatten
"#{NUMBERS[matches.first] || matches.first}#{NUMBERS[matches.last] || matches.last}".to_i
end.sum
end
private
# Processes each line of the input file and stores the result in the dataset
# def process_input(line)
# line.map(&:to_i)
# end
# Processes the dataset as a whole
# def process_dataset(set)
# set
# end
end
end

View File

@ -0,0 +1,34 @@
# frozen_string_literal: true %>
class Solution
def self.part_1(*input)
new(*input).part_1
end
def self.part_2(*input)
new(*input).part_2
end
def initialize(input)
@input = input
end
def data
@data ||= begin
processed = @input.lines(chomp: true).map do |line|
process_input line
end
processed.length == 1 ? processed.first : process_dataset(processed)
end
end
private
def process_input(line)
line
end
def process_dataset(set)
set
end
end

View File

@ -0,0 +1,47 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Year2023::Day01 do
let(:input) { File.read(File.join(File.dirname(__FILE__), "../../../challenges/2023/01/input.txt")) }
describe "part 1" do
let(:example_input) {
<<~EOF
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
EOF
}
it "returns 142 for the example input" do
expect(described_class.part_1(example_input)).to eq(142)
end
it "returns nil for my input" do
expect(described_class.part_1(input)).to eq(55834)
end
end
describe "part 2" do
let(:example_input) {
<<~EOF
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
EOF
}
it "returns nil for the example input" do
expect(described_class.part_2(example_input)).to eq(281)
end
it "returns nil for my input" do
expect(described_class.part_2(input)).to eq(53221)
end
end
end

View File

@ -0,0 +1,13 @@
# frozen_string_literal: true
Dir[File.join(File.dirname(__FILE__), "..", "challenges", "shared", "**", "*.rb")].each do |file|
require file
end
Dir[File.join(File.dirname(__FILE__), "..", "challenges", "20*", "**", "*.rb")].each do |file|
require file
end
RSpec.configure do |config|
end