feat: added day 4 part 1 and 2
All checks were successful
Build and run challenges / Challenge for day (2) (push) Successful in 3s
Build and run challenges / Challenge for day (1) (push) Successful in 4s
Build and run challenges / Challenge for day (4) (push) Successful in 4s
Build and run challenges / Challenge for day (3) (push) Successful in 9s

Signed-off-by: Louis Vallat <contact@louis-vallat.fr>
This commit is contained in:
Louis Vallat 2024-12-04 23:45:24 +01:00
parent 7bc8ceecce
commit 33472462bd
Signed by: louis
SSH Key Fingerprint: SHA256:0iPwDU/PZtEy/K13Oar4TzmcunmI9H5U9IsOR3jyT/Q
3 changed files with 108 additions and 1 deletions

View File

@ -11,7 +11,7 @@ jobs:
name: Challenge for day
strategy:
matrix:
day_number: [1, 2, 3]
day_number: [1, 2, 3, 4]
runs-on: rust-bookworm
steps:
- name: Check out repository code

6
day4/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "day4"
version = "0.1.0"
edition = "2021"
[dependencies]

101
day4/src/main.rs Normal file
View File

@ -0,0 +1,101 @@
use std::{env, fs};
fn read_input(path: &str) -> String {
fs::read_to_string(path).expect("Cannot read file.")
}
fn parse_input(input: &str) -> Vec<Vec<char>> {
input
.lines()
.filter(|line| !line.is_empty())
.map(|line| line.chars().collect())
.collect()
}
fn has_word(
word: &str,
matrix: &Vec<Vec<char>>,
source: (usize, usize),
direction: (i32, i32),
) -> bool {
let word = word.chars().collect::<Vec<char>>();
let (row, col) = source;
let (i_row, i_col, i_word_len) = (row as i32, col as i32, word.len() as i32);
if !(0..matrix.len() as i32).contains(&(i_row + ((i_word_len - 1) * direction.0)))
|| !(0..matrix[row].len() as i32).contains(&(i_col + ((i_word_len - 1) * direction.1)))
{
return false;
}
for (offset, c) in word.iter().enumerate() {
let i_offset = offset as i32;
if matrix[(i_row + i_offset * direction.0) as usize]
[(i_col + i_offset * direction.1) as usize]
!= *c
{
return false;
}
}
true
}
fn part_1(matrix: &Vec<Vec<char>>) -> u32 {
let mut found = 0;
for (row, line) in matrix.iter().enumerate() {
for (col, c) in line.iter().enumerate() {
if *c != 'X' {
continue;
}
for i in -1..=1 {
for j in -1..=1 {
if i == 0 && j == 0 {
continue;
}
if has_word("XMAS", matrix, (row, col), (i, j)) {
found += 1;
}
}
}
}
}
found
}
fn is_mas(matrix: &Vec<Vec<char>>, row: usize, col: usize, flag: bool) -> bool {
if matrix[row][col] != 'A' {
return false;
}
if flag {
(matrix[row - 1][col - 1] == 'M' && matrix[row + 1][col + 1] == 'S')
|| (matrix[row - 1][col - 1] == 'S' && matrix[row + 1][col + 1] == 'M')
} else {
(matrix[row + 1][col - 1] == 'M' && matrix[row - 1][col + 1] == 'S')
|| (matrix[row + 1][col - 1] == 'S' && matrix[row - 1][col + 1] == 'M')
}
}
fn part_2(matrix: &Vec<Vec<char>>) -> u32 {
let mut found = 0;
for row in 1..(matrix.len() - 1) {
for col in 1..(matrix[row].len() - 1) {
if is_mas(matrix, row, col, true) && is_mas(matrix, row, col, false) {
found += 1;
}
}
}
found
}
fn main() {
let args: Vec<String> = env::args().collect();
for arg in args.iter().skip(1) {
let input = read_input(&arg);
let matrix = parse_input(&input);
println!("[{}]", &arg);
println!("\t[Part 1] => Answer is '{}'.", part_1(&matrix));
println!("\t[Part 2] => Answer is '{}'.", part_2(&matrix));
}
}