]> fbox.kageds.com Git - adventofcode.git/blobdiff - day9/day9.erl
commit
[adventofcode.git] / day9 / day9.erl
index 25c5547c72921458c86bf6ca4832542600c83dfb..190603055cb4da6a485987720d540975972d3896 100644 (file)
@@ -56,12 +56,12 @@ solution2(Table) ->
 
 
 get_basins(Low_points, Table) ->
-       get_basins(Low_points, Table, 0, []).
+       get_basins(Low_points, Table, []).
 
-get_basins([], _Table, _, Basins) -> Basins;
-get_basins([Low_point|Rest], Table, Count, Basins) ->
-  Basin = [Low_point|lists:usort(get_up_slopes([Low_point], Table))],
-  get_basins(Rest, Table, Count + 1, [Basin|Basins]).
+get_basins([], _Table, Basins) -> Basins;
+get_basins([Low_point|Rest], Table, Basins) ->
+  Basin = [Low_point|get_up_slopes([Low_point], Table)],
+  get_basins(Rest, Table, [Basin|Basins]).
 
 low_points([H|_] =  Table) ->
   X = length(H),
@@ -76,7 +76,7 @@ get_up_slopes([], _Table, Acc) -> Acc;
 get_up_slopes([{X, Y}|Rest], Table, Acc) ->
     V = get_value({X,Y}, Table),
     Neighbours = [{X+1, Y},{X-1, Y},{X, Y+1},{X, Y-1}],
-    Up_slopes = lists:filter(fun({X1,Y1}) -> C = get_value({X1,Y1}, Table), C /= 9 andalso C /=10 andalso C > V end, Neighbours),
+    Up_slopes = lists:filter(fun({X1,Y1}) -> C = get_value({X1,Y1}, Table), C /= 9 andalso C > V end, Neighbours),
     get_up_slopes(lists:usort(Rest ++ Up_slopes), Table, lists:usort(Acc ++ Up_slopes)).
 
 is_low_point({X,Y}, Table) ->
@@ -86,7 +86,7 @@ is_low_point({X,Y}, Table) ->
     V < get_value({X, Y+1}, Table) andalso
     V < get_value({X, Y-1}, Table).
 
-get_value({X,Y}, [H|_] = Table) when X == 0; Y == 0; Y > length(Table); X > length(H) -> 10;
+get_value({X,Y}, [H|_] = Table) when X == 0; Y == 0; Y > length(Table); X > length(H) -> 9;
 
 get_value({X,Y}, Table) ->
        lists:nth(X, lists:nth(Y, Table)).