diff --git a/tools_test.go b/aoc_test.go similarity index 100% rename from tools_test.go rename to aoc_test.go diff --git a/day18/example.txt b/day18/example.txt new file mode 100644 index 0000000..0ad754b --- /dev/null +++ b/day18/example.txt @@ -0,0 +1,14 @@ +R 6 (#70c710) +D 5 (#0dc571) +L 2 (#5713f0) +D 2 (#d2c081) +R 2 (#59c680) +D 2 (#411b91) +L 5 (#8ceee2) +U 2 (#caa173) +L 1 (#1b58a2) +U 2 (#caa171) +R 2 (#7807d2) +U 3 (#a77fa3) +L 2 (#015232) +U 2 (#7a21e3) \ No newline at end of file diff --git a/day18/input.txt b/day18/input.txt new file mode 100644 index 0000000..79212d7 --- /dev/null +++ b/day18/input.txt @@ -0,0 +1,766 @@ +R 4 (#0a7a60) +U 8 (#4453b3) +R 6 (#8e4f70) +U 2 (#4453b1) +R 4 (#0feb00) +U 4 (#355591) +R 8 (#2a09c0) +U 4 (#544c71) +R 4 (#472930) +U 2 (#199e33) +R 3 (#3d8df0) +U 5 (#199e31) +R 7 (#45de50) +U 4 (#57a941) +L 7 (#671c70) +U 5 (#5e5e81) +R 4 (#2e9b60) +U 8 (#490881) +L 7 (#1846f0) +U 3 (#304101) +L 4 (#1846f2) +D 2 (#58f3f1) +L 9 (#2e9b62) +D 2 (#56dcd1) +L 2 (#671c72) +D 9 (#275fd1) +L 4 (#76b7b2) +U 9 (#5b9641) +L 4 (#1d3212) +U 10 (#3d1841) +L 3 (#5ad5e2) +D 6 (#010211) +L 5 (#13ec52) +D 8 (#166471) +L 3 (#53ebc0) +D 5 (#0fdba1) +L 4 (#54e450) +D 9 (#440a21) +L 3 (#49b242) +D 5 (#2f0171) +L 6 (#447332) +D 4 (#182561) +L 8 (#1aaaa2) +D 6 (#0996a1) +L 4 (#1c1212) +D 6 (#5780d1) +L 7 (#0fc1d2) +D 7 (#36cb41) +L 3 (#505552) +D 7 (#8d8b81) +R 4 (#2bf742) +D 7 (#152791) +R 5 (#760492) +D 3 (#11a991) +R 3 (#5debc0) +D 10 (#59f211) +L 4 (#66d342) +D 8 (#1fc6b1) +R 4 (#66d340) +D 6 (#72dff1) +L 2 (#3f1b10) +D 4 (#2fca13) +L 10 (#2d0162) +D 5 (#8b5e53) +L 6 (#2d0160) +U 2 (#317053) +L 2 (#650c20) +U 7 (#2bed01) +R 10 (#409ed0) +U 2 (#166713) +L 10 (#939350) +U 6 (#166711) +L 4 (#38a180) +D 3 (#1d95f1) +L 6 (#11ea60) +D 5 (#931dc1) +R 6 (#11bf70) +D 7 (#536051) +L 3 (#995b60) +D 2 (#254e91) +L 3 (#332fd2) +U 6 (#7669f1) +L 3 (#574642) +U 6 (#1778a1) +L 3 (#1a00c2) +U 5 (#2eaa81) +L 3 (#1cf8e2) +U 6 (#0c71a1) +L 6 (#868472) +U 3 (#0a9fd1) +R 9 (#104c02) +U 4 (#082451) +L 7 (#850d02) +U 6 (#082453) +L 7 (#322552) +U 3 (#1339d1) +L 6 (#8e11d2) +U 4 (#6c5691) +R 6 (#1dda10) +U 3 (#25e5c3) +R 7 (#370670) +U 4 (#9324f1) +L 3 (#451030) +U 7 (#1f1021) +L 7 (#0ba3c0) +U 5 (#8a10a3) +L 8 (#67db30) +U 5 (#282473) +L 4 (#0cd350) +U 4 (#25e5c1) +R 3 (#6b4ee0) +U 4 (#333cf1) +R 5 (#02b610) +D 4 (#4e7cc1) +R 3 (#92abb0) +U 5 (#2f6ff1) +R 5 (#483360) +U 5 (#7b1cd1) +L 8 (#3a5d50) +U 3 (#1d2461) +R 8 (#3b2fd0) +U 5 (#630763) +R 6 (#5033c0) +U 3 (#252523) +R 7 (#08eeb0) +U 8 (#516633) +L 7 (#156240) +U 4 (#2db063) +L 3 (#791070) +D 5 (#2920a3) +L 3 (#0dafe0) +U 5 (#4185b3) +L 5 (#7ac0e0) +U 2 (#5a6cd3) +L 2 (#3ca790) +U 7 (#755403) +L 4 (#3b0740) +U 5 (#2004a3) +L 3 (#2db4f0) +U 6 (#602a23) +R 5 (#32c1c0) +D 2 (#000f53) +R 5 (#388360) +D 7 (#24d9e3) +R 4 (#4703a0) +D 3 (#2f1133) +R 3 (#4703a2) +U 11 (#52e853) +R 5 (#278132) +D 11 (#0665e3) +R 3 (#239112) +U 6 (#8f0ad1) +R 5 (#447de2) +U 6 (#8f0ad3) +R 6 (#636122) +U 2 (#187283) +R 3 (#5aae62) +U 10 (#1d8833) +L 5 (#476db0) +U 5 (#136393) +L 7 (#29e6b0) +U 5 (#136391) +R 7 (#3ba060) +U 3 (#635e23) +L 5 (#2042f0) +U 3 (#181643) +L 6 (#5af952) +U 8 (#28d0b1) +L 2 (#4a98f2) +U 9 (#28d0b3) +L 6 (#27a572) +D 3 (#3d7213) +L 4 (#516842) +D 11 (#21ef63) +L 5 (#38e342) +D 2 (#1118b3) +L 3 (#560502) +D 9 (#1118b1) +R 2 (#1c39a2) +D 2 (#303ab3) +R 6 (#716ba0) +D 6 (#780a93) +L 8 (#77ec30) +U 2 (#780a91) +L 3 (#271100) +U 10 (#5882f3) +L 4 (#2050c2) +D 3 (#434843) +L 3 (#7d9572) +D 2 (#3129b3) +L 5 (#21fb12) +D 5 (#8c2c33) +L 4 (#57bf92) +D 6 (#2730b3) +L 4 (#6a3902) +D 8 (#4656a3) +L 5 (#60ac80) +D 4 (#43b863) +R 9 (#3d69a0) +D 8 (#3ca913) +L 2 (#411d80) +D 3 (#725383) +L 8 (#4a98d0) +D 9 (#274553) +L 5 (#580d60) +U 5 (#568223) +L 6 (#473af0) +U 5 (#827593) +L 7 (#251dc2) +U 5 (#0fcea3) +L 8 (#5ae3d2) +U 3 (#4efa11) +R 9 (#389852) +U 2 (#7f7f91) +R 6 (#618342) +U 3 (#441143) +L 7 (#886df2) +U 3 (#26a093) +L 8 (#886df0) +U 4 (#63c7d3) +L 3 (#04f842) +D 10 (#0fcea1) +L 6 (#388e62) +U 10 (#3ae7f3) +L 3 (#2a6880) +U 6 (#3bfd53) +L 6 (#73a0c0) +U 5 (#3bfd51) +L 4 (#0d18a0) +U 2 (#19da93) +L 6 (#120600) +U 8 (#233b53) +L 6 (#2e1050) +U 3 (#4b2aa3) +L 4 (#5f9930) +U 10 (#6e65f1) +R 6 (#1eefa0) +U 9 (#50b9d3) +R 2 (#548ed0) +U 9 (#50b9d1) +R 7 (#8bd9f0) +U 4 (#5ae473) +R 6 (#0b0490) +U 10 (#186891) +R 4 (#764830) +U 7 (#33b0e1) +R 5 (#363c90) +U 8 (#7bd631) +R 7 (#031e30) +U 7 (#095881) +R 3 (#640e50) +D 5 (#02e7f3) +R 7 (#0922f0) +D 7 (#6b7af3) +R 9 (#0922f2) +D 3 (#62e543) +R 3 (#21e320) +U 9 (#5ead81) +R 7 (#01fd70) +D 9 (#3b0cf1) +R 2 (#2758a0) +D 5 (#7279b1) +R 8 (#5c7dc0) +D 7 (#3c8db1) +R 4 (#3c3740) +U 2 (#26b151) +R 6 (#11eb12) +U 4 (#587921) +R 6 (#11eb10) +U 4 (#320051) +R 4 (#038630) +U 3 (#6a2e61) +R 2 (#4ff680) +U 5 (#3c57f1) +R 9 (#0b2440) +U 8 (#497533) +L 6 (#704680) +U 11 (#549113) +R 6 (#23b470) +U 4 (#9e0641) +R 7 (#1aeec0) +D 7 (#4a6331) +R 2 (#547d80) +D 5 (#457a71) +R 9 (#7daa90) +D 4 (#16d7b1) +L 6 (#266810) +D 3 (#363561) +L 5 (#5312b2) +D 4 (#42c301) +R 5 (#9a0952) +D 7 (#42c303) +R 7 (#7d1df2) +D 4 (#51e321) +R 5 (#609422) +D 4 (#3b8f01) +R 6 (#644792) +U 4 (#2d8b11) +R 7 (#22bbc2) +D 7 (#6b39b1) +R 5 (#680e72) +U 2 (#7f9f31) +R 3 (#0e9b52) +U 3 (#3086c1) +R 3 (#5a60c0) +U 6 (#53b6e3) +R 7 (#8117d0) +U 7 (#53b6e1) +R 3 (#2cb930) +U 3 (#1bffd1) +R 6 (#4539f0) +U 8 (#680411) +R 4 (#6032c0) +U 6 (#255061) +R 3 (#5562a0) +U 6 (#088071) +R 3 (#3bf2f0) +U 5 (#52bdb1) +L 6 (#070500) +U 5 (#69e001) +R 6 (#070502) +U 3 (#071051) +R 2 (#036000) +U 4 (#0ea8a1) +R 5 (#05e4d0) +U 3 (#674de1) +R 3 (#804250) +D 6 (#3ffca1) +R 4 (#23dcb2) +D 6 (#9f9841) +L 5 (#45f210) +D 9 (#15efc1) +R 5 (#45f212) +D 4 (#969551) +L 9 (#23dcb0) +D 2 (#41bea1) +L 4 (#2f2a32) +D 4 (#71ffd1) +R 3 (#5c0bb2) +D 7 (#6f3501) +R 4 (#2b0a72) +D 4 (#7b3571) +R 3 (#1c67d2) +U 11 (#42f011) +R 3 (#51ae62) +D 3 (#75fcf1) +R 3 (#1f4742) +D 4 (#2813f1) +R 4 (#3d1812) +D 2 (#2b1d01) +R 11 (#1eb302) +U 3 (#27b0a1) +R 4 (#227b20) +U 5 (#34b7e1) +R 10 (#201220) +U 7 (#0502f3) +R 4 (#5ab7f0) +U 4 (#0502f1) +R 4 (#5d09b0) +U 7 (#504a81) +R 7 (#1e8250) +U 3 (#715aa1) +R 6 (#26d290) +U 6 (#9a12f1) +L 3 (#18be90) +U 8 (#037d11) +L 5 (#647600) +U 3 (#40b983) +L 5 (#04e450) +U 3 (#9a9a43) +L 8 (#04e452) +U 6 (#8af153) +R 8 (#6cbc90) +U 4 (#04d121) +L 4 (#285292) +U 3 (#688d51) +L 6 (#176332) +U 4 (#400a91) +L 5 (#176330) +U 5 (#43def1) +R 2 (#285290) +U 2 (#3e0711) +R 3 (#7eb9c0) +U 7 (#36f611) +R 7 (#666af0) +D 7 (#381b31) +R 3 (#298922) +U 3 (#475671) +R 6 (#977c12) +D 4 (#602d01) +R 6 (#25b0c2) +D 6 (#7ede81) +R 3 (#1d58c2) +D 8 (#873523) +L 9 (#265ae2) +D 3 (#57d663) +R 7 (#1565c2) +D 8 (#06ed11) +R 4 (#a348c2) +D 2 (#06ed13) +R 8 (#12d052) +D 4 (#2d3741) +L 7 (#0ccfd2) +D 3 (#1fcd61) +R 4 (#39a4c2) +D 3 (#44f7f1) +R 6 (#5c3202) +D 8 (#2625c1) +L 8 (#2b08c2) +D 2 (#20d7e1) +L 2 (#67b892) +D 10 (#546471) +R 7 (#484ef2) +D 7 (#49bc91) +R 4 (#91aa72) +U 11 (#499591) +R 3 (#0048b2) +U 7 (#34a7a1) +R 7 (#514600) +U 8 (#1cdc43) +L 7 (#471470) +U 3 (#8c8393) +R 3 (#18a780) +U 5 (#8c8391) +R 3 (#4e6f80) +U 3 (#1cdc41) +R 8 (#428930) +U 5 (#5dcb51) +R 3 (#4b21e2) +U 4 (#0464b1) +R 9 (#850552) +D 8 (#464881) +R 7 (#543852) +D 3 (#2d3a11) +R 3 (#081b22) +D 5 (#0cc731) +R 7 (#6990e0) +D 3 (#79d7b1) +R 3 (#509520) +D 5 (#438571) +R 3 (#27d510) +D 3 (#30d2d3) +L 8 (#1b7a00) +D 5 (#8a0da3) +L 8 (#1b7a02) +U 5 (#027cb3) +L 7 (#350130) +D 8 (#2ff771) +R 7 (#16dc02) +D 2 (#68f1c1) +R 5 (#02f782) +D 6 (#4e2a41) +R 11 (#448e32) +D 5 (#2cfac1) +L 11 (#417502) +D 5 (#2cfac3) +L 4 (#22f022) +U 9 (#1e9f01) +L 5 (#543572) +U 3 (#076e31) +L 6 (#081b20) +U 5 (#1ae851) +L 3 (#823132) +U 9 (#2d7993) +L 4 (#5738f2) +D 8 (#79e273) +L 4 (#5c9ad2) +D 8 (#4ed1b3) +L 3 (#311722) +D 4 (#0d5493) +L 6 (#5d63e0) +D 6 (#178cf3) +L 5 (#75b1b0) +D 5 (#178cf1) +L 6 (#11d550) +D 6 (#44ed03) +L 3 (#76a842) +U 6 (#3b6993) +L 5 (#184892) +D 4 (#5c8163) +L 3 (#14d872) +U 2 (#261a01) +L 5 (#2d01e2) +U 9 (#326ca3) +L 4 (#59ad32) +U 8 (#326ca1) +R 4 (#389ab2) +U 9 (#261a03) +L 4 (#50cb42) +D 4 (#2f8263) +L 5 (#9f3752) +D 6 (#4bb383) +L 4 (#2693f2) +D 4 (#19dca3) +R 4 (#475bc2) +D 6 (#960e13) +L 7 (#0c90a2) +D 8 (#3463d3) +L 6 (#5a82e2) +D 3 (#5a5c43) +R 7 (#120bf2) +D 3 (#5069e3) +R 3 (#4b47a2) +D 6 (#635b63) +R 4 (#66ca10) +U 4 (#1418f1) +R 7 (#1d5470) +D 4 (#1418f3) +R 8 (#66d4f0) +D 5 (#3c5533) +R 4 (#2f6e02) +D 3 (#2cf5e3) +R 4 (#633ec2) +D 7 (#2cf5e1) +R 7 (#5846b2) +D 3 (#4f9d63) +R 4 (#533380) +D 11 (#13c7e3) +R 2 (#222b62) +D 3 (#928ab3) +R 5 (#222b60) +U 5 (#0a4793) +R 5 (#15d3b0) +U 8 (#363fe1) +R 4 (#2feb30) +U 5 (#05d8b1) +R 3 (#39e030) +U 3 (#05d8b3) +R 2 (#3cd780) +U 8 (#02c781) +R 5 (#186452) +D 3 (#37bba1) +R 5 (#9e1232) +D 5 (#58e6f1) +R 5 (#182c30) +D 5 (#072db1) +R 5 (#85e600) +D 4 (#607651) +R 5 (#186450) +D 3 (#0aca91) +L 5 (#0ceb10) +D 5 (#860001) +L 6 (#54c8b0) +U 5 (#393971) +L 4 (#37c390) +D 3 (#1f1433) +L 5 (#3d8790) +D 6 (#353623) +R 4 (#78c830) +D 4 (#6fafa3) +R 6 (#2b0612) +D 5 (#31fff3) +R 7 (#8b49b2) +D 9 (#6f0ee3) +R 5 (#4212e0) +D 6 (#5d11f3) +R 6 (#648340) +D 8 (#393743) +R 6 (#34ddd0) +U 8 (#346fb3) +R 5 (#34fc22) +D 4 (#71c453) +R 6 (#5048f2) +D 4 (#0bb993) +R 4 (#42b9f0) +D 9 (#5cdf83) +R 5 (#42b9f2) +D 3 (#6f3f93) +R 3 (#3dbae2) +D 8 (#495363) +R 9 (#328ba2) +D 3 (#77afa1) +R 5 (#575710) +D 2 (#10d861) +R 4 (#257822) +D 4 (#6efe61) +R 9 (#257820) +D 5 (#521d01) +R 5 (#575712) +D 6 (#494cf1) +L 5 (#683ab2) +D 4 (#5e79c3) +L 3 (#4242a2) +D 4 (#5e79c1) +L 7 (#2c3442) +D 8 (#2fbd23) +L 5 (#636fb2) +D 5 (#63bd53) +L 2 (#299960) +D 6 (#31ea33) +R 3 (#51dfb0) +D 7 (#4e4ec3) +R 4 (#7b7912) +D 5 (#4b4bf3) +R 4 (#54e882) +D 5 (#7d2193) +R 3 (#3b77d2) +D 3 (#826973) +R 8 (#646ec2) +D 4 (#076c63) +L 10 (#674052) +D 6 (#3676b1) +L 9 (#3bd890) +D 4 (#7feb71) +L 3 (#3bd892) +D 3 (#27fc21) +R 8 (#66bba2) +D 8 (#645c73) +R 5 (#43e132) +D 5 (#51d9e3) +R 8 (#43e130) +D 6 (#2827f3) +R 4 (#412c92) +D 5 (#7e7f63) +L 4 (#218122) +D 4 (#546e83) +L 9 (#82f672) +D 8 (#6ed383) +L 3 (#701412) +D 7 (#47de23) +L 5 (#46f912) +D 7 (#0d9ec3) +L 4 (#515142) +D 3 (#4699e3) +L 9 (#2ed562) +U 6 (#00a273) +L 9 (#8d7d12) +U 2 (#6c3a83) +L 7 (#318fa2) +U 7 (#0bd473) +L 2 (#6d5050) +U 10 (#415673) +R 3 (#9e8420) +U 6 (#1ff213) +R 5 (#0c8bf0) +U 2 (#479a11) +R 6 (#977360) +U 5 (#479a13) +L 8 (#250680) +U 2 (#1ff211) +L 3 (#216630) +U 3 (#22d143) +L 3 (#3b4342) +U 7 (#9d4ff3) +L 5 (#43aca2) +U 6 (#29efd3) +L 3 (#5c6a12) +D 11 (#134a73) +L 6 (#2a0982) +U 11 (#444723) +L 4 (#47e2e2) +U 4 (#6855c3) +L 4 (#06a382) +U 4 (#2222e3) +R 3 (#4edc62) +U 6 (#9a2683) +R 6 (#131872) +U 10 (#4f5a23) +R 4 (#858f70) +U 5 (#4d4823) +L 9 (#3f73f0) +U 3 (#46f5a3) +L 4 (#935370) +U 4 (#46f5a1) +L 5 (#3ca140) +U 6 (#07b1e3) +L 5 (#61ecd0) +U 5 (#654743) +L 5 (#3cec42) +U 6 (#30bb83) +R 3 (#920782) +U 6 (#1f9323) +R 8 (#3d8aa2) +U 8 (#7492c3) +R 3 (#301cd2) +U 3 (#32e2f1) +R 4 (#6936f2) +U 6 (#32e2f3) +R 6 (#5112c2) +U 7 (#535b53) +L 10 (#61f4d0) +U 3 (#915863) +L 2 (#51d540) +U 5 (#32dee1) +L 10 (#4ec320) +U 5 (#406bc1) +L 2 (#1b3890) +U 3 (#681181) +L 6 (#35ed60) +D 9 (#153fd3) +L 5 (#323e30) +U 5 (#381ad3) +L 3 (#69a550) +U 3 (#310f63) +L 4 (#2cbb80) +U 5 (#3448a3) +R 6 (#206282) +U 5 (#202ab1) +L 6 (#83af82) +U 4 (#202ab3) +L 4 (#248d02) +D 9 (#28a983) +L 4 (#32cf10) +D 7 (#249d73) +R 4 (#2f5c70) +D 6 (#021c93) +L 7 (#957782) +D 6 (#550e53) +R 5 (#957780) +D 6 (#50bb13) +R 9 (#49b7f2) +U 6 (#096e93) +R 4 (#769452) +D 3 (#6dc543) +R 5 (#2ad352) +D 7 (#167963) +L 10 (#673682) +D 5 (#152883) +L 10 (#13f332) +D 3 (#996721) +L 4 (#4be2a2) +D 4 (#9c2443) +L 4 (#2b83b0) +D 3 (#5d63b3) +L 9 (#9cfd40) +D 7 (#49e793) +L 7 (#0b5422) +D 8 (#5c51b3) +L 2 (#6db152) +D 4 (#215b53) +L 3 (#38dc12) +D 8 (#7dad01) +L 6 (#559e22) +D 3 (#929d23) +L 2 (#0f7560) +D 6 (#448d13) +L 7 (#73a972) +D 2 (#2221c3) +L 3 (#73a970) +D 5 (#53b553) +R 10 (#513590) +D 3 (#293c83) +L 10 (#58e2e0) +D 4 (#54dd21) +L 5 (#1bcdf2) +D 5 (#4b0b71) +L 5 (#1bcdf0) +U 6 (#43b811) +L 5 (#4df1d0) +U 6 (#36d6a3) +R 5 (#3bfb20) +U 5 (#75de61) +L 5 (#1e0ec2) +U 4 (#30b571) +L 7 (#2dfd20) +D 5 (#49ad61) +L 2 (#2dfd22) +D 11 (#715d41) +L 3 (#1e0ec0) +U 4 (#0f2091) +L 4 (#9d29c0) +U 9 (#475a73) +L 2 (#108610) +U 3 (#1e5423) +L 8 (#394192) +U 3 (#0274b3) diff --git a/day18/main.go b/day18/main.go new file mode 100644 index 0000000..91d1a05 --- /dev/null +++ b/day18/main.go @@ -0,0 +1,90 @@ +package main + +import ( + "bufio" + _ "embed" + "fmt" + "strconv" + "strings" + + aoc "go.sour.is/advent-of-code" + "golang.org/x/exp/maps" +) + +// var log = aoc.Log + +func main() { aoc.MustResult(aoc.Runner(run)) } + +type result struct { + valuePT1 int + valuePT2 int +} + +func (r result) String() string { return fmt.Sprintf("%#v", r) } + +func run(scan *bufio.Scanner) (*result, error) { + + var vecsPT1 []aoc.Vector + var vecsPT2 []aoc.Vector + + for scan.Scan() { + text := scan.Text() + + if len(text) == 0 { + continue + } + + v, color := fromLine(text) + + vecsPT1 = append(vecsPT1, v) + vecsPT2 = append(vecsPT2, fromColor(color)) + } + return &result{ + valuePT1: findArea(vecsPT1), + valuePT2: findArea(vecsPT2), + }, nil +} + +var OFFSET = map[string]aoc.Point{ + "R": {0, 1}, + "D": {1, 0}, + "L": {0, -1}, + "U": {-1, 0}, +} +var OFFSET_INDEXES = maps.Values(OFFSET) + +func fromLine(text string) (aoc.Vector, string) { + v := aoc.Vector{} + s, text, _ := strings.Cut(text, " ") + v.Offset = OFFSET[s] + + s, text, _ = strings.Cut(text, " ") + v.Scale = aoc.Atoi(s) + + _, text, _ = strings.Cut(text, "(#") + s, _, _ = strings.Cut(text, ")") + return v, s +} + +func fromColor(c string) aoc.Vector { + scale, _ := strconv.ParseInt(c[:5], 16, 64) + offset := OFFSET_INDEXES[c[5]-'0'] + + return aoc.Vector{ + Offset: offset, + Scale: int(scale), + } +} + +func findArea(vecs []aoc.Vector) int { + shoelace := []aoc.Point{{0,0}} + borderLength := 0 + + for _, vec := range vecs { + shoelace = append(shoelace, shoelace[len(shoelace)-1].Add(vec.Point())) + borderLength += vec.Scale + } + + return aoc.NumPoints(shoelace, borderLength) +} + diff --git a/day18/main_test.go b/day18/main_test.go new file mode 100644 index 0000000..a7c83af --- /dev/null +++ b/day18/main_test.go @@ -0,0 +1,42 @@ +package main + +import ( + "bufio" + "bytes" + "testing" + + _ "embed" + + "github.com/matryer/is" +) + +//go:embed example.txt +var example []byte + +//go:embed input.txt +var input []byte + +func TestExample(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(example)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result) + is.Equal(result.valuePT1, 62) + is.Equal(result.valuePT2, 952408144115) +} + +func TestSolution(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(input)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result) + is.True(result.valuePT1 < 68834) // first attempt too high. + is.Equal(result.valuePT1, 46334) + is.Equal(result.valuePT2, 102000662718092) +} diff --git a/grids.go b/grids.go new file mode 100644 index 0000000..43810be --- /dev/null +++ b/grids.go @@ -0,0 +1,51 @@ +package aoc + +type Vector struct { + Offset Point + Scale int +} + +func (v Vector) Point() Point { + return v.Offset.Scale(v.Scale) +} + +type Point [2]int + +func (p Point) Add(a Point) Point { + return Point{p[0] + a[0], p[1] + a[1]} +} +func (p Point) Scale(m int) Point { + return Point{p[0] * m, p[1] * m} +} + +func Transpose[T any](matrix [][]T) [][]T { + rows, cols := len(matrix), len(matrix[0]) + + m := make([][]T, cols) + for i := range m { + m[i] = make([]T, rows) + } + + for i := 0; i < cols; i++ { + for j := 0; j < rows; j++ { + m[i][j] = matrix[j][i] + } + } + return m +} + +// NumPoints the number of the points inside an outline plus the number of points in the outline +func NumPoints(outline []Point, borderLength int) int { + // shoelace - find the float area in a shape + sum := 0 + for _, p := range Pairwise(outline) { + row1, col1 := p[0][0], p[0][1] + row2, col2 := p[1][0], p[1][1] + + sum += row1*col2 - row2*col1 + } + area := sum / 2 + + // pick's theorem - find the number of points in a shape given its area + return (ABS(area) - borderLength/2 + 1) + borderLength +} diff --git a/itertools.go b/itertools.go new file mode 100644 index 0000000..c6925d5 --- /dev/null +++ b/itertools.go @@ -0,0 +1,57 @@ +package aoc + +import ( + "strconv" +) + +func Atoi(s string) int { + i, _ := strconv.Atoi(s) + return i +} + +func Repeat[T any](s T, i int) []T { + lis := make([]T, i) + for i := range lis { + lis[i] = s + } + return lis +} + +func Reduce[T, U any](fn func(int, T, U) U, u U, list ...T) U { + for i, t := range list { + u = fn(i, t, u) + } + return u +} + +func Reverse[T any](arr []T) []T { + for i := 0; i < len(arr)/2; i++ { + arr[i], arr[len(arr)-i-1] = arr[len(arr)-i-1], arr[i] + } + return arr +} + + +func SliceMap[T, U any](fn func(T) U, in ...T) []U { + lis := make([]U, len(in)) + for i := range lis { + lis[i] = fn(in[i]) + } + return lis +} +func SliceIMap[T, U any](fn func(int, T) U, in ...T) []U { + lis := make([]U, len(in)) + for i := range lis { + lis[i] = fn(i, in[i]) + } + return lis +} + +// Pairwise iterates over a list pairing i, i+1 +func Pairwise[T any](arr []T) [][2]T { + var pairs [][2]T + for i := range arr[:len(arr)-1] { + pairs = append(pairs, [2]T{arr[i], arr[i+1]}) + } + return pairs +} \ No newline at end of file diff --git a/lists.go b/lists.go new file mode 100644 index 0000000..fc611ef --- /dev/null +++ b/lists.go @@ -0,0 +1,101 @@ +package aoc + +import "fmt" + + +type Node[T any] struct { + value T + pos int + left *Node[T] +} + +func (n *Node[T]) add(a *Node[T]) *Node[T] { + if a == nil { + return n + } + + if n == nil { + return a + } + + n.left = a + return a +} + +func (n *Node[T]) Value() (value T, ok bool) { + if n == nil { + return + } + return n.value, true +} + +func (n *Node[T]) Position() int { + if n == nil { + return -1 + } + return n.pos +} +func (n *Node[T]) SetPosition(i int) { + if n == nil { + return + } + n.pos = i +} +func (n *Node[T]) Next() *Node[T] { + if n == nil { + return nil + } + return n.left +} + +func (n *Node[T]) String() string { + if n == nil { + return "EOL" + } + return fmt.Sprintf("node %v", n.value) +} + +type List[T any] struct { + head *Node[T] + n *Node[T] + p map[int]*Node[T] +} + +func NewList[T any](a *Node[T]) *List[T] { + lis := &List[T]{ + head: a, + n: a, + p: make(map[int]*Node[T]), + } + lis.add(a) + + return lis +} +func (l *List[T]) Add(value T, pos int) { + a := &Node[T]{value: value, pos: pos} + l.add(a) +} +func (l *List[T]) add(a *Node[T]) { + if l.head == nil { + l.head = a + } + if a == nil { + return + } + + l.n = l.n.add(a) + l.p[a.pos] = a +} +func (l *List[T]) Get(pos int) *Node[T] { + return l.p[pos] +} +func (l *List[T]) GetN(pos ...int) []*Node[T] { + lis := make([]*Node[T], len(pos)) + for i, p := range pos { + lis[i] = l.p[p] + } + return lis +} +func (l *List[T]) Head() *Node[T] { + return l.head +} diff --git a/math.go b/math.go new file mode 100644 index 0000000..b6d1124 --- /dev/null +++ b/math.go @@ -0,0 +1,96 @@ +package aoc + +import "cmp" + +type uinteger interface { + uint | uint8 | uint16 | uint32 | uint64 +} +type sinteger interface { + int | int8 | int16 | int32 | int64 +} +type integer interface { + sinteger | uinteger +} + +// type float interface { +// complex64 | complex128 | float32 | float64 +// } +// type number interface{ integer | float } + +// greatest common divisor (GCD) via Euclidean algorithm +func GCD[T integer](a, b T) T { + for b != 0 { + t := b + b = a % b + a = t + } + return a +} + +// find Least Common Multiple (LCM) via GCD +func LCM[T integer](integers ...T) T { + if len(integers) == 0 { + return 0 + } + if len(integers) == 1 { + return integers[0] + } + + a, b := integers[0], integers[1] + result := a * b / GCD(a, b) + + for _, c := range integers[2:] { + result = LCM(result, c) + } + + return result +} + +func Sum[T integer](arr ...T) T { + var acc T + for _, a := range arr { + acc += a + } + return acc +} +func SumFunc[T any, U integer](fn func(T) U, input ...T) U { + return Sum(SliceMap(fn, input...)...) +} +func SumIFunc[T any, U integer](fn func(int, T) U, input ...T) U { + return Sum(SliceIMap(fn, input...)...) +} + +func Power2(n int) int { + if n == 0 { + return 1 + } + p := 2 + for ; n > 1; n-- { + p *= 2 + } + return p +} + +func ABS(i int) int { + if i < 0 { + return -i + } + return i +} + +func Max[T cmp.Ordered](a T, v ...T) T { + for _, b := range v { + if b > a { + a = b + } + } + return a +} +func Min[T cmp.Ordered](a T, v ...T) T { + for _, b := range v { + if b < a { + a = b + } + } + return a +} diff --git a/runner.go b/runner.go new file mode 100644 index 0000000..c69849b --- /dev/null +++ b/runner.go @@ -0,0 +1,47 @@ +package aoc + +import ( + "bufio" + "fmt" + "os" + "path/filepath" + "strings" +) + +func Runner[R any, F func(*bufio.Scanner) (R, error)](run F) (R, error) { + if len(os.Args) != 2 { + Log("Usage:", filepath.Base(os.Args[0]), "FILE") + os.Exit(22) + } + + input, err := os.Open(os.Args[1]) + if err != nil { + Log(err) + os.Exit(1) + } + + scan := bufio.NewScanner(input) + return run(scan) +} + +func MustResult[T any](result T, err error) { + if err != nil { + fmt.Println("ERR", err) + os.Exit(1) + } + + Log("result", result) +} + +func Log(v ...any) { fmt.Fprintln(os.Stderr, v...) } +func Logf(format string, v ...any) { + if !strings.HasSuffix(format, "\n") { + format += "\n" + } + fmt.Fprintf(os.Stderr, format, v...) +} + + +func ReadStringToInts(fields []string) []int { + return SliceMap(Atoi, fields...) +} diff --git a/search.go b/search.go new file mode 100644 index 0000000..8a8eea8 --- /dev/null +++ b/search.go @@ -0,0 +1,103 @@ +package aoc + +import ( + "sort" +) + +type PQElem[T any, I integer] struct { + Value T + Priority I +} +type PQList[T any, I integer] []PQElem[T, I] + +func (pq PQList[T, I]) Len() int { + return len(pq) +} +func (pq PQList[T, I]) Less(i int, j int) bool { + return pq[i].Priority < pq[j].Priority +} +func (pq PQList[T, I]) Swap(i int, j int) { + pq[i], pq[j] = pq[j], pq[i] +} + +var _ sort.Interface = (*PQList[rune, int])(nil) + +type PriorityQueue[T any, I integer] struct { + elem PQList[T, I] +} + +func (pq *PriorityQueue[T, I]) Enqueue(elem T, priority I) { + pq.elem = append(pq.elem, PQElem[T, I]{elem, priority}) + sort.Sort(pq.elem) +} +func (pq *PriorityQueue[T, I]) IsEmpty() bool { + return len(pq.elem) == 0 +} +func (pq *PriorityQueue[T, I]) Dequeue() (T, bool) { + var elem T + if pq.IsEmpty() { + return elem, false + } + + elem, pq.elem = pq.elem[0].Value, pq.elem[1:] + return elem, true +} + +type Vertex[V comparable, I integer] struct { + to V + score I +} +type graph[V comparable, I uinteger] struct { + adj map[V][]Vertex[V, I] +} + +func Graph[V comparable, I uinteger](size int) *graph[V, I] { + return &graph[V, I]{ + adj: make(map[V][]Vertex[V, I], size), + } +} +func (g *graph[V, I]) AddEdge(u, v V, w I) { + g.adj[u] = append(g.adj[u], Vertex[V, I]{to: v, score: w}) + g.adj[v] = append(g.adj[v], Vertex[V, I]{to: u, score: w}) +} +func (g *graph[V, I]) Dijkstra(m interface{ Get() }, src V) map[V]I { + pq := PriorityQueue[V, I]{} + dist := make(map[V]I, len(g.adj)) + visited := make(map[V]bool, len(g.adj)) + var INF I + INF = ^INF + + pq.Enqueue(src, 0) + dist[src] = 0 + + for !pq.IsEmpty() { + u, _ := pq.Dequeue() + + if _, ok := visited[u]; ok { + continue + } + visited[u] = true + + for _, v := range g.adj[u] { + _, ok := visited[v.to] + var du, dv I + if d, inf := dist[u]; !inf { + du = INF + } else { + du = d + } + if d, inf := dist[v.to]; !inf { + dv = INF + } else { + dv = d + } + + if !ok && du+v.score < dv { + dist[v.to] = du + v.score + pq.Enqueue(v.to, du+v.score) + } + } + } + + return dist +} diff --git a/tools.go b/tools.go deleted file mode 100644 index 367d79a..0000000 --- a/tools.go +++ /dev/null @@ -1,395 +0,0 @@ -package aoc - -import ( - "bufio" - "cmp" - "fmt" - "os" - "path/filepath" - "sort" - "strconv" - "strings" -) - -func Runner[R any, F func(*bufio.Scanner) (R, error)](run F) (R, error) { - if len(os.Args) != 2 { - Log("Usage:", filepath.Base(os.Args[0]), "FILE") - os.Exit(22) - } - - input, err := os.Open(os.Args[1]) - if err != nil { - Log(err) - os.Exit(1) - } - - scan := bufio.NewScanner(input) - return run(scan) -} - -func MustResult[T any](result T, err error) { - if err != nil { - fmt.Println("ERR", err) - os.Exit(1) - } - - Log("result", result) -} - -func Log(v ...any) { fmt.Fprintln(os.Stderr, v...) } -func Logf(format string, v ...any) { - if !strings.HasSuffix(format, "\n") { - format += "\n" - } - fmt.Fprintf(os.Stderr, format, v...) -} - -func Reverse[T any](arr []T) []T { - for i := 0; i < len(arr)/2; i++ { - arr[i], arr[len(arr)-i-1] = arr[len(arr)-i-1], arr[i] - } - return arr -} - -type uinteger interface { - uint | uint8 | uint16 | uint32 | uint64 -} -type sinteger interface { - int | int8 | int16 | int32 | int64 -} -type integer interface { - sinteger | uinteger -} - -// type float interface { -// complex64 | complex128 | float32 | float64 -// } -// type number interface{ integer | float } - -// greatest common divisor (GCD) via Euclidean algorithm -func GCD[T integer](a, b T) T { - for b != 0 { - t := b - b = a % b - a = t - } - return a -} - -// find Least Common Multiple (LCM) via GCD -func LCM[T integer](integers ...T) T { - if len(integers) == 0 { - return 0 - } - if len(integers) == 1 { - return integers[0] - } - - a, b := integers[0], integers[1] - result := a * b / GCD(a, b) - - for _, c := range integers[2:] { - result = LCM(result, c) - } - - return result -} - -func ReadStringToInts(fields []string) []int { - return SliceMap(Atoi, fields...) -} - -type Node[T any] struct { - value T - pos int - left *Node[T] -} - -func (n *Node[T]) add(a *Node[T]) *Node[T] { - if a == nil { - return n - } - - if n == nil { - return a - } - - n.left = a - return a -} - -func (n *Node[T]) Value() (value T, ok bool) { - if n == nil { - return - } - return n.value, true -} - -func (n *Node[T]) Position() int { - if n == nil { - return -1 - } - return n.pos -} -func (n *Node[T]) SetPosition(i int) { - if n == nil { - return - } - n.pos = i -} -func (n *Node[T]) Next() *Node[T] { - if n == nil { - return nil - } - return n.left -} - -func (n *Node[T]) String() string { - if n == nil { - return "EOL" - } - return fmt.Sprintf("node %v", n.value) -} - -type List[T any] struct { - head *Node[T] - n *Node[T] - p map[int]*Node[T] -} - -func NewList[T any](a *Node[T]) *List[T] { - lis := &List[T]{ - head: a, - n: a, - p: make(map[int]*Node[T]), - } - lis.add(a) - - return lis -} -func (l *List[T]) Add(value T, pos int) { - a := &Node[T]{value: value, pos: pos} - l.add(a) -} -func (l *List[T]) add(a *Node[T]) { - if l.head == nil { - l.head = a - } - if a == nil { - return - } - - l.n = l.n.add(a) - l.p[a.pos] = a -} -func (l *List[T]) Get(pos int) *Node[T] { - return l.p[pos] -} -func (l *List[T]) GetN(pos ...int) []*Node[T] { - lis := make([]*Node[T], len(pos)) - for i, p := range pos { - lis[i] = l.p[p] - } - return lis -} -func (l *List[T]) Head() *Node[T] { - return l.head -} - -func SliceMap[T, U any](fn func(T) U, in ...T) []U { - lis := make([]U, len(in)) - for i := range lis { - lis[i] = fn(in[i]) - } - return lis -} -func SliceIMap[T, U any](fn func(int, T) U, in ...T) []U { - lis := make([]U, len(in)) - for i := range lis { - lis[i] = fn(i, in[i]) - } - return lis -} - -func Atoi(s string) int { - i, _ := strconv.Atoi(s) - return i -} - -func Repeat[T any](s T, i int) []T { - lis := make([]T, i) - for i := range lis { - lis[i] = s - } - return lis -} - -func Sum[T integer](arr ...T) T { - var acc T - for _, a := range arr { - acc += a - } - return acc -} -func SumFunc[T any, U integer](fn func(T) U, input ...T) U { - return Sum(SliceMap(fn, input...)...) -} -func SumIFunc[T any, U integer](fn func(int, T) U, input ...T) U { - return Sum(SliceIMap(fn, input...)...) -} - -func Power2(n int) int { - if n == 0 { - return 1 - } - p := 2 - for ; n > 1; n-- { - p *= 2 - } - return p -} - -func ABS(i int) int { - if i < 0 { - return -i - } - return i -} - -func Transpose[T any](matrix [][]T) [][]T { - rows, cols := len(matrix), len(matrix[0]) - - m := make([][]T, cols) - for i := range m { - m[i] = make([]T, rows) - } - - for i := 0; i < cols; i++ { - for j := 0; j < rows; j++ { - m[i][j] = matrix[j][i] - } - } - return m -} - -func Reduce[T, U any](fn func(int, T, U) U, u U, list ...T) U { - for i, t := range list { - u = fn(i, t, u) - } - return u -} - -func Max[T cmp.Ordered](a T, v ...T) T { - for _, b := range v { - if b > a { - a = b - } - } - return a -} -func Min[T cmp.Ordered](a T, v ...T) T { - for _, b := range v { - if b < a { - a = b - } - } - return a -} - -type PQElem[T any, I integer] struct { - Value T - Priority I -} -type PQList[T any, I integer] []PQElem[T, I] - -func (pq PQList[T, I]) Len() int { - return len(pq) -} -func (pq PQList[T, I]) Less(i int, j int) bool { - return pq[i].Priority < pq[j].Priority -} -func (pq PQList[T, I]) Swap(i int, j int) { - pq[i], pq[j] = pq[j], pq[i] -} - -var _ sort.Interface = (*PQList[rune, int])(nil) - -type PriorityQueue[T any, I integer] struct { - elem PQList[T, I] -} - -func (pq *PriorityQueue[T, I]) Enqueue(elem T, priority I) { - pq.elem = append(pq.elem, PQElem[T, I]{elem, priority}) - sort.Sort(pq.elem) -} -func (pq *PriorityQueue[T, I]) IsEmpty() bool { - return len(pq.elem) == 0 -} -func (pq *PriorityQueue[T, I]) Dequeue() (T, bool) { - var elem T - if pq.IsEmpty() { - return elem, false - } - - elem, pq.elem = pq.elem[0].Value, pq.elem[1:] - return elem, true -} - -type Vertex[V comparable, I integer] struct { - to V - score I -} -type graph[V comparable, I uinteger] struct { - adj map[V][]Vertex[V, I] -} - -func Graph[V comparable, I uinteger](size int) *graph[V, I] { - return &graph[V, I]{ - adj: make(map[V][]Vertex[V, I], size), - } -} -func (g *graph[V, I]) AddEdge(u, v V, w I) { - g.adj[u] = append(g.adj[u], Vertex[V, I]{to: v, score: w}) - g.adj[v] = append(g.adj[v], Vertex[V, I]{to: u, score: w}) -} -func (g *graph[V, I]) Dijkstra(m interface{Get()}, src V) map[V]I { - pq := PriorityQueue[V, I]{} - dist := make(map[V]I, len(g.adj)) - visited := make(map[V]bool, len(g.adj)) - var INF I - INF = ^INF - - pq.Enqueue(src, 0) - dist[src] = 0 - - for !pq.IsEmpty() { - u, _ := pq.Dequeue() - - if _, ok := visited[u]; ok { - continue - } - visited[u] = true - - for _, v := range g.adj[u] { - _, ok := visited[v.to] - var du, dv I - if d, inf := dist[u]; !inf { - du = INF - } else { - du = d - } - if d, inf := dist[v.to]; !inf { - dv = INF - } else { - dv = d - } - - if !ok && du+v.score < dv { - dist[v.to] = du + v.score - pq.Enqueue(v.to, du+v.score) - } - } - } - - return dist -}