๐ŸŽ„ advent of code 2025

day3 gleam

okkdev e08ac8dc f443f7ed

Changed files
+60
day3
+60
day3/day3.gleam
··· 1 + import gleam/int 2 + import gleam/io 3 + import gleam/list 4 + import gleam/result 5 + import gleam/string 6 + import simplifile 7 + 8 + pub fn main() { 9 + let assert Ok(raw_input) = simplifile.read("src/day3/input.txt") 10 + 11 + let input = 12 + raw_input 13 + |> string.trim 14 + |> string.split("\n") 15 + 16 + let part1 = 17 + list.flat_map(input, fn(in) { 18 + string.to_graphemes(in) 19 + |> list.combination_pairs() 20 + |> list.map(fn(x) { 21 + { x.0 <> x.1 } 22 + |> int.parse 23 + |> result.unwrap(0) 24 + }) 25 + |> list.sort(fn(a, b) { int.compare(b, a) }) 26 + |> list.take(1) 27 + }) 28 + |> int.sum 29 + 30 + io.println("Part 1: " <> int.to_string(part1)) 31 + 32 + let part2 = 33 + list.map(input, fn(in) { 34 + string.to_graphemes(in) 35 + |> search_largest(12, "") 36 + |> int.parse 37 + |> result.unwrap(0) 38 + }) 39 + |> int.sum 40 + 41 + io.println("Part 2: " <> int.to_string(part2)) 42 + } 43 + 44 + fn search_largest(gs: List(String), amount: Int, acc: String) -> String { 45 + case amount { 46 + 0 -> acc 47 + _ -> { 48 + let assert [#(index, num), ..] = 49 + list.reverse(gs) 50 + |> list.split(amount - 1) 51 + |> fn(x) { 52 + list.reverse(x.1) 53 + |> list.index_map(fn(x, i) { #(i, x) }) 54 + } 55 + |> list.sort(fn(a, b) { string.compare(b.1, a.1) }) 56 + 57 + search_largest(list.drop(gs, index + 1), amount - 1, acc <> num) 58 + } 59 + } 60 + }