--- Day 9: Movie Theater ---
Mix.install([{:kino_aoc, "~> 0.1"}, {:vega_lite, "~> 0.1.6"}, {:kino_vega_lite, "~> 0.1.9"}])
Setup
{:ok, puzzle_input} =
KinoAOC.download_puzzle("2025", "9", System.fetch_env!("LB_AOC_SESSION_COOKIE"))
test_input = Kino.Input.textarea("test_input")
test_input = Kino.Input.read(test_input)
defmodule MovieTheater do
def parse(input) do
input
|> String.split()
|> Enum.map(fn xy ->
xy |> String.split(",") |> Enum.map(&String.to_integer/1) |> List.to_tuple()
end)
end
def parse_for_chart(input) do
input
|> String.split()
|> Enum.with_index(fn xy, index ->
[x, y] = xy |> String.split(",") |> Enum.map(&String.to_integer/1)
%{x: x, y: y, index: index}
end)
end
def area({x1, y1}, {x2, y2}) do
(abs(x1 - x2) + 1) * (abs(y1 - y2) + 1)
end
def maximal_area(grid) do
grid
|> Enum.flat_map(&for(corner_b <- grid, corner_b != &1, do: area(&1, corner_b)))
|> Enum.max()
end
end
Part 1
import MovieTheater
puzzle_input
|> parse()
|> maximal_area()
Part 2
import MovieTheater
points = parse_for_chart(puzzle_input)
point_map = Map.new(points, &{&1.index, {&1.x, &1.y}})
outliers =
point_map
|> Enum.filter(fn {index, {x, y}} ->
{prev_x, _prev_y} = Map.get(point_map, index - 1, {x, y})
{next_x, _next_y} = Map.get(point_map, index + 1, {x, y})
x - prev_x > 10_000 or x - next_x > 10_000
end)
|> Enum.map(fn {index, {x, y}} -> %{x: x, y: y, index: index} end)
points = points -- outliers
:ok
VegaLite.new(width: 750, height: 750)
|> VegaLite.layers([
VegaLite.new()
|> VegaLite.data_from_values(points, only: ["x", "y", "index"])
|> VegaLite.mark(:point)
|> VegaLite.encode_field(:x, "x", type: :quantitative)
|> VegaLite.encode_field(:y, "y", type: :quantitative)
|> VegaLite.encode_field(:color, "index",
type: :quantitative,
scale: [scheme: "category10"]
),
VegaLite.new()
|> VegaLite.data_from_values(outliers, only: ["x", "y", "index"])
|> VegaLite.mark(:point)
|> VegaLite.encode_field(:x, "x", type: :quantitative)
|> VegaLite.encode_field(:y, "y", type: :quantitative)
|> VegaLite.encode_field(:color, "index", type: :temporal, scale: [scheme: "dark2"])
])
%{y: top} = Enum.max_by(points, & &1.y)
%{x: right} = Enum.max_by(points, & &1.x)
%{y: bottom} = Enum.min_by(points, & &1.y)
%{x: left} = Enum.min_by(points, & &1.x)
equator = (top - bottom) / 2
prime_meridian = (right - left) / 2
south_west = Enum.filter(points, fn %{x: x, y: y} -> y < equator and x < prime_meridian end)
[%{index: 249, x: x1, y: y1}, _] = outliers
%{x: x2, y: y2} = Enum.max_by(south_west, fn %{x: x2, y: y2} -> area({x1, y1}, {x2, y2}) end)