From 72b1f346b9b03de2f91387464713859d9b494a8b Mon Sep 17 00:00:00 2001 From: Ishan Jain Date: Mon, 18 Dec 2023 14:18:12 +0530 Subject: [PATCH] day18: added/optimized --- src/day18/1.rs | 61 ++++- src/day18/2.rs | 75 +++++- src/day18/input.txt | 624 +++++++++++++++++++++++++++++++++++++++++++ src/day18/sample.txt | 14 + 4 files changed, 772 insertions(+), 2 deletions(-) create mode 100644 src/day18/input.txt create mode 100644 src/day18/sample.txt diff --git a/src/day18/1.rs b/src/day18/1.rs index b51073e..ee43716 100644 --- a/src/day18/1.rs +++ b/src/day18/1.rs @@ -1 +1,60 @@ -const INPUTS: [&'static str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; +#![feature(test)] +extern crate test; + +const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; + +fn process(data: &str) -> i64 { + let mut answer = 0; + let mut internal = 0; + + let (mut sx, mut sy) = (0, 0); + + for line in data.lines() { + let line = line.as_bytes(); + let l = line.len(); + + let line = &line[0..l - 10]; + let dist = { + let mut dist = 0; + let mut power = 10i64.pow(line.len() as u32 - 3); + for c in line[2..].iter() { + dist += power * (c - b'0') as i64; + power /= 10; + } + dist + }; + + let (x, y) = match line[0] { + b'R' => (0, 1), + b'D' => (1, 0), + b'L' => (0, -1), + b'U' => (-1, 0), + _ => unreachable!(), + }; + + internal += ((x * dist) + (y * dist)).abs(); + let (dx, dy) = (sx + (x * dist), sy + (y * dist)); + + answer += sx * dy - dx * sy; + sx = dx; + sy = dy; + } + + let answer = answer.abs(); + + ((answer / 2) + 1) + (internal / 2) +} + +fn main() { + for input in INPUTS.iter() { + println!("answer = {}", process(input)); + } +} + +#[bench] +fn part1(b: &mut test::Bencher) { + b.iter(|| { + let v = process(INPUTS[INPUTS.len() - 1]); + test::black_box(v); + }); +} diff --git a/src/day18/2.rs b/src/day18/2.rs index b51073e..d798554 100644 --- a/src/day18/2.rs +++ b/src/day18/2.rs @@ -1 +1,74 @@ -const INPUTS: [&'static str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; +#![feature(test)] +extern crate test; + +const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; + +fn process(data: &str) -> i64 { + let mut answer = 0; + let (mut sx, mut sy) = (0, 0); + let mut internal = 0; + + for line in data.lines() { + let line = line.as_bytes(); + let l = line.len(); + + let line = &line[l - 6 - 1..line.len() - 1]; + + let dist = hex2int(&line[0..5]); + + let (x, y): (i64, i64) = match line[5] { + b'0' => (0, 1), + b'1' => (1, 0), + b'2' => (0, -1), + b'3' => (-1, 0), + _ => unreachable!(), + }; + + internal += ((x * dist) + (y * dist)).abs(); + let (dx, dy) = (sx + (x * dist), sy + (y * dist)); + + answer += sx * dy - dx * sy; + sx = dx; + sy = dy; + } + + let answer = answer.abs(); + + ((answer / 2) + 1) + (internal / 2) +} + +#[inline] +const fn hex2int(ip: &[u8]) -> i64 { + let mut out = 0; + + let mut i = 0; + while i < ip.len() { + let byte = match ip[i] { + v @ b'0'..=b'9' => v - b'0', + v @ b'a'..=b'f' => v - b'a' + 10, + v @ b'A'..=b'F' => v - b'A' + 10, + _ => unreachable!(), + }; + + out <<= 4; + out |= byte as i64; + + i += 1; + } + + out +} + +fn main() { + for input in INPUTS.iter() { + println!("answer = {}", process(input)); + } +} + +#[bench] +fn part2(b: &mut test::Bencher) { + b.iter(|| { + let v = process(INPUTS[INPUTS.len() - 1]); + test::black_box(v); + }); +} diff --git a/src/day18/input.txt b/src/day18/input.txt new file mode 100644 index 0000000..281765a --- /dev/null +++ b/src/day18/input.txt @@ -0,0 +1,624 @@ +R 4 (#5ca782) +U 6 (#79d133) +R 8 (#90d7f2) +U 3 (#13cc11) +R 3 (#57f4c0) +U 4 (#b45f81) +R 4 (#234cb2) +U 2 (#2538d1) +R 6 (#acb112) +U 6 (#78f4b1) +R 3 (#48a010) +U 10 (#515b51) +R 5 (#875db0) +U 6 (#b4e871) +R 2 (#57f4c2) +U 4 (#0e2931) +R 4 (#75a752) +U 2 (#2d79f3) +R 4 (#1130c2) +U 4 (#2d79f1) +L 10 (#6e3da2) +U 3 (#87e5e3) +L 3 (#366410) +U 5 (#2dc7b3) +L 2 (#78d162) +U 5 (#e262a1) +R 7 (#17ac42) +U 3 (#e262a3) +R 5 (#635eb2) +U 3 (#69fb03) +R 3 (#101b10) +U 5 (#b0f563) +R 4 (#79b6e0) +U 2 (#b0f561) +R 3 (#6a0a60) +U 10 (#912f03) +R 3 (#366412) +U 7 (#302683) +R 5 (#c46a92) +D 6 (#99c7f3) +R 7 (#0aba12) +D 2 (#4bdfc3) +R 4 (#4eb3c2) +D 8 (#4f2b93) +R 3 (#99d312) +D 3 (#4f2b91) +R 7 (#115f42) +U 6 (#85bef3) +R 8 (#b2c2e0) +U 6 (#05e021) +R 3 (#5095a0) +D 4 (#05e023) +R 4 (#d67ee0) +D 5 (#7b9f03) +L 4 (#513952) +D 3 (#711513) +R 3 (#2561f2) +D 3 (#5699d3) +L 3 (#422e92) +D 2 (#180d43) +L 9 (#824592) +D 2 (#16a7f3) +L 7 (#3ef900) +D 3 (#b45793) +L 3 (#3ef902) +D 3 (#37b143) +L 4 (#68ebc2) +D 4 (#575041) +R 8 (#0776e2) +D 4 (#508391) +R 4 (#5a7180) +D 4 (#73de71) +R 10 (#5a7182) +D 3 (#336151) +R 4 (#0776e0) +D 4 (#224441) +R 2 (#35dc42) +D 8 (#9efca3) +R 4 (#ae0b52) +U 5 (#1c8623) +R 3 (#936a12) +U 5 (#0747c3) +R 8 (#0be1b2) +U 7 (#6892e3) +R 4 (#4b2442) +U 6 (#1bbc83) +R 8 (#c3a9b2) +U 7 (#6f5891) +R 4 (#2698d2) +U 3 (#241921) +R 3 (#33c282) +U 2 (#234ef1) +R 3 (#349fc2) +D 10 (#9038a1) +R 5 (#804490) +U 10 (#be6041) +R 4 (#804492) +U 2 (#107031) +R 3 (#349fc0) +U 5 (#a3c331) +L 3 (#007b22) +U 2 (#775fe1) +L 8 (#753eb2) +U 5 (#5a2f83) +L 3 (#58a392) +U 2 (#67c2f3) +L 4 (#650412) +U 7 (#c1f271) +L 5 (#218ac2) +U 8 (#775fe3) +L 4 (#246d02) +D 3 (#9e1483) +L 3 (#0c7de0) +D 6 (#088e23) +L 6 (#80ba52) +U 6 (#0ee173) +L 5 (#137bb0) +U 3 (#2b1473) +L 5 (#3461e2) +D 3 (#0cc673) +L 3 (#874222) +D 5 (#863193) +L 7 (#874220) +U 3 (#7db443) +L 3 (#3461e0) +U 5 (#2298c3) +L 2 (#137bb2) +U 9 (#2e3dd3) +R 2 (#80ba50) +U 4 (#9fc773) +R 3 (#a23ba0) +D 7 (#977673) +R 3 (#611a50) +D 5 (#200cb3) +R 4 (#39d100) +U 8 (#5ba623) +R 2 (#72df42) +U 4 (#ad4d53) +R 5 (#72df40) +D 4 (#109f33) +R 10 (#156490) +U 4 (#05a421) +R 10 (#151e60) +U 4 (#c8aa61) +R 4 (#915270) +U 5 (#6b50d1) +R 4 (#09c930) +U 7 (#82ee83) +R 2 (#5dbf10) +U 6 (#a57ad3) +R 5 (#6a8e10) +U 4 (#6a0bc3) +R 3 (#2ea192) +U 5 (#6a1103) +R 6 (#2ea190) +U 6 (#1b2583) +R 6 (#5d55a0) +D 7 (#bd5831) +R 5 (#a47990) +D 5 (#3779a1) +R 9 (#180f20) +D 2 (#e21591) +R 5 (#36c4b0) +D 4 (#40c431) +L 10 (#495330) +D 4 (#3cf2c3) +R 10 (#80a260) +D 5 (#680673) +R 4 (#a20f32) +U 9 (#6d6bf3) +R 4 (#a20f30) +U 4 (#21b1d3) +R 3 (#390070) +D 7 (#6e3ef3) +L 4 (#d53000) +D 5 (#7e9633) +R 2 (#d53002) +D 6 (#1e6f03) +R 2 (#10bf70) +D 6 (#19d9c3) +R 9 (#5ac080) +D 2 (#ac2383) +R 9 (#660c72) +D 4 (#1889f3) +R 5 (#5b3242) +D 4 (#03ca63) +L 5 (#07b580) +D 4 (#bbcd23) +R 9 (#b98930) +D 6 (#36db03) +L 9 (#681ea0) +D 4 (#1fc5e3) +L 5 (#6e2cb0) +U 9 (#81eb61) +L 2 (#5f6120) +U 5 (#86ed81) +L 5 (#7a2940) +D 2 (#4c4981) +L 5 (#033720) +D 6 (#9f9d31) +L 3 (#91c310) +D 7 (#be82a3) +L 2 (#1ddea0) +D 5 (#157ac3) +R 4 (#d75c30) +D 3 (#1c5083) +R 2 (#507330) +D 3 (#aa1b53) +R 6 (#5dc6b0) +D 4 (#6bdf73) +L 6 (#8935b0) +D 5 (#4bd1b3) +R 3 (#d0e220) +D 3 (#6d4633) +R 3 (#8d25e2) +D 5 (#3a9593) +R 3 (#ce32c2) +D 8 (#400cb3) +R 4 (#6654a0) +D 6 (#7465d3) +R 6 (#1daa50) +D 4 (#7465d1) +R 3 (#6c9720) +D 4 (#5b4e83) +R 6 (#3e8190) +D 7 (#3b1c93) +L 6 (#2c4100) +D 6 (#57c2b3) +L 3 (#7b8e70) +D 4 (#187083) +L 7 (#2d3150) +D 3 (#8a55d3) +L 3 (#088240) +D 4 (#61e153) +R 7 (#96b800) +D 4 (#d3d763) +R 5 (#3b9e60) +D 7 (#8bd823) +R 2 (#d4b570) +D 3 (#98db33) +R 6 (#2579c0) +D 3 (#746983) +R 6 (#a82662) +U 2 (#af7991) +R 5 (#688d32) +U 6 (#af7993) +R 5 (#721f12) +U 5 (#a8fb13) +R 7 (#084200) +U 3 (#b1f903) +R 9 (#8eba60) +U 6 (#0daa83) +R 4 (#4dc7e0) +U 3 (#bfa381) +L 10 (#9e0e60) +U 5 (#774113) +L 2 (#2579c2) +U 4 (#6c0423) +L 4 (#4f2000) +U 3 (#c57e41) +R 7 (#8f4370) +D 4 (#68dee1) +R 3 (#99c890) +U 4 (#831fa3) +R 6 (#5ee090) +U 4 (#831fa1) +L 5 (#5efdf0) +U 2 (#c5acc1) +L 8 (#382f90) +U 3 (#781e41) +R 4 (#4c9430) +U 2 (#1274f3) +R 8 (#33cb92) +D 2 (#cfa303) +R 4 (#33cb90) +D 4 (#5bb313) +R 4 (#671a50) +D 6 (#7bd3c1) +R 3 (#3c4db0) +D 4 (#45d821) +R 7 (#9a3600) +D 3 (#6c6db1) +R 2 (#3029b0) +D 7 (#0df2c1) +R 2 (#0a9690) +D 3 (#0bece1) +R 4 (#191ca0) +D 4 (#9524f1) +R 3 (#ac57e0) +D 2 (#499f11) +R 7 (#7d41d2) +D 2 (#d183f1) +R 4 (#365eb2) +D 3 (#298241) +R 4 (#b3a080) +D 8 (#6a7a71) +R 4 (#1435a0) +D 5 (#969391) +R 2 (#953330) +D 9 (#969393) +R 4 (#55cac0) +D 8 (#669863) +R 6 (#551580) +D 2 (#746923) +R 2 (#e56c90) +D 7 (#4a38c3) +R 5 (#e56c92) +U 6 (#2d75a3) +R 2 (#551582) +U 6 (#4f41d3) +R 5 (#020350) +D 6 (#a252f3) +R 4 (#a3ea10) +D 6 (#8ace11) +R 3 (#ba1110) +D 3 (#6aef11) +L 4 (#001ab0) +D 3 (#6c0921) +L 6 (#cc0fc0) +D 5 (#86b143) +R 8 (#5ebe50) +D 2 (#5046f3) +R 8 (#24a770) +D 3 (#e63171) +L 5 (#013f50) +D 3 (#d24e81) +L 4 (#8aa4b0) +D 2 (#70a8c1) +L 7 (#5c4072) +D 3 (#a3dcd1) +L 7 (#5c4070) +U 4 (#d231b1) +L 3 (#096312) +U 4 (#95ee41) +R 5 (#51d6b2) +U 7 (#a57861) +L 5 (#51d6b0) +U 3 (#189ad1) +L 3 (#6ccb30) +D 2 (#a0a771) +L 5 (#6ccb32) +D 3 (#2e0d11) +L 3 (#026ee2) +D 5 (#dc1313) +L 9 (#91e212) +D 4 (#788961) +L 3 (#0ad8e2) +D 4 (#831c71) +R 3 (#91bd42) +D 2 (#1b8e51) +R 6 (#2a4602) +D 5 (#677ca1) +R 6 (#8726b2) +D 4 (#0b47d1) +L 6 (#488d42) +D 3 (#9ee8a1) +L 6 (#488d40) +D 3 (#48a5a1) +L 6 (#670ad0) +D 6 (#beb191) +L 5 (#670ad2) +D 5 (#be0a21) +R 3 (#7a29e2) +D 6 (#e6aa11) +R 7 (#58bc92) +D 8 (#2cfe51) +L 7 (#477c02) +D 8 (#a5b351) +L 3 (#673772) +D 3 (#aa4693) +L 3 (#091692) +U 5 (#71dd41) +L 6 (#1d84f2) +U 4 (#295091) +L 4 (#be5bc2) +U 3 (#9b2dd3) +L 4 (#cfee42) +U 4 (#3f6493) +R 5 (#1e1a50) +U 9 (#066d33) +L 5 (#6621a0) +U 4 (#841dc3) +L 7 (#be46f0) +U 4 (#841dc1) +L 5 (#7262a0) +U 3 (#c94363) +L 5 (#397ab2) +U 3 (#5374e3) +L 7 (#90d0e2) +D 4 (#5e3043) +L 6 (#d38892) +D 7 (#18f6f3) +L 8 (#41ea42) +D 5 (#c43043) +L 10 (#9cbe22) +D 5 (#979ff3) +R 5 (#658930) +D 9 (#c235e3) +R 4 (#32e980) +D 3 (#076193) +R 5 (#98a460) +D 4 (#8f9c83) +L 5 (#5f4180) +D 11 (#96fe11) +L 2 (#9f8cc0) +U 11 (#549153) +L 5 (#131c80) +D 2 (#b10f13) +L 2 (#53cd72) +D 4 (#dc1311) +L 8 (#0a8fb2) +D 4 (#7fcf03) +L 5 (#269722) +D 6 (#2233f3) +L 7 (#d9bd72) +D 5 (#2233f1) +L 2 (#118912) +D 3 (#343c53) +L 5 (#750cc0) +U 5 (#5ac783) +L 3 (#531090) +U 7 (#5ac781) +L 3 (#49c050) +D 4 (#741333) +L 4 (#1c8f32) +D 8 (#061ca3) +L 4 (#350ad2) +D 2 (#10a3c3) +L 8 (#bdc742) +D 4 (#614513) +L 5 (#41c4d2) +D 3 (#829203) +L 6 (#988f42) +D 3 (#ad64c1) +L 3 (#2280d2) +U 4 (#3de311) +L 3 (#6d2930) +U 2 (#b10451) +L 9 (#282010) +U 4 (#3d0731) +L 7 (#845e22) +U 4 (#2237f1) +L 5 (#8b2812) +U 5 (#983263) +R 7 (#977292) +U 7 (#742c43) +R 6 (#77f762) +U 4 (#3df793) +L 4 (#4a8312) +U 5 (#632a53) +L 2 (#45db72) +U 7 (#83b681) +L 3 (#937372) +D 12 (#d016e1) +L 4 (#8f5232) +U 5 (#59b321) +L 4 (#4e3af2) +U 9 (#5d5b01) +L 3 (#dc9200) +U 3 (#59aaa1) +R 5 (#dc9202) +U 8 (#50c7e1) +R 6 (#8f9cb2) +U 6 (#d770a3) +R 8 (#9bba62) +D 6 (#5294d3) +R 3 (#021cc2) +U 6 (#bd5323) +R 8 (#920862) +U 3 (#bd5321) +L 8 (#70e1b2) +U 5 (#764791) +L 6 (#077692) +U 6 (#6a2b11) +L 3 (#5f9b32) +U 4 (#2cbef1) +L 5 (#5f9b30) +D 2 (#666931) +L 2 (#7e7f32) +D 10 (#31e6a3) +L 2 (#086bc2) +D 3 (#97fb53) +L 4 (#0bc712) +U 9 (#29e693) +L 3 (#b01952) +U 2 (#c1e1e1) +L 5 (#9ee102) +D 4 (#31e6a1) +L 5 (#52f962) +D 8 (#c8d7e1) +L 6 (#6b34b0) +D 5 (#191591) +L 8 (#4b7020) +D 2 (#03d891) +L 5 (#6b49a0) +D 4 (#dc8871) +L 3 (#3cd680) +D 6 (#0836d1) +L 5 (#1148f0) +D 3 (#60f903) +L 3 (#730dc0) +D 4 (#873093) +L 9 (#3a2890) +D 4 (#006e43) +L 3 (#176180) +U 6 (#66eea1) +L 4 (#077690) +U 3 (#51bad1) +R 8 (#b70b00) +U 7 (#2beee1) +R 3 (#a1e5d0) +U 5 (#18d051) +L 11 (#6b60e0) +U 4 (#0d97a1) +L 5 (#601240) +D 5 (#748cc1) +L 3 (#3ac062) +D 4 (#122871) +R 5 (#3ac060) +D 11 (#6bf481) +L 5 (#601242) +D 5 (#263f51) +L 3 (#0695c0) +U 7 (#2a4c51) +L 4 (#8a21a0) +D 8 (#75a591) +L 8 (#72b020) +U 8 (#4ca4b1) +L 3 (#09d770) +U 3 (#63d501) +R 5 (#26f910) +U 2 (#4b7b81) +R 10 (#9ffc40) +U 3 (#0a7ef1) +L 4 (#894270) +U 3 (#78f8f1) +L 2 (#2f9e10) +U 6 (#103a51) +L 4 (#6d8430) +U 4 (#8ed851) +L 5 (#b91d00) +U 5 (#0b6e71) +R 3 (#3466a0) +U 3 (#47c791) +R 8 (#34d510) +U 2 (#e20e53) +R 3 (#6f69c0) +U 6 (#4eedf1) +L 4 (#5e7dc2) +U 2 (#6850a1) +L 8 (#00f422) +D 7 (#3d80c1) +L 9 (#1d3cd2) +U 7 (#74e111) +L 8 (#c959f2) +U 2 (#74e113) +L 3 (#1b0e22) +U 9 (#4dbd41) +R 5 (#c18852) +U 6 (#337961) +R 5 (#559b42) +U 4 (#37eb81) +R 3 (#83e362) +U 6 (#730011) +R 6 (#622d42) +U 10 (#9357b3) +R 5 (#7bb662) +U 5 (#1793e3) +R 8 (#8d1892) +U 6 (#6b4043) +L 6 (#79f3b2) +U 3 (#61e3a1) +L 8 (#5d9b82) +U 5 (#8f3cc1) +L 3 (#6b58f2) +U 8 (#5384b1) +L 2 (#22cff2) +U 4 (#756671) +L 4 (#89c1c2) +D 12 (#5d5f21) +L 4 (#0f58c2) +D 5 (#078e81) +R 5 (#99f7a2) +D 4 (#520851) +R 8 (#8f2762) +D 5 (#539c23) +L 8 (#80b8a2) +D 5 (#554ee1) +L 5 (#566802) +D 8 (#9ae051) +L 3 (#566800) +U 3 (#46d531) +L 3 (#7f19a2) +U 7 (#074733) +L 4 (#a639d2) +U 5 (#278b83) +L 4 (#2636b2) +D 6 (#0b9fa3) +L 2 (#09a9b0) +D 6 (#b62373) +L 4 (#bbdce0) +U 5 (#158a33) +L 4 (#06e9f0) +U 11 (#30e473) +R 5 (#404bc2) +U 3 (#681dc3) +R 10 (#6b04d0) +U 2 (#932813) +R 6 (#a5e770) +U 3 (#932811) +L 4 (#2f31c0) +U 5 (#8f8a13) +R 7 (#238d72) +U 3 (#347933) +L 7 (#2ca2f2) +U 5 (#4fe7e3) +L 3 (#74c422) +D 10 (#4fe7e1) +L 2 (#4a8a02) +D 3 (#8c6563) +L 4 (#4c8982) +U 2 (#64def3) +L 5 (#21f8b2) +U 10 (#88fd83) diff --git a/src/day18/sample.txt b/src/day18/sample.txt new file mode 100644 index 0000000..0ad754b --- /dev/null +++ b/src/day18/sample.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