From f977975326166ce5d01f2d9e55f405afe0eec8c7 Mon Sep 17 00:00:00 2001 From: Bearmine Date: Tue, 27 May 2025 22:10:38 -0500 Subject: [PATCH] Start implementing day 4 --- day_04/small_input.txt | 10 ++++++ day_04/src/main.rs | 73 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 day_04/small_input.txt diff --git a/day_04/small_input.txt b/day_04/small_input.txt new file mode 100644 index 0000000..1f4eda2 --- /dev/null +++ b/day_04/small_input.txt @@ -0,0 +1,10 @@ +MMMSXXMASM +MSAMXMSMSA +AMXSXMAAMM +MSAMASMSMX +XMASAMXAMM +XXAMMXXAMA +SMSMSASXSS +SAXAMASAAA +MAMMMXMMMM +MXMXAXMASX diff --git a/day_04/src/main.rs b/day_04/src/main.rs index e7a11a9..65bbf3f 100644 --- a/day_04/src/main.rs +++ b/day_04/src/main.rs @@ -1,3 +1,72 @@ -fn main() { - println!("Hello, world!"); +//https://adventofcode.com/2024/day/4 +// +// Run command: cargo run ./input.txt + +use std::env; +use std::error::Error; +use std::fmt::Display; +use std::fs::File; +use std::io::{BufReader, prelude::*}; + +#[derive(Debug)] +struct Crossword { + data: Vec>, + width: usize, + height: usize, +} + +impl Crossword { + fn new(file: File) -> Result> { + let mut input_buffer = BufReader::new(file); + + let mut data: Vec> = Vec::new(); + + let mut first_line = String::new(); + input_buffer.read_line(&mut first_line)?; + let first_line = first_line.trim_end().to_owned(); + let width = first_line.len(); + for line in input_buffer.lines() { + let line = line?; + if line.len() != width { + println!("{}", line); + Err("Line length of input are not consistent")?; + } + data.push(line.chars().collect()); + } + let height = data.len(); + + Ok(Crossword { + data, + width, + height, + }) + } +} + +impl Display for Crossword { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + for row in &self.data { + let row = String::from_iter(row.iter()); + writeln!(f, "{}", row)?; + } + Ok(()) + } +} + +fn main() -> Result<(), Box> { + // Handle command input + let args: Vec = env::args().collect(); + if args.len() != 2 { + panic!( + "{} must be run with a single argument of files name!", + &args[0] + ) + } + let input_file_string = &args[1]; + + let crossword = Crossword::new(File::open(input_file_string)?)?; + + println!("{}", crossword); + + Ok(()) }