2020-12-24 05:29:33 +00:00
|
|
|
#lang curly-fn racket
|
|
|
|
|
|
|
|
(require "scripts/aoc.rkt")
|
|
|
|
|
|
|
|
;; solution for day 24
|
|
|
|
|
2020-12-24 06:32:21 +00:00
|
|
|
(define *dirs* (hash 'e 1-1i 'ne 1 'nw +1i 'w -1+1i 'sw -1 'se -1i))
|
|
|
|
(define *dirs-values* (hash-values *dirs*))
|
2020-12-24 05:29:33 +00:00
|
|
|
|
|
|
|
(define (paths->tiles input)
|
2020-12-24 05:54:24 +00:00
|
|
|
;; white is #f
|
|
|
|
;; black is #t
|
2020-12-24 05:29:33 +00:00
|
|
|
(define tiles (make-hash))
|
|
|
|
(for ([path (in-list input)])
|
|
|
|
(define posn
|
2020-12-24 06:32:21 +00:00
|
|
|
(for/fold ([posn 0]) ([p (in-list path)])
|
|
|
|
(+ posn (hash-ref *dirs* p))))
|
2020-12-24 05:29:33 +00:00
|
|
|
(hash-update! tiles posn not #f))
|
|
|
|
tiles)
|
|
|
|
|
2020-12-24 05:54:24 +00:00
|
|
|
(define (count-black tiles)
|
2020-12-24 05:29:33 +00:00
|
|
|
(for/sum ([(_ v) (in-hash tiles)] #:when v) 1))
|
|
|
|
|
2020-12-24 05:54:24 +00:00
|
|
|
(define (part1 input)
|
|
|
|
(count-black (paths->tiles input)))
|
|
|
|
|
2020-12-24 05:29:33 +00:00
|
|
|
(define (part2 input)
|
|
|
|
(define tiles (paths->tiles input))
|
|
|
|
|
2020-12-24 05:54:24 +00:00
|
|
|
(define (step tiles radius)
|
2020-12-24 06:32:21 +00:00
|
|
|
(define new-tiles (hash-copy tiles))
|
|
|
|
(for* ([x (in-range (- radius) (add1 radius))] [y (in-range (- radius) (add1 radius))])
|
|
|
|
(define thispos (make-rectangular x y))
|
2020-12-24 05:29:33 +00:00
|
|
|
(define thistile (hash-ref tiles thispos #f))
|
2020-12-24 05:54:24 +00:00
|
|
|
(define num-blacks
|
2020-12-24 06:32:21 +00:00
|
|
|
(for/sum ([neigh (in-list *dirs-values*)] #:when (hash-ref tiles (+ thispos neigh) #f))
|
|
|
|
1))
|
2020-12-24 05:29:33 +00:00
|
|
|
(hash-set! new-tiles thispos
|
2020-12-24 05:54:24 +00:00
|
|
|
(match* (thistile num-blacks)
|
|
|
|
[(#t (or 0 (not (or 1 2)))) #f]
|
|
|
|
[(#t _) #t]
|
|
|
|
[(#f 2) #t]
|
|
|
|
[(#f _) #f])))
|
2020-12-24 05:29:33 +00:00
|
|
|
new-tiles)
|
|
|
|
|
2020-12-24 06:32:21 +00:00
|
|
|
(define radius (apply max (map #{max (abs (real-part %)) (abs (imag-part %))}
|
2020-12-24 05:54:24 +00:00
|
|
|
(hash-keys tiles))))
|
2020-12-24 06:32:21 +00:00
|
|
|
(count-black
|
|
|
|
(for/fold ([tiles tiles]) ([i (in-range 100)] [radius (in-range (add1 radius) +inf.0)])
|
|
|
|
(step tiles radius))))
|
2020-12-24 05:29:33 +00:00
|
|
|
|
|
|
|
;; 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"))
|