From 61ea8031287fc145e16c73da57cb3d16fe36bb81 Mon Sep 17 00:00:00 2001 From: StarlitGhost <679547+StarlitGhost@users.noreply.github.com> Date: Thu, 19 Dec 2024 22:52:45 +0000 Subject: [PATCH] [2024/19] p2 solved --- .aoc_tiles/tiles/2024/19.png | Bin 5199 -> 5242 bytes 2024/19/script.py | 36 +++++++++++++++++------------------ README.md | 4 ++-- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/.aoc_tiles/tiles/2024/19.png b/.aoc_tiles/tiles/2024/19.png index 50d74c1e60620cc996d5c45721318761eda99e4f..9f6528138276614af92edc963ed7788a2a3baeff 100644 GIT binary patch literal 5242 zcmV-=6ou=FP) zd0bOxy2qcKY$SvLVNF;>*%3DiD4^oLRIL@Q-5q;J>(!ZdKCRki?7eflTyMweVi)Z= z>U2XpTE$ySsfr5<2r2>!Dm$`BkVV!cWS>7y4JRktNkUFGe}C{HZ{D0=-sgPtp7*@x zT_`E99|B7y7K<(ZESt@Kt=d;GHvJUP;oYKDO9P{muoscK7Qr>hjo5(bZ|nr|LxWu0N{DCUS2X~-qZ*`FONoH z|DN1(0NAr-5t_4iP?GuPA%9qsLXL)d+>Mc6Pibg<_-~VM@*zWsUkp(tW3HVpGNi zasgmRcJWJ}oo?$H8e3lnqza5*3bC#pjM1D5rD}StAUcGnN!fL*^l-uLD8BEWEsKI4 zUf?X2Ge#LFDx&qHqERUTaJ8-z0OShQ82UOURbZsXQYw|2|9w1}*jA_T!Eg+Qsyl7H z-<_*gC{;D>z59N}-#M$*V3wRQ$~gIQJT@<#f=y`<_8ZD9S12qta#*UsWaAyxQ?uf+ zFAHzArNxKp&#H6zlPZ+hmsI38lsO)B-A_9^RoPpYQUC;g96D9&7n^grVN8yXl_OIH zCfit41X-FK`JazZ)uddh?PwJCqj!a<;=}xYw_p;Dio}HQHZ7foJAz83?!vUs=1mR* zfMqEYyjToePG_eI03rgp;{v%jspxx0zkh= z`p$Qk0HC~~v$~}_aa;hJzJ2xdty!svf!2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uT|MFc&)#aO=uscPUjEvQNNgSH(h067 zkN%|9YK>AUi3?|!MJ{2aiV_#~7UB6OB$tv5%eCY=AS5P;`Z&^NvJRyO0_%;x&6jMc)XN^5V26OWJ(ZbEIf7Jn)yFo zDlV;UnK;&K#k`4U3v2Sq>hR;7ckbM|d-v|=pMNfqNY0!&!@+1rxQuc8n?jvl|I^j- zrp}(@xm8A~9~`(CHQICAUuVvX8ME*7t@VFiWH@^M{rBI;#KdG~X7b?E!5C=h)$%ap z4rmJZoVv5;)E!gQ`c}o-z2^bxe5u;nS^z+1W+sheHN}M&XKQ&Faw+6_=ka)EAMkD5 zxUs0H=-$11FvkbUg%p=-c^GmjpA%a<>oKYxB@W##JCtBZ?^;SZFA3oq{1@-XD`CVz0y6m=x; z`3RRb*La@yk-X<4Tno9xJntiU&qugc;r8cwAIW<@!nGpz{{i6*(On}GdLsY;002ov JPDHLkV1hTXhBN>G diff --git a/2024/19/script.py b/2024/19/script.py index 485bc25..41adc04 100644 --- a/2024/19/script.py +++ b/2024/19/script.py @@ -3,20 +3,16 @@ @cache -def validate(design: str, patterns: frozenset[str]) -> bool: - if design in patterns: - return True - else: - # try all splits - valid = False - for i in range(1, len(design)): - if aoc.args.verbose: - print(f" trying {design[:i]}-{design[i:]}") - valid = validate(design[:i], patterns) and validate(design[i:], patterns) - if valid: - return True - - return False +def validate(design: str, patterns: frozenset[str]) -> int: + # if there's nothing left of the design after recursively slicing patterns from it, + # we found a valid arrangement. return 1 to count it in our sum of all arrangements + if not design: + return 1 + + # try removing all patterns from the start of the design, + # then recursively do that for each remaining segment of the design + return sum(validate(design[len(pattern):], patterns) + for pattern in patterns if design[:len(pattern)] == pattern) def main(): @@ -25,19 +21,23 @@ def main(): designs = designs.splitlines() valid = 0 + total_arrangements = 0 for design in designs: - if validate(design, patterns): + arrangements = validate(design, patterns) + if arrangements: if aoc.args.verbose or aoc.args.progress: - print(f"1 - {design}") + print(f"1 - {design} - {arrangements}") valid += 1 + total_arrangements += arrangements else: if aoc.args.verbose or aoc.args.progress: print(f"0 - {design}") - if aoc.args.progress: - print(f"cache: {validate.cache_info().hits} hits, {validate.cache_info().misses} misses") + if aoc.args.verbose or aoc.args.progress: + print(f"validate() {validate.cache_info()}") print(f"p1: {valid}") + print(f"p2: {total_arrangements}") if __name__ == "__main__": diff --git a/README.md b/README.md index 643ab02..0706a66 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code

- Advent of Code - 196/488 ⭐ + Advent of Code - 197/488 ⭐

- 2024 - 35 ⭐ - Python + 2024 - 36 ⭐ - Python