Skip to content

Commit

Permalink
Initial setup for AoC 2024
Browse files Browse the repository at this point in the history
Generators galore!
  • Loading branch information
ariejan committed Nov 15, 2024
1 parent 05e2ec3 commit 27a85f1
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 17 deletions.
19 changes: 2 additions & 17 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,11 @@
/tmp/

# Used by dotenv library to load environment variables.
# .env
.env

# Ignore Byebug command history file.
.byebug_history

## Specific to RubyMotion:
.dat*
.repl_history
build/
*.bridgesupport
build-iPhoneOS/
build-iPhoneSimulator/

## Specific to RubyMotion (use of CocoaPods):
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# vendor/Pods/

## Documentation cache and generated files:
/.yardoc/
/_yardoc/
Expand All @@ -42,6 +26,7 @@ build-iPhoneSimulator/
/.bundle/
/vendor/bundle
/lib/bundler/man/
.ruby-lsp

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
Expand Down
Empty file added .gitlab-ci.yml
Empty file.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format documentation
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.3.4
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

ruby '3.3.4'

gem 'rspec'
30 changes: 30 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.5.1)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.2)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)

PLATFORMS
arm64-darwin-23
ruby

DEPENDENCIES
rspec

RUBY VERSION
ruby 3.3.4p94

BUNDLED WITH
2.5.22
13 changes: 13 additions & 0 deletions bin/aoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env ruby

require_relative '../lib/application'

if ARGV.empty?
puts "Usage: bin/aoc DAY"
puts "Example: bin/aoc 1"
exit 1
end

day = ARGV[0].to_i
AdventOfCode2024.run(day)

47 changes: 47 additions & 0 deletions bin/generate
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env ruby

require 'fileutils'

def create_day(day)
day_str = day.to_s.rjust(2, '0')

# Create directories if they don't exist
FileUtils.mkdir_p('lib/solutions')
FileUtils.mkdir_p('lib/input')
FileUtils.mkdir_p('spec/solutions')
FileUtils.mkdir_p('spec/input')

# Copy solution template
solution_template = File.read('templates/day.rb')
File.write(
"lib/solutions/day_#{day_str}.rb",
solution_template % { day: day_str }
)

# Copy test template
test_template = File.read('templates/day_spec.rb')
File.write(
"spec/solutions/day_#{day_str}_spec.rb",
test_template % { day: day_str }
)

# Create empty input file
FileUtils.touch("lib/input/day_#{day_str}.txt")
FileUtils.touch("spec/input/day_#{day_str}_test.txt")

puts "Created files for day #{day_str}:"
puts " - lib/solutions/day_#{day_str}.rb"
puts " - lib/input/day_#{day_str}.txt"
puts " - spec/solutions/day_#{day_str}_spec.rb"
puts " - spec/input/day_#{day_str}_test.txt"
end

if ARGV.empty?
puts "Usage: bin/generate DAY"
puts "Example: bin/generate 1"
exit 1
end

day = ARGV[0].to_i
create_day(day)

24 changes: 24 additions & 0 deletions lib/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'fileutils'

class AdventOfCode2024
def self.run(day)
day_str = day.to_s.rjust(2, '0')
day_file = File.join(__dir__, 'solutions', "day_#{day_str.downcase}.rb")

begin
require day_file
solution_class = Object.const_get("Day#{day_str}")

input = File.read(
File.join(__dir__, 'input', "day_#{day_str}.txt")
).strip

puts "Part 1: #{solution_class.new.part_one(input)}"
puts "Part 2: #{solution_class.new.part_two(input)}"
rescue LoadError
puts "Error: Could not find solution file for day #{day_str}"
rescue NameError
puts "Error: Could not find Day#{day_str} class in solution file"
end
end
end
14 changes: 14 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'rspec/expectations'
require_relative '../lib/application'

RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end

config.shared_context_metadata_behavior = :apply_to_host_groups
end
10 changes: 10 additions & 0 deletions templates/day.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Day%{day}
def part_one(input)
input.split("\n").map(&:to_i).sum
end

def part_two(input)
0
end
end

19 changes: 19 additions & 0 deletions templates/day_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "spec_helper"
require "solutions/day_%{day}"

RSpec.describe Day%{day} do
let(:input) { File.read(File.join(__dir__, '..', '..', 'spec', 'input', 'day_%{day}_test.txt')).strip }

describe '#part_one' do
it 'calculates the correct solutions for part one' do
expect(subject.part_one(input)).to eq(0)
end
end

describe '#part_two' do
it 'calculates the correct solutions for part two' do
expect(subject.part_two(input)).to eq(0)
end
end
end

0 comments on commit 27a85f1

Please sign in to comment.