Skip to content

Commit deffe41

Browse files
committed
finished day 6
1 parent 8f58e71 commit deffe41

File tree

4 files changed

+131
-2
lines changed

4 files changed

+131
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Advent of Code 2025
22

33
[![Tests](https://github.com/devries/advent_of_code_2025/actions/workflows/test.yml/badge.svg)](https://github.com/devries/advent_of_code_2025/actions/workflows/test.yml)
4-
[![Stars: 10](https://img.shields.io/badge/⭐_Stars-10-yellow)](https://adventofcode.com/2025)
4+
[![Stars: 12](https://img.shields.io/badge/⭐_Stars-12-yellow)](https://adventofcode.com/2025)
55

66
This year will be my second year doing Advent of Code in [Gleam](https://gleam.run).
77
Last year I was still learning the language, and in the past year I have used it
@@ -40,3 +40,4 @@ information.
4040
- [Day 3](https://adventofcode.com/2025/day/3): [⭐ ⭐ solution](src/day03/solution.gleam)
4141
- [Day 4](https://adventofcode.com/2025/day/4): [⭐ ⭐ solution](src/day04/solution.gleam)
4242
- [Day 5](https://adventofcode.com/2025/day/5): [⭐ ⭐ solution](src/day05/solution.gleam)
43+
- [Day 6](https://adventofcode.com/2026/day/6): [⭐ ⭐ solution](src/day06/solution.gleam)

inputs

src/day06/solution.gleam

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}

test/day06_test.gleam

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import day06/solution
2+
import gleam/string
3+
4+
const testinput = "123 328 51 64
5+
45 64 387 23
6+
6 98 215 314
7+
* + * + "
8+
9+
pub fn part1_test() {
10+
let lines = string.split(testinput, "\n")
11+
assert solution.solve_p1(lines) == Ok("4277556")
12+
}
13+
14+
pub fn part2_test() {
15+
let lines = string.split(testinput, "\n")
16+
assert solution.solve_p2(lines) == Ok("3263827")
17+
}

0 commit comments

Comments
 (0)