-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import gleam/dict.{type Dict} | ||
import gleam/float | ||
import gleam/int | ||
import gleam/io | ||
import gleam/list | ||
import gleam/result | ||
import gleam/string | ||
|
||
type Point = | ||
#(Int, Int) | ||
|
||
type Grid = | ||
Dict(Point, String) | ||
|
||
fn parse_column(line, y) { | ||
use grid, char, x <- list.index_fold(line, dict.new()) | ||
dict.insert(grid, #(x, y), char) | ||
} | ||
|
||
fn square(input: Float) -> Float { | ||
case float.power(input, 2.0) { | ||
Ok(val) -> val | ||
_ -> 0.0 | ||
} | ||
} | ||
|
||
fn find_collinears(point_a: Point, point_b: Point) -> List(Point) { | ||
let #(x1, y1) = point_a | ||
let #(x2, y2) = point_b | ||
|
||
let x1f = int.to_float(x1) | ||
let y1f = int.to_float(y1) | ||
let x2f = int.to_float(x2) | ||
let y2f = int.to_float(y2) | ||
|
||
let assert Ok(distance) = | ||
float.add( | ||
square(float.subtract(x2f, x1f)), | ||
square(float.subtract(y2f, y1f)), | ||
) | ||
|> float.square_root | ||
|
||
let assert Ok(dx) = float.divide(float.subtract(x2f, x1f), distance) | ||
let assert Ok(dy) = float.divide(float.subtract(y2f, y1f), distance) | ||
|
||
let x3f_forward = float.add(x2f, float.multiply(dx, distance)) | ||
let y3f_forward = float.add(y2f, float.multiply(dy, distance)) | ||
let x3f_reverse = float.subtract(x1f, float.multiply(dx, distance)) | ||
let y3f_reverse = float.subtract(y1f, float.multiply(dy, distance)) | ||
|
||
let x3_forward = float.round(x3f_forward) | ||
let y3_forward = float.round(y3f_forward) | ||
|
||
let x3_reverse = float.round(x3f_reverse) | ||
let y3_reverse = float.round(y3f_reverse) | ||
|
||
[#(x3_forward, y3_forward), #(x3_reverse, y3_reverse)] | ||
} | ||
|
||
fn is_not_empty(cell: String) -> Bool { | ||
case cell { | ||
"." -> False | ||
_ -> True | ||
} | ||
} | ||
|
||
fn find_antenna_locations(antenna: String, grid: Grid) -> List(Point) { | ||
dict.keys(dict.filter(grid, fn(_, ant) { ant == antenna })) | ||
} | ||
|
||
fn point_within_map(point: Point, g: Grid) -> Bool { | ||
result.is_ok(list.find(dict.keys(g), fn(x) { x == point })) | ||
} | ||
|
||
fn collinears_for_antenna(antenna: String, g: Grid) -> List(Point) { | ||
find_antenna_locations(antenna, g) | ||
|> list.combinations(2) | ||
|> list.flat_map(fn(points) { | ||
case points { | ||
[l1, l2] -> find_collinears(l1, l2) | ||
_ -> [] | ||
} | ||
}) | ||
|> list.filter(point_within_map(_, g)) | ||
|> list.unique | ||
} | ||
|
||
fn parse_input(input: String) -> Grid { | ||
input | ||
|> string.trim() | ||
|> string.split("\n") | ||
|> list.map(string.to_graphemes) | ||
|> list.index_map(parse_column) | ||
|> list.reduce(dict.merge) | ||
|> result.unwrap(dict.new()) | ||
} | ||
|
||
pub fn solve_a(input: String) -> Int { | ||
let grid = parse_input(input) | ||
|
||
list.filter(list.unique(dict.values(grid)), is_not_empty) | ||
|> list.map(collinears_for_antenna(_, grid)) | ||
|> list.flatten | ||
|> list.unique | ||
|> list.length | ||
} | ||
|
||
pub fn solve_b(_inp: String) -> Int { | ||
0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import gleam/string | ||
import gleeunit/should | ||
import resonant_collinearity as day08 | ||
|
||
fn test_data() -> String { | ||
string.join( | ||
[ | ||
"............", "........0...", ".....0......", ".......0....", | ||
"....0.......", "......A.....", "............", "............", | ||
"........A...", ".........A..", "............", "............", | ||
], | ||
"\n", | ||
) | ||
} | ||
|
||
pub fn solve_a_test() { | ||
should.equal(day08.solve_a(test_data()), 14) | ||
} | ||
|
||
pub fn solve_b_test() { | ||
should.equal(day08.solve_b(test_data()), 0) | ||
} |