From 61f870ddac2fe97e8ef1f108d6f6a6aa0d6d8a28 Mon Sep 17 00:00:00 2001 From: haskal Date: Thu, 17 Dec 2020 00:17:44 -0500 Subject: [PATCH] day 17: awful submission lol --- 17.rkt | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 17.rkt diff --git a/17.rkt b/17.rkt new file mode 100644 index 0000000..bdb9499 --- /dev/null +++ b/17.rkt @@ -0,0 +1,98 @@ +#lang curly-fn racket + +(require "scripts/aoc.rkt") + +;; solution for day 17 + +(struct pt [x y z w] #:transparent) + +(define (part1 input) + (define world (hash-copy input)) + (define (simulate world) + (define new-world (make-hash)) + (define min-x (apply min (map pt-x (hash-keys world)))) + (define max-x (apply max (map pt-x (hash-keys world)))) + (define min-y (apply min (map pt-y (hash-keys world)))) + (define max-y (apply max (map pt-y (hash-keys world)))) + (define min-z (apply min (map pt-z (hash-keys world)))) + (define max-z (apply max (map pt-z (hash-keys world)))) + (for* ([x (in-range (- min-x 1) (+ max-x 2))] + [y (in-range (- min-y 1) (+ max-y 2))] + [z (in-range (- min-z 1) (+ max-z 2))]) + (define ct (for*/sum ([dx (in-range -1 2)] + [dy (in-range -1 2)] + [dz (in-range -1 2)] + #:unless (and (zero? dx) (zero? dy) (zero? dz)) + #:when (hash-ref world (pt (+ x dx) (+ y dy) (+ z dz) 0) #f)) + 1)) + (define this (pt x y z 0)) + (cond + [(hash-ref world this #f) + (when (or (= 2 ct) (= 3 ct)) + (hash-set! new-world this #t))] + [else + (when (= 3 ct) + (hash-set! new-world this #t))])) + new-world) + + (for ([i (in-range 6)]) + (set! world (simulate world))) + (length (filter identity (hash-values world)))) + +(define (part2 input) + (define world (hash-copy input)) + (define (simulate world) + (define new-world (make-hash)) + (define min-x (apply min (map pt-x (hash-keys world)))) + (define max-x (apply max (map pt-x (hash-keys world)))) + (define min-y (apply min (map pt-y (hash-keys world)))) + (define max-y (apply max (map pt-y (hash-keys world)))) + (define min-z (apply min (map pt-z (hash-keys world)))) + (define max-z (apply max (map pt-z (hash-keys world)))) + (define min-w (apply min (map pt-w (hash-keys world)))) + (define max-w (apply max (map pt-w (hash-keys world)))) + (for* ([x (in-range (- min-x 1) (+ max-x 2))] + [y (in-range (- min-y 1) (+ max-y 2))] + [z (in-range (- min-z 1) (+ max-z 2))] + [w (in-range (- min-w 1) (+ max-w 2))]) + (define ct (for*/sum ([dx (in-range -1 2)] + [dy (in-range -1 2)] + [dz (in-range -1 2)] + [dw (in-range -1 2)] + #:unless (and (zero? dx) (zero? dy) (zero? dz) (zero? dw)) + #:when (hash-ref world (pt (+ x dx) (+ y dy) (+ z dz) (+ w dw)) #f)) + 1)) + (define this (pt x y z w)) + (cond + [(hash-ref world this #f) + (when (or (= 2 ct) (= 3 ct)) + (hash-set! new-world this #t))] + [else + (when (= 3 ct) + (hash-set! new-world this #t))])) + new-world) + + (for ([i (in-range 6)]) + (set! world (simulate world))) + (length (filter identity (hash-values world)))) + +;; parse input file +(define (parse fname) + (define input (file->lines fname)) + (define world (make-hash)) + (for ([line (in-list input)] [y (in-naturals)]) + (for ([char (in-string line)] [x (in-naturals)]) + (when (char=? #\# char) + (hash-set! world (pt x y 0 0) #t)))) + world) + +(module+ test + (require rackunit) + ;; tests here + (displayln "no tests :(")) + +(module+ main + (define input (parse "inputs/17")) + (answer 17 1 (time (part1 input))) + (answer 17 2 (time (part2 input))) + (displayln "meow"))