From 4766956a924fa46c8660afa208e88b5d67d1c727 Mon Sep 17 00:00:00 2001 From: Bearmine Date: Sat, 31 May 2025 08:50:55 -0500 Subject: [PATCH] Start day 6 --- day_06/Cargo.lock | 7 +++++ day_06/small_input.txt | 10 +++++++ day_06/src/main.rs | 61 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 day_06/Cargo.lock create mode 100644 day_06/small_input.txt diff --git a/day_06/Cargo.lock b/day_06/Cargo.lock new file mode 100644 index 0000000..03343c6 --- /dev/null +++ b/day_06/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "day_06" +version = "0.1.0" diff --git a/day_06/small_input.txt b/day_06/small_input.txt new file mode 100644 index 0000000..b60e466 --- /dev/null +++ b/day_06/small_input.txt @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... \ No newline at end of file diff --git a/day_06/src/main.rs b/day_06/src/main.rs index e7a11a9..e706463 100644 --- a/day_06/src/main.rs +++ b/day_06/src/main.rs @@ -1,3 +1,60 @@ -fn main() { - println!("Hello, world!"); +use std::{ + env, + error::Error, + fs::File, + io::{BufReader, prelude::*}, +}; + +fn load_map(input_file: &str) -> Result<(Vec>, (usize, usize)), Box> { + let buffer = BufReader::new(File::open(input_file)?); + let mut lines = buffer.lines().peekable(); + + let width = match lines.peek() { + Some(l) => match l { + Ok(li) => li.len(), + Err(_) => Err("Error reading first line of map file")?, + }, + None => Err("Cannot load empty map file.")?, + }; + + let mut data: Vec> = Vec::new(); + let mut guard_position = (0, 0); + + for (y, line) in lines.enumerate() { + let line = line?; + if line.len() != width { + println!("{}", line); + Err("Line length of input are not consistent")?; + } + let parsed_data: Vec = line + .chars() + .enumerate() + .map(|(x, c)| { + if c == '^' { + guard_position = (x, y); + }; + c == '#' + }) + .collect(); + data.push(parsed_data); + } + let height = data.len(); + + Ok((data, guard_position)) +} + +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 = &args[1]; + let data = load_map(input_file)?; + println!("{:?}", data); + + Ok(()) }