diff --git a/2023/day18/.gitignore b/2023/day18/.gitignore new file mode 100644 index 0000000..2a7f9d1 --- /dev/null +++ b/2023/day18/.gitignore @@ -0,0 +1,2 @@ +/target +/temp \ No newline at end of file diff --git a/2023/day18/Cargo.lock b/2023/day18/Cargo.lock new file mode 100644 index 0000000..5bdd842 --- /dev/null +++ b/2023/day18/Cargo.lock @@ -0,0 +1,197 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + +[[package]] +name = "aoc" +version = "0.1.0" +dependencies = [ + "itertools", + "sscanf", +] + +[[package]] +name = "const_format" +version = "0.2.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3a214c7af3d04997541b18d432afaff4c455e79e2029079647e72fc2bd27673" +dependencies = [ + "const_format_proc_macros", +] + +[[package]] +name = "const_format_proc_macros" +version = "0.2.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7f6ff08fd20f4f299298a28e2dfa8a8ba1036e6cd2460ac1de7b425d76f2500" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "convert_case" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "itertools" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0" +dependencies = [ + "either", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "proc-macro2" +version = "1.0.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax 0.8.2", +] + +[[package]] +name = "regex-automata" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax 0.8.2", +] + +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] +name = "sscanf" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c713ebd15ce561dd4a13ed62bc2a0368e16806fc30dcaf66ecf1256b2a3fdde6" +dependencies = [ + "const_format", + "lazy_static", + "regex", + "sscanf_macro", +] + +[[package]] +name = "sscanf_macro" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84955aa74a157e5834d58a07be11af7f0ab923f0194a0bb2ea6b3db8b5d1611d" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "regex-syntax 0.6.29", + "strsim", + "syn", + "unicode-width", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "2.0.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c8b28c477cc3bf0e7966561e3460130e1255f7a1cf71931075f1c5e7a7e269" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "unicode-segmentation" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" + +[[package]] +name = "unicode-width" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" + +[[package]] +name = "unicode-xid" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" diff --git a/2023/day18/Cargo.toml b/2023/day18/Cargo.toml new file mode 100644 index 0000000..9123055 --- /dev/null +++ b/2023/day18/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "aoc" +version = "0.1.0" +edition = "2021" + +[dependencies] +itertools = "*" +sscanf = "*" diff --git a/2023/day18/src/input.txt b/2023/day18/src/input.txt new file mode 100644 index 0000000..b5d73ca --- /dev/null +++ b/2023/day18/src/input.txt @@ -0,0 +1,716 @@ +L 6 (#0a4720) +U 2 (#1cc2c3) +L 9 (#778410) +U 5 (#07bb73) +R 4 (#38de80) +U 3 (#04ba13) +R 10 (#508fe2) +U 5 (#243983) +L 4 (#4b1ec2) +U 10 (#5de613) +L 5 (#14b3f2) +D 10 (#077b53) +L 5 (#170fd0) +U 5 (#539233) +L 4 (#1a5d20) +U 8 (#79f613) +L 4 (#282a70) +U 2 (#56bba1) +L 4 (#4f0980) +D 5 (#343d31) +L 5 (#2ae2c0) +D 5 (#3b74e1) +L 2 (#11f010) +D 9 (#071a91) +L 4 (#5fc4f0) +U 6 (#04bb03) +L 3 (#073b40) +U 2 (#073083) +L 4 (#3133e0) +D 10 (#3d8033) +L 2 (#50f690) +U 10 (#3d8031) +L 5 (#0d2900) +U 5 (#1a2013) +R 6 (#09cff0) +U 3 (#6d8ca3) +R 8 (#41fcd2) +U 3 (#05dc43) +L 3 (#10ee20) +U 6 (#1d4d83) +R 3 (#689090) +U 4 (#55c6c3) +R 9 (#797eb2) +D 7 (#1e4503) +R 7 (#41fcd0) +U 7 (#1d2f13) +R 3 (#7a4fa0) +U 4 (#5ec473) +L 8 (#222180) +U 4 (#63fd03) +L 7 (#61f092) +U 3 (#015d71) +L 6 (#416ec2) +U 7 (#015d73) +L 8 (#3635b2) +D 5 (#2ea023) +L 3 (#600390) +U 6 (#3f87c3) +L 4 (#2af7c0) +D 6 (#165183) +L 5 (#4e99b0) +U 5 (#193ee3) +L 3 (#313aa0) +U 6 (#1927c1) +L 2 (#734030) +U 3 (#5d7cc1) +L 7 (#44a4f2) +U 7 (#0c2873) +L 4 (#477752) +U 6 (#0c2871) +L 7 (#672412) +U 6 (#267201) +R 7 (#535b60) +U 5 (#6049d1) +R 8 (#08c560) +D 3 (#5ec2e1) +R 7 (#08c562) +D 8 (#0c85a1) +R 5 (#175970) +U 7 (#155581) +R 3 (#3a8840) +U 4 (#211863) +R 8 (#1d9122) +U 4 (#5657a3) +R 3 (#210292) +U 4 (#54be43) +L 9 (#3e93b0) +U 7 (#14b993) +R 9 (#4e0340) +U 3 (#649ec1) +R 3 (#5fadf0) +U 6 (#785b63) +R 9 (#071be0) +U 6 (#0b3e83) +R 5 (#558052) +U 4 (#4ca3f3) +R 6 (#4aa490) +U 4 (#0ff041) +L 7 (#2eed50) +U 8 (#0ff043) +L 4 (#19a8d0) +U 6 (#014723) +L 5 (#50d400) +U 4 (#3ccd43) +L 6 (#410de2) +U 4 (#4c1f83) +L 7 (#23ba42) +U 5 (#332d41) +L 3 (#0ef462) +U 3 (#332d43) +L 7 (#705232) +D 3 (#1c9fe3) +L 3 (#558050) +D 6 (#4b23a3) +L 7 (#5a8090) +D 5 (#2a3953) +L 6 (#0aac20) +D 3 (#3e46e3) +R 4 (#38af90) +D 2 (#523a33) +R 5 (#4b99f0) +U 4 (#1f4701) +R 4 (#1446d2) +D 4 (#4c2741) +R 3 (#025680) +D 3 (#173151) +L 3 (#025682) +D 6 (#3ce361) +L 7 (#1446d0) +D 6 (#35e271) +L 9 (#138dd0) +U 6 (#34cc71) +L 3 (#3a7a10) +U 6 (#7bb9d1) +L 9 (#444da0) +D 5 (#5ae901) +L 3 (#74a360) +D 5 (#4e1481) +L 5 (#4c56e2) +U 4 (#7e7fe1) +L 7 (#304112) +U 6 (#786c73) +L 3 (#278062) +U 10 (#061373) +R 2 (#4f52c2) +U 2 (#2e0671) +R 4 (#5e9010) +U 4 (#5ec471) +R 3 (#35aed0) +U 4 (#6cadd1) +R 9 (#32dd90) +U 2 (#19e3b1) +R 3 (#281882) +U 4 (#0fb9c1) +R 8 (#101430) +U 4 (#1047b1) +R 4 (#72ff50) +D 5 (#130a41) +R 3 (#16e8f2) +D 8 (#657ec1) +R 5 (#62fd72) +U 3 (#222fa1) +R 2 (#092d22) +U 10 (#0cd021) +R 5 (#5dc442) +D 4 (#27ee51) +R 5 (#40be72) +U 6 (#57b7f3) +R 8 (#038da2) +U 5 (#7dd4f3) +L 7 (#038da0) +U 6 (#19eba3) +L 6 (#614542) +D 6 (#0f6751) +L 4 (#445ee2) +U 6 (#4b8241) +L 5 (#433ad2) +D 6 (#2fbf81) +L 6 (#32ae10) +U 3 (#692201) +L 7 (#32ae12) +U 3 (#4f1591) +L 5 (#1c1eb2) +U 6 (#070df1) +R 2 (#788110) +U 3 (#1cf1e1) +R 8 (#410e90) +U 4 (#6021e1) +R 2 (#1fe2c2) +U 3 (#4dac81) +R 5 (#1fe2c0) +U 7 (#275021) +R 5 (#410e92) +U 2 (#380bc1) +R 3 (#132530) +U 10 (#3c8801) +R 3 (#7328a0) +U 2 (#1a1443) +R 4 (#0ebce0) +U 4 (#6d23e3) +R 2 (#6b6240) +U 8 (#1d8aa3) +R 4 (#156f22) +U 4 (#878cf3) +R 4 (#156f20) +D 10 (#26b683) +R 4 (#6b6242) +D 6 (#139df3) +R 3 (#50e170) +U 6 (#219b13) +R 6 (#2960e0) +D 4 (#219b11) +R 8 (#43cac0) +D 6 (#2a4fd1) +R 5 (#76fbc0) +D 3 (#1a8703) +R 6 (#2c5f50) +D 3 (#88a193) +R 2 (#2f68b0) +D 6 (#85b411) +L 8 (#1db590) +D 3 (#02ed81) +L 3 (#3dd880) +D 4 (#5936c3) +L 2 (#44fe30) +D 2 (#72b903) +L 4 (#003092) +U 5 (#680e73) +L 3 (#21c2b2) +D 5 (#0ff893) +L 5 (#327812) +D 4 (#72fc33) +R 2 (#442af2) +D 4 (#02ae83) +R 6 (#3c0d90) +D 4 (#3dffe3) +R 5 (#1e9970) +D 5 (#3dffe1) +R 4 (#3dbeb0) +D 5 (#032913) +R 3 (#003090) +D 9 (#0a0453) +R 6 (#2c0f92) +D 7 (#5762a3) +R 2 (#304810) +D 5 (#44f1f3) +R 5 (#4af9c0) +U 2 (#217c33) +R 2 (#7b41d2) +U 9 (#350253) +R 3 (#2c0f90) +D 6 (#618dd3) +R 3 (#656e30) +D 4 (#5a0e71) +R 9 (#096d22) +D 4 (#207221) +R 2 (#096d20) +D 10 (#647551) +R 3 (#5f7f90) +D 5 (#29a2d1) +R 4 (#5d74a0) +D 3 (#6ac771) +R 4 (#074cb2) +U 5 (#3a6771) +R 3 (#77bb92) +D 5 (#3a6773) +R 4 (#592412) +D 4 (#576e41) +L 11 (#556e50) +D 4 (#348451) +R 2 (#3037a0) +D 3 (#424fa1) +R 6 (#3a1620) +D 3 (#76d3f3) +R 6 (#187040) +D 5 (#3f1c41) +R 3 (#6d18c0) +D 5 (#5fced1) +R 3 (#1ee522) +D 9 (#4b4d21) +R 4 (#1ee520) +D 2 (#3be2b1) +R 3 (#2e1d90) +D 3 (#44cd71) +R 2 (#806ee0) +D 9 (#506161) +R 5 (#1add12) +D 4 (#02b5e1) +R 7 (#06ed02) +D 5 (#7e12f1) +R 8 (#598a22) +D 6 (#80c8d3) +R 6 (#58b882) +D 6 (#10cff1) +R 5 (#57d712) +D 4 (#32be61) +L 3 (#467812) +D 5 (#499ce1) +L 9 (#5ff792) +U 5 (#527631) +L 3 (#1a42b2) +D 2 (#241dc1) +L 4 (#4e3562) +D 3 (#469821) +R 3 (#511262) +D 3 (#2fb991) +R 3 (#57c2d2) +D 2 (#40f9b1) +R 9 (#42c7e2) +D 4 (#434ba1) +R 4 (#1d6482) +D 4 (#603dd1) +R 5 (#38bb52) +D 7 (#488571) +R 3 (#62c7a2) +D 10 (#30b171) +R 3 (#737832) +D 3 (#312d51) +R 6 (#1bdee0) +D 4 (#45c731) +R 7 (#6e5170) +D 4 (#51ec71) +R 5 (#1ae560) +D 11 (#45a503) +R 3 (#43cec0) +D 4 (#7561a3) +R 3 (#2c1500) +D 8 (#064953) +R 3 (#4bead0) +D 5 (#567691) +R 4 (#6977c0) +U 6 (#4e0651) +R 4 (#6977c2) +U 10 (#1cd311) +L 4 (#04d670) +U 9 (#55d451) +L 3 (#4ba6a0) +U 5 (#16cd41) +R 7 (#1df010) +U 9 (#136903) +R 3 (#09fa70) +D 6 (#56f603) +R 8 (#09fa72) +D 2 (#451ec3) +R 3 (#37f260) +D 11 (#013d63) +R 5 (#16b850) +D 5 (#07b491) +R 6 (#27e752) +D 5 (#3e9921) +R 6 (#273bc0) +D 10 (#2b3ae1) +R 3 (#275060) +U 7 (#2b3ae3) +R 9 (#3de0d0) +U 6 (#370481) +R 7 (#8c6cf2) +U 2 (#28bf41) +R 3 (#27e750) +U 6 (#0a4e91) +L 10 (#4edab0) +U 4 (#005b21) +R 8 (#07ea60) +U 3 (#3040d1) +R 2 (#48f532) +U 5 (#681021) +L 6 (#4af062) +U 4 (#32f941) +L 3 (#4d9e72) +U 7 (#6a3b51) +L 4 (#2f3752) +U 6 (#5f0681) +R 3 (#685aa2) +U 3 (#58feb1) +R 6 (#3689e2) +U 5 (#1355d1) +L 6 (#408240) +U 3 (#1c5ce1) +L 3 (#6651d0) +U 6 (#448761) +L 5 (#254900) +U 4 (#1c3a11) +L 3 (#00c012) +U 6 (#4a4cb1) +L 5 (#500032) +U 4 (#1fc5e1) +L 8 (#7b5cd2) +U 5 (#0e1e11) +L 3 (#240ad2) +U 6 (#22d171) +L 8 (#1cf592) +U 4 (#3d24d3) +L 5 (#0821b2) +U 4 (#41a4a3) +L 4 (#60edd2) +U 4 (#68f813) +R 4 (#195642) +U 7 (#0e2813) +L 4 (#3c6fe0) +U 7 (#5a9193) +R 6 (#32e610) +U 6 (#1c6ef3) +R 2 (#32e612) +U 3 (#646723) +R 3 (#3c6fe2) +U 3 (#7c6583) +L 7 (#01bb32) +U 9 (#4a48a1) +L 7 (#025432) +U 4 (#1adf61) +R 3 (#1f38a2) +U 10 (#686211) +R 2 (#3feb62) +U 3 (#205341) +R 3 (#19eeb2) +D 7 (#1f84a1) +R 2 (#7e5df0) +D 6 (#376f91) +R 4 (#7e5df2) +U 3 (#3df2a1) +R 7 (#012820) +U 5 (#3aca21) +R 5 (#012822) +U 4 (#43c271) +R 3 (#412ed2) +U 8 (#2a38d1) +R 3 (#303f92) +U 5 (#3c0fb1) +R 7 (#8b0562) +U 5 (#474a31) +R 3 (#19d7c2) +D 10 (#42fb21) +R 4 (#87ff22) +D 8 (#43d7e1) +R 6 (#3ce282) +U 9 (#73af71) +R 2 (#0664c2) +U 3 (#3aad71) +R 3 (#7e6832) +U 11 (#1cb851) +R 5 (#2a8062) +D 5 (#1cb853) +R 4 (#5a8c92) +D 7 (#424493) +R 3 (#22fe32) +D 4 (#2da233) +R 5 (#089392) +D 3 (#12d133) +R 3 (#206a02) +D 7 (#02c843) +R 4 (#41d442) +D 5 (#840093) +L 6 (#154262) +D 2 (#466b43) +L 11 (#0921f0) +D 4 (#7b2f33) +L 4 (#0921f2) +D 5 (#1fc403) +L 7 (#287e92) +U 9 (#00b123) +L 4 (#13a4f0) +D 4 (#3552e3) +L 5 (#071620) +D 4 (#639f63) +L 6 (#1c3640) +D 3 (#2906e3) +L 8 (#471a60) +D 4 (#82df91) +R 9 (#2db8f0) +D 2 (#09c6b1) +R 5 (#138f70) +D 4 (#3f1a33) +R 4 (#066c90) +U 7 (#0c4483) +R 7 (#05d050) +D 7 (#3a3a73) +R 6 (#665160) +U 7 (#455c13) +R 5 (#665162) +D 7 (#49b5c3) +R 6 (#4c7ec2) +D 7 (#20dbe3) +R 3 (#4f70f2) +D 5 (#200423) +R 6 (#1c9b50) +D 4 (#4c8e73) +L 7 (#527ca0) +D 6 (#50c431) +L 4 (#315e70) +D 4 (#38f6f3) +R 4 (#3a9d50) +D 4 (#4b32f3) +R 7 (#3a9d52) +D 3 (#12f343) +R 3 (#227d30) +D 5 (#059223) +R 7 (#2a6c80) +D 2 (#214061) +R 4 (#139500) +D 7 (#4d03f1) +R 6 (#139502) +D 3 (#2e6af1) +R 2 (#49c970) +D 7 (#50c433) +R 2 (#15b340) +D 6 (#75d6e3) +R 5 (#60ebb2) +U 4 (#2194b3) +R 4 (#4296f2) +U 7 (#7c7eb3) +L 4 (#384b02) +U 10 (#11cb73) +R 4 (#277422) +U 9 (#07e211) +R 6 (#144802) +D 8 (#7d0121) +R 4 (#144800) +D 4 (#0966f1) +R 10 (#15ca42) +D 4 (#572933) +L 5 (#1d58f2) +D 2 (#261ce3) +L 2 (#1677d2) +D 8 (#293a23) +R 7 (#346822) +D 3 (#49b713) +R 11 (#32b9b0) +D 5 (#2ee333) +L 6 (#32b9b2) +D 8 (#3fbd03) +L 5 (#520bc2) +U 8 (#2c6361) +L 3 (#002b52) +D 4 (#452a71) +L 3 (#802ea2) +D 4 (#452a73) +L 5 (#309202) +D 6 (#47aba1) +L 5 (#108062) +D 3 (#4b1803) +L 3 (#86d332) +D 4 (#164e93) +L 5 (#18e3b2) +U 5 (#3d4a63) +L 9 (#31cb32) +U 2 (#163ec1) +L 6 (#1ed2c2) +U 7 (#86a751) +L 3 (#1ed2c0) +D 4 (#01cae1) +L 3 (#1d6ea2) +D 5 (#328e01) +L 5 (#56a3a2) +D 5 (#2f9b81) +L 3 (#279cf2) +D 4 (#44bae1) +L 11 (#108122) +D 3 (#4e7291) +R 11 (#22ba70) +D 5 (#61fdc3) +L 4 (#5fb660) +D 7 (#092193) +R 7 (#3a4e40) +U 4 (#303033) +R 6 (#099990) +U 9 (#4b4ea1) +R 4 (#323870) +D 3 (#3ee241) +R 2 (#1f18e0) +D 10 (#111ea1) +R 6 (#11d2a0) +D 8 (#3b1833) +L 4 (#45fc10) +U 4 (#5ae0c3) +L 8 (#2a4810) +D 8 (#0df533) +L 6 (#3c1730) +U 8 (#0bd5f1) +L 4 (#1bcf30) +D 4 (#364c11) +L 3 (#8a08a0) +D 5 (#4cddf1) +L 4 (#8a08a2) +D 5 (#14ee31) +R 4 (#2da520) +D 4 (#134281) +L 3 (#1fd280) +D 4 (#4b7501) +L 3 (#348f32) +D 10 (#5a47f1) +L 3 (#1a1042) +D 3 (#305e31) +L 4 (#3f3df2) +D 6 (#4248b1) +L 8 (#7f62a2) +D 2 (#316c01) +L 3 (#5e6472) +D 3 (#03dab1) +L 3 (#603612) +D 3 (#03dab3) +L 11 (#272482) +D 5 (#016b63) +L 8 (#0be902) +D 5 (#0d4c63) +R 11 (#4e58b2) +D 6 (#81d8e3) +R 3 (#480df2) +D 4 (#0d08f3) +L 6 (#0d4632) +U 7 (#835683) +L 3 (#0f8ec2) +D 7 (#18fe83) +L 5 (#30fb10) +D 3 (#3d63e3) +R 8 (#30fb12) +D 3 (#386af3) +L 5 (#35f182) +D 5 (#6a03f3) +L 3 (#4f0732) +U 5 (#137183) +L 4 (#71b2d2) +U 3 (#2fd0f3) +L 6 (#721082) +U 2 (#02e3f3) +L 8 (#670070) +U 5 (#3186f3) +R 6 (#628c20) +U 2 (#0dc693) +R 8 (#677d10) +U 6 (#0dc691) +L 3 (#1249b0) +U 8 (#3186f1) +L 5 (#34f770) +D 5 (#43aa93) +L 3 (#700862) +U 3 (#22e603) +L 7 (#0573a2) +U 9 (#554633) +L 4 (#463472) +U 5 (#096c93) +R 9 (#27db92) +U 3 (#7b9ed3) +R 4 (#4e9670) +U 4 (#3899a3) +L 8 (#4e9672) +U 8 (#7b3683) +L 5 (#3ebc32) +U 5 (#39c403) +L 5 (#4aca20) +D 7 (#378803) +L 3 (#796400) +D 3 (#1f5f33) +L 6 (#2850c0) +D 3 (#7cc673) +L 6 (#07ac50) +D 5 (#853a91) +L 2 (#162f10) +D 7 (#0309b1) +L 5 (#534730) +D 4 (#2f1b81) +L 3 (#30f2f0) +D 6 (#214cc1) +L 7 (#0eedb0) +D 2 (#46e153) +L 9 (#12cf10) +U 5 (#354783) +L 3 (#5e79a0) +U 6 (#330193) +L 5 (#59aa10) +D 11 (#618491) +L 6 (#1ce360) +D 7 (#4da5d1) +L 5 (#728550) +U 4 (#2b1fe1) +L 5 (#037270) +U 3 (#510281) +L 11 (#0a91a0) +U 4 (#252b03) +R 6 (#3b4430) +U 4 (#7edd33) +R 10 (#350530) +U 3 (#7edd31) +L 4 (#1c4fc0) +U 8 (#315b03) +L 5 (#106ae0) +U 10 (#3970f3) +L 7 (#077d82) +U 2 (#242323) +L 5 (#7e2d02) +U 10 (#00c0b3) +L 4 (#175982) +D 4 (#00aa53) +L 3 (#03a892) +D 8 (#2bc153) +L 3 (#5d8b92) +D 11 (#707e03) +L 5 (#613420) +U 6 (#030a83) +L 4 (#4425a0) +U 7 (#3fc6d1) +L 7 (#1638b2) +U 5 (#55f951) +R 11 (#1638b0) +U 3 (#066581) +L 8 (#26a7f0) +U 2 (#5cbaa3) +L 4 (#4defe0) +U 8 (#366f63) +L 4 (#6f17f0) +U 3 (#3b5e43) +L 6 (#1d99f0) +U 5 (#40e183) +L 5 (#8c2162) +D 8 (#4458b3) +L 3 (#31c512) +U 10 (#009c73) diff --git a/2023/day18/src/main.rs b/2023/day18/src/main.rs new file mode 100644 index 0000000..bd4ec0a --- /dev/null +++ b/2023/day18/src/main.rs @@ -0,0 +1,166 @@ +// Problem: https://adventofcode.com/2023/day/18 + +use std::collections::HashSet; + +use itertools::Itertools; +use sscanf::sscanf; + +type Result = usize; +type Point = (i64, i64); + +#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +enum Dir { + Up, + Down, + Right, + Left, +} +impl Dir { + fn mv(&self, (r, c): Point) -> Point { + match self { + Up => (r - 1, c), + Down => (r + 1, c), + Right => (r, c + 1), + Left => (r, c - 1), + } + } +} +use Dir::*; + +type Instruction = (Dir, i64, String); + +type Input = Vec; + +fn parse_input(input: &str) -> Input { + input + .trim() + .lines() + .map(|line| { + let (ch, amount, color) = sscanf!(line, "{char} {i64} (#{String})").unwrap(); + ( + match ch { + 'R' => Right, + 'D' => Down, + 'L' => Left, + 'U' => Up, + _ => panic!(), + }, + amount, + color, + ) + }) + .collect() +} + +// ------------------------------------------ + +fn full_surface(map: &HashSet) -> usize { + let (min_r, max_r) = match map.iter().map(|(r, _)| r).minmax() { + itertools::MinMaxResult::NoElements => panic!(), + itertools::MinMaxResult::OneElement(v) => (*v, *v), + itertools::MinMaxResult::MinMax(min, max) => (*min, *max), + }; + let (min_c, max_c) = match map.iter().map(|(_, c)| c).minmax() { + itertools::MinMaxResult::NoElements => panic!(), + itertools::MinMaxResult::OneElement(v) => (*v, *v), + itertools::MinMaxResult::MinMax(min, max) => (*min, *max), + }; + let mut fill_map = HashSet::new(); + let mut queue = vec![(min_r - 1, min_c - 1)]; + let lines = &((min_r - 1)..=(max_r + 1)); + let cols = &((min_c - 1)..=(max_c + 1)); + while let Some(point) = queue.pop() { + for dir in [Up, Down, Left, Right] { + let next = dir.mv(point); + if lines.contains(&next.0) + && cols.contains(&next.1) + && !map.contains(&next) + && !fill_map.contains(&next) + { + fill_map.insert(next); + queue.push(next); + } + } + } + let mut surface = 0; + for r in (min_r)..=(max_r) { + for c in (min_c)..=(max_c) { + if !fill_map.contains(&(r, c)) { + surface += 1; + } + } + } + surface +} + +fn part1(input: &Input) -> Result { + let mut cur = (0, 0); + let mut map: HashSet = HashSet::new(); + map.insert(cur); + for (dir, amount, _) in input { + for _ in 0..(*amount as usize) { + cur = dir.mv(cur); + map.insert(cur); + } + } + + full_surface(&map) +} + +#[test] +fn test_part1() { + let input = parse_input(include_str!("test.txt")); + dbg!(&input); + assert_eq!(part1(&input), 62); +} + +// ------------------------------------------ + +fn decode(color: &str) -> (i32, Dir) { + ( + i32::from_str_radix(&color[0..5], 16).unwrap(), + match &color[5..6] { + "0" => Right, + "1" => Down, + "2" => Left, + "3" => Up, + _ => panic!(), + }, + ) +} + +fn showlace(map: &Vec) -> i64 { + let mut area: i64 = 0; + for ((r1, c1), (r2, c2)) in map.iter().tuple_windows() { + area += r1 * c2 - r2 * c1; + } + map.len() as i64 + area.abs() / 2 - map.len() as i64 / 2 +} + +fn part2(input: &Input) -> Result { + let mut cur = (0, 0); + let mut map = vec![]; + map.push(cur); + for (_, _, color) in input { + let (amount, dir) = decode(color); + for _ in 0..(amount as usize) { + cur = dir.mv(cur); + map.push(cur); + } + } + showlace(&map) as usize +} + +#[test] +fn test_part2() { + let input = parse_input(include_str!("test.txt")); + assert_eq!(part2(&input), 952408144115); +} + +// ------------------------------------------ + +fn main() { + let input = parse_input(include_str!("input.txt")); + println!("Part 1: {:?}", part1(&input)); + println!("Part 2: {:?}", part2(&input)); +} diff --git a/2023/day18/src/test.txt b/2023/day18/src/test.txt new file mode 100644 index 0000000..fc7612e --- /dev/null +++ b/2023/day18/src/test.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)