|
| 1 | +import gleam/int |
| 2 | +import gleam/io |
| 3 | +import gleam/list |
| 4 | +import gleam/result |
| 5 | +import gleam/string |
| 6 | +import internal/aoc_utils |
| 7 | + |
| 8 | +pub fn main() { |
| 9 | + let filename = "inputs/day06.txt" |
| 10 | + |
| 11 | + let lines_result = aoc_utils.read_lines(from: filename) |
| 12 | + case lines_result { |
| 13 | + Ok(lines) -> { |
| 14 | + // If the file was converting into a list of lines |
| 15 | + // successfully then run each part of the problem |
| 16 | + aoc_utils.run_part_and_print("Part 1", fn() { solve_p1(lines) }) |
| 17 | + aoc_utils.run_part_and_print("Part 2", fn() { solve_p2(lines) }) |
| 18 | + } |
| 19 | + Error(_) -> io.println("Error reading file") |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// Part 1 |
| 24 | +pub fn solve_p1(lines: List(String)) -> Result(String, String) { |
| 25 | + lines |
| 26 | + |> list.map(fn(line) { |
| 27 | + line |
| 28 | + |> string.split(" ") |
| 29 | + |> list.filter(fn(v) { v != "" }) |
| 30 | + }) |
| 31 | + |> list.transpose |
| 32 | + |> list.map(perform_calculation) |
| 33 | + |> int.sum |
| 34 | + |> int.to_string |
| 35 | + |> Ok |
| 36 | +} |
| 37 | + |
| 38 | +// Part 2 |
| 39 | +pub fn solve_p2(lines: List(String)) -> Result(String, String) { |
| 40 | + let line_length = |
| 41 | + list.fold(lines, 0, fn(acc, v) { int.max(acc, string.length(v)) }) |
| 42 | + |
| 43 | + lines |
| 44 | + |> list.map(fn(line) { |
| 45 | + let new_line = |
| 46 | + string.to_graphemes(line) |
| 47 | + |> list.reverse |
| 48 | + |
| 49 | + let n = line_length - list.length(new_line) |
| 50 | + list.append(list.repeat(" ", n), new_line) |
| 51 | + }) |
| 52 | + |> list.transpose |
| 53 | + |> list.filter(fn(l) { |
| 54 | + list.fold_until(l, False, fn(_, v) { |
| 55 | + case v == " " { |
| 56 | + True -> list.Continue(False) |
| 57 | + False -> list.Stop(True) |
| 58 | + } |
| 59 | + }) |
| 60 | + }) |
| 61 | + |> list.map(fn(column) { |
| 62 | + let l = list.length(column) |
| 63 | + let #(numbers, operation) = list.split(column, l - 1) |
| 64 | + let assert Ok(value) = string.join(numbers, "") |> string.trim |> int.parse |
| 65 | + #(value, operation) |
| 66 | + }) |
| 67 | + |> accumulate_operation(0, []) |
| 68 | + |> int.to_string |
| 69 | + |> Ok |
| 70 | +} |
| 71 | + |
| 72 | +fn perform_calculation(parts: List(String)) -> Int { |
| 73 | + let l = list.length(parts) |
| 74 | + let #(values, operation) = list.split(parts, l - 1) |
| 75 | + |
| 76 | + let int_values = |
| 77 | + values |
| 78 | + |> list.map(int.parse) |
| 79 | + |> result.values |
| 80 | + |
| 81 | + case operation { |
| 82 | + ["+"] -> int.sum(int_values) |
| 83 | + ["*"] -> list.fold(int_values, 1, fn(acc, v) { acc * v }) |
| 84 | + _ -> { |
| 85 | + echo parts |
| 86 | + panic as "unexpected operation" |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +fn accumulate_operation( |
| 92 | + tuples: List(#(Int, List(String))), |
| 93 | + sum: Int, |
| 94 | + values: List(Int), |
| 95 | +) -> Int { |
| 96 | + case tuples { |
| 97 | + [] -> sum |
| 98 | + [first, ..rest] -> { |
| 99 | + case first { |
| 100 | + #(n, [" "]) -> accumulate_operation(rest, sum, [n, ..values]) |
| 101 | + #(n, ["*"]) -> { |
| 102 | + let product = list.fold([n, ..values], 1, fn(p, v) { p * v }) |
| 103 | + accumulate_operation(rest, sum + product, []) |
| 104 | + } |
| 105 | + #(n, ["+"]) -> |
| 106 | + accumulate_operation(rest, sum + int.sum([n, ..values]), []) |
| 107 | + _ -> panic as "unexpected tuple" |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments