From 959be4dcc9bef4f95931bd7fd615ea0b94432cc8 Mon Sep 17 00:00:00 2001 From: StarlitGhost <679547+StarlitGhost@users.noreply.github.com> Date: Fri, 13 Dec 2024 08:54:13 +0000 Subject: [PATCH] [2024/12] p2 solved --- .aoc_tiles/tiles/2024/12.png | Bin 4659 -> 4703 bytes 2024/12/example_ab | 6 ++++ 2024/12/{example_s => example_abcde} | 0 2024/12/example_ex | 5 ++++ 2024/12/example_xo | 5 ++++ 2024/12/script.py | 41 +++++++++++++++++++++++---- README.md | 4 +-- 7 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 2024/12/example_ab rename 2024/12/{example_s => example_abcde} (100%) create mode 100644 2024/12/example_ex create mode 100644 2024/12/example_xo diff --git a/.aoc_tiles/tiles/2024/12.png b/.aoc_tiles/tiles/2024/12.png index 92acbbf53920a5c5ebcce477fa33eede934546de..a381db49870161e5cadf5aeffe7b4122a37e1b89 100644 GIT binary patch literal 4703 zcmV-l5}@sgP) zd0Z4{n#Z5M51NMNrV#|W5mXF{h(@DEyrP3<9Fxqtx}$M*CUGYo*@@XJGaqwIG@6NS z9F2)a%_b8`G{hr<5*0;7@Bjn_IYb({&u;qOKPooW)mK#?Rn^`6{@~N~@>YHOed^a$ zZ@u*%j7e`C1$!lz%kAD*C=`xxJ($a+gz9Zk_Cl_m#&Xlrj&Moi+G8yDE$s-GG%g#R z`q7%1*~wCLib|vF=#xKe?LAZ0aN*7)wN~fWr5)jt#${W{2LN0)QyL(W28gDmMAXW9 zHt#R2wHHi>(mKek_}?|D>kk+it8M~m+5{-NADmyl=@XN$(t>GVpCmd9o>nG7T)F5Lf%O^cRq|E{IWOt1)1 z3CX>p%;T^_{MaFW!YN78-_1$=ruhEHKa^{jQujZ!Za#_#laIA1VH+ywQ~I zc2TZs>hR%n!vjQI_V82DjMPzcCPWq7YH}SHXyz)w^HeC8%kyv4>U4TTN>)+?07QrS z9p9Gw+gYj701=PFPKgTo+xq9Hj~#`Fibw7kgabamxita>$gHQmCq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Va%q+#8%lNx&K6pW^lCNKycIfxdpZLSFZLiJ8Yr;qo02MYI)BF-3-FKF zfzREyu25WB<5hSc_<5&}xO9cb3%keiR4cyNkgbwSKg_?pqo6!_a?rsIiJ z>smw0$nQ-*J?I;Z3;(%3O#k3e{>7Uw%}c-^z*#!g=kVA6GMP+vsk9`9tt%jxG*U&2 zi+Kz3ku`4PWqwuIYZ&q+JkFYTx+Wq(mk}3cDanm(D~yeeJ#^@hBPzT^vUP>!vbhf^ zh^qjkv5dwcQi~KpLVUHi+j{Vp`<;C!Pk#SWk_rH@WXY29^75vpCP!3AiE8VPb7^s{ zZbGklKIOUTlRr9AeynWdE%P|llTd15*{mr_x%ASF``Ab9L~396w7k5$u&}VKtgNkD zw^C4HG@7zJ2Du}~LL`zGX2qx!vL)$JxUq=TU2iV@{B(6)bLX_lfvXlwJ5he?(v244 zIA>*L<@M{=w`|#>R;!O6KQ6*($GN<5M-GJ+6#dt^o9(@W`9Cz+rM|tpC~;!IUw-@K zg4D=eg%#_6v&eSz{IkzKOHEBZdi1D-#dGwdFUw<)Yjr5xajbI3u}Vi&{e%8BJ5K`2 z`BKfz%>aO-M~^aCS4UiUi8jk)kV~V^yF?;!dV_D%rcG5bzSn|I^V@qN1V#0|T?Ovr|)3b8>PZR}qp6Bavr$404^%5QG pJs; int: @@ -10,19 +11,44 @@ def area(region: list[Vec2]) -> int: def neighbours(pos: Vec2) -> list[Vec2]: return [ (pos + Vec2(Dir.UP)).as_tuple(), - (pos + Vec2(Dir.RIGHT)).as_tuple(), (pos + Vec2(Dir.DOWN)).as_tuple(), (pos + Vec2(Dir.LEFT)).as_tuple(), + (pos + Vec2(Dir.RIGHT)).as_tuple(), ] -def perimeter(region: list[Vec2]) -> int: +def perimeter(region: set[tuple]) -> int: p = 0 for pos in region: - p += sum(1 for n in neighbours(pos) if n not in region) + p += sum(n not in region for n in neighbours(pos)) return p +def sides(region: set[tuple]) -> int: + fences = defaultdict(list) + for pos in region: + for n, d in Dir.map_nswe(neighbours(pos)).items(): + if n not in region: + fences[d].append(pos) + for d, fs in fences.items(): + fences[d] = sorted(fs, key=lambda f: f[::-1] if d in {Dir.N, Dir.S} else f) + + num_sides = 0 + dirmap = { + Dir.N: Dir.E, + Dir.S: Dir.E, + Dir.W: Dir.S, + Dir.E: Dir.S, + } + for d, fs in fences.items(): + num_sides += 1 + for f1, f2 in zip(fs, fs[1:]): + if (Vec2(f1) + dirmap[d]).as_tuple() != f2: + num_sides += 1 + + return num_sides + + def main(): grid = Grid(aoc.read_lines()) regions = [] @@ -37,13 +63,18 @@ def main(): if aoc.args.progress or aoc.args.verbose: for region in regions: r = grid[next(iter(region))] - print(f"{r} | area: {area(region)}, perimeter: {perimeter(region)}, " - f"cost: {area(region) * perimeter(region)}") + print(f"{r} | " + f"area: {area(region)}, " + f"perimeter: {perimeter(region)}, " + f"sides: {sides(region)} | " + f"p1 cost: {area(region) * perimeter(region)} | " + f"p2 cost: {area(region) * sides(region)}") if aoc.args.verbose: print(f"> {region}") print(f"{len(regions)} regions") print(f"p1: {sum(area(region) * perimeter(region) for region in regions)}") + print(f"p2: {sum(area(region) * sides(region) for region in regions)}") if __name__ == "__main__": diff --git a/README.md b/README.md index e09326d..fd0c468 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code

- Advent of Code - 184/474 ⭐ + Advent of Code - 185/476 ⭐

- 2024 - 23 ⭐ - Python + 2024 - 24 ⭐ - Python