my aoc solutions
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

day 6

nnuuvv 9323131d c6535e49

+177
+177
src/aoc_2025/day_6.gleam
··· 1 + import gleam/int 2 + import gleam/list 3 + import gleam/option.{None, Some} 4 + import gleam/pair 5 + import gleam/result 6 + import gleam/string 7 + 8 + pub type Problem { 9 + Problem(numbers: List(Int), operator: Operator) 10 + } 11 + 12 + fn new_problem() { 13 + Problem([], Unset) 14 + } 15 + 16 + pub type Operator { 17 + Unset 18 + Plus 19 + Multiply 20 + } 21 + 22 + pub fn pt_1(input: String) { 23 + input 24 + // get rows 25 + |> string.split("\n") 26 + // split by space 27 + |> list.map(string.split(_, " ")) 28 + // drop back to back whitespaces 29 + |> list.map(list.filter(_, fn(item) { item != "" })) 30 + |> list.interleave() 31 + // parse list of "int, int, int, operator, int, int, int, operator" into Problem(List(Int), Operator) 32 + |> list.fold(#([], new_problem()), fn(acc, cur_item) { 33 + let #(parsed_problems, Problem(numbers:, operator:)) = acc 34 + case cur_item { 35 + // empty item 36 + "" -> acc 37 + // problem finished 38 + "*" -> #([Problem(numbers, Multiply), ..parsed_problems], new_problem()) 39 + "+" -> #([Problem(numbers, Plus), ..parsed_problems], new_problem()) 40 + // number 41 + number -> { 42 + let assert Ok(number) = int.parse(number) as "invalid input" 43 + #(parsed_problems, Problem([number, ..numbers], operator)) 44 + } 45 + } 46 + }) 47 + |> pair.first() 48 + |> solve_problems() 49 + } 50 + 51 + pub fn pt_2(input: String) { 52 + let row = 53 + input 54 + // get rows 55 + |> string.split("\n") 56 + 57 + let assert [first_row, ..] = row 58 + 59 + let grapheme_rows = 60 + row 61 + |> list.flat_map(string.to_graphemes) 62 + |> list.sized_chunk(string.length(first_row)) 63 + 64 + let assert Ok(operator_row) = list.last(grapheme_rows) as "invalid input" 65 + 66 + let problem_count = 67 + operator_row 68 + |> list.filter(fn(item) { item != " " }) 69 + |> list.length() 70 + 71 + // echo first_row as "first_row" 72 + 73 + let interleaved = 74 + grapheme_rows 75 + // |> echo 76 + |> list.interleave() 77 + let row_count = list.length(grapheme_rows) 78 + 79 + let problems = 80 + parse_problems_by_column(interleaved, row_count, new_problem(), []) 81 + // |> echo 82 + 83 + echo list.length(problems) as "problems" 84 + echo problem_count as "problem_count" 85 + 86 + problems 87 + |> solve_problems() 88 + } 89 + 90 + /// parses problems one column at a time 91 + fn parse_problems_by_column( 92 + chars: List(String), 93 + row_count: Int, 94 + current_problem, 95 + problems, 96 + ) { 97 + let column = 98 + chars 99 + |> list.take(row_count) 100 + 101 + let column_unique = list.unique(column) 102 + 103 + case column { 104 + [] -> [current_problem, ..problems] 105 + // fully empty column 106 + _ if column_unique == [" "] -> 107 + parse_problems_by_column( 108 + list.drop(chars, row_count), 109 + row_count, 110 + new_problem(), 111 + [current_problem, ..problems], 112 + ) 113 + column -> { 114 + let current_problem = case parse_column(column, 0) { 115 + #(number, Some(operator)) -> 116 + Problem([number, ..current_problem.numbers], operator) 117 + 118 + #(number, None) -> 119 + Problem([number, ..current_problem.numbers], current_problem.operator) 120 + } 121 + 122 + parse_problems_by_column( 123 + list.drop(chars, row_count), 124 + row_count, 125 + current_problem, 126 + problems, 127 + ) 128 + } 129 + } 130 + } 131 + 132 + /// parsed one column worth of data 133 + fn parse_column(column_chars: List(String), number) { 134 + case column_chars { 135 + // last char 136 + [" "] -> #(number, None) 137 + ["*"] -> #(number, Some(Multiply)) 138 + ["+"] -> #(number, Some(Plus)) 139 + // drop leading whitespace 140 + [" ", ..rest] -> parse_column(rest, number) 141 + // number 142 + [num, ..rest] 143 + if num == "0" 144 + || num == "1" 145 + || num == "2" 146 + || num == "3" 147 + || num == "4" 148 + || num == "5" 149 + || num == "6" 150 + || num == "7" 151 + || num == "8" 152 + || num == "9" 153 + -> { 154 + let assert Ok(num) = int.parse(num) 155 + parse_column(rest, number * 10 + num) 156 + } 157 + _ -> panic as "shouldn't happen" 158 + } 159 + } 160 + 161 + fn solve_problems(problems: List(Problem)) -> Int { 162 + problems 163 + |> list.fold(0, fn(acc, problem) { 164 + // echo problem 165 + 166 + let assert Ok(result) = case problem { 167 + Problem(numbers: _, operator: Unset) -> panic as "welp" 168 + 169 + Problem(numbers:, operator: Plus) -> { 170 + list.reduce(numbers, int.add) 171 + } 172 + Problem(numbers:, operator: Multiply) -> 173 + list.reduce(numbers, int.multiply) 174 + } 175 + result + acc 176 + }) 177 + }