I don't like learning Rust, continue in c++ for now -- advent_of_code

This commit is contained in:
2021-12-19 01:00:35 +01:00
parent 7a8d39f5ca
commit b0c82a440c
3 changed files with 47 additions and 2 deletions

View File

@@ -5,3 +5,12 @@ version = 3
[[package]] [[package]]
name = "d4" name = "d4"
version = "0.1.0" 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"

View File

@@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
text_io = "0.1.9"

View File

@@ -3,6 +3,8 @@ use std::path::Path;
use std::fs::File; use std::fs::File;
use std::collections::HashMap; use std::collections::HashMap;
use text_io::scan;
// -- PART 1 -- // -- 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 // 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 // Metrics are the last called number multiplied by sum of all numbers that were not needed for bingo
#[derive(Debug,PartialEq)]
struct Card { struct Card {
numbers : HashMap<u64, (usize, usize)>, numbers : HashMap<u64, (usize, usize)>,
opens : [[bool; 5]; 5], opens : [[bool; 5]; 5],
@@ -78,8 +81,8 @@ fn main(){
Ok(file) => file, Ok(file) => file,
}; };
let lines = io::BufReader::new(file).lines(); let mut lines = io::BufReader::new(file).lines();
let mut numbers = Vec::<u8>::new(); let mut numbers = Vec::<u32>::new();
let mut bingo_cards = Vec::<Card>::new(); let mut bingo_cards = Vec::<Card>::new();
// let mut c = Card::new(); // let mut c = Card::new();
@@ -95,4 +98,36 @@ fn main(){
// assert_eq!(c.call(4), None); // assert_eq!(c.call(4), None);
// assert_eq!(c.call(5), Some(0)); // 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);
}
} }