Day 07 part 2
const
CAPACITY = 70_000_000
NEEDED = 30_000_000
type
Entry = ref object
parent: Entry
size: Natural
var
answer: Natural
cwd = Entry()
level: Natural
dirSizes: seq[Natural]
proc goUp(e: var Entry) {.inline.} =
cwd.parent.size.inc cwd.size
dirSizes.add cwd.size
cwd = cwd.parent
level.dec
proc goDown(e: var Entry) {.inline.} =
cwd = Entry(parent: cwd)
level.inc
for l in "input.txt".lines:
var
size: int
item: string
if l.scanf("$$ cd $+", item):
if item == "..": cwd.goUp
else: cwd.goDown
elif l.scanf("$i $w", size, item): cwd.size.inc size
while level > 0: cwd.goUp
for d in dirSizes.sorted:
answer = d
if d > abs(CAPACITY - NEEDED - cwd.size): break
Answer is: 214171