Skip to content

Instantly share code, notes, and snippets.

@sotolf2
Created August 1, 2023 10:30
Show Gist options
  • Save sotolf2/9e5490c8938658438cd18449c5fb1b4f to your computer and use it in GitHub Desktop.
Save sotolf2/9e5490c8938658438cd18449c5fb1b4f to your computer and use it in GitHub Desktop.
import
std/streams,
std/strformat,
std/strutils,
std/sequtils
type
Parsed = string
Input = seq[string]
proc parse(inn: Input):Parsed =
inn[0]
proc score(stream: Stream): int =
var level = 0
var inGarbage = false
while not stream.atEnd:
let ch = stream.readChar()
if inGarbage:
case ch:
of '!': discard stream.readChar()
of '>': inGarbage = false
else: continue
else:
case ch:
of '{': inc level
of '<': inGarbage = true
of '}':
result += level
dec level
else: continue
proc countGarbage(stream: Stream): int =
var inGarbage = false
while not stream.atEnd:
let ch = stream.readChar()
if inGarbage:
case ch:
of '>': inGarbage = false
of '!': discard stream.readChar()
else: inc result
else:
case ch:
of '<': inGarbage = true
else: continue
proc part1(input: Parsed) =
var strm = newStringStream input
var sum = score strm
echo &"Part1: {sum}"
proc part2(input: Parsed) =
var strm = newStringStream input
var sum = countGarbage strm
echo &"Part2: {sum}"
let input = toSeq lines("day09.txt")
let parsed = parse(input)
part1 parsed
part2 parsed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment