Skip to content

Commit

Permalink
Year 2023: Day 04
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Feb 24, 2024
1 parent 856e1e3 commit 2d00862
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ I'm also adding notes that may be useful if you're learning Ruby.
Notes for solving:
* [2015, complete](year_2015.md)
* [2016, up to day 08](year_2016.md)
* [2023, up to day 03](year_2023.md)
* [2023, up to day 04](year_2023.md)

# How to use
Install dependencies with `bundle install`. If you are in a hurry, just install [RSpec](https://github.com/rspec/rspec-metagem) with `gem install rspec` and run `rspec` in the root directory.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions spec/year_2015/day_04_spec.rb → spec/year_2023/day_04_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'year_2015/day_04'
require 'year_2023/day_04'

describe Year2015::Day04 do
describe Year2023::Day04 do
context 'when Part 1' do
subject(:sample_one) do
described_class.new(File.read('spec/year_2015/day_04_sample_one'), true)
described_class.new(File.read('spec/year_2023/day_04_sample_one'), true)
end

it 'finds winning numbers' do
Expand All @@ -25,7 +25,7 @@

context 'when Part 2' do
subject(:sample_two) do
described_class.new(File.read('spec/year_2015/day_04_sample_one'))
described_class.new(File.read('spec/year_2023/day_04_sample_one'))
end

it 'distributes cards' do
Expand All @@ -41,7 +41,7 @@

context 'when Results' do
subject(:input_data) do
File.read('spec/year_2015/day_04_input')
File.read('spec/year_2023/day_04_input')
end

it 'correctly answers part 1' do
Expand Down
22 changes: 21 additions & 1 deletion year_2023.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,24 @@ I'm clearly not good enough with spatial representations (something about off-by

Maybe I'll revisit this one with a better solution.

Also, thanks to the kind people or [/r/adventofcode](https://www.reddit.com/r/adventofcode/comments/189q9wv/2023_day_3_another_sample_grid_to_use/) for the sample grids they provided, they were a great help in debugging the first part properly.
Also, thanks to the kind people or [/r/adventofcode](https://www.reddit.com/r/adventofcode/comments/189q9wv/2023_day_3_another_sample_grid_to_use/) for the sample grids they provided, they were a great help in debugging the first part properly.

## Day 04: Scratchcards

```
Year2015::Day04
Part 1
finds winning numbers
calculates points
gives a final result
Part 2
distributes cards
gives a final result
Results
correctly answers part 1
correctly answers part 2
```

Part one is pretty easy. One useful method is the [`#&`](https://ruby-doc.org/core-3.0.1/Array.html#method-i-26) operator that takes two arrays and returns a new one with only the matching contents. The syntax is clearly inspired by [bit masking](https://en.wikipedia.org/wiki/Mask_(computing)), which is a subject you should look into if you've never heard of it.

Part two is a bit more annoying and requires to do a two-pass, but nothing really improbable.
2 changes: 1 addition & 1 deletion year_2015/day_04.rb → year_2023/day_04.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Year2015
class Year2023
class Day04
attr_reader :version, :lines

Expand Down

0 comments on commit 2d00862

Please sign in to comment.