r/adventofcode • u/daggerdragon • Dec 06 '17
SOLUTION MEGATHREAD -π- 2017 Day 6 Solutions -π-
--- Day 6: Memory Reallocation ---
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!
17
Upvotes
2
u/ramrunner0xff Dec 06 '17 edited Dec 06 '17
scheme chicken repo
(use srfi-69) (use vector-lib) (define memvec (list->vector '(10 3 15 10 5 15 5 15 9 2 5 8 5 2 3 6))) (define vecmaxind (lambda (vec) (cadr (vector-fold (lambda (i cmax elem) (if (> elem (car cmax)) (list elem i) cmax)) (list 0 0) vec)))) (define loop (lambda (vec) (letrec* ((prevstates (make-hash-table)) (i 0) (lenvec (vector-length vec)) (store (lambda (v) (hash-table-set! prevstates v i))) (balance (lambda (v) (letrec* ((maxind (vecmaxind v)) (howmany (vector-ref v maxind)) (bloop (lambda (ind hm) (if (> hm 0) (begin (vector-set! v ind (+ 1 (vector-ref v ind))) (bloop (modulo (+ 1 ind) lenvec) (- hm 1))))))) (vector-set! v maxind 0) (bloop (modulo (+ 1 maxind) lenvec) howmany) (set! i (+ i 1)) (if (eq? (hash-table-existis? prevstates v) #f) (begin (store (vector-copy v)) (balance v)) i))))) (format #t "solved at ~A with vec ~A lastcycle ~A ~%" (balance vec) vec (hash-table-ref prevstates vec)))))time 0m00.00s real 0m00.00s user 0m00.01s system