y2024d4
This commit is contained in:
20
y2024/src/bin/d4.rs
Normal file
20
y2024/src/bin/d4.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use std::fs;
|
||||
|
||||
use y2024::days::d4;
|
||||
|
||||
fn main() {
|
||||
part1();
|
||||
part2();
|
||||
}
|
||||
|
||||
fn part1() {
|
||||
let root = env!("CARGO_MANIFEST_DIR");
|
||||
let content = fs::read_to_string(format!("{root}/resources/4_input.txt")).unwrap();
|
||||
println!("{}", d4::process_part1(&content));
|
||||
}
|
||||
|
||||
fn part2() {
|
||||
let root = env!("CARGO_MANIFEST_DIR");
|
||||
let content = fs::read_to_string(format!("{root}/resources/4_input.txt")).unwrap();
|
||||
println!("{}", d4::process_part2(&content));
|
||||
}
|
||||
164
y2024/src/days/d4.rs
Normal file
164
y2024/src/days/d4.rs
Normal file
@@ -0,0 +1,164 @@
|
||||
use std::cmp;
|
||||
|
||||
use utils::grid::Grid;
|
||||
|
||||
pub fn process_part1(input: &str) -> i32 {
|
||||
let grid = input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
line.chars()
|
||||
.map(|char| char.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
})
|
||||
.collect::<Vec<Vec<String>>>();
|
||||
let mut grid = Grid::new(grid).unwrap();
|
||||
let mut res = 0;
|
||||
for row in &grid.rows {
|
||||
let row = row.join("");
|
||||
res += row.matches("XMAS").count();
|
||||
res += row.matches("SAMX").count();
|
||||
}
|
||||
for diag_grave in grid.get_diagonals_grave() {
|
||||
let diag_grave = diag_grave.join("");
|
||||
res += diag_grave.matches("XMAS").count();
|
||||
res += diag_grave.matches("SAMX").count();
|
||||
}
|
||||
for diag_aigu in grid.get_diagonals_aigu() {
|
||||
let diag_aigu = diag_aigu.join("");
|
||||
res += diag_aigu.matches("XMAS").count();
|
||||
res += diag_aigu.matches("SAMX").count();
|
||||
}
|
||||
for col in grid.rotate_90().rows {
|
||||
let col = col.join("");
|
||||
res += col.matches("XMAS").count();
|
||||
res += col.matches("SAMX").count();
|
||||
}
|
||||
res.try_into().unwrap()
|
||||
}
|
||||
|
||||
pub fn process_part2(input: &str) -> i32 {
|
||||
let grid = input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
line.chars()
|
||||
.map(|char| char.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
})
|
||||
.collect::<Vec<Vec<String>>>();
|
||||
let grid = Grid::new(grid).unwrap();
|
||||
let mut res = 0;
|
||||
// Irrelevant as this finds + not x
|
||||
// But actually helpful as I can just create diagonals, left and right pad to create a square
|
||||
// grid and apply this
|
||||
//for (row_idx, row) in grid.rows.iter().enumerate() {
|
||||
// if row_idx == 0 || row_idx == grid.rows.len() - 1 {
|
||||
// continue;
|
||||
// }
|
||||
// let row = row.join("");
|
||||
// for (match_idx, _) in row.match_indices("MAS") {
|
||||
// let above = grid.rows[row_idx - 1][match_idx + 1].clone();
|
||||
// let below = grid.rows[row_idx + 1][match_idx + 1].clone();
|
||||
// if (above == "M" && below == "S") || (above == "S" && below == "M") {
|
||||
// res += 1;
|
||||
// }
|
||||
// }
|
||||
// for (match_idx, _) in row.match_indices("SAM") {
|
||||
// let above = grid.rows[row_idx - 1][match_idx + 1].clone();
|
||||
// let below = grid.rows[row_idx + 1][match_idx + 1].clone();
|
||||
// if (above == "M" && below == "S") || (above == "S" && below == "M") {
|
||||
// res += 1;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
let diagonals = grid.get_diagonals_grave();
|
||||
// new width is as long as the longest diagonal
|
||||
// the diagonal length goes from 1..min(grid width, grid height)
|
||||
// then pad on each side by difference / 2
|
||||
let new_width = cmp::min(grid.width as usize, grid.rows.len());
|
||||
let mut even_grid = Vec::new();
|
||||
let mut odd_grid = Vec::new();
|
||||
for diagonal in diagonals {
|
||||
let diagonal_length = diagonal.len();
|
||||
let pad = (new_width - diagonal_length) / 2;
|
||||
let new_row = [
|
||||
vec!["#".to_string(); pad],
|
||||
diagonal,
|
||||
vec!["#".to_string(); pad],
|
||||
]
|
||||
.concat();
|
||||
if diagonal_length % 2 == 0 {
|
||||
even_grid.push(new_row);
|
||||
} else {
|
||||
odd_grid.push(new_row);
|
||||
}
|
||||
}
|
||||
for (row_idx, row) in even_grid.iter().enumerate() {
|
||||
if row_idx == 0 || row_idx == even_grid.len() - 1 {
|
||||
continue;
|
||||
}
|
||||
let row = row.join("");
|
||||
for (match_idx, _) in row.match_indices("MAS") {
|
||||
let above = even_grid[row_idx - 1][match_idx + 1].clone();
|
||||
let below = even_grid[row_idx + 1][match_idx + 1].clone();
|
||||
if (above == "M" && below == "S") || (above == "S" && below == "M") {
|
||||
res += 1;
|
||||
}
|
||||
}
|
||||
for (match_idx, _) in row.match_indices("SAM") {
|
||||
let above = even_grid[row_idx - 1][match_idx + 1].clone();
|
||||
let below = even_grid[row_idx + 1][match_idx + 1].clone();
|
||||
if (above == "M" && below == "S") || (above == "S" && below == "M") {
|
||||
res += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (row_idx, row) in odd_grid.iter().enumerate() {
|
||||
if row_idx == 0 || row_idx == odd_grid.len() - 1 {
|
||||
continue;
|
||||
}
|
||||
let row = row.join("");
|
||||
for (match_idx, _) in row.match_indices("MAS") {
|
||||
let above = odd_grid[row_idx - 1][match_idx + 1].clone();
|
||||
let below = odd_grid[row_idx + 1][match_idx + 1].clone();
|
||||
if (above == "M" && below == "S") || (above == "S" && below == "M") {
|
||||
res += 1;
|
||||
}
|
||||
}
|
||||
for (match_idx, _) in row.match_indices("SAM") {
|
||||
let above = odd_grid[row_idx - 1][match_idx + 1].clone();
|
||||
let below = odd_grid[row_idx + 1][match_idx + 1].clone();
|
||||
if (above == "M" && below == "S") || (above == "S" && below == "M") {
|
||||
res += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const INPUT: &str = "MMMSXXMASM
|
||||
MSAMXMSMSA
|
||||
AMXSXMAAMM
|
||||
MSAMASMSMX
|
||||
XMASAMXAMM
|
||||
XXAMMXXAMA
|
||||
SMSMSASXSS
|
||||
SAXAMASAAA
|
||||
MAMMMXMMMM
|
||||
MXMXAXMASX";
|
||||
|
||||
#[test]
|
||||
fn part1() {
|
||||
let result = process_part1(INPUT);
|
||||
assert_eq!(result, 18);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2() {
|
||||
let result = process_part2(INPUT);
|
||||
assert_eq!(result, 9);
|
||||
}
|
||||
}
|
||||
@@ -3,3 +3,5 @@ pub mod d1;
|
||||
pub mod d2;
|
||||
|
||||
pub mod d3;
|
||||
|
||||
pub mod d4;
|
||||
|
||||
Reference in New Issue
Block a user