Paste: aoc 7
Author: | Kren/chunes |
Mode: | factor |
Date: | Wed, 7 Dec 2022 06:53:38 |
Plain Text |
USING: assocs assocs.extras grouping.extras io.encodings.ascii
io.files kernel literals math math.parser multiline namespaces
peg peg.ebnf prettyprint sequences sorting strings ;
IN: aoc2022.day07
CONSTANT: total-space 70,000,000
CONSTANT: update-size 30,000,000
CONSTANT: input $[
"vocab:aoc2022/day07/input.txt" ascii file-lines
]
SYMBOLS: files cwd ;
H{ } clone files set-global
V{ } clone cwd set-global
EBNF: construct-files [=[
cdin = "$ cd "~ .+ => [[ cwd swap '[ _ >string suffix! ] change ignore ]]
cdout = "$ cd .." => [[ cwd [ dup pop* ] change ignore ]]
cd = cdout | cdin
ls = "$ ls"~
dir = "dir "~ (.+)~
num = [0-9]+ => [[ dec> ]]
file = num:n " "~ (.+)~ => [[ cwd get head-clump [ concat ] map [ n swap files get at+ ] each ignore ]]
output = file | cd | ls | dir
]=]
: part1 ( -- )
input [ construct-files drop ] each files get
[ 100,000 <= ] filter-values sum-values . ;
: unused-space ( -- n ) total-space "/" files get at - ;
: target-deletion ( -- n ) update-size unused-space - ;
: part2 ( -- )
files get values natural-sort
target-deletion '[ _ >= ] find nip . ;
: day07 ( -- ) part1 part2 ;
MAIN: day07
New Annotation