Skip to content

Commit bebc232

Browse files
committed
Use Yog.DisjointSet
1 parent 73896b0 commit bebc232

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

lib/2024/day_12.ex

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ defmodule AdventOfCode.Y2024.Day12 do
33
--- Day 12: Garden Groups ---
44
Problem Link: https://adventofcode.com/2024/day/12
55
Difficulty: m
6-
Tags: geometry disjoint-set
6+
Tags: geometry2d disjoint-set
77
"""
8-
alias AdventOfCode.Algorithms.DisjointSet
98
alias AdventOfCode.Algorithms.Grid
109
alias AdventOfCode.Helpers.{InputReader, Transformers}
10+
alias Yog.DisjointSet
1111

1212
def input, do: InputReader.read_from_file(2024, 12)
1313

14+
@spec run(binary()) :: {number(), nil}
1415
def run(input \\ input()) do
1516
input = parse(input)
1617

@@ -37,15 +38,19 @@ defmodule AdventOfCode.Y2024.Day12 do
3738
end
3839

3940
defp regions(plants) do
40-
Enum.reduce(plants, DisjointSet.new(plants), fn plant, region ->
41+
plant_set = MapSet.new(plants)
42+
43+
plants
44+
|> Enum.reduce(DisjointSet.new(), fn plant, dsu ->
45+
dsu = DisjointSet.add(dsu, plant)
46+
4147
plant
4248
|> Grid.surrounding4()
43-
|> Enum.reduce(region, fn neighbour, region_acc ->
44-
region_acc
45-
|> DisjointSet.union(plant, neighbour)
46-
end)
49+
|> Enum.filter(&MapSet.member?(plant_set, &1))
50+
|> Enum.reduce(dsu, &DisjointSet.union(&2, plant, &1))
4751
end)
48-
|> DisjointSet.components()
52+
|> DisjointSet.to_lists()
53+
|> Enum.map(&MapSet.new/1)
4954
end
5055

5156
defp calculate_price(plant_set, multiply_by) do

0 commit comments

Comments
 (0)