-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
85 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import extra | ||
import gleam/int | ||
import gleam/list | ||
import gleam/order | ||
import gleam/string | ||
|
||
pub type Row { | ||
|
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,65 @@ | ||
import gleam/dict | ||
import gleam/list | ||
import gleam/set | ||
import grid.{type Dims, type Grid, type XY} | ||
|
||
pub fn parse(input: String) -> Grid(String) { | ||
grid.from_string(input) | ||
|> grid.filter(fn(_, c) { c != "." }) | ||
} | ||
|
||
pub fn pt_1(input: Grid(String)) { | ||
input | ||
|> grid.to_list | ||
|> list.group(by: fn(cell) { cell.1 }) | ||
|> dict.values | ||
|> list.map(fn(nodes) { list.map(nodes, fn(node) { node.0 }) }) | ||
|> list.fold(from: set.new(), with: fn(acc, nodes) { | ||
nodes | ||
|> list.combination_pairs | ||
|> list.flat_map(antinodes_xy_pt1) | ||
|> list.filter(fn(xy) { grid.in_grid(input, xy) }) | ||
|> set.from_list | ||
|> set.union(acc) | ||
}) | ||
|> set.size | ||
} | ||
|
||
fn antinodes_xy_pt1(pair: #(XY, XY)) -> List(XY) { | ||
let #(a, b) = pair | ||
let diff = grid.xy_sub(a, b) | ||
[grid.xy_add(a, diff), grid.xy_sub(b, diff)] | ||
} | ||
|
||
pub fn pt_2(input: Grid(String)) { | ||
input | ||
|> grid.to_list | ||
|> list.group(by: fn(cell) { cell.1 }) | ||
|> dict.values | ||
|> list.map(fn(nodes) { list.map(nodes, fn(node) { node.0 }) }) | ||
|> list.fold(from: set.new(), with: fn(acc, nodes) { | ||
nodes | ||
|> list.combination_pairs | ||
|> list.flat_map(antinodes_xy_pt2(input.dims, _)) | ||
|> set.from_list | ||
|> set.union(acc) | ||
}) | ||
|> set.size | ||
} | ||
|
||
fn antinodes_xy_pt2(dims: Dims, pair: #(XY, XY)) -> List(XY) { | ||
let #(a, b) = pair | ||
list.append( | ||
cast_ray(dims, a, grid.xy_sub(a, b), [a]), | ||
cast_ray(dims, b, grid.xy_sub(b, a), [b]), | ||
) | ||
} | ||
|
||
/// Also return the start point | ||
fn cast_ray(dims: Dims, start: XY, d: XY, acc: List(XY)) -> List(XY) { | ||
let next = grid.xy_add(start, d) | ||
case grid.in_dims(dims, next) { | ||
False -> acc | ||
True -> cast_ray(dims, next, d, [next, ..acc]) | ||
} | ||
} |
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