diff options
Diffstat (limited to 'aoc-2020-gleam/src/ext')
-rw-r--r-- | aoc-2020-gleam/src/ext/intx.gleam | 15 | ||||
-rw-r--r-- | aoc-2020-gleam/src/ext/iteratorx.gleam | 21 | ||||
-rw-r--r-- | aoc-2020-gleam/src/ext/listx.gleam | 7 |
3 files changed, 38 insertions, 5 deletions
diff --git a/aoc-2020-gleam/src/ext/intx.gleam b/aoc-2020-gleam/src/ext/intx.gleam index 077b4f2..5c9bcc0 100644 --- a/aoc-2020-gleam/src/ext/intx.gleam +++ b/aoc-2020-gleam/src/ext/intx.gleam @@ -1,4 +1,5 @@ import gleam/int +import gleam/bool import gleam/order.{Eq, Gt, Lt} pub fn is_between(number: Int, min: Int, and max: Int) { @@ -9,7 +10,7 @@ pub fn ceil_divide(dividend: Int, by divisor: Int) -> Int { { dividend + divisor - 1 } / divisor } -fn gcd(a: Int, b: Int) -> Int { +pub fn gcd(a: Int, b: Int) -> Int { case b == 0 { True -> a False -> gcd(b, a % b) @@ -22,3 +23,15 @@ pub fn lcm(a: Int, b: Int) -> Int { Lt -> { b / gcd(a, b) } * a } } + +fn do_reverse_bits(val: Int, rev: Int, length: Int) -> Int { + use <- bool.guard(when: length == 0, return: rev) + let lsb = int.bitwise_and(val, 1) + let val = int.bitwise_shift_right(val, 1) + let rev = int.bitwise_shift_left(rev, 1) + do_reverse_bits(val, int.bitwise_or(rev, lsb), length - 1) +} + +pub fn reverse_bits(val: Int, length: Int) -> Int { + do_reverse_bits(val, 0, length) +} diff --git a/aoc-2020-gleam/src/ext/iteratorx.gleam b/aoc-2020-gleam/src/ext/iteratorx.gleam index 0e59f17..6c7838d 100644 --- a/aoc-2020-gleam/src/ext/iteratorx.gleam +++ b/aoc-2020-gleam/src/ext/iteratorx.gleam @@ -1,3 +1,6 @@ +import gleam/int +import gleam/result as res +import gleam/dict.{type Dict} import gleam/iterator.{type Iterator, Next} as iter pub fn length(iterator: Iterator(a)) -> Int { @@ -11,6 +14,19 @@ pub fn count(iterator: Iterator(a), satisfying predicate: fn(a) -> Bool) -> Int |> length } +pub fn counts(iterator: Iterator(a)) -> Dict(a, Int) { + iterator + |> iter.fold(from: dict.new(), with: fn(acc, value) { + acc + |> dict.insert( + value, + dict.get(acc, value) + |> res.unwrap(or: 0) + |> int.add(1), + ) + }) +} + pub fn filter_map( iterator: Iterator(a), with mapper: fn(a) -> Result(b, c), @@ -25,8 +41,5 @@ pub fn filter_map( } pub fn unfold_infinitely(from state: a, with fun: fn(a) -> a) -> Iterator(a) { - iter.unfold( - from: state, - with: fn(s) { Next(element: s, accumulator: fun(s)) }, - ) + iter.unfold(from: state, with: fn(s) { Next(element: s, accumulator: fun(s)) }) } diff --git a/aoc-2020-gleam/src/ext/listx.gleam b/aoc-2020-gleam/src/ext/listx.gleam index 2ca8648..0a3d7c3 100644 --- a/aoc-2020-gleam/src/ext/listx.gleam +++ b/aoc-2020-gleam/src/ext/listx.gleam @@ -1,6 +1,7 @@ import gleam/pair import gleam/iterator as iter import gleam/result as res +import gleam/dict.{type Dict} import ext/iteratorx as iterx pub fn count(list: List(a), satisfying predicate: fn(a) -> Bool) -> Int { @@ -9,6 +10,12 @@ pub fn count(list: List(a), satisfying predicate: fn(a) -> Bool) -> Int { |> iterx.count(satisfying: predicate) } +pub fn counts(list: List(a)) -> Dict(a, Int) { + list + |> iter.from_list + |> iterx.counts +} + fn set_helper(list: List(a), value: a, index: Int, counter: Int) -> List(a) { case list { [] -> [] |