@@ -11,7 +11,7 @@ defmodule AdventOfCode.Y2024.Day12 do
1111
1212 def input , do: InputReader . read_from_file ( 2024 , 12 )
1313
14- @ spec run ( binary ( ) ) :: { number ( ) , nil }
14+ @ spec run ( binary ( ) ) :: { number ( ) , number ( ) }
1515 def run ( input \\ input ( ) ) do
1616 input = parse ( input )
1717
@@ -24,8 +24,35 @@ defmodule AdventOfCode.Y2024.Day12 do
2424 end )
2525 end
2626
27- defp run_2 ( _input ) do
28- nil
27+ defp run_2 ( input ) do
28+ Enum . sum_by ( input , fn { _ , plants } ->
29+ plants |> Enum . sum_by ( fn plant -> calculate_price ( plant , & sides / 1 ) end )
30+ end )
31+ end
32+
33+ defp sides ( plant_set ) do
34+ plant_set
35+ |> Enum . map ( fn plant -> count_corners ( plant , plant_set ) end )
36+ end
37+
38+ defp count_corners ( { x , y } , region ) do
39+ [
40+ { { 0 , - 1 } , { 1 , 0 } , { 1 , - 1 } } ,
41+ { { 1 , 0 } , { 0 , 1 } , { 1 , 1 } } ,
42+ { { 0 , 1 } , { - 1 , 0 } , { - 1 , 1 } } ,
43+ { { - 1 , 0 } , { 0 , - 1 } , { - 1 , - 1 } }
44+ ]
45+ |> Enum . count ( fn { d1 , d2 , diag } ->
46+ n1 = { x + elem ( d1 , 0 ) , y + elem ( d1 , 1 ) }
47+ n2 = { x + elem ( d2 , 0 ) , y + elem ( d2 , 1 ) }
48+ nd = { x + elem ( diag , 0 ) , y + elem ( diag , 1 ) }
49+
50+ is_n1 = MapSet . member? ( region , n1 )
51+ is_n2 = MapSet . member? ( region , n2 )
52+ is_nd = MapSet . member? ( region , nd )
53+
54+ ( not is_n1 and not is_n2 ) or ( is_n1 and is_n2 and not is_nd )
55+ end )
2956 end
3057
3158 def parse ( data \\ input ( ) ) ,
0 commit comments