Compare commits

...

2 Commits

Author SHA1 Message Date
Érico Nogueira 30b0110035 Add Advent 2021 - day 12. 2021-12-12 04:26:45 -03:00
Érico Nogueira 31e1fa2682 Add Advent 2021 - day 11. 2021-12-11 03:32:37 -03:00
7 changed files with 163 additions and 0 deletions

53
2021/day11/p.rkt Normal file
View File

@ -0,0 +1,53 @@
#lang racket/base
(require racket/match)
(define (char->number c)
(string->number (string c)))
(define (make-line t)
(list->vector (map char->number (string->list t))))
(define sv (for/vector ([t (in-lines)]) (make-line t)))
(define v (vector-length sv))
(define h (vector-length (vector-ref sv 0)))
(define (access pv ph)
(vector-ref (vector-ref sv pv) ph))
(define (change pv ph v)
(vector-set! (vector-ref sv pv) ph v))
(define (apply-step i)
; keep track of octopuses that have flashed
(define index 0)
(define nines (make-vector 300 null)) ; arbitrary length
(define (try-add-nine pv ph)
(when (= (access pv ph) 9)
(vector-set! nines index (cons pv ph))
(set! index (add1 index)))
(change pv ph (add1 (access pv ph))))
(for* ([vi (in-range v)] [hi (in-range h)])
(when (> (access vi hi) 9) (change vi hi 0)) ; optimization to make reset simpler
(try-add-nine vi hi))
; loop through nines but being able to add more flashes
(for ([j (in-naturals)] #:break (= j index))
(match-define (cons pv ph) (vector-ref nines j))
(define (touch-point pv ph)
(when (and (and (>= pv 0) (< pv v)) (and (>= ph 0) (< ph h)))
(try-add-nine pv ph)))
(for* ([dv (in-range -1 2)] [dh (in-range -1 2)] #:unless (= 0 dv dh))
(touch-point (+ pv dv) (+ ph dh))))
index)
; part 1
(for/sum ([i (in-range 100)])
(apply-step i))
; part 2
(for/last ([i (in-naturals)])
#:break (= (apply-step i) (* v h))
(+ i 100 2))

10
2021/day11/test1.txt Normal file
View File

@ -0,0 +1,10 @@
5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526

10
2021/day11/test2.txt Normal file
View File

@ -0,0 +1,10 @@
4721224663
6875415276
2742448428
4878231556
5684643743
3553681866
4788183625
4255856532
1415818775
2326886125

43
2021/day12/p.rkt Normal file
View File

@ -0,0 +1,43 @@
#lang racket/base
(require racket/string)
(require racket/set)
(define (cdrm l)
(car (cdr l)))
(define (make-line t)
(sort (string-split t "-") string<?))
(define sset (for/set ([t (in-lines)]) (make-line t)))
(define caves (for/mutable-set ([s sset]) (car s)))
(set-union! caves (for/set ([s sset]) (cdrm s)))
(set-subtract! caves (set "start" "end"))
(define (is-small s)
(char-lower-case? (string-ref s 0)))
(define (valid-path p1 p2)
(set-member? sset (sort (list p1 p2) string<?)))
(define (cannot-use-cave c used)
;(hash-has-key? used c)) ; part 1
(and (hash-has-key? used c) (hash-has-key? used 0))) ; part 2
(define (hash-handle! used c)
(hash-update! used c add1 0)
(when (= (hash-ref used c) 2)
(hash-set! used 0 #t)))
(for/sum ([cave caves])
(define (recurse-path path used c)
(when (is-small c) (hash-handle! used c))
(set! path (append path (list c)))
(+ (if (valid-path "end" c) 1 0)
(for/sum ([nc caves]
#:unless (or (equal? nc c) (cannot-use-cave nc used) (not (valid-path c nc))))
(recurse-path path (hash-copy used) nc))))
(if (valid-path "start" cave)
(recurse-path null (make-hash) cave)
0))

7
2021/day12/test1.txt Normal file
View File

@ -0,0 +1,7 @@
start-A
start-b
A-c
A-b
b-d
A-end
b-end

18
2021/day12/test2.txt Normal file
View File

@ -0,0 +1,18 @@
fs-end
he-DX
fs-he
start-DX
pj-DX
end-zg
zg-sl
zg-pj
pj-he
RW-he
fs-DX
pj-RW
zg-RW
start-pj
he-WI
zg-he
pj-fs
start-RW

22
2021/day12/test3.txt Normal file
View File

@ -0,0 +1,22 @@
end-MY
MY-xc
ho-NF
start-ho
NF-xc
NF-yf
end-yf
xc-TP
MY-qo
yf-TP
dc-NF
dc-xc
start-dc
yf-MY
MY-ho
EM-uh
xc-yf
ho-dc
uh-NF
yf-ho
end-uh
start-NF