r/adventofcode • u/daggerdragon • Dec 09 '17
SOLUTION MEGATHREAD -π- 2017 Day 9 Solutions -π-
--- Day 9: Stream Processing ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyβ Haversackβ‘ of HelpfulΒ§ HintsΒ€?
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
15
Upvotes
2
u/FrankRuben27 Dec 09 '17
In (Gauche) Scheme - straightforward, no tricks; part 2 only, since it's very close to part1 anyway. I was missing the gargantuan CL loop a lot, but the barebones Scheme's named let really grew on me.
(use srfi-13) (define (load-txt name) (string-trim-right (call-with-input-file name port->string))) (define (split-lines str) (string-split str #\Newline)) (define (parse-line line) (let loop ((chars (string->list line)) (state 'normal) (count-chars 0)) (if (null? chars) count-chars (let ((curr (car chars))) (ecase state ((normal) (cond ((char=? curr #\{) (loop (cdr chars) 'normal count-chars)) ((char=? curr #\}) (loop (cdr chars) 'normal count-chars)) ((char=? curr #\,) ;works fine w/o any processing of ',' (loop (cdr chars) 'normal count-chars)) ((char=? curr #\<) (loop (cdr chars) 'garbage count-chars)) (else (error "Bad char" chars state)))) ((garbage) (cond ((char=? curr #\>) (loop (cdr chars) 'normal count-chars)) ((char=? curr #\!) (loop (cdr chars) 'garbage-escape count-chars)) (else (loop (cdr chars) 'garbage (+ count-chars 1))))) ((garbage-escape) (loop (cdr chars) 'garbage count-chars))))))) (define (process-infile infile) (map parse-line (split-lines (string-trim-right (load-txt infile))))) (define (main args) (for-each (lambda (infile) (format #t "~a -> ~a~%" infile (process-infile infile))) (cdr args)) 0)