diff --git a/advent_of_code/d4/Cargo.lock b/advent_of_code/d4/Cargo.lock index 35783ca..2bd9e0d 100644 --- a/advent_of_code/d4/Cargo.lock +++ b/advent_of_code/d4/Cargo.lock @@ -5,3 +5,12 @@ version = 3 [[package]] name = "d4" version = "0.1.0" +dependencies = [ + "text_io", +] + +[[package]] +name = "text_io" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee74b5019b48991b09803402aaf9e65a053b3993fe9d9c475ab67a395358ba76" diff --git a/advent_of_code/d4/Cargo.toml b/advent_of_code/d4/Cargo.toml index d057ef5..752536b 100644 --- a/advent_of_code/d4/Cargo.toml +++ b/advent_of_code/d4/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +text_io = "0.1.9" \ No newline at end of file diff --git a/advent_of_code/d4/src/main.rs b/advent_of_code/d4/src/main.rs index 200927b..3275c59 100644 --- a/advent_of_code/d4/src/main.rs +++ b/advent_of_code/d4/src/main.rs @@ -3,6 +3,8 @@ use std::path::Path; use std::fs::File; use std::collections::HashMap; +use text_io::scan; + // -- PART 1 -- @@ -12,6 +14,7 @@ use std::collections::HashMap; // Goal is to find the bingo card that has bingo first and calculate some metrics of it // Metrics are the last called number multiplied by sum of all numbers that were not needed for bingo +#[derive(Debug,PartialEq)] struct Card { numbers : HashMap, opens : [[bool; 5]; 5], @@ -78,8 +81,8 @@ fn main(){ Ok(file) => file, }; - let lines = io::BufReader::new(file).lines(); - let mut numbers = Vec::::new(); + let mut lines = io::BufReader::new(file).lines(); + let mut numbers = Vec::::new(); let mut bingo_cards = Vec::::new(); // let mut c = Card::new(); @@ -95,4 +98,36 @@ fn main(){ // assert_eq!(c.call(4), None); // assert_eq!(c.call(5), Some(0)); + let first_line = lines.nth(0).unwrap().unwrap(); + let mut acc: u32 = 0; + for c in first_line.chars() { + if c != ',' && c != '\n' { + acc *= 10; + acc += c.to_digit(10).unwrap(); + } else{ + numbers.push(acc); + acc = 0; + } + } + + let mut n_start = 2; + let mut bingo_start_line = lines.nth(n_start); + + while !bingo_start_line.is_none() { + let card = Card::new(); + + for i in 0..5 { + let cur_line = lines.nth(n_start + i).unwrap().unwrap(); + let a: i32; + let b: i32; + let c: i32; + let d: i32; + let e: i32; + scan!(cur_line.bytes() => "{} {} {} {} {}", a, b, c, d, e); + println!("{} {} {} {} {}", a, b, c, d, e); + } + + n_start += 6; + bingo_start_line = lines.nth(n_start); + } } \ No newline at end of file