Skip to content

Commit

Permalink
2024-08 * ** Gleam
Browse files Browse the repository at this point in the history
  • Loading branch information
Janiczek committed Dec 8, 2024
1 parent 0d438ec commit 792376f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/aoc_2024/day_7.gleam
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 {
Expand Down
65 changes: 65 additions & 0 deletions src/aoc_2024/day_8.gleam
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])
}
}
24 changes: 20 additions & 4 deletions src/grid.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pub fn xy_add(a: XY, b: XY) -> XY {
#(a.0 + b.0, a.1 + b.1)
}

pub fn xy_sub(a: XY, b: XY) -> XY {
#(a.0 - b.0, a.1 - b.1)
}

pub fn xy_scale(xy: XY, k: Int) -> XY {
#(xy.0 * k, xy.1 * k)
}
Expand Down Expand Up @@ -97,6 +101,10 @@ pub fn map(grid: Grid(a), fun: fn(XY, a) -> b) -> Grid(b) {
Grid(dims: grid.dims, data: grid.data |> dict.map_values(fun))
}

pub fn filter(grid: Grid(a), pred: fn(XY, a) -> Bool) -> Grid(a) {
Grid(..grid, data: grid.data |> dict.filter(pred))
}

pub fn filter_map(grid: Grid(a), fun: fn(XY, a) -> Result(b, Nil)) -> Grid(b) {
Grid(
dims: grid.dims,
Expand All @@ -123,15 +131,23 @@ pub fn find_exact(grid: Grid(a), needle: a) -> Result(XY, Nil) {
})
}

pub fn to_list(grid: Grid(a)) -> List(#(XY, a)) {
dict.to_list(grid.data)
}

pub fn values(grid: Grid(a)) -> List(a) {
dict.values(grid.data)
}

pub fn in_grid(grid: Grid(a), xy: XY) -> Bool {
xy.0 >= grid.dims.min_x
&& xy.0 <= grid.dims.max_x
&& xy.1 >= grid.dims.min_y
&& xy.1 <= grid.dims.max_y
in_dims(grid.dims, xy)
}

pub fn in_dims(dims: Dims, xy: XY) -> Bool {
xy.0 >= dims.min_x
&& xy.0 <= dims.max_x
&& xy.1 >= dims.min_y
&& xy.1 <= dims.max_y
}

pub fn extend(dims: Dims, xy: XY) -> Dims {
Expand Down

0 comments on commit 792376f

Please sign in to comment.