Skip to content

Commit

Permalink
day 08: full solution initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sreedevk committed Dec 8, 2024
1 parent 40e238d commit 8b05f58
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions src/resonant_collinearity.gleam
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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
}
}
Expand Down

0 comments on commit 8b05f58

Please sign in to comment.