day 11
This commit is contained in:
parent
a51a25f50e
commit
a249bedeb6
|
@ -0,0 +1,89 @@
|
|||
#lang curly-fn racket
|
||||
|
||||
(require "scripts/aoc.rkt")
|
||||
|
||||
;; solution for day 11
|
||||
|
||||
;; helper functions here
|
||||
|
||||
(define (count-adj input x y)
|
||||
(for*/sum ([dx (in-range (sub1 x) (+ 2 x))] [dy (in-range (sub1 y) (+ 2 y))]
|
||||
#:unless (and (= dx x) (= dy y))
|
||||
#:unless (or (< dx 0) (< dy 0) (>= dx (vector-length (vector-ref input 0)))
|
||||
(>= dy (vector-length input))))
|
||||
(if (equal? 'full (vector-ref (vector-ref input dy) dx))
|
||||
1
|
||||
0)))
|
||||
|
||||
(define (simulate input count-adj max-adj)
|
||||
(for/vector ([yv (in-vector input)] [y (in-naturals)])
|
||||
(for/vector ([xv (in-vector yv)] [x (in-naturals)])
|
||||
(define ct (count-adj input x y))
|
||||
(cond
|
||||
[(and (equal? xv 'full) (>= ct max-adj)) 'empty]
|
||||
[(and (equal? xv 'empty) (zero? ct)) 'full]
|
||||
[else xv]))))
|
||||
|
||||
|
||||
(define (part1 input)
|
||||
(define next (simulate input count-adj 4))
|
||||
(if (equal? input next)
|
||||
(for*/sum ([line (in-vector next)] [val (in-vector line)] #:when (equal? val 'full)) 1)
|
||||
(part1 next)))
|
||||
|
||||
(define (count-adj2 input x y)
|
||||
(define (find-first sx sy dx dy)
|
||||
(cond
|
||||
[(or (< sx 0) (< sy 0) (>= sy (vector-length input))
|
||||
(>= sx (vector-length (vector-ref input 0))))
|
||||
#f]
|
||||
[else
|
||||
(define v (vector-ref (vector-ref input sy) sx))
|
||||
(if v
|
||||
v
|
||||
(find-first (+ sx dx) (+ sy dy) dx dy))]))
|
||||
|
||||
(for/sum ([searches (in-list '((1 1) (1 0) (1 -1)
|
||||
(0 1) (0 -1)
|
||||
(-1 1) (-1 0) (-1 -1)))])
|
||||
(define dx (first searches))
|
||||
(define dy (second searches))
|
||||
(if (equal? 'full (find-first (+ dx x) (+ dy y) (first searches) (second searches)))
|
||||
1
|
||||
0)))
|
||||
|
||||
(define (part2 input)
|
||||
(define next (simulate input count-adj2 5))
|
||||
(if (equal? input next)
|
||||
(for*/sum ([line (in-vector next)] [val (in-vector line)] #:when (equal? val 'full)) 1)
|
||||
(part2 next)))
|
||||
|
||||
|
||||
(module+ test
|
||||
(require rackunit)
|
||||
;; tests here
|
||||
(displayln "no tests :("))
|
||||
|
||||
(define (parse file)
|
||||
(for/vector ([line (in-list (file->lines file))])
|
||||
(for/vector ([chr (in-string line)])
|
||||
(match chr
|
||||
[#\. #f]
|
||||
[#\L 'empty]
|
||||
[#\# 'full]))))
|
||||
|
||||
; (define test (parse "test"))
|
||||
; (define meow (simulate test count-adj2 5))
|
||||
; (define meow2 (simulate meow count-adj2 5))
|
||||
; (define meow3 (simulate meow2 count-adj2 5))
|
||||
; (pretty-write meow3)
|
||||
|
||||
(module+ main
|
||||
(define input (parse "inputs/11"))
|
||||
;; part 1
|
||||
(answer 11 1 (part1 input))
|
||||
|
||||
;; part 2
|
||||
(answer 11 2 (part2 input))
|
||||
|
||||
(displayln "meow"))
|
Loading…
Reference in New Issue