diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-12-22 18:31:14 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-12-22 18:31:14 +0100 |
commit | 7a5f1983f9189422ad5e12afde11d11bec30a3f1 (patch) | |
tree | 46a02028e2712beaad7cf0886696ff7cd37798cf /aoc-2020-gleam/src/ext/iteratorx.gleam | |
parent | d8e183f02f67522d94deafa328e19b3081ca41be (diff) | |
download | gleam_aoc2020-7a5f1983f9189422ad5e12afde11d11bec30a3f1.tar.gz gleam_aoc2020-7a5f1983f9189422ad5e12afde11d11bec30a3f1.zip |
Solve part 1 of day 20
Diffstat (limited to 'aoc-2020-gleam/src/ext/iteratorx.gleam')
-rw-r--r-- | aoc-2020-gleam/src/ext/iteratorx.gleam | 21 |
1 files changed, 17 insertions, 4 deletions
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)) }) } |