Skip to content

Commit

Permalink
chore: refactor day16a to prepare for part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaxwood committed Dec 16, 2023
1 parent a24b97f commit 5c64699
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 9 additions & 5 deletions lib/day16.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ def load(file)
}
end

def part1
start = [0, 0]
def part1(start, facing)
best = 0
queue = [[start, @direction_map[[@map[start], :east]].first]]
visited = Set.new
iterations = 0
visited = Set.new
queue = []

@direction_map[[@map[start], facing]].each do |direction|
queue << [start, direction]
end

while queue.any?
iterations += 1
Expand All @@ -65,8 +68,9 @@ def part1
queue << [move, new_direction]
end

next unless iterations % 1000000 == 0
next unless (iterations % 1_000_000).zero?
return best if best == visited.length

best = visited.length
end
best
Expand Down
4 changes: 2 additions & 2 deletions test/unit/day16_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ class Day16Test < Minitest::Test
def test_day16a
sut = Day16.new
sut.load('data/day16a.txt')
assert_equal(46, sut.part1)
assert_equal(46, sut.part1([0, 0], :east))
end

def _test_day16_part1
sut = Day16.new
sut.load('data/day16.txt')
assert_equal(7884, sut.part1)
assert_equal(7884, sut.part1([0, 0], :east))
end
end

0 comments on commit 5c64699

Please sign in to comment.