Skip to content

Commit

Permalink
Year 2016: Day 18
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Mar 13, 2024
1 parent 5c87ee0 commit bf08043
Show file tree
Hide file tree
Showing 5 changed files with 68 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 17](year_2016.md)
* [2016, up to day 18](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_18_input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
^.^^^.^..^....^^....^^^^.^^.^...^^.^.^^.^^.^^..^.^...^.^..^.^^.^..^.....^^^.^.^^^..^^...^^^...^...^.
25 changes: 25 additions & 0 deletions spec/year_2016/day_18_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'year_2016/day_18'

describe Year2016::Day18 do
context 'when Part 1' do
it 'gives a final result' do
[['..^^.', 3, 6], ['.^^.^.^^^^', 10, 38]].each do |input, len, result|
expect(described_class.new(input, true).to_i(len)).to eq(result)
end
end
end

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

it 'correctly answers part 1' do
expect(described_class.new(input_data, true).to_i(40)).to eq(1926)
end

it 'correctly answers part 2' do
expect(described_class.new(input_data).to_i(400_000)).to eq(19_986_699)
end
end
end
17 changes: 17 additions & 0 deletions year_2016.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,20 @@ Year2016::Day17
```

Nothing we haven't seen yet.

## Day 18: Like a Rogue

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

Very funny exercise. For testing purpose, the first version generated each string and kept it in memory to compare it in testing. Ugh.

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".
24 changes: 24 additions & 0 deletions year_2016/day_18.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Year2016
class Day18
def initialize(input_data, input_part_one = false)
@version = input_part_one ? 1 : 2
@input = input_data.chomp.chars.map do |char|
char == '.'
end
end

def to_i(len)
safe_tiles_cnt = @input.count(true)
1.upto(len - 1) do |_idx|
@input = @input.map.each_with_index do |_char, idx|
left = idx.zero? ? true : @input[idx - 1]
right = @input[idx + 1].nil? ? true : @input[idx + 1]
left == right
end

safe_tiles_cnt += @input.count(true)
end
safe_tiles_cnt
end
end
end

0 comments on commit bf08043

Please sign in to comment.