my solutions to advent of code
aoc advent-of-code

this code is garbage and i couldnt figure out how to do it on my own someone told me the tip for counting the last line

aylac.top 1aed72fc 5e1187a8

verified
Changed files
+75 -23
2025
6
gleam
+75 -23
2025/6/gleam/src/main.gleam
··· 10 10 type Part2Dict = 11 11 dict.Dict(Int, Int) 12 12 13 + type Align { 14 + Left 15 + Right 16 + } 17 + 18 + type Operation { 19 + Sum 20 + Mul 21 + } 22 + 23 + type Part2Line { 24 + Part2Line(align: Align, op: Operation, numbers: List(String)) 25 + } 26 + 13 27 pub fn main() { 14 28 let assert Ok(input) = file.read(from: "../input.txt") 15 29 as "Input file not found" ··· 39 53 |> int.sum 40 54 echo part_1 41 55 42 - let assert Ok(regex) = regexp.from_string(" *") 43 - let input_pt_2 = regexp.replace(regex, input, "X ") 44 - 45 - echo input_pt_2 56 + let lines = 57 + input 46 58 |> string.split("\n") 47 - |> list.map(fn(i) { string.trim(i) |> string.split(" ") }) 48 - |> list.transpose 49 - 59 + |> list.reverse 60 + let assert Ok(last_line) = list.first(lines) 61 + let #(_, bounds) = 62 + { last_line <> " *" } 63 + |> string.to_graphemes 64 + |> list.index_fold(#(0, list.new()), fn(acc, char, i) { 65 + let #(bound_start, bounds) = acc 66 + case char { 67 + "*" | "+" if i > 0 -> #(i, list.append([#(bound_start, i - 1)], bounds)) 68 + _ -> acc 69 + } 70 + }) 71 + let input_pt_2 = 72 + bounds 73 + |> list.index_fold(dict.new(), fn(d, bound, i) { 74 + let numbers = 75 + list.map(lines, fn(line) { 76 + string.slice(line, bound.0, bound.1 - bound.0) 77 + }) 78 + let align = 79 + numbers 80 + |> list.drop(1) 81 + |> list.fold_until(Left, fn(res, number) { 82 + case 83 + string.trim(number) == number, 84 + string.trim_start(number) == number 85 + { 86 + True, _ -> list.Continue(res) 87 + _, True -> list.Stop(Left) 88 + _, _ -> list.Stop(Right) 89 + } 90 + }) 91 + let assert Ok(sign) = list.first(numbers) 92 + let sign = case string.trim(sign) { 93 + "*" -> Mul 94 + "+" -> Sum 95 + _ -> panic as sign 96 + } 97 + dict.insert( 98 + d, 99 + i, 100 + Part2Line( 101 + align, 102 + sign, 103 + numbers |> list.drop(1) |> list.map(string.trim) |> list.reverse, 104 + ), 105 + ) 106 + }) 50 107 let part_2 = 51 108 input_pt_2 52 - |> string.split("\n") 53 - |> list.map(fn(i) { string.trim(i) |> string.split(" ") }) 54 - |> list.transpose 55 - |> list.index_map(fn(ninput, col) { 56 - let i = list.reverse(ninput) 57 - let assert Ok(s) = list.first(i) 58 - let i = list.drop(i, 1) 59 - let i = list.reverse(i) 109 + |> dict.to_list 110 + |> list.map(fn(i) { i.1 }) 111 + |> list.map(fn(line) { 60 112 let d: Part2Dict = dict.new() 61 113 let d = 62 - i 114 + line.numbers 63 115 |> list.fold(d, fn(d, number) { 64 116 let number_len = string.length(number) 65 117 string.to_graphemes(number) 66 118 |> list.index_fold(d, fn(d, digit, index) { 67 119 let assert Ok(digit) = digit |> int.parse 68 - let pos = case col % 2 { 69 - 0 -> number_len - index 70 - _ -> index 120 + let pos = case line.align { 121 + Right -> number_len - index 122 + Left -> index 71 123 } 72 124 dict.insert( 73 125 d, ··· 76 128 ) 77 129 }) 78 130 }) 131 + echo #(d, line) 79 132 let numbers = 80 133 dict.to_list(d) 81 134 |> list.map(fn(n) { n.1 }) 82 135 83 - let r = case s { 84 - "+" -> int.sum(numbers) 85 - "*" -> list.reduce(numbers, int.multiply) |> result.unwrap(0) 86 - _ -> panic as "invalid" 136 + let r = case line.op { 137 + Sum -> int.sum(numbers) 138 + Mul -> list.reduce(numbers, int.multiply) |> result.unwrap(0) 87 139 } 88 140 r 89 141 })