refactor day15 a bit
Remove need to duplicate first value of pushable_boxes Fail fast when left branch produces :wall in part 2
This commit is contained in:
parent
0825d8db0e
commit
e8a87d4a9c
1 changed files with 13 additions and 17 deletions
|
@ -20,7 +20,7 @@ defmodule Day15 do
|
||||||
attempt_moves(next_moves, robot, warehouse)
|
attempt_moves(next_moves, robot, warehouse)
|
||||||
|
|
||||||
{:ok, :box} ->
|
{:ok, :box} ->
|
||||||
case get_pushable_boxes(next, move, warehouse, [next]) do
|
case get_pushable_boxes(next, move, warehouse) do
|
||||||
:wall ->
|
:wall ->
|
||||||
attempt_moves(next_moves, robot, warehouse)
|
attempt_moves(next_moves, robot, warehouse)
|
||||||
|
|
||||||
|
@ -35,13 +35,14 @@ defmodule Day15 do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_pushable_boxes(box, move, warehouse, pushable_boxes) do
|
def get_pushable_boxes(box, move, warehouse, pushable_boxes \\ []) do
|
||||||
next = move.(box)
|
next = move.(box)
|
||||||
|
pushable_boxes = [box | pushable_boxes]
|
||||||
|
|
||||||
case Map.fetch(warehouse, next) do
|
case Map.fetch(warehouse, next) do
|
||||||
:error -> pushable_boxes
|
:error -> pushable_boxes
|
||||||
{:ok, :wall} -> :wall
|
{:ok, :wall} -> :wall
|
||||||
{:ok, :box} -> get_pushable_boxes(next, move, warehouse, [next | pushable_boxes])
|
{:ok, :box} -> get_pushable_boxes(next, move, warehouse, pushable_boxes)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -78,7 +79,7 @@ defmodule Day15 do
|
||||||
attempt_moves2(next_moves, robot, warehouse)
|
attempt_moves2(next_moves, robot, warehouse)
|
||||||
|
|
||||||
{:ok, {:box, side}} ->
|
{:ok, {:box, side}} ->
|
||||||
case get_pushable_boxes2(next, side, move, warehouse, [{next, side}]) do
|
case get_pushable_boxes2(next, side, move, warehouse) do
|
||||||
:wall ->
|
:wall ->
|
||||||
attempt_moves2(next_moves, robot, warehouse)
|
attempt_moves2(next_moves, robot, warehouse)
|
||||||
|
|
||||||
|
@ -102,8 +103,9 @@ defmodule Day15 do
|
||||||
# [][] []<--make sure RHS moves too
|
# [][] []<--make sure RHS moves too
|
||||||
# []
|
# []
|
||||||
# @<- pushing up pushes all
|
# @<- pushing up pushes all
|
||||||
def get_pushable_boxes2(box, side, move, warehouse, pushable_boxes) do
|
def get_pushable_boxes2(box, side, move, warehouse, pushable_boxes \\ []) do
|
||||||
next = move.(box)
|
next = move.(box)
|
||||||
|
pushable_boxes = [{box, side} | pushable_boxes]
|
||||||
|
|
||||||
case abs(elem(next, 1) - elem(box, 1)) do
|
case abs(elem(next, 1) - elem(box, 1)) do
|
||||||
# horizontal
|
# horizontal
|
||||||
|
@ -116,9 +118,7 @@ defmodule Day15 do
|
||||||
:wall
|
:wall
|
||||||
|
|
||||||
{:ok, {:box, next_side}} ->
|
{:ok, {:box, next_side}} ->
|
||||||
get_pushable_boxes2(next, next_side, move, warehouse, [
|
get_pushable_boxes2(next, next_side, move, warehouse, pushable_boxes)
|
||||||
{next, next_side} | pushable_boxes
|
|
||||||
])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# vertical
|
# vertical
|
||||||
|
@ -154,27 +154,23 @@ defmodule Day15 do
|
||||||
:wall
|
:wall
|
||||||
|
|
||||||
{{{:ok, {:box, side}}, :error}, left, _right} ->
|
{{{:ok, {:box, side}}, :error}, left, _right} ->
|
||||||
get_pushable_boxes2(left, side, move, warehouse, [{left, side} | pushable_boxes])
|
get_pushable_boxes2(left, side, move, warehouse, pushable_boxes)
|
||||||
|
|
||||||
{{:error, {:ok, {:box, side}}}, _left, right} ->
|
{{:error, {:ok, {:box, side}}}, _left, right} ->
|
||||||
get_pushable_boxes2(right, side, move, warehouse, [{right, side} | pushable_boxes])
|
get_pushable_boxes2(right, side, move, warehouse, pushable_boxes)
|
||||||
|
|
||||||
{{{:ok, {:box, :left}}, {:ok, {:box, :right}}}, left, _} ->
|
{{{:ok, {:box, :left}}, {:ok, {:box, :right}}}, left, _} ->
|
||||||
# if left-right, just one box, only need to check one side
|
# if left-right, just one box, only need to check one side
|
||||||
# []
|
# []
|
||||||
# []
|
# []
|
||||||
get_pushable_boxes2(left, :left, move, warehouse, [{left, :left} | pushable_boxes])
|
get_pushable_boxes2(left, :left, move, warehouse, pushable_boxes)
|
||||||
|
|
||||||
{{{:ok, {:box, :right}}, {:ok, {:box, :left}}}, left, right} ->
|
{{{:ok, {:box, :right}}, {:ok, {:box, :left}}}, left, right} ->
|
||||||
# if right-left, two boxes, so need to check both...
|
# if right-left, two boxes, so need to check both...
|
||||||
# [][]
|
# [][]
|
||||||
# []
|
# []
|
||||||
left = get_pushable_boxes2(left, :right, move, warehouse, [{left, :right}])
|
with left when left != :wall <- get_pushable_boxes2(left, :right, move, warehouse, []),
|
||||||
right = get_pushable_boxes2(right, :left, move, warehouse, [{right, :left}])
|
right when right != :wall <- get_pushable_boxes2(right, :left, move, warehouse, []) do
|
||||||
|
|
||||||
if left == :wall or right == :wall do
|
|
||||||
:wall
|
|
||||||
else
|
|
||||||
pushable_boxes ++ left ++ right
|
pushable_boxes ++ left ++ right
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue