-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.rkt
50 lines (40 loc) · 1.35 KB
/
day12.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#lang racket
(require "aoc.rkt")
(define (parse-initial data)
(define state (drop (string->list (first data)) 15))
(for/list ([(c i) (in-indexed state)]
#:when (char=? c #\#))
i))
(define (extract-fruitful data)
(define rules (drop data 2))
(define (is-fruitful? rule)
(define splitted (string-split rule))
(and (string=? "#" (last splitted))
(string->list (first splitted))))
(filter-map is-fruitful? rules))
(define (grow plants)
(define (creates-new-life? i)
(define pattern
(for/list ([j (in-inclusive-range (- i 2) (+ i 2))])
(if (member j plants) #\# #\.)))
(member pattern fruitful))
(for/list ([i (in-inclusive-range (- (first plants) 2)
(+ (last plants) 2))]
#:when (creates-new-life? i))
i))
(define (live plants [generations 20])
(for/fold ([plants plants])
([_ (in-range generations)])
(grow plants)))
(define (part-2 plants [generations 50000000000])
(define after-200 (live plants 200))
(define increase-rate
(- (apply + (grow after-200))
(apply + after-200)))
(+ (apply + after-200)
(* increase-rate (- generations 200))))
(define data (read-input 12))
(define initial-plants (parse-initial data))
(define fruitful (extract-fruitful data))
(apply + (live initial-plants))
(part-2 initial-plants)