-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvent.comp
More file actions
66 lines (56 loc) · 1.55 KB
/
advent.comp
File metadata and controls
66 lines (56 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
Advent of Code 2025, Day 1 (part 1)
Parse dial movement instructions to find number of 0 landings.
Highlights:
- Parsing lines into simple relative offsets
- No `%` modifier operator, but the math `divmod` functions works cleanly
*/
!func aoc-2025-day1 [
read-lines
| map parse
| accumulate 50 :($.accum + $.value)
| count :([$ | divmod 100].remainder == 0)
| output
]
!pure parse ~text (
!my sign [($.#0 == "R") | pick 1 (-1)]
!my step [$ | slice 1 | parse-num]
sign * step
)
/**
Advent of Code 2022, Day 7 (part 1)
No Space Left On Device
Parse terminal output to reconstruct directory sizes.
Highlights:
- !my rebinding tracks mutable state (cwd) across map iterations
- path-ancestors fans each file size to all parent directories in one step
- No tree construction — paths as strings with group-by replaces recursion
- The main pipeline reads as a recipe: parse, expand, group, filter, sum
*/
!startup main [
read-lines
| parse-entries
| map @flat :[$.path | path-ancestors | map :{path=$ size=$$.size}]
| group :($.path) | sum :($.size)
| values
| where :($ <= 100000)
| sum
| output
]
!func parse-entries ~texts (
!my cwd "/"
[$ | map :(
!on [$ | slice 0 4]
~text<eq="$ ls"> skip
~text<eq="dir "> skip
~text<eq="$ cd"> (!my cwd [$ | slice 5 | path-resolve cwd] skip)
~any {path=cwd size=[$ | split-whitespace limit=1 | $.#0 | parse-num]}
)]
)
/// All ancestor paths including self
/// "/a/e" -> {"/" "/a" "/a/e"}
!pure path-ancestors ~text [
$ | split "/"
| accumulate "" :[$.accum | join "/" $.value]
| map :[$ | if-empty "/"]
]