File tree Expand file tree Collapse file tree 2 files changed +24
-4
lines changed Expand file tree Collapse file tree 2 files changed +24
-4
lines changed Original file line number Diff line number Diff line change
1
+ import extra
2
+ import gleam/dict
1
3
import gleam/int
2
- import gleam/io
3
4
import gleam/list
5
+ import gleam/result
4
6
import gleam/string
5
7
6
8
pub type Input =
@@ -28,12 +30,14 @@ pub fn pt_1(input: Input) {
28
30
let sorted_xs = xs |> list . sort ( by : int . compare )
29
31
let sorted_ys = ys |> list . sort ( by : int . compare )
30
32
list . map2 ( sorted_xs , sorted_ys , with : fn ( x , y ) { int . absolute_value ( x - y ) } )
31
- |> list . fold ( from : 0 , with : int . add )
33
+ |> int . sum
32
34
}
33
35
34
36
pub fn pt_2 ( input : Input ) {
35
37
let # ( xs , ys ) = input
38
+ let freqs = extra . frequencies ( ys )
39
+
36
40
xs
37
- |> list . map ( fn ( x ) { x * list . count ( ys , where : fn ( y ) { x == y } ) } )
38
- |> list . fold ( from : 0 , with : int . add )
41
+ |> list . map ( fn ( x ) { x * { dict . get ( freqs , x ) |> result . unwrap ( 0 ) } } )
42
+ |> int . sum
39
43
}
Original file line number Diff line number Diff line change
1
+ import gleam/dict . { type Dict }
2
+ import gleam/list
3
+ import gleam/result
4
+
5
+ pub fn frequencies ( xs : List ( a) ) -> Dict ( a, Int ) {
6
+ xs
7
+ |> list . fold ( from : dict . new ( ) , with : fn ( counter , x ) {
8
+ let old : Int =
9
+ counter
10
+ |> dict . get ( x )
11
+ |> result . unwrap ( 0 )
12
+
13
+ counter
14
+ |> dict . insert ( x , old + 1 )
15
+ } )
16
+ }
You can’t perform that action at this time.
0 commit comments