Advent of Code solutions

refactoring gleam

+100 -5
+2
.gitignore
··· 7 7 *.hi 8 8 *.o 9 9 p1 10 + !p1/ 10 11 .p1 11 12 p2 13 + !p2/ 12 14 .p2 13 15 dist-newstyle
+1 -1
2024/11/gleam.toml 2024/11/p1/gleam.toml
··· 1 - name = "day11" 1 + name = "p1" 2 2 version = "1.0.0" 3 3 4 4 [dependencies]
+1 -1
2024/11/manifest.toml 2024/11/p2/manifest.toml
··· 3 3 4 4 packages = [ 5 5 { name = "gleam_erlang", version = "0.33.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "A1D26B80F01901B59AABEE3475DD4C18D27D58FA5C897D922FCB9B099749C064" }, 6 - { name = "gleam_stdlib", version = "0.47.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "3B22D46743C46498C8355365243327AC731ECD3959216344FA9CF9AD348620AC" }, 6 + { name = "gleam_stdlib", version = "0.51.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "14AFA8D3DDD7045203D422715DBB822D1725992A31DF35A08D97389014B74B68" }, 7 7 ] 8 8 9 9 [requirements]
+11
2024/11/p1/manifest.toml
··· 1 + # This file was generated by Gleam 2 + # You typically do not need to edit this file 3 + 4 + packages = [ 5 + { name = "gleam_erlang", version = "0.33.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "A1D26B80F01901B59AABEE3475DD4C18D27D58FA5C897D922FCB9B099749C064" }, 6 + { name = "gleam_stdlib", version = "0.51.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "14AFA8D3DDD7045203D422715DBB822D1725992A31DF35A08D97389014B74B68" }, 7 + ] 8 + 9 + [requirements] 10 + gleam_erlang = { version = ">= 0.33.0 and < 1.0.0" } 11 + gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
+6
2024/11/p2/gleam.toml
··· 1 + name = "p2" 2 + version = "1.0.0" 3 + 4 + [dependencies] 5 + gleam_stdlib = ">= 0.34.0 and < 2.0.0" 6 + gleam_erlang = ">= 0.33.0 and < 1.0.0"
+76
2024/11/p2/src/p2.gleam
··· 1 + import gleam/bool 2 + import gleam/dict 3 + import gleam/erlang 4 + import gleam/int 5 + import gleam/io 6 + import gleam/list 7 + import gleam/option 8 + import gleam/pair 9 + import gleam/result 10 + import gleam/string 11 + 12 + fn count_digits(n) { 13 + case n { 14 + n if n < 10 -> 1 15 + n -> 1 + count_digits(n / 10) 16 + } 17 + } 18 + 19 + fn pow(n, i) { 20 + case i { 21 + 0 -> 1 22 + i -> n * pow(n, i - 1) 23 + } 24 + } 25 + 26 + fn update_stone(stone) { 27 + case stone { 28 + 0 -> [1] 29 + n -> { 30 + let len = count_digits(n) 31 + case len % 2 { 32 + 0 -> [n / pow(10, len / 2), n % pow(10, len / 2)] 33 + 1 -> [n * 2024] 34 + _ -> panic 35 + } 36 + } 37 + } 38 + } 39 + 40 + fn count(items) { 41 + use d, #(item, n) <- list.fold(items, dict.new()) 42 + use prev <- dict.upsert(d, item) 43 + option.unwrap(prev, 0) + n 44 + } 45 + 46 + fn spread_second(p) { 47 + let #(f, s) = p 48 + list.map(f, pair.new(_, s)) 49 + } 50 + 51 + fn blink(stones, n) { 52 + use <- bool.guard(n == 0, stones) 53 + stones 54 + |> dict.to_list() 55 + |> list.map(pair.map_first(_, update_stone)) 56 + |> list.flat_map(spread_second) 57 + |> count() 58 + |> blink(n - 1) 59 + } 60 + 61 + fn show(stones) { 62 + stones |> dict.values() |> int.sum() |> io.debug() 63 + } 64 + 65 + pub fn main() { 66 + erlang.get_line("") 67 + |> result.unwrap("") 68 + |> string.trim() 69 + |> string.split(" ") 70 + |> list.try_map(int.parse) 71 + |> result.unwrap([]) 72 + |> list.map(pair.new(_, 1)) 73 + |> count() 74 + |> blink(75) 75 + |> show 76 + }
-3
2024/11/src/day11.gleam 2024/11/p1/src/p1.gleam
··· 1 1 import gleam/bool 2 2 import gleam/dict 3 3 import gleam/erlang 4 - import gleam/function 5 4 import gleam/int 6 5 import gleam/io 7 6 import gleam/list ··· 73 72 |> list.map(pair.new(_, 1)) 74 73 |> count() 75 74 |> blink(25) 76 - |> function.tap(show) 77 - |> blink(50) 78 75 |> show 79 76 }
+3
Justfile
··· 32 32 time php "$fn" < {{input}} 33 33 else if test -f {{part}}.ex 34 34 time elixir "$fn" < {{input}} 35 + else if test -d {{part}} -a -f {{part}}/gleam.toml 36 + pushd {{part}} 37 + time gleam run "$fn" < ../{{input}} 35 38 else if test -f {{part}}.erl 36 39 erl -compile "$fn" 37 40 and time erl -noshell -s {{part}} main -s init stop < {{input}}