Skip to content

Commit

Permalink
Use a stdlib fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Janiczek committed Dec 2, 2024
1 parent 782c2b0 commit ac5092d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/aoc_2024/day_2.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn is_descending(line: List(Int)) {
}

fn has_good_differences(line: List(Int)) {
extra.consecutive_pairs(line)
list.window_by_2(line)
|> list.map(fn(pair) { int.absolute_value(pair.0 - pair.1) })
|> list.all(fn(n) { n >= 1 && n <= 3 })
}
Expand Down
14 changes: 2 additions & 12 deletions src/extra.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gleam/dict.{type Dict}
import gleam/list
import gleam/option
import gleam/otp/task
import gleam/result

/// Needs the operation to be associative and there to be a zero (monoid).
/// Uses parallelism under the hood.
Expand Down Expand Up @@ -39,21 +39,11 @@ pub fn pmap2(xs: List(x), ys: List(y), with fun: fn(x, y) -> z) -> List(z) {
pub fn frequencies(xs: List(a)) -> Dict(a, Int) {
xs
|> list.fold(from: dict.new(), with: fn(counter, x) {
let old: Int =
counter
|> dict.get(x)
|> result.unwrap(0)

counter
|> dict.insert(x, old + 1)
|> dict.upsert(x, fn(old) { option.unwrap(old, 0) + 1 })
})
}

/// consecutive_pairs([5,10,2]) -> [ #(5,10), #(10,2) ]
pub fn consecutive_pairs(xs: List(a)) -> List(#(a, a)) {
list.map2(xs, list.drop(xs, 1), fn(x, y) { #(x, y) })
}

/// remove_at([5,10,2],1) -> [5,2]
pub fn remove_at(xs: List(a), at i: Int) -> List(a) {
list.append(list.take(xs, i), list.drop(xs, i + 1))
Expand Down

0 comments on commit ac5092d

Please sign in to comment.