Day 22 - Advent of Code 2022
Mix.install([:kino, :benchee])
:ok
Links
Prompt
--- Day 22: Monkey Map ---
The monkeys take you on a surprisingly easy trail through the jungle. They're even going in roughly the right direction according to your handheld device's Grove Positioning System.
As you walk, the monkeys explain that the grove is protected by a force field. To pass through the force field, you have to enter a password; doing so involves tracing a specific path on a strangely-shaped board.
At least, you're pretty sure that's what you have to do; the elephants aren't exactly fluent in monkey.
The monkeys give you notes that they took when they last saw the password entered (your puzzle input).
For example:
...#
.#..
#...
....
...#.......#
........#...
..#....#....
..........#.
...#....
.....#..
.#......
......#.
10R5L5R10L4R5L5
The first half of the monkeys' notes is a map of the board. It is comprised of a set of open tiles (on which you can move, drawn .) and solid walls (tiles which you cannot enter, drawn #).
The second half is a description of the path you must follow. It consists of alternating numbers and letters:
- A number indicates the number of tiles to move in the direction you are facing. If you run into a wall, you stop moving forward and continue with the next instruction.
- A letter indicates whether to turn 90 degrees clockwise (
R) or counterclockwise (L). Turning happens in-place; it does not change your current tile.
So, a path like 10R5 means "go forward 10 tiles, then turn clockwise 90 degrees, then go forward 5 tiles".
You begin the path in the leftmost open tile of the top row of tiles. Initially, you are facing to the right (from the perspective of how the map is drawn).
If a movement instruction would take you off of the map, you wrap around to the other side of the board. In other words, if your next tile is off of the board, you should instead look in the direction opposite of your current facing as far as you can until you find the opposite edge of the board, then reappear there.
For example, if you are at A and facing to the right, the tile in front of you is marked B; if you are at C and facing down, the tile in front of you is marked D:
...#
.#..
#...
....
...#.D.....#
........#...
B.#....#...A
.....C....#.
...#....
.....#..
.#......
......#.
It is possible for the next tile (after wrapping around) to be a wall; this still counts as there being a wall in front of you, and so movement stops before you actually wrap to the other side of the board.
By drawing the last facing you had with an arrow on each tile you visit, the full path taken by the above example looks like this:
>>v#
.#v.
#.v.
..v.
...#...v..v#
>>>v...>#.>>
..#v...#....
...>>>>v..#.
...#....
.....#..
.#......
......#.
To finish providing the password to this strange input device, you need to determine numbers for your final row, column, and facing as your final position appears from the perspective of the original map. Rows start from 1 at the top and count downward; columns start from 1 at the left and count rightward. (In the above example, row 1, column 1 refers to the empty space with no tile on it in the top-left corner.) Facing is 0 for right (>), 1 for down (v), 2 for left (<), and 3 for up (^). The final password is the sum of 1000 times the row, 4 times the column, and the facing.
In the above example, the final row is 6, the final column is 8, and the final facing is 0. So, the final password is 1000 * 6 + 4 * 8 + 0: 6032.
Follow the path given in the monkeys' notes. What is the final password?
To begin, get your puzzle input.
--- Part Two ---
As you reach the force field, you think you hear some Elves in the distance. Perhaps they've already arrived?
You approach the strange input device, but it isn't quite what the monkeys drew in their notes. Instead, you are met with a large cube; each of its six faces is a square of 50x50 tiles.
To be fair, the monkeys' map does have six 50x50 regions on it. If you were to carefully fold the map, you should be able to shape it into a cube!
In the example above, the six (smaller, 4x4) faces of the cube are:
1111
1111
1111
1111
222233334444
222233334444
222233334444
222233334444
55556666
55556666
55556666
55556666
You still start in the same position and with the same facing as before, but the wrapping rules are different. Now, if you would walk off the board, you instead proceed around the cube. From the perspective of the map, this can look a little strange. In the above example, if you are at A and move to the right, you would arrive at B facing down; if you are at C and move down, you would arrive at D facing up:
...#
.#..
#...
....
...#.......#
........#..A
..#....#....
.D........#.
...#..B.
.....#..
.#......
..C...#.
Walls still block your path, even if they are on a different face of the cube. If you are at E facing up, your movement is blocked by the wall marked by the arrow:
...#
.#..
-->#...
....
...#..E....#
........#...
..#....#....
..........#.
...#....
.....#..
.#......
......#.
Using the same method of drawing the last facing you had with an arrow on each tile you visit, the full path taken by the above example now looks like this:
>>v#
.#v.
#.v.
..v.
...#..^...v#
.>>>>>^.#.>>
.^#....#....
.^........#.
...#..v.
.....#v.
.#v<<<<.
..v...#.
The final password is still calculated from your final position and facing from the perspective of the map. In this example, the final row is 5, the final column is 7, and the final facing is 3, so the final password is 1000 * 5 + 4 * 7 + 3 = 5031.
Fold the map into a cube, then follow the path given in the monkeys' notes. What is the final password?
Although it hasn't changed, you can still get your puzzle input.
Input
input = Kino.Input.textarea("Please paste your input file:")
input = input |> Kino.Input.read()
" ..#...........##..#..............#.....................................#..#....#..#..#..............\n ...............#........#............#..........................................#..............#....\n ...........#..................................#.......#..#..#.................#...................#.\n ..........#.....#.................#.......#....#..#..........#................................#.....\n ...#............#...............................................................#...#.....#.........\n ..#............#..#........#.................#..#....#.......#....................#............#....\n ..#..........#......#...................#.........#.................................#.....#.........\n ......................#..#.....#....#........##....#...............#...............#......#..#......\n ..........#...............................##...................................#...#................\n .#................#...................#......#................#...........#.........#............#.#\n .#....................#....#........##.#.......##....#.......#.......................#............#.\n .......................#...#....................#...............#.....#....................#..#.....\n ..........##.......#...#......................#.....#........#..............#..##...................\n ........#.#.............................#.........#................................#....#.........#.\n ..........................................#.....#................#.............#...#................\n ...........#...............................................#..........#...#.....#....#.............#\n ..#....................##.........#.........#.............#...#.....#...............................\n ..#..#..#.............#....##....#...#...##....#.#............#....................#........#.......\n #........#......#...#......#............................#.............#...................#...#.....\n ....#............#.............#..........................................#.........#..........#....\n ...........#......#..#..........##..#.....#.....#.........................#.......###...........#...\n .....#...#............#.......#.....................................................#.....#....#....\n .......##..............#....#....#......#.....................#........#................#......#....\n #..........................#...#....#.....#..#.....................#................................\n .#.......................#..#....#.##......#..#......#....#......#................#.....#...........\n ...........#.#......#.......#.......#............#...............#.....................#....#.......\n #...#......#..##..#......#...#..#........#........................#.........................#...#.##\n " <> ...
Solution
defmodule Day22 do
alias __MODULE__.Input
@facing_values %{
?> => 0,
?v => 1,
?< => 2,
?^ => 3
}
def part1(input) do
{grid, instructions, start_point} = Input.parse(input)
process_all_instructions(grid, instructions, start_point, :flat)
end
def part2(input) do
{grid, instructions, start_point} = Input.parse(input)
process_all_instructions(grid, instructions, start_point, :cube)
end
def process_all_instructions(grid, instructions, start_point, shape) do
instructions
|> Enum.reduce({start_point, ?>}, fn
dir, acc when dir in 'LR' ->
change_facing(dir, acc)
num, acc when is_integer(num) ->
move(num, acc, grid, shape)
end)
|> then(fn {{col, row}, facing} ->
1000 * row + 4 * col + Map.get(@facing_values, facing)
end)
end
def change_facing(?R, {point, ?^}), do: {point, ?>}
def change_facing(?R, {point, ?>}), do: {point, ?v}
def change_facing(?R, {point, ?v}), do: {point, ?<}
def change_facing(?R, {point, ?<}), do: {point, ?^}
def change_facing(?L, {point, ?^}), do: {point, ?<}
def change_facing(?L, {point, ?<}), do: {point, ?v}
def change_facing(?L, {point, ?v}), do: {point, ?>}
def change_facing(?L, {point, ?>}), do: {point, ?^}
def move(num_moves, {point, facing}, grid, shape) do
Enum.reduce_while(1..num_moves, {point, facing}, fn
_num, acc ->
case move_one(acc, grid, shape) do
{:blocked, _} -> {:halt, acc}
new_acc -> {:cont, new_acc}
end
end)
end
def move_one({point, facing}, grid, shape) do
next_point = next_point(point, facing)
case Map.get(grid, next_point) do
?# -> {:blocked, facing}
?. -> {next_point, facing}
nil -> wrap_next_point(point, facing, grid, shape)
end
end
def next_point({x, y}, ?^), do: {x, y - 1}
def next_point({x, y}, ?>), do: {x + 1, y}
def next_point({x, y}, ?v), do: {x, y + 1}
def next_point({x, y}, ?<), do: {x - 1, y}
def find_point(grid, point, new_facing, old_facing \\ nil) do
case grid[point] do
?. -> {point, new_facing}
?# -> {:blocked, old_facing || new_facing}
_ -> nil
end
end
def wrap_next_point(xy, facing, grid, :flat), do: flat_wrap_next_point(xy, facing, grid)
def wrap_next_point(xy, facing, grid, :cube), do: cube_wrap_next_point(xy, facing, grid)
@doc """
_ 2 1
_ 3 _
5 4 _
6 _ _
"""
# Up from 2
# Right from left of 6
# Same 6 y relative to 2 x
def cube_wrap_next_point({x, 1}, ?^, grid) when x in 51..100 do
find_point(grid, {1, x + 100}, ?>, ?^)
end
# Up from 1
# Up from bottom of 6
# Same 6 x relative to 1 x
def cube_wrap_next_point({x, 1}, ?^, grid) when x in 101..150 do
find_point(grid, {x - 100, 200}, ?^, ?^)
end
# Right from 1
# Left from right of 4
# Inverted y top to bottom
# 1 -> 150
# 2 -> 149
# ...
# 50 -> 101
def cube_wrap_next_point({150, y}, ?>, grid) when y in 1..50 do
find_point(grid, {100, 151 - y}, ?<, ?>)
end
# Down from 1
# Left from right of 3
# Same 3 y relative to 1 x
# 101 -> 51
# 102 -> 52
# ...
# 150 -> 100
def cube_wrap_next_point({x, 50}, ?v, grid) when x in 101..150 do
find_point(grid, {100, x - 50}, ?<, ?v)
end
# Right from 3
# Up from bottom of 1
# Same 1 x relative to 3 y
def cube_wrap_next_point({100, y}, ?>, grid) when y in 51..100 do
find_point(grid, {y + 50, 50}, ?^, ?>)
end
# Right from 4
# Left from right of 1
# Inverted y
# 101 -> 50
# 102 -> 49
# ...
# 150 -> 1
def cube_wrap_next_point({100, y}, ?>, grid) when y in 101..150 do
find_point(grid, {150, 151 - y}, ?<, ?>)
end
# Down from 4
# Left from right of 6
# Same 6 y relative to 4 x
def cube_wrap_next_point({x, 150}, ?v, grid) when x in 51..100 do
find_point(grid, {50, x + 100}, ?<, ?v)
end
# Right from 6
# Up from bottom of 4
# Same 4 x relative to 6 y
def cube_wrap_next_point({50, y}, ?>, grid) when y in 151..200 do
find_point(grid, {y - 100, 150}, ?^, ?>)
end
# Down from 6
# Down from top of 1
# Same 1 x relative to 6 x
def cube_wrap_next_point({x, 200}, ?v, grid) when x in 1..50 do
find_point(grid, {x + 100, 1}, ?v, ?v)
end
# Left from 6
# Down from top of 2
# Same 2 x relative to 6 y
def cube_wrap_next_point({1, y}, ?<, grid) when y in 151..200 do
find_point(grid, {y - 100, 1}, ?v, ?<)
end
# Left from 5
# Right from left of 2
# Inverted y
# 101 -> 50
# 102 -> 49
# ...
# 150 -> 1
def cube_wrap_next_point({1, y}, ?<, grid) when y in 101..150 do
find_point(grid, {51, 151 - y}, ?>, ?<)
end
# Up from 5
# Right from left of 3
# Same 3 y relative to 5 x
def cube_wrap_next_point({x, 101}, ?^, grid) when x in 1..50 do
find_point(grid, {51, x + 50}, ?>, ?^)
end
# Left from 3
# Down from top of 5
# Same 5 x relative to 3 y
def cube_wrap_next_point({51, y}, ?<, grid) when y in 51..100 do
find_point(grid, {y - 50, 101}, ?v, ?<)
end
# Left from 2
# Right from left of 5
# Inverted y
# 1 -> 150
# 2 ->
# ...
# 50 -> 101
def cube_wrap_next_point({51, y}, ?<, grid) when y in 1..50 do
find_point(grid, {1, 151 - y}, ?>, ?<)
end
# If facing is ?^, then start at y = num_rows and go down to 1
# Find the first ?. or ?# value
def flat_wrap_next_point({x, _}, ?^, grid) do
Enum.find_value(
grid.num_rows..1//-1,
&find_point(grid, {x, &1}, ?^)
)
end
# If facing is ?>, then start at x = 1 and go up to num_cols
def flat_wrap_next_point({_, y}, ?>, grid) do
Enum.find_value(
1..grid.num_cols//1,
&find_point(grid, {&1, y}, ?>)
)
end
# If facing is ?v, then start at y = 1 and go up to num_rows
def flat_wrap_next_point({x, _}, ?v, grid) do
Enum.find_value(
1..grid.num_rows//1,
&find_point(grid, {x, &1}, ?v)
)
end
# If facing is ?<, then start at x = num_cols go down to 1
def flat_wrap_next_point({_, y}, ?<, grid) do
Enum.find_value(
grid.num_cols..1//-1,
&find_point(grid, {&1, y}, ?<)
)
end
defmodule Input do
def parse(input) when is_binary(input) do
with [grid, instructions] <- String.split(input, "\n\n", trim: true) do
grid = parse_grid(grid)
{
grid,
parse_instructions(instructions),
start_point(grid)
}
end
end
def parse_grid(input) do
grid_lines = String.split(input, "\n", trim: true)
grid_lines
|> Enum.with_index(1)
|> Enum.reduce(%{}, &parse_grid_line/2)
|> then(fn grid ->
grid
|> Map.put(
:num_rows,
grid |> Stream.map(fn {{_, y}, _} -> y end) |> Enum.max()
)
|> Map.put(
:num_cols,
grid |> Stream.map(fn {{x, _}, _} -> x end) |> Enum.max()
)
end)
end
def parse_grid_line({line, row}, grid) do
line
|> String.to_charlist()
|> Enum.with_index(1)
|> Enum.reduce(grid, fn
{?\s, _}, acc -> acc
{char, col}, acc -> Map.put(acc, {col, row}, char)
end)
end
def parse_instructions(instructions) do
Regex.scan(~r/(\d+|[LR])/, instructions, capture: :all_but_first)
|> Enum.map(fn
["L"] -> ?L
["R"] -> ?R
[num] -> String.to_integer(num)
end)
end
def start_point(grid, col \\ 1) do
case Map.get(grid, {col, 1}) do
?. -> {col, 1}
_ -> start_point(grid, col + 1)
end
end
end
end
{:module, Day22, <<70, 79, 82, 49, 0, 0, 38, ...>>,
{:module, Day22.Input, <<70, 79, 82, ...>>, {:start_point, 2}}}
Follow the path given in the monkeys' notes. What is the final password?
Your puzzle answer was 88226.
Day22.part1(input)
88226
Fold the map into a cube, then follow the path given in the monkeys' notes. What is the final password?
Your puzzle answer was 57305.
Day22.part2(input)
57305
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your Advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.
Tests
ExUnit.start(auto_run: false)
defmodule Day22Test do
use ExUnit.Case, async: false
setup_all do
[
input:
" ...#\n .#..\n #...\n ....\n...#.......#\n........#...\n..#....#....\n..........#.\n ...#....\n .....#..\n .#......\n ......#.\n\n10R5L5R10L4R5L5"
]
end
describe "part1" do
test "returns expected value", %{input: input} do
assert Day22.part1(input) == 6032
end
end
end
ExUnit.run()
.
Finished in 0.00 seconds (0.00s async, 0.00s sync)
1 test, 0 failures
Randomized with seed 962705
%{excluded: 0, failures: 0, skipped: 0, total: 1}
Benchmarking
# https://github.com/bencheeorg/benchee
Benchee.run(%{
"Part 1" => fn -> Day22.part1(input) end,
"Part 2" => fn -> Day22.part2(input) end
})
nil
Operating System: macOS
CPU Information: Apple M1
Number of Available Cores: 8
Available memory: 8 GB
Elixir 1.14.2
Erlang 25.1.2
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
reduction time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 14 s
Benchmarking Part 1 ...
Benchmarking Part 2 ...
Name ips average deviation median 99th %
Part 2 120.38 8.31 ms ±4.25% 8.45 ms 8.89 ms
Part 1 115.83 8.63 ms ±4.08% 8.69 ms 9.27 ms
Comparison:
Part 2 120.38
Part 1 115.83 - 1.04x slower +0.33 ms
nil