-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_12.ex
64 lines (54 loc) · 1.6 KB
/
day_12.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
57
58
59
60
61
62
63
64
defmodule AdventOfCode.Y2024.Day12 do
@moduledoc """
--- Day 12: Garden Groups ---
Problem Link: https://adventofcode.com/2024/day/12
Difficulty: m
Tags: geometry disjoint-set
"""
alias AdventOfCode.Algorithms.DisjointSet
alias AdventOfCode.Algorithms.Grid
alias AdventOfCode.Helpers.{InputReader, Transformers}
def input, do: InputReader.read_from_file(2024, 12)
def run(input \\ input()) do
input = parse(input)
{run_1(input), run_2(input)}
end
defp run_1(input) do
Enum.sum_by(input, fn {_, plants} ->
plants |> Enum.sum_by(fn plant -> calculate_price(plant, &perimeters/1) end)
end)
end
defp run_2(_input) do
nil
end
def parse(data \\ input()),
do: Transformers.lines(data) |> Enum.map(&String.graphemes/1) |> Grid.grid2d() |> plants()
def plants(grid) do
grid
|> Enum.group_by(fn {_, plant} -> plant end, fn {grid, _} -> grid end)
|> Map.new(fn {plant, locations} -> {plant, regions(locations)} end)
end
defp regions(plants) do
Enum.reduce(plants, DisjointSet.new(plants), fn plant, region ->
plant
|> Grid.surrounding4()
|> Enum.reduce(region, fn neighbour, region_acc ->
region_acc
|> DisjointSet.union(plant, neighbour)
end)
end)
|> DisjointSet.components()
end
defp calculate_price(plant_set, multiply_by) do
Enum.sum(multiply_by.(plant_set)) * Enum.count(plant_set)
end
defp perimeters(plant_set) do
plant_set
|> Enum.map(fn plant ->
plant
|> Grid.surrounding4()
|> Enum.reject(&MapSet.member?(plant_set, &1))
|> Enum.count()
end)
end
end