diff options
Diffstat (limited to '2021/14')
-rw-r--r-- | 2021/14/code.tl | 48 | ||||
-rw-r--r-- | 2021/14/codes.tl | 42 | ||||
-rw-r--r-- | 2021/14/input | 102 | ||||
-rw-r--r-- | 2021/14/testinput | 18 |
4 files changed, 210 insertions, 0 deletions
diff --git a/2021/14/code.tl b/2021/14/code.tl new file mode 100644 index 0000000..0af2ac7 --- /dev/null +++ b/2021/14/code.tl @@ -0,0 +1,48 @@ +(defstruct polym () + input + rewrites + (memo (hash))) + +(defun read-input (: (name "input")) + (let ((po (new polym))) + (each ((line (file-get-lines name))) + (match-case line + (`@{x 1}@{y 1} -> @z` (push ^((,(intern-fb x) + ,(intern-fb y)) + ,(intern-fb z)) + po.rewrites)) + (`@{x #/.+/}` -> (set po.input (flow x + (tuples 1) + (mapcar intern-fb)))))) + po)) + +(defmeth polym rec1 (po pair depth : (leftmost t)) + (let ((key ^(,pair ,depth ,leftmost))) + (condlet + (((re [po.memo key])) + re) + (((rw (and (plusp (pdec depth)) + [find pair po.rewrites : car]))) + (tree-bind ((x y) z) rw + (let ((lhist po.(rec1 ^(,x ,z) depth leftmost)) + (rhist po.(rec1 ^(,z ,y) depth nil))) + (set [po.memo key] + [hash-uni lhist rhist +])))) + (leftmost + (hash-zip pair '(1 1))) + (t + (hash-zip (cdr pair) '(1)))))) + +(defmeth polym rec (po pairs depth : (leftmost t)) + (let ((hist (hash))) + (each ((p pairs) + (c 0)) + (let ((rhist po.(rec1 p depth (zerop c)))) + (set hist [hash-uni hist rhist +]))) + hist)) + +(defun solve (: (name "input") (depth 10)) + (let* ((po (read-input name)) + (hist po.(rec (tuples* 2 po.input) depth))) + (- (cdr [find-max hist : cdr]) + (cdr [find-min hist : cdr])))) diff --git a/2021/14/codes.tl b/2021/14/codes.tl new file mode 100644 index 0000000..dcaccba --- /dev/null +++ b/2021/14/codes.tl @@ -0,0 +1,42 @@ +(defstruct polym () + input + rewrites + (memo (hash))) + +(defun read-input (: (name "input")) + (let ((po (new polym))) + (each ((line (file-get-lines name))) + (match-case line + (`@a -> @b` (push `@a@b` po.rewrites)) + (`@{a 1}@b` (set po.input `@a@b`)))) + po)) + +(defmeth polym rec1 (po pair depth : (leftmost t)) + (placelet ((memo [po.memo ^(,pair ,depth ,leftmost)])) + (condlet + (((re memo)) + re) + (((rw (and (plusp (pdec depth)) + [find pair po.rewrites starts-with]))) + (match `@{x 1}@{y 1}@{z 1}` rw + (let ((lhist po.(rec1 `@x@z` depth leftmost)) + (rhist po.(rec1 `@z@y` depth nil))) + (set memo [hash-uni lhist rhist +])))) + (leftmost + (set memo (hash-zip pair '(1 1)))) + (t + (set memo (hash-zip (rest pair) '(1))))))) + +(defmeth polym rec (po pairs depth : (leftmost t)) + (let ((hist (hash))) + (each ((p pairs) + (c 0)) + (let ((rhist po.(rec1 p depth (zerop c)))) + (set hist [hash-uni hist rhist +]))) + hist)) + +(defun solve (: (name "input") (depth 10)) + (let* ((po (read-input name)) + (hist po.(rec (tuples* 2 po.input) depth))) + (- (cdr [find-max hist : cdr]) + (cdr [find-min hist : cdr])))) diff --git a/2021/14/input b/2021/14/input new file mode 100644 index 0000000..4711af4 --- /dev/null +++ b/2021/14/input @@ -0,0 +1,102 @@ +VCOPVNKPFOOVPVSBKCOF + +NO -> K +PO -> B +HS -> B +FP -> V +KN -> S +HV -> S +KC -> S +CS -> B +KB -> V +OB -> V +HN -> S +OK -> N +PC -> H +OO -> P +HF -> S +CB -> C +SB -> V +FN -> B +PH -> K +KH -> P +NB -> F +KF -> P +FK -> N +FB -> P +FO -> H +CV -> V +CN -> P +BN -> N +SC -> N +PB -> K +VS -> N +BP -> P +CK -> O +PS -> N +PF -> H +HB -> S +VN -> V +OS -> V +OC -> O +BB -> F +SK -> S +NF -> F +FS -> S +SN -> N +FC -> S +BH -> N +HP -> C +VK -> F +CC -> N +SV -> H +SO -> F +HH -> C +PK -> P +NV -> B +KS -> H +NP -> H +VO -> C +BK -> V +VV -> P +HK -> B +CF -> B +BF -> O +OV -> B +OH -> C +PP -> S +SP -> S +CH -> B +OF -> F +NK -> F +FV -> F +KP -> O +OP -> O +SS -> P +CP -> H +BO -> O +KK -> F +HC -> N +KO -> V +CO -> F +NC -> P +ON -> P +KV -> C +BV -> K +HO -> F +PV -> H +VC -> O +NH -> B +PN -> H +VP -> O +NS -> N +NN -> S +BS -> H +SH -> P +VB -> V +VH -> O +FH -> K +FF -> H +SF -> N +BC -> H +VF -> P diff --git a/2021/14/testinput b/2021/14/testinput new file mode 100644 index 0000000..b5594dd --- /dev/null +++ b/2021/14/testinput @@ -0,0 +1,18 @@ +NNCB + +CH -> B +HH -> N +CB -> H +NH -> C +HB -> C +HC -> B +HN -> C +NN -> C +BH -> H +NC -> B +NB -> B +BN -> B +BB -> N +BC -> B +CC -> N +CN -> C |