This commit is contained in:
2024-12-04 22:18:21 +01:00
parent 464f5846ed
commit 48c6f5db00
7 changed files with 186 additions and 0 deletions

1
day-04-rust/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
target/

7
day-04-rust/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day-04-rust"
version = "0.1.0"

6
day-04-rust/Cargo.toml Normal file
View File

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

View File

@@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"

22
day-04-rust/src/main.rs Normal file
View File

@@ -0,0 +1,22 @@
#![feature(let_chains)]
mod task1;
mod task2;
fn main() {
let args = std::env::args().collect::<Vec<_>>();
if args.len() != 3 {
eprintln!("Usage: {} <1|2> <input file>", args[0]);
std::process::exit(1);
}
match args[1].as_str() {
"1" => {
task1::run(&*std::fs::read_to_string(&args[2]).expect("Failed to read from input"))
}
"2" => {
task2::run(&*std::fs::read_to_string(&args[2]).expect("Failed to read from input"))
}
_ => eprintln!("Unknown task {}", args[1]),
}
}

109
day-04-rust/src/task1.rs Normal file
View File

@@ -0,0 +1,109 @@
type Number = i32;
pub fn run(input: &str) {
let lines = input.split('\n').collect::<Vec<_>>();
let mut max_column: Number = 0;
let mut occurrences = 0;
for line in &lines {
max_column = max_column.max(line.len() as Number);
let mut index = 0;
while let Some(match_index) = line[index..].find("XMAS") {
occurrences += 1;
index += match_index + 4;
}
index = 0;
while let Some(match_index) = line[index..].find("SAMX") {
occurrences += 1;
index += match_index + 4;
}
}
println!("After horizontal: {}", occurrences);
{
let mut buffer = String::with_capacity(5);
for column in 0..max_column {
buffer.clear();
for line in &lines {
if perform_buffer_action(&mut buffer, line.chars().nth(column as usize)) {
occurrences += 1;
}
}
}
}
println!("After vertical: {}", occurrences);
let max_line: Number = lines.len() as Number;
{
let mut buffer = String::with_capacity(5);
let mut r_buffer = String::with_capacity(5);
for start in 0..max_line {
buffer.clear();
r_buffer.clear();
for i in 0..(max_line - start) {
if perform_buffer_action(
&mut buffer,
lines.get((start + i) as usize)
.and_then(|line| line.chars().nth(i as usize))
) {
occurrences += 1;
}
if perform_buffer_action (
&mut r_buffer,
lines.get((start + i) as usize)
.and_then(|line| line.chars().nth((max_column - i - 1) as usize))
) {
occurrences += 1;
}
}
}
println!("After diagonal line starting: {}", occurrences);
for start in 1..max_line {
buffer.clear();
r_buffer.clear();
for i in 0..(max_column - start) {
if perform_buffer_action(
&mut buffer,
lines.get(i as usize)
.and_then(|line| line.chars().nth((start + i) as usize))
) {
occurrences += 1;
}
if perform_buffer_action(
&mut r_buffer,
lines.get(i as usize)
.and_then(|line| line.chars().nth((max_column - start - 1 - i) as usize))
) {
occurrences += 1;
}
}
}
println!("After diagonal column starting: {}", occurrences);
}
println!("Result: {}", occurrences);
}
fn perform_buffer_action(buffer: &mut String, char: Option<char>) -> bool {
if let Some(ch) = char {
buffer.push(ch);
if buffer.len() > 4 {
buffer.remove(0);
}
if buffer == "XMAS" {
return true;
} else if buffer == "SAMX" {
return true;
}
} else {
buffer.clear();
}
false
}

39
day-04-rust/src/task2.rs Normal file
View File

@@ -0,0 +1,39 @@
pub fn run(input: &str) {
let lines = input
.split('\n')
.map(|line| line.chars().collect::<Vec<_>>())
.filter(|chars| !chars.is_empty())
.collect::<Vec<_>>();
if lines.is_empty() {
return;
}
let cols = lines[0].len();
if lines.iter().any(|line| line.len() != cols) {
eprintln!("Invalid line length detected");
return;
}
let mut occurrences = 0;
for l in 1..(lines.len() - 1) {
for c in 1..(lines[l].len() - 1) {
let mid = lines[l][c];
if mid != 'A' {
continue;
}
let top_left = lines[l - 1][c - 1];
let top_right = lines[l - 1][c + 1];
let bottom_left = lines[l + 1][c - 1];
let bottom_right = lines[l + 1][c + 1];
if ((top_left == 'M' && bottom_right == 'S')
|| (top_left == 'S' && bottom_right == 'M'))
&& ((top_right == 'M' && bottom_left == 'S')
|| (top_right == 'S' && bottom_left == 'M'))
{
occurrences += 1;
}
}
}
println!("X-MASes: {}", occurrences);
}