X-Git-Url: https://fbox.kageds.com/gitweb/adventofcode.git/blobdiff_plain/b4e1a94b5704c5a3335f69c1850577fba80dc66d..0ed26518ace56da4fd0c04d77b10877d57b07f20:/day9/day9.erl?ds=inline diff --git a/day9/day9.erl b/day9/day9.erl index 25c5547..1906030 100644 --- a/day9/day9.erl +++ b/day9/day9.erl @@ -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)).