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