Day 18
This commit is contained in:
@@ -59,4 +59,8 @@ defmodule Aoc2023.Common do
|
||||
|
||||
def repeat(_, 0), do: []
|
||||
def repeat(lst, n), do: Enum.concat(lst, repeat(lst, n - 1))
|
||||
|
||||
def add_vec({y, x}, {y_, x_}) do
|
||||
{y + y_, x + x_}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -13,8 +13,6 @@ defmodule Aoc2023.Day10 do
|
||||
?S => []
|
||||
}
|
||||
|
||||
defp add_coords({y, x}, {y_, x_}), do: {y + y_, x + x_}
|
||||
|
||||
defp path(s_conns, conns) do
|
||||
loop = fn recur, checked, checking ->
|
||||
checked_ = MapSet.union(checked, checking)
|
||||
@@ -53,7 +51,7 @@ defmodule Aoc2023.Day10 do
|
||||
|> Enum.map(fn {c, x} ->
|
||||
{
|
||||
{y, x},
|
||||
Map.get(@connections, c) |> Enum.map(pipe(add_coords({y, x})))
|
||||
Map.get(@connections, c) |> Enum.map(pipe(add_vec({y, x})))
|
||||
}
|
||||
end)
|
||||
end)
|
||||
@@ -71,7 +69,7 @@ defmodule Aoc2023.Day10 do
|
||||
end
|
||||
|
||||
def part2({{sy, sx}, s_conns, grid, path}) do
|
||||
s_dirs = s_conns |> Enum.map(pipe(add_coords({-sy, -sx}))) |> Enum.sort()
|
||||
s_dirs = s_conns |> Enum.map(pipe(add_vec({-sy, -sx}))) |> Enum.sort()
|
||||
s_char = @connections |> Enum.find(fn {_, conns} -> Enum.sort(conns) == s_dirs end) |> elem(0)
|
||||
grid_ = grid |> list_map_at(sy, pipe(map_nth(0, pipe(List.replace_at(sx, s_char)))))
|
||||
|
||||
|
||||
@@ -15,10 +15,6 @@ defmodule Aoc2023.Day16 do
|
||||
end
|
||||
end
|
||||
|
||||
defp add_vec({y, x}, {y_, x_}) do
|
||||
{y + y_, x + x_}
|
||||
end
|
||||
|
||||
def parse(input) do
|
||||
input
|
||||
|> lines
|
||||
|
||||
67
lib/day18.ex
Normal file
67
lib/day18.ex
Normal file
@@ -0,0 +1,67 @@
|
||||
defmodule Aoc2023.Day18 do
|
||||
require Integer
|
||||
use Aoc2023
|
||||
|
||||
def parse(input) do
|
||||
input
|
||||
|> lines
|
||||
|> Enum.map(fn line ->
|
||||
[dir, n, color] =
|
||||
Regex.run(~r/^([RDLU]) (\d+) \(#([0-9a-f]{6})\)$/, line, capture: :all_but_first)
|
||||
|
||||
{
|
||||
dir |> String.to_charlist() |> head,
|
||||
n |> String.to_integer(),
|
||||
color
|
||||
}
|
||||
end)
|
||||
end
|
||||
|
||||
defp simulate({pos, dist, points}, {dir, n}) do
|
||||
p =
|
||||
case dir do
|
||||
:u -> {-n, 0}
|
||||
:d -> {n, 0}
|
||||
:r -> {0, n}
|
||||
:l -> {0, -n}
|
||||
end
|
||||
|> add_vec(pos)
|
||||
|
||||
{p, dist + n, [p | points]}
|
||||
end
|
||||
|
||||
defp area(perim, points),
|
||||
# Shoelace formula
|
||||
do:
|
||||
Stream.zip(points, points |> Stream.cycle() |> Stream.drop(1))
|
||||
|> Stream.map(fn {{y, x}, {y1, x1}} -> x * y1 - x1 * y end)
|
||||
|> Enum.sum()
|
||||
|> abs
|
||||
|> then(&((&1 + perim) / 2))
|
||||
|> trunc
|
||||
|
||||
defp run(instrs) do
|
||||
{_, dist, points} = instrs |> Enum.reduce({{0, 0}, 2, [{0, 0}]}, &simulate(&2, &1))
|
||||
area(dist, points)
|
||||
end
|
||||
|
||||
def part1(input) do
|
||||
run(for {dir, n, _} <- input, do: {case dir do
|
||||
?U -> :u
|
||||
?D -> :d
|
||||
?L -> :l
|
||||
?R -> :r
|
||||
end, n})
|
||||
end
|
||||
|
||||
def part2(input) do
|
||||
run(for {_, _, <<len::binary-size(5), dir::integer>>} <- input do
|
||||
{case dir do
|
||||
?0 -> :r
|
||||
?1 -> :d
|
||||
?2 -> :l
|
||||
?3 -> :u
|
||||
end, String.to_integer(len, 16)}
|
||||
end)
|
||||
end
|
||||
end
|
||||
@@ -18,6 +18,7 @@ defmodule Mix.Tasks.Aoc do
|
||||
defp module(14), do: Aoc2023.Day14
|
||||
defp module(15), do: Aoc2023.Day15
|
||||
defp module(16), do: Aoc2023.Day16
|
||||
defp module(18), do: Aoc2023.Day18
|
||||
# [MODULE INSERTION POINT]
|
||||
|
||||
defp base_dir(), do: System.get_env("AOC_BASE")
|
||||
|
||||
Reference in New Issue
Block a user