tangled
alpha
login
or
join now
eldridge.cam
/
advent-of-code
2
fork
atom
Advent of Code solutions
2
fork
atom
overview
issues
pulls
pipelines
p1 worked, then i try some stuff
eldridge.cam
3 months ago
6680869e
f26fa6aa
+182
3 changed files
expand all
collapse all
unified
split
2025
10
p1-copy.tri
p1.tri
p2.tri
+60
2025/10/p1-copy.tri
···
1
1
+
import "trilogy:io" use readlines
2
2
+
import "trilogy:debug" use dbg
3
3
+
import "trilogy:core" use to_array, length
4
4
+
import "trilogy:bits" as bits
5
5
+
import "trilogy:record" use collect, merge_by, contains_key
6
6
+
import "trilogy:compare" use min
7
7
+
import "trilogy:parsec" use parse, apply, between, many_1, char_of, char, integer, sep_by_1
8
8
+
import "trilogy:array" use map, fold, filter
9
9
+
import "trilogy:iterator" as it
10
10
+
11
11
+
func target_state '.' = false
12
12
+
func target_state '#' = true
13
13
+
14
14
+
func set_bit bb index = bb | (0bb1 ~~> index)
15
15
+
16
16
+
proc machine!() {
17
17
+
let target = bits::from_array
18
18
+
<| map target_state
19
19
+
<| apply
20
20
+
<| between (char '[') (char ']')
21
21
+
<| many_1
22
22
+
<| char_of ".#"
23
23
+
apply <| char ' '
24
24
+
let buttons = map (fold set_bit (target ^ target))
25
25
+
<| apply
26
26
+
<| sep_by_1 (char ' ')
27
27
+
<| between (char '(') (char ')')
28
28
+
<| sep_by_1 (char ',') integer
29
29
+
apply <| char ' '
30
30
+
let _joltage = apply
31
31
+
<| between (char '{') (char '}')
32
32
+
<| sep_by_1 (char ',') integer
33
33
+
return target:buttons
34
34
+
}
35
35
+
36
36
+
func press_button round states button = states
37
37
+
|> to_array
38
38
+
|> filter (fn state:presses. presses == round)
39
39
+
|> map (fn state:presses. state ^ button : presses + 1)
40
40
+
|> it::from
41
41
+
|> collect
42
42
+
43
43
+
proc solve!(target:buttons) {
44
44
+
let mut states = {| target ^ target => 0 |}
45
45
+
let mut i = 0
46
46
+
while !(contains_key target states) {
47
47
+
states = map (press_button i states) buttons
48
48
+
|> fold (merge_by (fn k a b. min a b)) states
49
49
+
i += 1
50
50
+
}
51
51
+
return states.target
52
52
+
}
53
53
+
54
54
+
proc main!() {
55
55
+
readlines
56
56
+
|> it::map (parse machine)
57
57
+
|> it::map solve
58
58
+
|> it::fold (+) 0
59
59
+
|> dbg
60
60
+
}
+59
2025/10/p1.tri
···
1
1
+
import "trilogy:io" use readlines
2
2
+
import "trilogy:debug" use dbg
3
3
+
import "trilogy:core" use to_array, length
4
4
+
import "trilogy:bits" as bits
5
5
+
import "trilogy:compare" use min
6
6
+
import "trilogy:parsec" use parse, apply, between, many_1, char_of, char, integer, sep_by_1
7
7
+
import "trilogy:array" use map, fold, filter, reduce
8
8
+
import "trilogy:set" use contains, difference, union, collect
9
9
+
import "trilogy:iterator" as it
10
10
+
11
11
+
func target_state '.' = false
12
12
+
func target_state '#' = true
13
13
+
14
14
+
func set_bit bb index = bb | (0bb1 ~~> index)
15
15
+
16
16
+
proc machine!() {
17
17
+
let target = bits::from_array
18
18
+
<| map target_state
19
19
+
<| apply
20
20
+
<| between (char '[') (char ']')
21
21
+
<| many_1
22
22
+
<| char_of ".#"
23
23
+
apply <| char ' '
24
24
+
let buttons = map (fold set_bit (target ^ target))
25
25
+
<| apply
26
26
+
<| sep_by_1 (char ' ')
27
27
+
<| between (char '(') (char ')')
28
28
+
<| sep_by_1 (char ',') integer
29
29
+
apply <| char ' '
30
30
+
let _joltage = apply
31
31
+
<| between (char '{') (char '}')
32
32
+
<| sep_by_1 (char ',') integer
33
33
+
return target:buttons
34
34
+
}
35
35
+
36
36
+
37
37
+
proc solve!(target:buttons) {
38
38
+
let mut visited = [| target ^ target |]
39
39
+
let mut states = [| target ^ target |]
40
40
+
let mut i = 0
41
41
+
while !(contains target states) {
42
42
+
states = states
43
43
+
|> it::from
44
44
+
|> it::flat_map (fn state. it::from <| map ((^) state) buttons)
45
45
+
|> collect
46
46
+
visited union= states
47
47
+
states = difference states visited
48
48
+
i += 1
49
49
+
}
50
50
+
return i
51
51
+
}
52
52
+
53
53
+
proc main!() {
54
54
+
readlines
55
55
+
|> it::map (parse machine)
56
56
+
|> it::map solve
57
57
+
|> it::fold (+) 0
58
58
+
|> dbg
59
59
+
}
+63
2025/10/p2.tri
···
1
1
+
import "trilogy:io" use readlines
2
2
+
import "trilogy:debug" use dbg
3
3
+
import "trilogy:core" use to_array, length
4
4
+
import "trilogy:bits" as bits
5
5
+
import "trilogy:record" use collect, merge_by, contains_key
6
6
+
import "trilogy:compare" use min
7
7
+
import "trilogy:parsec" use parse, apply, between, many_1, char_of, char, integer, sep_by_1
8
8
+
import "trilogy:array" use map, fold, filter
9
9
+
import "trilogy:iterator" as it
10
10
+
11
11
+
proc machine!() {
12
12
+
let _target = apply
13
13
+
<| between (char '[') (char ']')
14
14
+
<| many_1
15
15
+
<| char_of ".#"
16
16
+
apply <| char ' '
17
17
+
let buttons = apply
18
18
+
<| sep_by_1 (char ' ')
19
19
+
<| between (char '(') (char ')')
20
20
+
<| sep_by_1 (char ',') integer
21
21
+
apply <| char ' '
22
22
+
let joltage = apply
23
23
+
<| between (char '{') (char '}')
24
24
+
<| sep_by_1 (char ',') integer
25
25
+
return joltage:buttons
26
26
+
}
27
27
+
28
28
+
proc update_state!(state, n) {
29
29
+
state.n += 1
30
30
+
}
31
31
+
32
32
+
func apply_button state [] = state
33
33
+
func apply_button state [n, ..rest] = apply_button update_state!(state, n) rest
34
34
+
35
35
+
func press_button round states button = states
36
36
+
|> to_array
37
37
+
|> filter (fn state:presses. presses == round)
38
38
+
|> map (fn state:presses. apply_button state button : presses + 1)
39
39
+
|> it::from
40
40
+
|> collect
41
41
+
42
42
+
func below [] [] = true
43
43
+
func below [a, ..ra] [b, ..rb] = if b <= a then below ra rb else false
44
44
+
45
45
+
proc solve!(target:buttons) {
46
46
+
let mut states = {| map (fn _. 0) target => 0 |}
47
47
+
let mut i = 0
48
48
+
while !(contains_key target states) {
49
49
+
states = map (press_button i states) buttons
50
50
+
|> filter (fn state:_. below target state)
51
51
+
|> fold (merge_by (fn k a b. min a b)) states
52
52
+
i += 1
53
53
+
}
54
54
+
return states.target
55
55
+
}
56
56
+
57
57
+
proc main!() {
58
58
+
readlines
59
59
+
|> it::map (parse machine)
60
60
+
|> it::map solve
61
61
+
|> it::fold (+) 0
62
62
+
|> dbg
63
63
+
}