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 1
regnault.dev
3 months ago
2f49c67a
7500ffbd
+126
2 changed files
expand all
collapse all
unified
split
src
aoc.gleam
days
day1.gleam
+2
src/aoc.gleam
···
1
1
import birl
2
2
import birl/duration
3
3
+
import days/day1
3
4
import gleam/erlang
4
5
import gleam/int
5
6
import gleam/io
···
19
20
let start = birl.now()
20
21
case day {
21
22
0 -> io.println_error("Invalid day")
23
23
+
1 -> day1.start()
22
24
_ -> io.println("Tried to run day " <> int.to_string(day))
23
25
}
24
26
birl.now()
+124
src/days/day1.gleam
···
1
1
+
import gleam/dynamic
2
2
+
import gleam/float
3
3
+
import gleam/int
4
4
+
import gleam/io
5
5
+
import gleam/list
6
6
+
import gleam/result
7
7
+
import gleam/string
8
8
+
import simplifile
9
9
+
10
10
+
type Direction {
11
11
+
Left
12
12
+
Right
13
13
+
}
14
14
+
15
15
+
type Action {
16
16
+
Action(Direction, Int)
17
17
+
}
18
18
+
19
19
+
pub fn start() -> Nil {
20
20
+
let assert Ok(content) = simplifile.read("inputs/day1.txt")
21
21
+
let data = parse(content)
22
22
+
23
23
+
let _ = io.debug(part1(data))
24
24
+
let _ = io.debug(part2(data))
25
25
+
26
26
+
Nil
27
27
+
}
28
28
+
29
29
+
fn parse(data: String) {
30
30
+
data
31
31
+
|> string.split("\n")
32
32
+
|> list.map(fn(l) {
33
33
+
let chars =
34
34
+
l
35
35
+
|> string.to_graphemes()
36
36
+
37
37
+
let direction_string =
38
38
+
chars
39
39
+
|> list.first
40
40
+
|> result.unwrap("")
41
41
+
42
42
+
let direction = case direction_string {
43
43
+
"L" -> Left
44
44
+
"R" -> Right
45
45
+
_ -> Left
46
46
+
}
47
47
+
48
48
+
let number =
49
49
+
chars
50
50
+
|> list.drop(1)
51
51
+
|> string.join("")
52
52
+
|> int.parse
53
53
+
|> result.unwrap(0)
54
54
+
55
55
+
Action(direction, number)
56
56
+
})
57
57
+
}
58
58
+
59
59
+
fn part1(data: List(Action)) {
60
60
+
data
61
61
+
|> list.fold([50], fn(acc: List(Int), elem: Action) {
62
62
+
let current =
63
63
+
acc
64
64
+
|> list.first
65
65
+
|> result.unwrap(0)
66
66
+
67
67
+
let new = case elem {
68
68
+
Action(Left, x) -> { current - x } % 100
69
69
+
Action(Right, x) -> { current + x } % 100
70
70
+
}
71
71
+
72
72
+
let new = case new < 0 {
73
73
+
True -> new + 100
74
74
+
False -> new
75
75
+
}
76
76
+
77
77
+
acc
78
78
+
|> list.prepend(new)
79
79
+
})
80
80
+
|> list.count(fn(x) { x == 0 })
81
81
+
}
82
82
+
83
83
+
fn part2(data: List(Action)) {
84
84
+
data
85
85
+
|> list.fold([#(50, 0)], fn(acc: List(#(Int, Int)), elem: Action) {
86
86
+
let #(current, _) =
87
87
+
acc
88
88
+
|> list.first
89
89
+
|> result.unwrap(#(0, 0))
90
90
+
91
91
+
let Action(dir, clicks) = elem
92
92
+
93
93
+
let remaining = clicks % 100
94
94
+
95
95
+
let reached_zero = case dir {
96
96
+
Left -> current - remaining <= 0 && current != 0
97
97
+
Right -> current + remaining >= 100 && current != 0
98
98
+
}
99
99
+
100
100
+
let reached =
101
101
+
{ clicks / 100 }
102
102
+
+ case reached_zero {
103
103
+
True -> 1
104
104
+
False -> 0
105
105
+
}
106
106
+
107
107
+
let new = case elem {
108
108
+
Action(Left, x) -> { current - x } % 100
109
109
+
Action(Right, x) -> { current + x } % 100
110
110
+
}
111
111
+
112
112
+
let new = case new < 0 {
113
113
+
True -> new + 100
114
114
+
False -> new
115
115
+
}
116
116
+
117
117
+
acc
118
118
+
|> list.prepend(#(new, reached))
119
119
+
})
120
120
+
|> list.fold(0, fn(acc, x) {
121
121
+
let #(_, cnt) = x
122
122
+
acc + cnt
123
123
+
})
124
124
+
}