-
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
d249338
commit a84d822
Showing
8 changed files
with
176 additions
and
17 deletions.
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
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,13 @@ | ||
2413432311323 | ||
3215453535623 | ||
3255245654254 | ||
3446585845452 | ||
4546657867536 | ||
1438598798454 | ||
4457876987766 | ||
3637877979653 | ||
4654967986887 | ||
4564679986453 | ||
1224686865563 | ||
2546548887735 | ||
4322674655533 |
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,105 @@ | ||
class Day17 | ||
require 'fc' | ||
include Util | ||
|
||
Crucible = Struct.new( | ||
:x, :y, :cost, :dir, :moves_in_dir | ||
) do | ||
|
||
def pos | ||
[x, y] | ||
end | ||
end | ||
|
||
def part1(input) | ||
grid = parse_grid(input, :to_i) | ||
goal = [grid.length - 1, grid.first.length - 1] | ||
|
||
queue = FastContainers::PriorityQueue.new(:min) | ||
queue.push(Crucible.new(0, 0, 0, :down, 0), 0) | ||
visited = Set.new | ||
|
||
loop do | ||
crucible = queue.top | ||
queue.pop | ||
|
||
return crucible.cost if crucible.pos == goal | ||
|
||
next_steps = available_moves(grid, crucible.pos, crucible.cost, crucible.dir, crucible.moves_in_dir) | ||
|
||
next_steps.each do |next_step| | ||
hash = [next_step.pos, next_step.dir, next_step.moves_in_dir] | ||
next if visited.include?(hash) | ||
|
||
visited.add(hash) | ||
queue.push(next_step, next_step.cost) | ||
end | ||
|
||
break if queue.none? | ||
end | ||
end | ||
|
||
LEFT_TO_RIGHT = :right | ||
RIGHT_TO_LEFT = :left | ||
DOWN = :down | ||
UP = :up | ||
|
||
def available_moves(grid, position, current_cost, current_direction, num_direction) | ||
moves = [] | ||
|
||
if position[1] != grid.first.length - 1 && | ||
current_direction != UP && | ||
!(current_direction == DOWN && num_direction >= 3) | ||
|
||
moves << Crucible.new( | ||
position[0], | ||
position[1] + 1, | ||
current_cost + grid[position[0]][position[1] + 1], | ||
DOWN, | ||
current_direction == DOWN ? num_direction + 1 : 1) | ||
end | ||
|
||
if !position[1].zero? && | ||
current_direction != DOWN && | ||
!(current_direction == UP && num_direction >= 3) | ||
|
||
moves << Crucible.new( | ||
position[0], | ||
position[1] - 1, | ||
current_cost + grid[position[0]][position[1] - 1], | ||
UP, | ||
current_direction == UP ? num_direction + 1 : 1) | ||
end | ||
|
||
if position[0] != grid.length - 1 && | ||
current_direction != RIGHT_TO_LEFT && | ||
!(current_direction == LEFT_TO_RIGHT && num_direction >= 3) | ||
|
||
moves << Crucible.new( | ||
position[0] + 1, | ||
position[1], | ||
current_cost + grid[position[0] + 1][position[1]], | ||
LEFT_TO_RIGHT, | ||
current_direction == LEFT_TO_RIGHT ? num_direction + 1 : 1) | ||
end | ||
|
||
if !position[0].zero? && | ||
current_direction != LEFT_TO_RIGHT && | ||
!(current_direction == RIGHT_TO_LEFT && num_direction >= 3) | ||
|
||
moves << Crucible.new( | ||
position[0] - 1, | ||
position[1], | ||
current_cost + grid[position[0] - 1][position[1]], | ||
RIGHT_TO_LEFT, | ||
current_direction == RIGHT_TO_LEFT ? num_direction + 1 : 1) | ||
end | ||
|
||
moves | ||
end | ||
|
||
def part2(_input) | ||
'TODO' | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module Util | ||
def parse_grid(input, fn = nil) | ||
lines = input.split("\n") | ||
grid = [] | ||
|
||
lines.length.times do |n| | ||
lines[n].chars.each_with_index do |char, i| | ||
grid[i] ||= [] | ||
grid[i] << (fn ? char.send(fn) : char) | ||
end | ||
end | ||
|
||
grid | ||
end | ||
|
||
def print_grid(grid) | ||
grid.first.length.times do |y| | ||
grid.length.times do |x| | ||
print grid[x][y] | ||
end | ||
print "\n" | ||
end | ||
end | ||
|
||
def percent(x, y) | ||
"#{(x / y.to_f * 100).round(1)}%" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'minitest/autorun' | ||
require 'minitest/pride' | ||
require_relative '../app' | ||
|
||
class TestDay17 < Minitest::Test | ||
def setup | ||
@data = File.read(File.join(APP_ROOT, 'examples', '17.txt')).rstrip | ||
@day = Day17.new | ||
end | ||
|
||
def test_part1 | ||
assert_equal @day.part1(@data), 102 | ||
end | ||
|
||
def test_part2 | ||
assert_equal @day.part2(@data), '' | ||
end | ||
end |