diff --git a/24.rkt b/24.rkt new file mode 100644 index 0000000..431f724 --- /dev/null +++ b/24.rkt @@ -0,0 +1,80 @@ +#lang curly-fn racket + +(require "scripts/aoc.rkt") + +;; solution for day 24 + +; (define d->r degrees->radians) +; (define (i->e x) (inexact->exact (round x))) +(struct hex [x y z] #:transparent) +(define (hex+ a b) + (hex (+ (hex-x a) (hex-x b)) + (+ (hex-y a) (hex-y b)) + (+ (hex-z a) (hex-z b)))) + +(define *dirs* (hash + 'e (hex 1 -1 0) + 'ne (hex 1 0 -1) + 'nw (hex 0 1 -1) + 'w (hex -1 1 0) + 'sw (hex -1 0 1) + 'se (hex 0 -1 1))) + +(define (paths->tiles input) + (define tiles (make-hash)) + (for ([path (in-list input)]) + (define posn + (for/fold ([posn (hex 0 0 0)]) ([p (in-list path)]) + (hex+ posn (hash-ref *dirs* p)))) + (hash-update! tiles posn not #f)) + tiles) + +(define (part1 input) + ;; white is #f + ;; black is #t + (define tiles (paths->tiles input)) + (for/sum ([(_ v) (in-hash tiles)] #:when v) 1)) + +(define (part2 input) + (define tiles (paths->tiles input)) + + (define (step tiles) + (define new-tiles (make-hash)) + (define minx (apply min (map hex-x (hash-keys tiles)))) + (define maxx (apply max (map hex-x (hash-keys tiles)))) + (define miny (apply min (map hex-y (hash-keys tiles)))) + (define maxy (apply max (map hex-y (hash-keys tiles)))) + (define minz (apply min (map hex-z (hash-keys tiles)))) + (define maxz (apply max (map hex-z (hash-keys tiles)))) + (for* ([x (in-range (sub1 minx) (+ maxx 2))] [y (in-range (sub1 miny) (+ maxy 2))] [z (in-range (sub1 minz) (+ maxz 2))] #:when (zero? (+ x y z))) + (define thispos (hex x y z)) + (define thistile (hash-ref tiles thispos #f)) + (define num-blacks (for/sum ([t (in-list (hash-values *dirs*))] #:when (hash-ref tiles (hex+ thispos t) #f)) 1)) + (hash-set! new-tiles thispos + (cond + [(and thistile (or (zero? num-blacks) (> num-blacks 2))) #f] + [thistile #t] + [(= num-blacks 2) #t] + [else #f]))) + new-tiles) + + (define end (for/fold ([tiles tiles]) ([i (in-range 100)]) (displayln i) (step tiles))) + + (for/sum ([(_ v) (in-hash end)] #:when v) 1)) + +;; parse input file +(define (parse fname) + (for/list ([line (in-list (file->lines fname))]) + (map string->symbol (regexp-match* #px"(e|se|sw|w|nw|ne)" line)))) + + +(module+ test + (require rackunit) + ;; tests here + (displayln "no tests :(")) + +(module+ main + (define input (parse "inputs/24")) + (answer 24 1 (time (part1 input))) + (answer 24 2 (time (part2 input))) + (displayln "meow"))