From 1f4fa9cbd2f0bc1ed381c7ba04d80922c0d7ded9 Mon Sep 17 00:00:00 2001 From: Philipp Polterauer Date: Fri, 22 Dec 2023 20:37:47 +0100 Subject: [PATCH] added day18 --- .vscode/launch.json | 2 +- data/day18/input1.txt | 794 +++++++++++++++++++++++++++++++++++++ data/day18/input2.txt | 794 +++++++++++++++++++++++++++++++++++++ data/day18/test_input1.txt | 14 + data/day18/test_input2.txt | 14 + src/days/day18.rs | 105 ++++- src/util.rs | 214 ++++++++++ 7 files changed, 1932 insertions(+), 5 deletions(-) create mode 100644 data/day18/input1.txt create mode 100644 data/day18/input2.txt create mode 100644 data/day18/test_input1.txt create mode 100644 data/day18/test_input2.txt diff --git a/.vscode/launch.json b/.vscode/launch.json index d26047b..88407ec 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -19,7 +19,7 @@ "kind": "bin" } }, - "args": ["17","2", "-t"], + "args": ["18","1", "-t"], "cwd": "${workspaceFolder}" }, ] diff --git a/data/day18/input1.txt b/data/day18/input1.txt new file mode 100644 index 0000000..6bee4c1 --- /dev/null +++ b/data/day18/input1.txt @@ -0,0 +1,794 @@ +R 4 (#4b18e0) +U 4 (#0b4f93) +R 4 (#6d70b0) +U 12 (#86edc3) +R 4 (#435460) +U 3 (#07f023) +R 8 (#33dd00) +U 5 (#599aa3) +L 11 (#83c702) +U 5 (#3f0501) +L 7 (#4bf232) +U 9 (#3f0503) +L 2 (#4955d2) +U 5 (#24f2e3) +L 9 (#16abf2) +U 6 (#60f753) +L 3 (#678dd2) +U 3 (#736093) +L 3 (#007482) +D 7 (#446e11) +L 6 (#0482a2) +D 8 (#997fd1) +L 4 (#4891e2) +D 6 (#2547b1) +L 5 (#501bf2) +D 8 (#7ed033) +L 8 (#0dabe2) +U 8 (#7184d3) +L 2 (#748a42) +U 5 (#12e093) +L 7 (#672432) +D 7 (#19c6a3) +L 4 (#1c5c22) +D 2 (#6e8a23) +L 10 (#5cd602) +U 9 (#1d8ff3) +L 7 (#359ce2) +U 6 (#333143) +R 11 (#0422f2) +D 10 (#235323) +R 6 (#64bc72) +U 10 (#6ea5f3) +R 11 (#68df60) +U 10 (#0952c3) +L 4 (#5206d2) +U 6 (#09c2a3) +L 5 (#45f7e0) +U 6 (#6d15e3) +L 6 (#271740) +D 6 (#6d15e1) +L 7 (#2e6a20) +U 9 (#178883) +L 9 (#430370) +U 4 (#2ee7e3) +L 7 (#8bdcf0) +D 9 (#5cebd3) +L 3 (#3d5a50) +D 4 (#5ecc23) +L 8 (#095b52) +U 9 (#3b7121) +L 10 (#8061b2) +U 4 (#3b7123) +L 7 (#209272) +U 4 (#493833) +L 9 (#7d9b02) +U 5 (#283633) +L 3 (#0db702) +U 3 (#437d73) +L 14 (#721282) +U 4 (#059a63) +L 2 (#4630d2) +U 5 (#6c5f43) +L 13 (#2949c2) +U 3 (#590a93) +R 5 (#4f5650) +U 5 (#71c3e3) +L 8 (#308540) +D 15 (#4fbc53) +L 2 (#4fa760) +U 15 (#41d523) +L 8 (#6bd380) +U 6 (#5cb553) +R 6 (#76acc0) +U 10 (#6be3a3) +R 6 (#5d61b0) +D 10 (#6dd101) +R 4 (#04f600) +U 4 (#4ea101) +R 2 (#6c2630) +U 5 (#498ac1) +R 5 (#0667a0) +U 5 (#120ef3) +R 3 (#31c080) +U 5 (#861cd3) +R 11 (#364e90) +D 5 (#6dd103) +R 9 (#6fe5b0) +D 11 (#119d43) +R 6 (#111310) +D 14 (#2ff323) +R 7 (#177432) +U 12 (#0ccbd3) +R 2 (#77c592) +U 9 (#6eee43) +R 10 (#21e622) +U 8 (#0bc513) +L 8 (#5398a0) +U 2 (#53cca3) +L 10 (#463c20) +U 5 (#53cca1) +R 8 (#174b20) +U 11 (#09ddc3) +R 6 (#20c940) +U 11 (#2fc6c3) +R 8 (#7ab1b0) +U 6 (#7ab291) +R 3 (#057160) +U 6 (#008331) +R 11 (#62c440) +U 12 (#3233a1) +R 2 (#1ceaa0) +U 4 (#4ef663) +R 3 (#7e5670) +U 2 (#4ef661) +R 5 (#3ccc40) +U 8 (#6e3531) +L 13 (#3964a0) +U 5 (#68b801) +R 13 (#83d220) +U 5 (#459901) +R 2 (#053a70) +U 6 (#4d18d3) +R 11 (#8a23d0) +U 8 (#4d18d1) +R 9 (#2cce90) +D 14 (#48af61) +L 6 (#045500) +D 6 (#0b4971) +L 8 (#18fa70) +D 7 (#7d8d03) +L 3 (#6ba530) +D 9 (#061783) +R 9 (#2c9c30) +D 13 (#402993) +R 8 (#18e9c0) +D 7 (#51e7a3) +R 15 (#18e9c2) +D 3 (#0ae133) +R 5 (#4929f0) +D 3 (#8d4623) +R 4 (#277680) +U 11 (#034583) +R 7 (#1ea1a0) +D 11 (#6cc5e3) +R 8 (#8894e0) +U 5 (#2fc6c1) +R 6 (#4dd6d0) +U 3 (#7f5b21) +R 11 (#219440) +U 4 (#2db8a1) +R 6 (#4053a0) +U 6 (#2df7e1) +R 12 (#086710) +U 8 (#47c7c1) +R 4 (#41ee90) +U 3 (#1dfa61) +R 3 (#5c3610) +U 7 (#483023) +R 10 (#2b6060) +D 3 (#327133) +R 8 (#2e4b30) +D 14 (#212321) +R 8 (#4bfd00) +D 5 (#5f0b81) +R 4 (#718450) +D 14 (#320dd1) +R 5 (#103630) +D 6 (#911953) +R 4 (#3f6190) +D 7 (#212323) +L 4 (#7f84b0) +D 10 (#2816a1) +R 5 (#026f80) +D 2 (#528ab1) +R 8 (#04e160) +D 11 (#655ca3) +R 2 (#0a1bf0) +D 3 (#6872c3) +R 12 (#7ad510) +D 8 (#72fe63) +R 7 (#109a50) +D 9 (#66ae31) +R 3 (#0828a2) +D 12 (#039eb1) +R 8 (#6f2c60) +D 6 (#46dae1) +R 12 (#76c900) +D 8 (#724711) +L 10 (#0a41a0) +D 8 (#73f301) +L 2 (#029092) +D 5 (#2ee5b3) +R 9 (#794702) +U 9 (#2ee5b1) +R 9 (#745f72) +U 2 (#108e61) +R 4 (#30e792) +U 10 (#099281) +R 11 (#347f32) +U 4 (#319ab1) +L 5 (#44a712) +U 9 (#623681) +L 6 (#3b5002) +U 10 (#25e431) +R 6 (#4f3f22) +U 4 (#226f51) +R 5 (#26f7b2) +U 8 (#588341) +R 4 (#09de02) +D 11 (#423f21) +L 6 (#6c9ee2) +D 10 (#423f23) +R 6 (#3badf2) +D 4 (#2c0a93) +R 4 (#521a32) +U 7 (#4ee803) +R 15 (#47f6d2) +U 2 (#673651) +R 4 (#5c10b2) +U 9 (#4dc641) +L 11 (#4d7032) +U 6 (#299111) +L 8 (#8d06b2) +U 8 (#22e4d1) +L 7 (#7dac02) +U 4 (#58e171) +L 7 (#17e332) +D 12 (#211931) +L 4 (#7e1732) +D 5 (#502911) +L 4 (#88c6c2) +D 5 (#430ba1) +L 3 (#3e4de0) +D 13 (#481b11) +L 5 (#5925e0) +U 4 (#1cecb1) +L 7 (#11ecf0) +U 7 (#634bc1) +R 7 (#6218d2) +U 7 (#7990f3) +L 5 (#576b42) +U 4 (#7990f1) +L 10 (#145832) +U 9 (#609ef1) +L 3 (#0d8822) +U 4 (#2b1f83) +L 3 (#77a562) +U 5 (#2b1f81) +L 6 (#3d05c2) +U 7 (#4b56d1) +L 5 (#1f6272) +U 8 (#916891) +R 6 (#087ce0) +U 6 (#2cf203) +R 12 (#285e60) +U 9 (#6ae623) +R 9 (#8a34a0) +U 9 (#6ae621) +L 9 (#15b200) +U 11 (#40c541) +R 5 (#0c7fc0) +D 7 (#2d5ad3) +R 9 (#42f280) +D 7 (#8140a3) +R 3 (#255770) +D 10 (#8140a1) +R 12 (#4ac260) +D 5 (#2d5ad1) +R 8 (#083780) +U 15 (#40c543) +R 7 (#028ab0) +U 13 (#2cf201) +R 5 (#1461d0) +U 2 (#23bff1) +R 4 (#1a4992) +U 3 (#355fc1) +R 7 (#6cafd2) +U 5 (#355fc3) +R 6 (#226752) +D 6 (#5af021) +R 11 (#526e82) +D 6 (#055ac1) +R 4 (#460a42) +D 3 (#055ac3) +R 6 (#565542) +D 3 (#3cd6a1) +L 7 (#2da922) +D 10 (#708ea3) +R 7 (#687472) +D 4 (#4ec333) +R 4 (#58d602) +U 7 (#4dd3e3) +R 3 (#23a7c0) +U 10 (#51fa83) +R 5 (#23a7c2) +D 5 (#188363) +R 14 (#5bc432) +U 4 (#3f0d23) +R 8 (#27a940) +U 3 (#62a3a3) +R 10 (#27a942) +U 6 (#0ceab3) +R 2 (#4479c2) +U 12 (#8beb63) +L 2 (#0b84a2) +U 3 (#3aa313) +L 7 (#270672) +U 15 (#4bd563) +L 5 (#4d8072) +U 6 (#045183) +R 12 (#08fed2) +U 11 (#52d323) +R 2 (#050992) +U 9 (#618533) +R 4 (#17a282) +D 7 (#08e921) +R 3 (#1a0d22) +D 6 (#0e8541) +R 11 (#61b232) +D 7 (#6635f1) +R 5 (#617832) +D 5 (#08f4f3) +R 2 (#490c22) +D 3 (#234821) +R 3 (#476942) +D 12 (#234823) +R 11 (#31b6d2) +D 4 (#08f4f1) +L 6 (#129582) +D 6 (#507ed1) +R 6 (#1e7010) +D 8 (#5490e1) +R 10 (#093bb0) +D 4 (#7cefc1) +R 9 (#6fe430) +D 3 (#0160b1) +L 8 (#1a8570) +D 2 (#2722f3) +L 4 (#2eb860) +D 12 (#572d83) +L 7 (#556c20) +U 12 (#50d5c1) +L 6 (#3a0932) +D 3 (#0a1701) +L 6 (#2294f0) +D 4 (#5f04e1) +L 13 (#62c590) +D 7 (#5f04e3) +R 6 (#1660e0) +D 2 (#34e001) +R 6 (#23b340) +D 12 (#1e92e1) +R 6 (#786d10) +U 12 (#44f8d1) +R 4 (#786d12) +D 3 (#3f6cf1) +R 3 (#2e2812) +D 3 (#185ac3) +R 3 (#7976e2) +D 14 (#0a5201) +R 4 (#15e3e2) +D 3 (#825221) +R 3 (#699e12) +U 3 (#8ca423) +R 14 (#04ffb2) +U 3 (#185ac1) +R 2 (#0a3482) +U 4 (#59d541) +L 10 (#5b8ab2) +U 4 (#59d543) +L 6 (#395af2) +U 3 (#2fdcb1) +R 7 (#1d5670) +U 3 (#3801c1) +R 6 (#65de30) +U 7 (#522db1) +R 11 (#9a9020) +U 12 (#382413) +R 11 (#55b6c0) +U 8 (#382411) +L 4 (#4549e0) +U 7 (#2f1091) +L 7 (#127550) +U 7 (#3e2ee1) +R 7 (#1b7ba0) +U 5 (#0177e1) +L 10 (#581e30) +U 4 (#0177e3) +L 8 (#287b30) +U 5 (#4667a1) +R 6 (#3a6730) +U 6 (#7575e1) +R 6 (#4d8f90) +U 8 (#06a251) +R 2 (#32a980) +U 5 (#92bbf3) +R 3 (#465200) +D 4 (#1e82a3) +R 7 (#5e49b0) +U 4 (#282471) +R 6 (#95ba62) +U 3 (#3fc1e1) +R 3 (#95ba60) +U 9 (#495841) +R 3 (#3bece0) +D 13 (#06a253) +R 3 (#58ed10) +D 4 (#3c4df3) +R 7 (#830800) +D 10 (#3c4df1) +R 4 (#39bca0) +D 4 (#0e26a1) +R 6 (#09cd40) +D 6 (#725971) +R 10 (#624800) +D 11 (#49ae11) +R 11 (#3ed060) +D 7 (#49ae13) +L 12 (#3e0b20) +D 4 (#569191) +L 3 (#1bb070) +D 7 (#0b1803) +L 10 (#045fa0) +U 6 (#8cb0a3) +L 7 (#57ca40) +D 6 (#146f43) +L 9 (#5ad0b0) +D 3 (#22f873) +R 10 (#249e42) +D 5 (#429003) +R 11 (#35bf52) +D 7 (#162bc1) +R 7 (#5689a2) +D 10 (#162bc3) +R 12 (#286a32) +D 9 (#54b443) +R 10 (#505c32) +D 3 (#831903) +R 7 (#003ab0) +D 11 (#6a9af3) +R 10 (#39f710) +U 11 (#1f31b3) +R 5 (#6fc5c0) +D 4 (#071b73) +R 7 (#3fe110) +D 9 (#5fd6e3) +R 9 (#3fd500) +D 3 (#2fc9b3) +R 6 (#727a40) +D 7 (#531d23) +R 14 (#02afc0) +D 6 (#1e5cb1) +R 4 (#717490) +D 12 (#6efba1) +R 11 (#5996f0) +U 5 (#763861) +L 5 (#444cf0) +U 8 (#763863) +R 7 (#2e2c60) +U 7 (#543fa1) +L 7 (#95e230) +U 3 (#543fa3) +R 5 (#41fd20) +U 3 (#19bf61) +R 5 (#656540) +D 7 (#391283) +R 4 (#33ebc0) +D 13 (#391281) +R 9 (#709320) +D 6 (#498291) +R 3 (#03c942) +D 6 (#3391a1) +R 6 (#5a83e2) +D 5 (#4eaff1) +R 7 (#2ca112) +D 4 (#2de1c1) +L 6 (#8724f0) +D 9 (#4b7fc1) +L 5 (#005232) +U 9 (#322291) +L 7 (#6a7682) +D 4 (#5d2381) +L 5 (#6e5312) +U 13 (#5d2383) +L 8 (#4a5e52) +D 13 (#61a5a1) +L 3 (#2adb12) +D 6 (#1056e3) +L 3 (#74eb12) +U 10 (#1056e1) +L 12 (#4c3122) +D 9 (#02d251) +R 9 (#30a702) +D 7 (#7cd9f1) +R 10 (#56a522) +D 6 (#25e4f1) +R 7 (#720682) +D 3 (#11a711) +R 10 (#4810c0) +U 9 (#7233d1) +R 13 (#47d8b0) +D 5 (#222c51) +R 6 (#270630) +U 9 (#6c2991) +R 5 (#251ce0) +D 9 (#0ce373) +R 5 (#0f3af0) +D 4 (#234033) +L 13 (#399410) +D 3 (#1091f3) +L 11 (#12ab92) +D 3 (#2c05c3) +L 9 (#53cc00) +D 5 (#662eb3) +R 3 (#53cc02) +D 3 (#19fbf3) +R 8 (#12ab90) +D 11 (#13a3c3) +R 10 (#6b7ed0) +D 7 (#480071) +L 10 (#39ba10) +D 12 (#414181) +L 11 (#6be3c0) +D 4 (#308403) +L 4 (#5081d2) +U 3 (#4a1c33) +L 6 (#412360) +U 11 (#5cad03) +L 4 (#412362) +U 11 (#1670a3) +L 8 (#5081d0) +U 7 (#36b3f3) +L 8 (#4a86c0) +D 7 (#1903f1) +L 11 (#5aa690) +U 9 (#88cd01) +L 3 (#3843d0) +U 14 (#4c44d1) +L 8 (#92ea62) +D 5 (#205b31) +L 14 (#2d2130) +D 4 (#1600d1) +L 2 (#5eed20) +D 8 (#504601) +L 9 (#4238f2) +D 10 (#8a3821) +L 3 (#524dd2) +D 6 (#8a3823) +L 3 (#106f82) +U 8 (#3d0d81) +L 6 (#1ec032) +U 6 (#040993) +L 4 (#1ff1d2) +D 14 (#17b973) +L 3 (#3c2ae2) +D 7 (#7c2501) +L 7 (#633cd2) +D 4 (#6530d3) +L 3 (#082de2) +D 7 (#16f433) +L 13 (#89eb52) +D 5 (#17b971) +L 3 (#252512) +U 15 (#040991) +L 2 (#90d092) +U 10 (#0a1021) +L 8 (#1cd900) +U 6 (#4a13d1) +R 10 (#7a6bf2) +U 4 (#4683c1) +R 7 (#7a6bf0) +D 11 (#3a66f1) +R 8 (#488f20) +U 11 (#7135a1) +R 5 (#4cc380) +U 5 (#0bf121) +R 3 (#4c2e80) +U 2 (#7908a1) +R 3 (#259e00) +U 10 (#6bafe3) +L 5 (#52e6d0) +U 4 (#506603) +L 6 (#1fec10) +D 7 (#3a1983) +R 6 (#389e30) +D 2 (#2f7281) +L 6 (#55f170) +D 7 (#4cb8e1) +L 5 (#389e30) +U 6 (#2642e1) +L 10 (#7394e0) +U 3 (#642bf1) +R 10 (#7394e2) +U 7 (#132c01) +L 6 (#3c4a80) +D 4 (#3e4d21) +L 4 (#989ad0) +U 7 (#473211) +L 8 (#0aa900) +D 6 (#3a8a11) +L 12 (#4307a2) +D 9 (#13b0c1) +L 6 (#7e59a2) +D 11 (#00a041) +L 3 (#44b0d2) +D 6 (#0efc51) +L 6 (#8a1302) +D 9 (#160fa1) +L 4 (#4afa62) +D 2 (#68e871) +L 14 (#5ecd60) +D 5 (#6a51d1) +L 5 (#764000) +D 8 (#1a6d61) +L 4 (#713042) +D 14 (#0b92e1) +L 5 (#5b77e2) +D 3 (#0a0581) +R 10 (#212752) +D 3 (#0a0583) +R 14 (#3f0ca2) +D 8 (#13d911) +R 3 (#307832) +D 4 (#72d201) +R 6 (#133522) +U 13 (#6e4581) +R 4 (#70fe22) +D 13 (#082221) +R 7 (#44fff2) +D 4 (#731171) +R 3 (#10eda2) +D 9 (#0e5d31) +L 14 (#37bc32) +D 6 (#3852c3) +L 7 (#5f43c2) +U 13 (#8c7593) +L 9 (#2ff4f2) +U 10 (#1787b3) +L 13 (#68b4a2) +U 3 (#15fd23) +L 3 (#515622) +U 3 (#618313) +L 11 (#2d09d2) +U 10 (#6bb2f3) +L 5 (#1f22a2) +U 10 (#4d8e33) +L 6 (#1cb690) +U 3 (#09c3b3) +L 9 (#214c20) +U 9 (#5bfe23) +L 9 (#848910) +U 10 (#1a73e3) +L 10 (#3d9252) +U 3 (#089003) +R 11 (#7ddbb2) +U 8 (#20c473) +R 15 (#071dc2) +U 7 (#554823) +R 13 (#0cfb42) +U 7 (#1ccaa1) +L 15 (#847822) +U 8 (#5f0271) +L 4 (#03cdd2) +U 10 (#4f69b1) +L 3 (#4b1902) +U 8 (#8129b1) +L 4 (#5e69f2) +D 5 (#1f33b3) +L 5 (#2e2582) +D 13 (#67aea3) +L 4 (#54aba2) +U 3 (#08d7a3) +L 4 (#4eebe0) +D 8 (#030763) +L 9 (#2d4760) +D 14 (#79e223) +L 9 (#7c3342) +D 3 (#334c93) +L 4 (#2b0202) +D 9 (#04d053) +L 2 (#1475a2) +D 7 (#8aed93) +L 8 (#62b4f2) +D 5 (#14c931) +R 11 (#29bc82) +D 3 (#65a951) +R 12 (#656f62) +D 3 (#7a7283) +L 8 (#2bec52) +D 7 (#1a75a3) +L 8 (#988402) +D 4 (#1a75a1) +L 3 (#1aad22) +D 9 (#373923) +L 10 (#7e3ef0) +U 7 (#3e1c93) +L 11 (#3f89d0) +U 6 (#3e1c91) +L 3 (#652e40) +D 9 (#35acc3) +L 7 (#5b0fa0) +U 4 (#46ca63) +L 10 (#079fa0) +U 7 (#391523) +L 3 (#079fa2) +U 3 (#585573) +L 8 (#1f43e0) +D 2 (#403c21) +L 9 (#144b10) +D 5 (#8fa921) +L 5 (#144b12) +U 7 (#084fb1) +L 4 (#661600) +D 6 (#5e06f3) +L 13 (#3f87a0) +D 8 (#3ca8a1) +L 13 (#4214e0) +D 10 (#567b51) +R 4 (#7a3080) +D 8 (#17de11) +R 8 (#644090) +D 12 (#55ba83) +R 3 (#025e60) +D 8 (#554783) +R 6 (#0d2eb0) +D 10 (#552813) +L 6 (#61a7d0) +D 10 (#272cb3) +L 7 (#34d6a0) +D 3 (#36cf03) +R 11 (#14be00) +D 3 (#52b543) +R 4 (#1fa362) +D 11 (#713693) +L 3 (#652112) +D 8 (#5c8463) +L 12 (#4c1362) +D 4 (#66e201) +L 8 (#07caa2) +D 9 (#6ccbd1) +L 3 (#3b9ee2) +D 5 (#5798b1) +L 13 (#4fb342) +U 6 (#231223) +L 2 (#33e3b2) +U 5 (#022ea3) +L 6 (#3f1112) +U 6 (#3dde83) +L 6 (#2732a0) +U 8 (#1e6bc3) +L 8 (#10f4e0) +U 15 (#45e363) +L 3 (#10f4e2) +U 4 (#4c9523) +R 11 (#2732a2) +U 12 (#174303) +L 10 (#5b4532) +U 13 (#6195e1) +L 8 (#7fab60) +U 2 (#1e7dd1) +L 2 (#7fab62) +U 12 (#4da741) +L 4 (#09efc2) +D 5 (#3d0d83) +L 5 (#47d532) +U 6 (#1f7441) +L 12 (#558832) +D 6 (#24e093) +L 4 (#20bfc2) +U 6 (#606283) +L 11 (#639142) +U 2 (#854311) +L 5 (#318ff2) +D 8 (#7037c1) +L 12 (#1ee752) +U 5 (#6bad43) +L 5 (#662cd2) +U 6 (#23fec3) +L 6 (#11cec2) +U 9 (#0c5b23) +L 8 (#91c052) +U 3 (#62e783) +L 6 (#08ee02) +U 11 (#11a283) diff --git a/data/day18/input2.txt b/data/day18/input2.txt new file mode 100644 index 0000000..6bee4c1 --- /dev/null +++ b/data/day18/input2.txt @@ -0,0 +1,794 @@ +R 4 (#4b18e0) +U 4 (#0b4f93) +R 4 (#6d70b0) +U 12 (#86edc3) +R 4 (#435460) +U 3 (#07f023) +R 8 (#33dd00) +U 5 (#599aa3) +L 11 (#83c702) +U 5 (#3f0501) +L 7 (#4bf232) +U 9 (#3f0503) +L 2 (#4955d2) +U 5 (#24f2e3) +L 9 (#16abf2) +U 6 (#60f753) +L 3 (#678dd2) +U 3 (#736093) +L 3 (#007482) +D 7 (#446e11) +L 6 (#0482a2) +D 8 (#997fd1) +L 4 (#4891e2) +D 6 (#2547b1) +L 5 (#501bf2) +D 8 (#7ed033) +L 8 (#0dabe2) +U 8 (#7184d3) +L 2 (#748a42) +U 5 (#12e093) +L 7 (#672432) +D 7 (#19c6a3) +L 4 (#1c5c22) +D 2 (#6e8a23) +L 10 (#5cd602) +U 9 (#1d8ff3) +L 7 (#359ce2) +U 6 (#333143) +R 11 (#0422f2) +D 10 (#235323) +R 6 (#64bc72) +U 10 (#6ea5f3) +R 11 (#68df60) +U 10 (#0952c3) +L 4 (#5206d2) +U 6 (#09c2a3) +L 5 (#45f7e0) +U 6 (#6d15e3) +L 6 (#271740) +D 6 (#6d15e1) +L 7 (#2e6a20) +U 9 (#178883) +L 9 (#430370) +U 4 (#2ee7e3) +L 7 (#8bdcf0) +D 9 (#5cebd3) +L 3 (#3d5a50) +D 4 (#5ecc23) +L 8 (#095b52) +U 9 (#3b7121) +L 10 (#8061b2) +U 4 (#3b7123) +L 7 (#209272) +U 4 (#493833) +L 9 (#7d9b02) +U 5 (#283633) +L 3 (#0db702) +U 3 (#437d73) +L 14 (#721282) +U 4 (#059a63) +L 2 (#4630d2) +U 5 (#6c5f43) +L 13 (#2949c2) +U 3 (#590a93) +R 5 (#4f5650) +U 5 (#71c3e3) +L 8 (#308540) +D 15 (#4fbc53) +L 2 (#4fa760) +U 15 (#41d523) +L 8 (#6bd380) +U 6 (#5cb553) +R 6 (#76acc0) +U 10 (#6be3a3) +R 6 (#5d61b0) +D 10 (#6dd101) +R 4 (#04f600) +U 4 (#4ea101) +R 2 (#6c2630) +U 5 (#498ac1) +R 5 (#0667a0) +U 5 (#120ef3) +R 3 (#31c080) +U 5 (#861cd3) +R 11 (#364e90) +D 5 (#6dd103) +R 9 (#6fe5b0) +D 11 (#119d43) +R 6 (#111310) +D 14 (#2ff323) +R 7 (#177432) +U 12 (#0ccbd3) +R 2 (#77c592) +U 9 (#6eee43) +R 10 (#21e622) +U 8 (#0bc513) +L 8 (#5398a0) +U 2 (#53cca3) +L 10 (#463c20) +U 5 (#53cca1) +R 8 (#174b20) +U 11 (#09ddc3) +R 6 (#20c940) +U 11 (#2fc6c3) +R 8 (#7ab1b0) +U 6 (#7ab291) +R 3 (#057160) +U 6 (#008331) +R 11 (#62c440) +U 12 (#3233a1) +R 2 (#1ceaa0) +U 4 (#4ef663) +R 3 (#7e5670) +U 2 (#4ef661) +R 5 (#3ccc40) +U 8 (#6e3531) +L 13 (#3964a0) +U 5 (#68b801) +R 13 (#83d220) +U 5 (#459901) +R 2 (#053a70) +U 6 (#4d18d3) +R 11 (#8a23d0) +U 8 (#4d18d1) +R 9 (#2cce90) +D 14 (#48af61) +L 6 (#045500) +D 6 (#0b4971) +L 8 (#18fa70) +D 7 (#7d8d03) +L 3 (#6ba530) +D 9 (#061783) +R 9 (#2c9c30) +D 13 (#402993) +R 8 (#18e9c0) +D 7 (#51e7a3) +R 15 (#18e9c2) +D 3 (#0ae133) +R 5 (#4929f0) +D 3 (#8d4623) +R 4 (#277680) +U 11 (#034583) +R 7 (#1ea1a0) +D 11 (#6cc5e3) +R 8 (#8894e0) +U 5 (#2fc6c1) +R 6 (#4dd6d0) +U 3 (#7f5b21) +R 11 (#219440) +U 4 (#2db8a1) +R 6 (#4053a0) +U 6 (#2df7e1) +R 12 (#086710) +U 8 (#47c7c1) +R 4 (#41ee90) +U 3 (#1dfa61) +R 3 (#5c3610) +U 7 (#483023) +R 10 (#2b6060) +D 3 (#327133) +R 8 (#2e4b30) +D 14 (#212321) +R 8 (#4bfd00) +D 5 (#5f0b81) +R 4 (#718450) +D 14 (#320dd1) +R 5 (#103630) +D 6 (#911953) +R 4 (#3f6190) +D 7 (#212323) +L 4 (#7f84b0) +D 10 (#2816a1) +R 5 (#026f80) +D 2 (#528ab1) +R 8 (#04e160) +D 11 (#655ca3) +R 2 (#0a1bf0) +D 3 (#6872c3) +R 12 (#7ad510) +D 8 (#72fe63) +R 7 (#109a50) +D 9 (#66ae31) +R 3 (#0828a2) +D 12 (#039eb1) +R 8 (#6f2c60) +D 6 (#46dae1) +R 12 (#76c900) +D 8 (#724711) +L 10 (#0a41a0) +D 8 (#73f301) +L 2 (#029092) +D 5 (#2ee5b3) +R 9 (#794702) +U 9 (#2ee5b1) +R 9 (#745f72) +U 2 (#108e61) +R 4 (#30e792) +U 10 (#099281) +R 11 (#347f32) +U 4 (#319ab1) +L 5 (#44a712) +U 9 (#623681) +L 6 (#3b5002) +U 10 (#25e431) +R 6 (#4f3f22) +U 4 (#226f51) +R 5 (#26f7b2) +U 8 (#588341) +R 4 (#09de02) +D 11 (#423f21) +L 6 (#6c9ee2) +D 10 (#423f23) +R 6 (#3badf2) +D 4 (#2c0a93) +R 4 (#521a32) +U 7 (#4ee803) +R 15 (#47f6d2) +U 2 (#673651) +R 4 (#5c10b2) +U 9 (#4dc641) +L 11 (#4d7032) +U 6 (#299111) +L 8 (#8d06b2) +U 8 (#22e4d1) +L 7 (#7dac02) +U 4 (#58e171) +L 7 (#17e332) +D 12 (#211931) +L 4 (#7e1732) +D 5 (#502911) +L 4 (#88c6c2) +D 5 (#430ba1) +L 3 (#3e4de0) +D 13 (#481b11) +L 5 (#5925e0) +U 4 (#1cecb1) +L 7 (#11ecf0) +U 7 (#634bc1) +R 7 (#6218d2) +U 7 (#7990f3) +L 5 (#576b42) +U 4 (#7990f1) +L 10 (#145832) +U 9 (#609ef1) +L 3 (#0d8822) +U 4 (#2b1f83) +L 3 (#77a562) +U 5 (#2b1f81) +L 6 (#3d05c2) +U 7 (#4b56d1) +L 5 (#1f6272) +U 8 (#916891) +R 6 (#087ce0) +U 6 (#2cf203) +R 12 (#285e60) +U 9 (#6ae623) +R 9 (#8a34a0) +U 9 (#6ae621) +L 9 (#15b200) +U 11 (#40c541) +R 5 (#0c7fc0) +D 7 (#2d5ad3) +R 9 (#42f280) +D 7 (#8140a3) +R 3 (#255770) +D 10 (#8140a1) +R 12 (#4ac260) +D 5 (#2d5ad1) +R 8 (#083780) +U 15 (#40c543) +R 7 (#028ab0) +U 13 (#2cf201) +R 5 (#1461d0) +U 2 (#23bff1) +R 4 (#1a4992) +U 3 (#355fc1) +R 7 (#6cafd2) +U 5 (#355fc3) +R 6 (#226752) +D 6 (#5af021) +R 11 (#526e82) +D 6 (#055ac1) +R 4 (#460a42) +D 3 (#055ac3) +R 6 (#565542) +D 3 (#3cd6a1) +L 7 (#2da922) +D 10 (#708ea3) +R 7 (#687472) +D 4 (#4ec333) +R 4 (#58d602) +U 7 (#4dd3e3) +R 3 (#23a7c0) +U 10 (#51fa83) +R 5 (#23a7c2) +D 5 (#188363) +R 14 (#5bc432) +U 4 (#3f0d23) +R 8 (#27a940) +U 3 (#62a3a3) +R 10 (#27a942) +U 6 (#0ceab3) +R 2 (#4479c2) +U 12 (#8beb63) +L 2 (#0b84a2) +U 3 (#3aa313) +L 7 (#270672) +U 15 (#4bd563) +L 5 (#4d8072) +U 6 (#045183) +R 12 (#08fed2) +U 11 (#52d323) +R 2 (#050992) +U 9 (#618533) +R 4 (#17a282) +D 7 (#08e921) +R 3 (#1a0d22) +D 6 (#0e8541) +R 11 (#61b232) +D 7 (#6635f1) +R 5 (#617832) +D 5 (#08f4f3) +R 2 (#490c22) +D 3 (#234821) +R 3 (#476942) +D 12 (#234823) +R 11 (#31b6d2) +D 4 (#08f4f1) +L 6 (#129582) +D 6 (#507ed1) +R 6 (#1e7010) +D 8 (#5490e1) +R 10 (#093bb0) +D 4 (#7cefc1) +R 9 (#6fe430) +D 3 (#0160b1) +L 8 (#1a8570) +D 2 (#2722f3) +L 4 (#2eb860) +D 12 (#572d83) +L 7 (#556c20) +U 12 (#50d5c1) +L 6 (#3a0932) +D 3 (#0a1701) +L 6 (#2294f0) +D 4 (#5f04e1) +L 13 (#62c590) +D 7 (#5f04e3) +R 6 (#1660e0) +D 2 (#34e001) +R 6 (#23b340) +D 12 (#1e92e1) +R 6 (#786d10) +U 12 (#44f8d1) +R 4 (#786d12) +D 3 (#3f6cf1) +R 3 (#2e2812) +D 3 (#185ac3) +R 3 (#7976e2) +D 14 (#0a5201) +R 4 (#15e3e2) +D 3 (#825221) +R 3 (#699e12) +U 3 (#8ca423) +R 14 (#04ffb2) +U 3 (#185ac1) +R 2 (#0a3482) +U 4 (#59d541) +L 10 (#5b8ab2) +U 4 (#59d543) +L 6 (#395af2) +U 3 (#2fdcb1) +R 7 (#1d5670) +U 3 (#3801c1) +R 6 (#65de30) +U 7 (#522db1) +R 11 (#9a9020) +U 12 (#382413) +R 11 (#55b6c0) +U 8 (#382411) +L 4 (#4549e0) +U 7 (#2f1091) +L 7 (#127550) +U 7 (#3e2ee1) +R 7 (#1b7ba0) +U 5 (#0177e1) +L 10 (#581e30) +U 4 (#0177e3) +L 8 (#287b30) +U 5 (#4667a1) +R 6 (#3a6730) +U 6 (#7575e1) +R 6 (#4d8f90) +U 8 (#06a251) +R 2 (#32a980) +U 5 (#92bbf3) +R 3 (#465200) +D 4 (#1e82a3) +R 7 (#5e49b0) +U 4 (#282471) +R 6 (#95ba62) +U 3 (#3fc1e1) +R 3 (#95ba60) +U 9 (#495841) +R 3 (#3bece0) +D 13 (#06a253) +R 3 (#58ed10) +D 4 (#3c4df3) +R 7 (#830800) +D 10 (#3c4df1) +R 4 (#39bca0) +D 4 (#0e26a1) +R 6 (#09cd40) +D 6 (#725971) +R 10 (#624800) +D 11 (#49ae11) +R 11 (#3ed060) +D 7 (#49ae13) +L 12 (#3e0b20) +D 4 (#569191) +L 3 (#1bb070) +D 7 (#0b1803) +L 10 (#045fa0) +U 6 (#8cb0a3) +L 7 (#57ca40) +D 6 (#146f43) +L 9 (#5ad0b0) +D 3 (#22f873) +R 10 (#249e42) +D 5 (#429003) +R 11 (#35bf52) +D 7 (#162bc1) +R 7 (#5689a2) +D 10 (#162bc3) +R 12 (#286a32) +D 9 (#54b443) +R 10 (#505c32) +D 3 (#831903) +R 7 (#003ab0) +D 11 (#6a9af3) +R 10 (#39f710) +U 11 (#1f31b3) +R 5 (#6fc5c0) +D 4 (#071b73) +R 7 (#3fe110) +D 9 (#5fd6e3) +R 9 (#3fd500) +D 3 (#2fc9b3) +R 6 (#727a40) +D 7 (#531d23) +R 14 (#02afc0) +D 6 (#1e5cb1) +R 4 (#717490) +D 12 (#6efba1) +R 11 (#5996f0) +U 5 (#763861) +L 5 (#444cf0) +U 8 (#763863) +R 7 (#2e2c60) +U 7 (#543fa1) +L 7 (#95e230) +U 3 (#543fa3) +R 5 (#41fd20) +U 3 (#19bf61) +R 5 (#656540) +D 7 (#391283) +R 4 (#33ebc0) +D 13 (#391281) +R 9 (#709320) +D 6 (#498291) +R 3 (#03c942) +D 6 (#3391a1) +R 6 (#5a83e2) +D 5 (#4eaff1) +R 7 (#2ca112) +D 4 (#2de1c1) +L 6 (#8724f0) +D 9 (#4b7fc1) +L 5 (#005232) +U 9 (#322291) +L 7 (#6a7682) +D 4 (#5d2381) +L 5 (#6e5312) +U 13 (#5d2383) +L 8 (#4a5e52) +D 13 (#61a5a1) +L 3 (#2adb12) +D 6 (#1056e3) +L 3 (#74eb12) +U 10 (#1056e1) +L 12 (#4c3122) +D 9 (#02d251) +R 9 (#30a702) +D 7 (#7cd9f1) +R 10 (#56a522) +D 6 (#25e4f1) +R 7 (#720682) +D 3 (#11a711) +R 10 (#4810c0) +U 9 (#7233d1) +R 13 (#47d8b0) +D 5 (#222c51) +R 6 (#270630) +U 9 (#6c2991) +R 5 (#251ce0) +D 9 (#0ce373) +R 5 (#0f3af0) +D 4 (#234033) +L 13 (#399410) +D 3 (#1091f3) +L 11 (#12ab92) +D 3 (#2c05c3) +L 9 (#53cc00) +D 5 (#662eb3) +R 3 (#53cc02) +D 3 (#19fbf3) +R 8 (#12ab90) +D 11 (#13a3c3) +R 10 (#6b7ed0) +D 7 (#480071) +L 10 (#39ba10) +D 12 (#414181) +L 11 (#6be3c0) +D 4 (#308403) +L 4 (#5081d2) +U 3 (#4a1c33) +L 6 (#412360) +U 11 (#5cad03) +L 4 (#412362) +U 11 (#1670a3) +L 8 (#5081d0) +U 7 (#36b3f3) +L 8 (#4a86c0) +D 7 (#1903f1) +L 11 (#5aa690) +U 9 (#88cd01) +L 3 (#3843d0) +U 14 (#4c44d1) +L 8 (#92ea62) +D 5 (#205b31) +L 14 (#2d2130) +D 4 (#1600d1) +L 2 (#5eed20) +D 8 (#504601) +L 9 (#4238f2) +D 10 (#8a3821) +L 3 (#524dd2) +D 6 (#8a3823) +L 3 (#106f82) +U 8 (#3d0d81) +L 6 (#1ec032) +U 6 (#040993) +L 4 (#1ff1d2) +D 14 (#17b973) +L 3 (#3c2ae2) +D 7 (#7c2501) +L 7 (#633cd2) +D 4 (#6530d3) +L 3 (#082de2) +D 7 (#16f433) +L 13 (#89eb52) +D 5 (#17b971) +L 3 (#252512) +U 15 (#040991) +L 2 (#90d092) +U 10 (#0a1021) +L 8 (#1cd900) +U 6 (#4a13d1) +R 10 (#7a6bf2) +U 4 (#4683c1) +R 7 (#7a6bf0) +D 11 (#3a66f1) +R 8 (#488f20) +U 11 (#7135a1) +R 5 (#4cc380) +U 5 (#0bf121) +R 3 (#4c2e80) +U 2 (#7908a1) +R 3 (#259e00) +U 10 (#6bafe3) +L 5 (#52e6d0) +U 4 (#506603) +L 6 (#1fec10) +D 7 (#3a1983) +R 6 (#389e30) +D 2 (#2f7281) +L 6 (#55f170) +D 7 (#4cb8e1) +L 5 (#389e30) +U 6 (#2642e1) +L 10 (#7394e0) +U 3 (#642bf1) +R 10 (#7394e2) +U 7 (#132c01) +L 6 (#3c4a80) +D 4 (#3e4d21) +L 4 (#989ad0) +U 7 (#473211) +L 8 (#0aa900) +D 6 (#3a8a11) +L 12 (#4307a2) +D 9 (#13b0c1) +L 6 (#7e59a2) +D 11 (#00a041) +L 3 (#44b0d2) +D 6 (#0efc51) +L 6 (#8a1302) +D 9 (#160fa1) +L 4 (#4afa62) +D 2 (#68e871) +L 14 (#5ecd60) +D 5 (#6a51d1) +L 5 (#764000) +D 8 (#1a6d61) +L 4 (#713042) +D 14 (#0b92e1) +L 5 (#5b77e2) +D 3 (#0a0581) +R 10 (#212752) +D 3 (#0a0583) +R 14 (#3f0ca2) +D 8 (#13d911) +R 3 (#307832) +D 4 (#72d201) +R 6 (#133522) +U 13 (#6e4581) +R 4 (#70fe22) +D 13 (#082221) +R 7 (#44fff2) +D 4 (#731171) +R 3 (#10eda2) +D 9 (#0e5d31) +L 14 (#37bc32) +D 6 (#3852c3) +L 7 (#5f43c2) +U 13 (#8c7593) +L 9 (#2ff4f2) +U 10 (#1787b3) +L 13 (#68b4a2) +U 3 (#15fd23) +L 3 (#515622) +U 3 (#618313) +L 11 (#2d09d2) +U 10 (#6bb2f3) +L 5 (#1f22a2) +U 10 (#4d8e33) +L 6 (#1cb690) +U 3 (#09c3b3) +L 9 (#214c20) +U 9 (#5bfe23) +L 9 (#848910) +U 10 (#1a73e3) +L 10 (#3d9252) +U 3 (#089003) +R 11 (#7ddbb2) +U 8 (#20c473) +R 15 (#071dc2) +U 7 (#554823) +R 13 (#0cfb42) +U 7 (#1ccaa1) +L 15 (#847822) +U 8 (#5f0271) +L 4 (#03cdd2) +U 10 (#4f69b1) +L 3 (#4b1902) +U 8 (#8129b1) +L 4 (#5e69f2) +D 5 (#1f33b3) +L 5 (#2e2582) +D 13 (#67aea3) +L 4 (#54aba2) +U 3 (#08d7a3) +L 4 (#4eebe0) +D 8 (#030763) +L 9 (#2d4760) +D 14 (#79e223) +L 9 (#7c3342) +D 3 (#334c93) +L 4 (#2b0202) +D 9 (#04d053) +L 2 (#1475a2) +D 7 (#8aed93) +L 8 (#62b4f2) +D 5 (#14c931) +R 11 (#29bc82) +D 3 (#65a951) +R 12 (#656f62) +D 3 (#7a7283) +L 8 (#2bec52) +D 7 (#1a75a3) +L 8 (#988402) +D 4 (#1a75a1) +L 3 (#1aad22) +D 9 (#373923) +L 10 (#7e3ef0) +U 7 (#3e1c93) +L 11 (#3f89d0) +U 6 (#3e1c91) +L 3 (#652e40) +D 9 (#35acc3) +L 7 (#5b0fa0) +U 4 (#46ca63) +L 10 (#079fa0) +U 7 (#391523) +L 3 (#079fa2) +U 3 (#585573) +L 8 (#1f43e0) +D 2 (#403c21) +L 9 (#144b10) +D 5 (#8fa921) +L 5 (#144b12) +U 7 (#084fb1) +L 4 (#661600) +D 6 (#5e06f3) +L 13 (#3f87a0) +D 8 (#3ca8a1) +L 13 (#4214e0) +D 10 (#567b51) +R 4 (#7a3080) +D 8 (#17de11) +R 8 (#644090) +D 12 (#55ba83) +R 3 (#025e60) +D 8 (#554783) +R 6 (#0d2eb0) +D 10 (#552813) +L 6 (#61a7d0) +D 10 (#272cb3) +L 7 (#34d6a0) +D 3 (#36cf03) +R 11 (#14be00) +D 3 (#52b543) +R 4 (#1fa362) +D 11 (#713693) +L 3 (#652112) +D 8 (#5c8463) +L 12 (#4c1362) +D 4 (#66e201) +L 8 (#07caa2) +D 9 (#6ccbd1) +L 3 (#3b9ee2) +D 5 (#5798b1) +L 13 (#4fb342) +U 6 (#231223) +L 2 (#33e3b2) +U 5 (#022ea3) +L 6 (#3f1112) +U 6 (#3dde83) +L 6 (#2732a0) +U 8 (#1e6bc3) +L 8 (#10f4e0) +U 15 (#45e363) +L 3 (#10f4e2) +U 4 (#4c9523) +R 11 (#2732a2) +U 12 (#174303) +L 10 (#5b4532) +U 13 (#6195e1) +L 8 (#7fab60) +U 2 (#1e7dd1) +L 2 (#7fab62) +U 12 (#4da741) +L 4 (#09efc2) +D 5 (#3d0d83) +L 5 (#47d532) +U 6 (#1f7441) +L 12 (#558832) +D 6 (#24e093) +L 4 (#20bfc2) +U 6 (#606283) +L 11 (#639142) +U 2 (#854311) +L 5 (#318ff2) +D 8 (#7037c1) +L 12 (#1ee752) +U 5 (#6bad43) +L 5 (#662cd2) +U 6 (#23fec3) +L 6 (#11cec2) +U 9 (#0c5b23) +L 8 (#91c052) +U 3 (#62e783) +L 6 (#08ee02) +U 11 (#11a283) diff --git a/data/day18/test_input1.txt b/data/day18/test_input1.txt new file mode 100644 index 0000000..fc7612e --- /dev/null +++ b/data/day18/test_input1.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) diff --git a/data/day18/test_input2.txt b/data/day18/test_input2.txt new file mode 100644 index 0000000..fc7612e --- /dev/null +++ b/data/day18/test_input2.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) diff --git a/src/days/day18.rs b/src/days/day18.rs index d832586..c82d35b 100644 --- a/src/days/day18.rs +++ b/src/days/day18.rs @@ -1,6 +1,103 @@ -pub fn part1(_input: &str) -> i64 { - 0 +use crate::util::*; + +#[derive(Debug, PartialEq)] +struct Instruction { + direction: Direction, + distance: i64, } -pub fn part2(_input: &str) -> i64 { - 0 + + +fn dig(pos: &MatrixIdx, instruction: &Instruction) -> MatrixIdx { + let MatrixIdx { row, col } = pos; + let row = match instruction.direction { + Direction::Down => row + instruction.distance, + Direction::Up => row - instruction.distance, + Direction::Left | Direction::Right => *row, + }; + let col = match instruction.direction { + Direction::Left => col - instruction.distance, + Direction::Right => col + instruction.distance, + Direction::Up | Direction::Down => *col, + }; + MatrixIdx { row, col } +} + + +fn parse_line(line: &str) -> Instruction { + let mut parts = line.split_whitespace(); + let direction = Direction::from_char(parts.next().unwrap().chars().next().unwrap()); + let distance = parts.next().unwrap().parse::().unwrap(); + + Instruction { + direction, + distance, + } +} + +fn parse_line2(line: &str) -> Instruction { + let mut parts = line.split_whitespace(); + let color_str: String = parts.nth(2).unwrap().chars().skip(2).take(5).collect(); + let dir = line.chars().rev().nth(1).unwrap(); + let direction = match dir { + '0' => Direction::Right, + '1' => Direction::Down, + '2' => Direction::Left, + '3' => Direction::Up, + _ => panic!("should not happen"), + }; + let distance = i64::from_str_radix(&color_str, 16).unwrap(); + Instruction { + direction, + distance, + } +} + + + + +fn sholace(idzs: &[MatrixIdx]) -> i64 { + // A = 1/2 Sum (yi + yi1)*(xi - xi1) + let mut area = 0; + let mut curr = idzs[0]; + for next in idzs.iter().skip(1) { + let xi = curr.col; + let yi = curr.row; + let xii = next.col; + let yii = next.row; + area += xi * yii - xii * yi; + + curr = *next; + } + area / 2 +} + +pub fn part1(input: &str) -> i64 { + let instructions: Vec = input.lines().map(parse_line).collect(); + + let mut position = MatrixIdx { row: 0, col: 0 }; + let mut positions = vec![position]; + for instruction in instructions.iter() { + position = dig(&position, instruction); + positions.push(position); + } + + let a = sholace(&positions); + let b = instructions + .iter() + .fold(0, |peri, inst| peri + inst.distance); + a + b / 2 + 1 +} +pub fn part2(input: &str) -> i64 { + let instructions: Vec = input.lines().map(parse_line2).collect(); + let mut position = MatrixIdx { row: 0, col: 0 }; + let mut positions = vec![position]; + for instruction in instructions.iter() { + position = dig(&position, instruction); + positions.push(position); + } + let a = sholace(&positions); + let b = instructions + .iter() + .fold(0, |peri, inst| peri + inst.distance); + a + b / 2 + 1 } diff --git a/src/util.rs b/src/util.rs index 2607c7d..b898392 100644 --- a/src/util.rs +++ b/src/util.rs @@ -5,3 +5,217 @@ pub fn load_file(day: i32, part: i32, runtest: bool, data_path: &str) -> String println!("loading data from '{}'", path); std::fs::read_to_string(path).unwrap() } +use std::ops::{Add, Mul}; +use std::ops::{Index, IndexMut}; + +#[derive(PartialEq, Clone, Copy, Hash, Eq, Debug)] +pub enum Direction { + Up, + Right, + Down, + Left, +} +impl Direction { + pub fn from_char(value: char) -> Self { + match value { + 'U' => Direction::Up, + 'R' => Direction::Right, + 'D' => Direction::Down, + 'L' => Direction::Left, + _ => panic!("should not happen!"), + } + } +} + +#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] +pub struct MatrixIdx { + pub row: i64, + pub col: i64, +} + +fn get_rot(last: &Direction, next: &Direction) -> i64 { + if last == next { + return 0; + } + + match (last, next) { + (Direction::Up, Direction::Right) + | (Direction::Right, Direction::Down) + | (Direction::Down, Direction::Left) + | (Direction::Left, Direction::Up) => 1, + (Direction::Right, Direction::Up) + | (Direction::Down, Direction::Right) + | (Direction::Left, Direction::Down) + | (Direction::Up, Direction::Left) => -1, + _ => panic!("should not happen!"), + } +} +impl Direction { + fn get_delta(&self) -> MatrixIdx { + match self { + Direction::Up => MatrixIdx { row: -1, col: 0 }, + Direction::Down => MatrixIdx { row: 1, col: 0 }, + Direction::Left => MatrixIdx { row: 0, col: -1 }, + Direction::Right => MatrixIdx { row: 0, col: 1 }, + } + } + pub fn right(&self) -> Self { + match self { + Direction::Up => Direction::Right, + Direction::Down => Direction::Left, + Direction::Left => Direction::Up, + Direction::Right => Direction::Down, + } + } +} + +pub trait FromChar { + fn from_char(char: &char) -> Self; + fn default() -> Self; +} +pub trait MatrixElement: FromChar + Clone {} + +impl FromChar for i64 { + fn from_char(char: &char) -> Self { + *char as i64 - '0' as i64 + } + fn default() -> Self { + 0 + } +} +impl MatrixElement for i64 {} +pub struct Matrix +where + T: MatrixElement, +{ + data: Vec, + width: i64, +} +impl Index for Matrix +where + T: MatrixElement, +{ + type Output = T; + + fn index(&self, index: MatrixIdx) -> &Self::Output { + &self.data[self.linidx(&index)] + } +} + +impl IndexMut for Matrix +where + T: MatrixElement, +{ + fn index_mut(&mut self, index: MatrixIdx) -> &mut Self::Output { + self.data.index_mut(self.linidx(&index)) + } +} + +impl Mul for MatrixIdx { + type Output = MatrixIdx; + fn mul(self, rhs: i64) -> Self::Output { + MatrixIdx { + row: self.row * rhs, + col: self.col * rhs, + } + } +} +impl Add for MatrixIdx { + type Output = MatrixIdx; + fn add(self, rhs: i64) -> Self::Output { + MatrixIdx { + row: self.row + rhs, + col: self.col + rhs, + } + } +} +impl Add for MatrixIdx { + type Output = MatrixIdx; + fn add(self, rhs: MatrixIdx) -> Self::Output { + MatrixIdx { + row: self.row + rhs.row, + col: self.col + rhs.col, + } + } +} +impl Add<&MatrixIdx> for MatrixIdx { + type Output = MatrixIdx; + fn add(self, rhs: &MatrixIdx) -> Self::Output { + MatrixIdx { + row: self.row + rhs.row, + col: self.col + rhs.col, + } + } +} + +impl Matrix +where + T: MatrixElement, +{ + fn linidx(&self, idx: &MatrixIdx) -> usize { + (idx.row * self.width + idx.col) as usize + } + fn from_string(input: &str) -> Self { + let mut data = Vec::new(); + let mut width = 0; + for line in input.lines() { + width = line.len() as i64; + for c in line.chars() { + data.push(T::from_char(&c)); + } + } + + Self { data, width } + } + fn empty(nrows: usize, ncols: usize) -> Self { + Self { + data: vec![T::default(); nrows * ncols], + width: ncols as i64, + } + } + fn row(&self, idx: i64) -> Option> { + if idx >= self.height() { + None + } else { + let start = idx * self.width; + let stop = (idx + 1) * self.width; + Some(self.data[start as usize..stop as usize].to_owned()) + } + } + // fn rows(&self) -> FilterMap,Vec> { + // (0..self.height()).filter_map(|idx| self.row(idx)) + // } + fn height(&self) -> i64 { + self.data.len() as i64 / self.width + } + fn next(&self, idx: &MatrixIdx, direction: &Direction) -> Option { + let n = self.height() - 1; + let m = self.width - 1; + let MatrixIdx { row, col } = idx; + if (row == &0 && direction == &Direction::Up) + || (row == &n && direction == &Direction::Down) + || (col == &0 && direction == &Direction::Left) + || (col == &m && direction == &Direction::Right) + { + None + } else { + let (row, col) = match direction { + Direction::Down => (row + 1, *col), + Direction::Up => (row - 1, *col), + Direction::Left => (*row, col - 1), + Direction::Right => (*row, col + 1), + }; + Some(MatrixIdx { row, col }) + } + } +} + +fn print_matrix(matrix: &Matrix) { + for i in 0..matrix.height() { + let line = matrix.row(i).unwrap(); + for char in line { + print!("{}", char); + } + println!(); + } +}