-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25fd975
commit 48a1561
Showing
4 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'year_2016/day_13' | ||
|
||
describe Year2016::Day13 do | ||
context 'when Part 1' do | ||
subject(:sample_one) do | ||
described_class.new(10, 7, 4, true) | ||
end | ||
|
||
it 'gives a final result' do | ||
expect(sample_one.to_i).to eq(11) | ||
end | ||
end | ||
|
||
context 'when Results' do | ||
it 'correctly answers part 1' do | ||
expect(described_class.new(1_352, 31, 39, true).to_i).to eq(90) | ||
end | ||
|
||
it 'correctly answers part 2' do | ||
expect(described_class.new(1_352, 31, 39).to_i).to eq(135) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
class Year2016 | ||
class Day13 | ||
class << self | ||
def next_moves(state) | ||
[ | ||
[state[0] - 1, state[1]], | ||
[state[0] + 1, state[1]], | ||
[state[0], state[1] - 1], | ||
[state[0], state[1] + 1] | ||
].reject do |x, y| | ||
x.negative? || y.negative? | ||
end | ||
end | ||
end | ||
|
||
def initialize(input_favorite, input_x, input_y, input_part_one = false) | ||
@version = input_part_one ? 1 : 2 | ||
@favorite = input_favorite | ||
@reach_x = input_x | ||
@reach_y = input_y | ||
end | ||
|
||
def cell_value(pos_x, pos_y) | ||
[ | ||
(pos_x * pos_x), | ||
(3 * pos_x), | ||
(2 * pos_x * pos_y), | ||
pos_y, | ||
(pos_y * pos_y) | ||
].sum | ||
end | ||
|
||
def cell_is_a_wall?(pos) | ||
(cell_value(pos[0], pos[1]) + @favorite).to_s(2).chars.count('1').odd? | ||
end | ||
|
||
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity | ||
def search(states, moves_tried, moves) | ||
return moves if @version == 1 && states.any?([@reach_x, @reach_y]) | ||
return moves_tried.count{|pos| !cell_is_a_wall?(pos) } if @version == 2 && moves == 51 | ||
|
||
next_states = [] | ||
states.each do |state| | ||
next if moves_tried.include?(state) | ||
|
||
moves_tried.add(state) | ||
next if cell_is_a_wall?(state) | ||
|
||
Day13.next_moves(state).each do |next_state| | ||
next_states.push(next_state) unless moves_tried.include?(next_state) | ||
end | ||
end | ||
search(next_states, moves_tried, moves + 1) | ||
end | ||
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity | ||
|
||
def to_i | ||
search([[1, 1]], Set.new, 0) | ||
end | ||
end | ||
end |