Compare commits
2 commits
1e974c56ae
...
04970d3ed6
Author | SHA1 | Date | |
---|---|---|---|
04970d3ed6 | |||
fcbf4aa683 |
2 changed files with 35 additions and 29 deletions
|
@ -1,24 +1,30 @@
|
|||
#!/usr/bin/env elixir
|
||||
defmodule Patrol do
|
||||
defstruct obstacles: MapSet.new(),
|
||||
visited: MapSet.new(),
|
||||
x: 0,
|
||||
y: 0,
|
||||
max_x: nil,
|
||||
max_y: nil,
|
||||
dir_x: 0,
|
||||
dir_y: -1
|
||||
end
|
||||
|
||||
defmodule Day6 do
|
||||
def part1({max_x, max_y, {x, y}, obstacles}) do
|
||||
walk(x, y, max_x, max_y, obstacles, MapSet.new(), 0, -1)
|
||||
|> MapSet.size()
|
||||
def part1(%Patrol{} = patrol) when patrol.x == patrol.max_x or patrol.y == patrol.max_y do
|
||||
MapSet.size(patrol.visited)
|
||||
end
|
||||
|
||||
def walk(x, y, max_x, max_y, _obstacles, visited, _x_dir, _) when x == max_x or y == max_y do
|
||||
visited
|
||||
end
|
||||
def part1(%Patrol{} = patrol) do
|
||||
next_x = patrol.x + patrol.dir_x
|
||||
next_y = patrol.y + patrol.dir_y
|
||||
|
||||
def walk(x, y, max_x, max_y, obstacles, visited, x_dir, y_dir) do
|
||||
next_x = x + x_dir
|
||||
next_y = y + y_dir
|
||||
|
||||
if MapSet.member?(obstacles, {next_x, next_y}) do
|
||||
{x_dir, y_dir} = turn_right(x_dir, y_dir)
|
||||
walk(x, y, max_x, max_y, obstacles, visited, x_dir, y_dir)
|
||||
if MapSet.member?(patrol.obstacles, {next_x, next_y}) do
|
||||
{dir_x, dir_y} = turn_right(patrol.dir_x, patrol.dir_y)
|
||||
part1(%Patrol{patrol | dir_x: dir_x, dir_y: dir_y})
|
||||
else
|
||||
visited = MapSet.put(visited, {x, y})
|
||||
walk(next_x, next_y, max_x, max_y, obstacles, visited, x_dir, y_dir)
|
||||
visited = MapSet.put(patrol.visited, {patrol.x, patrol.y})
|
||||
part1(%Patrol{patrol | x: next_x, y: next_y, visited: visited})
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -35,16 +41,18 @@ defmodule Day6 do
|
|||
def input do
|
||||
with [input_filename] <- System.argv(),
|
||||
{:ok, input} <- File.read(input_filename) do
|
||||
for <<char::binary-1 <- input>>, reduce: {0, 0, 0, nil, MapSet.new()} do
|
||||
{x, max_x, y, guard, obstacles} ->
|
||||
case char do
|
||||
"." -> {x + 1, max_x, y, guard, obstacles}
|
||||
"#" -> {x + 1, max_x, y, guard, MapSet.put(obstacles, {x, y})}
|
||||
"^" -> {x + 1, max_x, y, {x, y}, obstacles}
|
||||
"\n" -> {0, x, y + 1, guard, obstacles}
|
||||
end
|
||||
end
|
||||
|> Tuple.delete_at(0)
|
||||
{_x, max_x, max_y, {x, y}, obstacles} =
|
||||
for <<char::binary-1 <- input>>, reduce: {0, 0, 0, nil, MapSet.new()} do
|
||||
{x, max_x, y, guard, obstacles} ->
|
||||
case char do
|
||||
"." -> {x + 1, max_x, y, guard, obstacles}
|
||||
"#" -> {x + 1, max_x, y, guard, MapSet.put(obstacles, {x, y})}
|
||||
"^" -> {x + 1, max_x, y, {x, y}, obstacles}
|
||||
"\n" -> {0, x, y + 1, guard, obstacles}
|
||||
end
|
||||
end
|
||||
|
||||
%Patrol{x: x, y: y, max_x: max_x, max_y: max_y, obstacles: obstacles}
|
||||
else
|
||||
_ -> :error
|
||||
end
|
||||
|
|
|
@ -5,8 +5,7 @@ defmodule Day8 do
|
|||
antennas
|
||||
|> find_pairs()
|
||||
|> Enum.flat_map(fn pair -> find_antinodes(pair, x, y) end)
|
||||
|> MapSet.new()
|
||||
|> MapSet.union(antinodes)
|
||||
|> Enum.into(antinodes)
|
||||
end)
|
||||
|> MapSet.size()
|
||||
end
|
||||
|
@ -30,8 +29,7 @@ defmodule Day8 do
|
|||
antennas
|
||||
|> find_pairs()
|
||||
|> Enum.flat_map(fn pair -> find_resonant_harmonics(pair, x, y) end)
|
||||
|> MapSet.new()
|
||||
|> MapSet.union(antinodes)
|
||||
|> Enum.into(antinodes)
|
||||
end)
|
||||
|> MapSet.size()
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue