Skip to content

Commit

Permalink
Shorten
Browse files Browse the repository at this point in the history
  • Loading branch information
dsemi committed Jul 22, 2023
1 parent 0ad291d commit 8bec1cd
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/year2022/day07.nim
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
import strutils
import std/algorithm
import std/math
import std/strutils

proc getAllSizes(input: string): seq[int] =
template moveUp() =
fstree[^2] += fstree[^1]
result.add(fstree.pop)
var fstree = @[0]
proc allSizes(input: string): seq[int] =
var fstree = newSeq[int]()
for line in input.splitLines:
if line.startsWith("$ cd "):
if line.endsWith("/"):
while fstree.len > 1:
moveUp
elif line.endsWith(".."):
moveUp
if line.endsWith(".."):
fstree[^2] += fstree[^1]
result.add fstree.pop
else:
fstree.add(0)
elif line.find({'0'..'9'}) == 0:
fstree[^1] += line.split[0].parseInt
while fstree.len > 1:
moveUp
result.add(fstree[0])
result.add fstree.reversed.cumsummed

proc part1*(input: string): int =
for size in input.getAllSizes:
for size in input.allSizes:
if size <= 100000:
result += size

proc part2*(input: string): int =
let sizes = input.getAllSizes
let sizes = input.allSizes
let target = sizes[^1] - 40000000
result = int.high
for size in sizes:
Expand Down

0 comments on commit 8bec1cd

Please sign in to comment.