Skip to content

Commit

Permalink
Year 2016: Day 19
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Mar 13, 2024
1 parent bf08043 commit bc22dc6
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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 18](year_2016.md)
* [2016, up to day 19](year_2016.md)
* [2023, up to day 04](year_2023.md)

# How to use
Expand Down
1 change: 1 addition & 0 deletions spec/year_2016/day_19_input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3012210
29 changes: 29 additions & 0 deletions spec/year_2016/day_19_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'year_2016/day_19'

describe Year2016::Day19 do
context 'when Part 1' do
it 'gives a final result' do
expect(described_class.new('5', true).solve).to eq(3)
end
end

context 'when Part 2' do
it 'gives a final result' do
expect(described_class.new('5').solve).to eq(2)
end
end

context 'when Results' do
subject(:input_data) do
File.read('spec/year_2016/day_19_input')
end

it 'correctly answers part 1' do
expect(described_class.new(input_data, true).solve).to eq(1_830_117)
end

it 'correctly answers part 2' do
expect(described_class.new(input_data).solve).to eq(1_417_887)
end
end
end
17 changes: 17 additions & 0 deletions year_2016.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,20 @@ Very funny exercise. For testing purpose, the first version generated each strin
The second version took...around 20 seconds for 400K lines, which was sub-optimal.

One thing you should always strive to do is not just *read the rules*, but *understand* them. In this case, the four rules for a trap can be summed up as: "different left and right will produce a trap".

## Day 19: An Elephant Named Joseph

```
Year2016::Day19
when Part 1
gives a final result
when Part 2
gives a final result
when Results
correctly answers part 1
correctly answers part 2
```

Oftentimes, the real answer to an exercise is all about understanding the math problem hidden behind. In this case, it's [Josephus Problem](https://en.wikipedia.org/wiki/Josephus_problem#Bitwise) (for the first part at least), and the answer is ridiculously simple.

For the second part, treating the elves as two sets with one stealing from the other makes the most sense, however, if you were to look at the first hundred results, you'd notice [an interesting pattern emerging](https://github.com/rHermes/adventofcode/blob/master/2016/19/y2016_d19_p02.py).
30 changes: 30 additions & 0 deletions year_2016/day_19.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Year2016
class Day19
def initialize(input_data, input_part_one = false)
@version = input_part_one ? 1 : 2
@input = input_data.chomp.to_i
end

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def solve
return @input.to_s(2).chars.rotate(1).join.to_i(2) if @version == 1

# Nice trick from https://pastebin.com/Zm7tLbAe
mid = (@input + 1.0) / 2
arr_v1 = Array.new(mid.floor - 1){|i| i + 1 }
arr_v2 = Array.new(mid.ceil.to_i){|i| mid + i }

loop do
if arr_v2.length >= arr_v1.length
arr_v2.shift
return arr_v1.first if arr_v2.empty?
else
arr_v1.pop
end
arr_v1.push(arr_v2.shift)
arr_v2.push(arr_v1.shift)
end
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
end
end

0 comments on commit bc22dc6

Please sign in to comment.