diff --git a/src/resonant_collinearity.gleam b/src/resonant_collinearity.gleam index ac94881..c0a4619 100644 --- a/src/resonant_collinearity.gleam +++ b/src/resonant_collinearity.gleam @@ -1,7 +1,6 @@ import gleam/dict.{type Dict} import gleam/float import gleam/int -import gleam/io import gleam/list import gleam/result import gleam/string @@ -71,36 +70,30 @@ fn find_collinear_in_direction( case direction { Forward -> #( - float.round(float.subtract(x1f, float.multiply(dx, distance))), - float.round(float.subtract(y1f, float.multiply(dy, distance))), - ) - Reverse -> #( float.round(float.add(x2f, float.multiply(dx, distance))), float.round(float.add(y2f, float.multiply(dy, distance))), ) + Reverse -> #( + float.round(float.subtract(x1f, float.multiply(dx, distance))), + float.round(float.subtract(y1f, float.multiply(dy, distance))), + ) } } -fn points_within_map(points: List(Point), grid: Grid) -> Bool { - list.all(points, point_within_map(_, grid)) -} - fn find_forward_harmonic_collinears( found: List(Point), grid: Grid, ) -> List(Point) { - let #(head, _rest) = list.split(found, 2) + let #(head, rest) = list.split(found, 2) let assert [p1, p2] = head - case points_within_map(head, grid) { - True -> { + let collinear_in_direction = find_collinear_in_direction(p1, p2, Forward) + + case point_within_map(collinear_in_direction, grid) { + True -> find_forward_harmonic_collinears( - list.append( - list.wrap(find_collinear_in_direction(p1, p2, Forward)), - found, - ), + list.append([p2, collinear_in_direction], list.append(rest, [p1])), grid, ) - } False -> found } } @@ -109,18 +102,16 @@ fn find_reverse_harmonic_collinears( found: List(Point), grid: Grid, ) -> List(Point) { - let #(head, _rest) = list.split(found, 2) + let #(head, rest) = list.split(found, 2) let assert [p1, p2] = head - case points_within_map(head, grid) { - True -> { + let collinear_in_direction = find_collinear_in_direction(p1, p2, Reverse) + + case point_within_map(collinear_in_direction, grid) { + True -> find_reverse_harmonic_collinears( - list.append( - list.wrap(find_collinear_in_direction(p1, p2, Reverse)), - found, - ), + list.append([collinear_in_direction, p1], list.append(rest, [p2])), grid, ) - } False -> found } }