tangled
alpha
login
or
join now
regnault.dev
/
gleam-aoc-2025
1
fork
atom
this repo has no description
1
fork
atom
overview
issues
pulls
pipelines
Day 3
regnault.dev
3 months ago
8f174c6c
ec64cb2a
+76
2 changed files
expand all
collapse all
unified
split
src
aoc.gleam
days
day3.gleam
+2
src/aoc.gleam
···
2
import birl/duration
3
import days/day1
4
import days/day2
0
5
import gleam/erlang
6
import gleam/int
7
import gleam/io
···
23
0 -> io.println_error("Invalid day")
24
1 -> day1.start()
25
2 -> day2.start()
0
26
_ -> io.println("Tried to run day " <> int.to_string(day))
27
}
28
birl.now()
···
2
import birl/duration
3
import days/day1
4
import days/day2
5
+
import days/day3
6
import gleam/erlang
7
import gleam/int
8
import gleam/io
···
24
0 -> io.println_error("Invalid day")
25
1 -> day1.start()
26
2 -> day2.start()
27
+
3 -> day3.start()
28
_ -> io.println("Tried to run day " <> int.to_string(day))
29
}
30
birl.now()
+74
src/days/day3.gleam
···
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
···
1
+
import gleam/float
2
+
import gleam/int
3
+
import gleam/io
4
+
import gleam/list
5
+
import gleam/result
6
+
import gleam/string
7
+
import simplifile
8
+
import utils/utils
9
+
10
+
pub fn start() -> Nil {
11
+
let assert Ok(content) = simplifile.read("inputs/day3.txt")
12
+
let data = parse(content)
13
+
14
+
let _ = io.debug(part1(data))
15
+
let _ = io.debug(part2(data))
16
+
17
+
Nil
18
+
}
19
+
20
+
fn parse(data: String) {
21
+
string.split(data, "\n")
22
+
|> list.map(string.to_graphemes)
23
+
|> list.map(list.map(_, int.parse))
24
+
|> list.map(list.map(_, result.unwrap(_, 0)))
25
+
}
26
+
27
+
type LstMaxIdx {
28
+
LstMaxIdx(idx: Int, value: Int)
29
+
}
30
+
31
+
fn max_int_from_index(lst: List(Int), start_from: Int, last_offset: Int) {
32
+
lst
33
+
|> list.drop(start_from)
34
+
|> list.index_fold(LstMaxIdx(0, 0), fn(max, num, idx) {
35
+
case idx >= { list.length(lst) - start_from - last_offset } {
36
+
True -> max
37
+
False ->
38
+
case max.value < num {
39
+
True -> LstMaxIdx(idx + start_from, num)
40
+
False -> max
41
+
}
42
+
}
43
+
})
44
+
}
45
+
46
+
fn part1(data: List(List(Int))) {
47
+
list.map(data, fn(x) {
48
+
let first = max_int_from_index(x, 0, 1)
49
+
let second = max_int_from_index(x, first.idx + 1, 0)
50
+
first.value * 10 + second.value
51
+
})
52
+
|> list.fold(0, int.add)
53
+
}
54
+
55
+
fn part2(data: List(List(Int))) {
56
+
let range = utils.create_int_range(0, 12)
57
+
list.map(data, fn(x) {
58
+
list.fold(range, #(0, 0), fn(acc, offset) {
59
+
let res = max_int_from_index(x, acc.1, { 11 - offset })
60
+
#(
61
+
acc.0
62
+
+ res.value
63
+
* {
64
+
int.power(10, float.subtract({ 12.0 -. 1.0 }, int.to_float(offset)))
65
+
|> result.unwrap(0.0)
66
+
|> float.round
67
+
},
68
+
res.idx + 1,
69
+
)
70
+
}).0
71
+
})
72
+
|> list.reduce(int.add)
73
+
|> result.unwrap(0)
74
+
}