From 8145a3087c664ba0f946d3571d05d4ccd3f77303 Mon Sep 17 00:00:00 2001 From: endernon Date: Wed, 4 Dec 2024 23:43:35 +0000 Subject: [PATCH] set the boundaries for width --- 2024/day4/part1/src/main.rs | 12 ++-- 2024/day4/part2/Cargo.lock | 7 ++ 2024/day4/part2/Cargo.toml | 6 ++ 2024/day4/part2/src/main.rs | 140 ++++++++++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+), 6 deletions(-) create mode 100644 2024/day4/part2/Cargo.lock create mode 100644 2024/day4/part2/Cargo.toml create mode 100644 2024/day4/part2/src/main.rs diff --git a/2024/day4/part1/src/main.rs b/2024/day4/part1/src/main.rs index 5d5c4a5..d41614b 100644 --- a/2024/day4/part1/src/main.rs +++ b/2024/day4/part1/src/main.rs @@ -33,25 +33,25 @@ fn main() { let leny = frfrvec.clone().len(); for y in 1..leny { - let widthup = y >= 4; - let widthdown = (leny - y) >= 4; + let widthup = y >= 2; + let widthdown = (leny - y) >= 2; for x in 1..lenx+1 { // check if enough space left and right - let widthleft = x >= 4; + let widthleft = x >= 2; - let widthright = (lenx - x) >= 3; + let widthright = (lenx - x) >= 1; // search algorithm starts here // check if the char is X - if frfrvec[y][x] == 'X' { + if frfrvec[y][x] == 'A' { println!("x is {x}"); println!("y is {y}"); println!("width up: {}", widthup); println!("width down: {}", widthdown); println!("width left: {}", widthleft); - // check if going upwards + // check if start from top if widthup { if frfrvec[y - 1][x] == 'M' && frfrvec[y - 2][x] == 'A' diff --git a/2024/day4/part2/Cargo.lock b/2024/day4/part2/Cargo.lock new file mode 100644 index 0000000..e73b84f --- /dev/null +++ b/2024/day4/part2/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "part1" +version = "0.1.0" diff --git a/2024/day4/part2/Cargo.toml b/2024/day4/part2/Cargo.toml new file mode 100644 index 0000000..d85b608 --- /dev/null +++ b/2024/day4/part2/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "part1" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/2024/day4/part2/src/main.rs b/2024/day4/part2/src/main.rs new file mode 100644 index 0000000..5d5c4a5 --- /dev/null +++ b/2024/day4/part2/src/main.rs @@ -0,0 +1,140 @@ +use std::fs::read_to_string; + +fn main() { + let mut pointcounter = 0; + + let lines: Vec = read_to_string("input.txt") + .unwrap() + .lines() + .map(String::from) + .collect(); + let mut frfrvec: Vec> = Vec::new(); + println!("ye the lines are {:?}", lines); + + // make y coord offset by 1 + let lenxold = lines[1].chars().count(); + let mut lenvec: Vec = Vec::new(); + for i in 0..lenxold { + lenvec.push('0'); + } + frfrvec.push(lenvec); + + // main section where you push stuff + for ministr in lines.clone() { + let mut pushvec = Vec::new(); + pushvec.push('0'); // make x coord offset by 1 + for minichar in ministr.chars() { + pushvec.push(minichar); + } + frfrvec.push(pushvec); + } + println!("{:?}", frfrvec); + let lenx = lines[1].chars().count(); + let leny = frfrvec.clone().len(); + + for y in 1..leny { + let widthup = y >= 4; + let widthdown = (leny - y) >= 4; + + for x in 1..lenx+1 { + // check if enough space left and right + + let widthleft = x >= 4; + + let widthright = (lenx - x) >= 3; + + // search algorithm starts here + // check if the char is X + if frfrvec[y][x] == 'X' { + println!("x is {x}"); + println!("y is {y}"); + println!("width up: {}", widthup); + println!("width down: {}", widthdown); + println!("width left: {}", widthleft); + // check if going upwards + if widthup { + if frfrvec[y - 1][x] == 'M' + && frfrvec[y - 2][x] == 'A' + && frfrvec[y - 3][x] == 'S' + { + pointcounter += 1; + println!("successful up"); + } + } + // check if going diag up left + if widthup && widthleft { + if frfrvec[y - 1][x - 1] == 'M' + && frfrvec[y - 2][x - 2] == 'A' + && frfrvec[y - 3][x - 3] == 'S' + { + pointcounter += 1; + println!("successful up left"); + } + } + // check if going left + if widthleft { + if frfrvec[y][x - 1] == 'M' + && frfrvec[y][x - 2] == 'A' + && frfrvec[y][x - 3] == 'S' + { + pointcounter += 1; + println!("successful left"); + } + } + // check if going diag down left + if widthdown && widthleft { + if frfrvec[y + 1][x - 1] == 'M' + && frfrvec[y + 2][x - 2] == 'A' + && frfrvec[y + 3][x - 3] == 'S' + { + pointcounter += 1; + println!("successful down left"); + } + } + // check if going down + if widthdown { + if frfrvec[y + 1][x] == 'M' + && frfrvec[y + 2][x] == 'A' + && frfrvec[y + 3][x] == 'S' + { + pointcounter += 1; + println!("successful down"); + } + } + // check if going diag down right + if widthdown && widthright { + if frfrvec[y + 1][x + 1] == 'M' + && frfrvec[y + 2][x + 2] == 'A' + && frfrvec[y + 3][x + 3] == 'S' + { + pointcounter += 1; + println!("successful down right"); + } + } + // check if going right + if widthright { + if frfrvec[y][x + 1] == 'M' + && frfrvec[y][x + 2] == 'A' + && frfrvec[y][x + 3] == 'S' + { + pointcounter += 1; + println!("successful right"); + } + } + // check if going diag right up + if widthright && widthup { + if frfrvec[y - 1][x + 1] == 'M' + && frfrvec[y - 2][x + 2] == 'A' + && frfrvec[y - 3][x + 3] == 'S' + { + pointcounter += 1; + println!("successful up right"); + } + } + } + println!("\n") + } + } + + println!("{pointcounter}"); +}