Day 14.
This commit is contained in:
parent
999d93f645
commit
dbf44b436d
|
@ -0,0 +1,102 @@
|
||||||
|
KHSSCSKKCPFKPPBBOKVF
|
||||||
|
|
||||||
|
OS -> N
|
||||||
|
KO -> O
|
||||||
|
SK -> B
|
||||||
|
NV -> N
|
||||||
|
SH -> V
|
||||||
|
OB -> V
|
||||||
|
HH -> F
|
||||||
|
HP -> H
|
||||||
|
BP -> O
|
||||||
|
HS -> K
|
||||||
|
SN -> B
|
||||||
|
PS -> C
|
||||||
|
BS -> K
|
||||||
|
CF -> H
|
||||||
|
SO -> C
|
||||||
|
NO -> H
|
||||||
|
PP -> H
|
||||||
|
SS -> P
|
||||||
|
KV -> B
|
||||||
|
KN -> V
|
||||||
|
CC -> S
|
||||||
|
HK -> H
|
||||||
|
FN -> C
|
||||||
|
OO -> K
|
||||||
|
CH -> H
|
||||||
|
CP -> V
|
||||||
|
HB -> N
|
||||||
|
VC -> S
|
||||||
|
SP -> F
|
||||||
|
BO -> F
|
||||||
|
SF -> H
|
||||||
|
VO -> B
|
||||||
|
FF -> P
|
||||||
|
CN -> O
|
||||||
|
NP -> H
|
||||||
|
KK -> N
|
||||||
|
OP -> S
|
||||||
|
BH -> F
|
||||||
|
CB -> V
|
||||||
|
HC -> P
|
||||||
|
KH -> V
|
||||||
|
OV -> V
|
||||||
|
NK -> S
|
||||||
|
PN -> F
|
||||||
|
VV -> N
|
||||||
|
HO -> S
|
||||||
|
KS -> C
|
||||||
|
FP -> F
|
||||||
|
FH -> F
|
||||||
|
BB -> C
|
||||||
|
FB -> V
|
||||||
|
SB -> K
|
||||||
|
KP -> B
|
||||||
|
FS -> C
|
||||||
|
KC -> P
|
||||||
|
SC -> C
|
||||||
|
VF -> F
|
||||||
|
VN -> B
|
||||||
|
CK -> C
|
||||||
|
KF -> H
|
||||||
|
NS -> C
|
||||||
|
FV -> K
|
||||||
|
HV -> B
|
||||||
|
HF -> K
|
||||||
|
ON -> S
|
||||||
|
CV -> N
|
||||||
|
BV -> F
|
||||||
|
NB -> N
|
||||||
|
NN -> F
|
||||||
|
BF -> N
|
||||||
|
VB -> V
|
||||||
|
VS -> K
|
||||||
|
BK -> V
|
||||||
|
VP -> P
|
||||||
|
PB -> F
|
||||||
|
KB -> C
|
||||||
|
VK -> O
|
||||||
|
NF -> F
|
||||||
|
FO -> F
|
||||||
|
PH -> N
|
||||||
|
VH -> B
|
||||||
|
HN -> B
|
||||||
|
FK -> K
|
||||||
|
PO -> H
|
||||||
|
CO -> B
|
||||||
|
FC -> V
|
||||||
|
OK -> F
|
||||||
|
OF -> V
|
||||||
|
PF -> F
|
||||||
|
BC -> B
|
||||||
|
BN -> O
|
||||||
|
NC -> K
|
||||||
|
SV -> H
|
||||||
|
OH -> B
|
||||||
|
PC -> O
|
||||||
|
OC -> C
|
||||||
|
CS -> P
|
||||||
|
PV -> V
|
||||||
|
NH -> C
|
||||||
|
PK -> H
|
5
lib.rkt
5
lib.rkt
|
@ -89,6 +89,11 @@
|
||||||
(define (char->number c)
|
(define (char->number c)
|
||||||
(- (char->integer c) (char->integer #\0)))
|
(- (char->integer c) (char->integer #\0)))
|
||||||
|
|
||||||
|
;; char->symbol : char? -> symbol?
|
||||||
|
;; Convert a character into the corresponding symbol
|
||||||
|
(define (char->symbol c)
|
||||||
|
(string->symbol (list->string (list c))))
|
||||||
|
|
||||||
;; string->vector : string? -> (vectorof char?)
|
;; string->vector : string? -> (vectorof char?)
|
||||||
(define (string->vector str)
|
(define (string->vector str)
|
||||||
(list->vector (string->list str)))
|
(list->vector (string->list str)))
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
#lang curly-fn racket
|
||||||
|
|
||||||
|
(require "../lib.rkt")
|
||||||
|
|
||||||
|
(define-values (template rules left-end right-end)
|
||||||
|
(match-let ([(list `(,template*) rules) (problem-input-grouped-lines 14)])
|
||||||
|
(define template (map char->symbol (string->list template*)))
|
||||||
|
(values (for/hash ([left template]
|
||||||
|
[right (rest template)])
|
||||||
|
(values (cons left right) 1))
|
||||||
|
(for/hash ([rule rules])
|
||||||
|
(match-let ([(list _ left right insert) (regexp-match #px"(\\w)(\\w) -> (\\w)" rule)])
|
||||||
|
(values (cons (string->symbol left)
|
||||||
|
(string->symbol right))
|
||||||
|
(string->symbol insert))))
|
||||||
|
(first template) (last template))))
|
||||||
|
|
||||||
|
(define (step template)
|
||||||
|
(for/fold ([polymer (hash)])
|
||||||
|
([pair (hash-keys template)])
|
||||||
|
(match-let* ([(cons left right) pair]
|
||||||
|
[insert (hash-ref rules pair)]
|
||||||
|
[count (hash-ref template pair)]
|
||||||
|
[polymer (hash-update polymer (cons left insert) #{+ % count} 0)]
|
||||||
|
[polymer (hash-update polymer (cons insert right) #{+ % count} 0)])
|
||||||
|
polymer)))
|
||||||
|
|
||||||
|
(define (count n)
|
||||||
|
(for/fold ([counts (hash right-end 1 left-end 1)]
|
||||||
|
#:result (let ([counts (hash-values counts)])
|
||||||
|
(/ (- (maximum counts) (minimum counts)) 2)))
|
||||||
|
([(k v) ((iterate step n) template)])
|
||||||
|
(match-let* ([(cons left right) k]
|
||||||
|
[counts (hash-update counts left #{+ % v} 0)]
|
||||||
|
[counts (hash-update counts right #{+ % v} 0)])
|
||||||
|
counts)))
|
||||||
|
|
||||||
|
(show-solution (count 10) (count 40))
|
Loading…
Reference in New Issue