-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_11.ex
56 lines (47 loc) · 1.74 KB
/
day_11.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
defmodule AdventOfCode.Y2023.Day11 do
@moduledoc """
--- Day 11: Cosmic Expansion ---
Problem Link: https://adventofcode.com/2023/day/11
Difficulty: s
Tags: grid measurement
"""
alias AdventOfCode.Algorithms.Grid
alias AdventOfCode.Helpers.{InputReader, Transformers}
def input, do: InputReader.read_from_file(2023, 11)
def run(input \\ input()) do
input = parse(input)
{all_pairs_distance(input, 2), all_pairs_distance(input, 1_000_000)}
end
def parse(data \\ input()) do
grid =
data
|> Transformers.lines()
|> Enum.map(&String.graphemes/1)
|> Grid.grid2d()
{{row_max, col_max}, _} = Enum.max(grid)
galaxies = for {p, "#"} <- grid, do: p
occupied_rows = for {row, _} <- galaxies, into: %MapSet{}, do: row
occupied_cols = for {_, col} <- galaxies, into: %MapSet{}, do: col
empty_rows = 0..row_max |> MapSet.new() |> MapSet.difference(occupied_rows)
empty_cols = 0..col_max |> MapSet.new() |> MapSet.difference(occupied_cols)
{galaxies, empty_rows, empty_cols}
end
defp all_pairs_distance({galaxies, empty_rows, empty_cols}, rate) do
galaxies
|> expand(empty_rows, empty_cols, rate)
|> then(fn [current | remaining] -> all_pairs_distance(0, current, remaining) end)
end
defp all_pairs_distance(distance, _, []), do: distance
defp all_pairs_distance(distance, {x1, y1}, [next | remaining] = galaxies) do
for {x2, y2} <- galaxies, reduce: distance do
acc -> acc + abs(x2 - x1) + abs(y2 - y1)
end
|> all_pairs_distance(next, remaining)
end
defp expand(galaxies, rows, cols, rate) do
for {x, y} <- galaxies,
do:
{x + (rate - 1) * Enum.count(rows, &(&1 < x)),
y + (rate - 1) * Enum.count(cols, &(&1 < y))}
end
end