Bingo cards for AoC day 4 not functional yet

This commit is contained in:
2021-12-15 00:37:31 +01:00
parent 2025f59963
commit 6533d3ccca

View File

@@ -1,7 +1,90 @@
use std::io::{self, BufRead}; use std::io::{self, BufRead};
use std::path::Path; use std::path::Path;
use std::fs::File; use std::fs::File;
use std::collections::HashMap;
// -- PART 1 --
// We playin bingo now
// Input is first a sequence of integers
// Followed by a number of bingo cards
// 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
struct Card {
numbers : HashMap<u64, (usize, usize)>,
opens : Vec::<Vec::<bool>>,
score : u64,
}
impl Card {
fn new() -> Card {
Card { numbers: HashMap::new(), opens: vec![vec![true; 5]; 5], score: 0 }
}
fn call(&mut self, n: u64) -> Option<u64> {
let search_result = self.numbers.get(&n);
match search_result {
None => return None,
Some(&coords) => return self.take(n, coords),
}
}
fn take(&mut self, n: u64, (x, y): (usize, usize)) -> Option<u64> {
self.score -= n;
self.opens[x][y] = false;
match self.check((x, y)) {
false => return None,
true => return Some(self.score),
}
}
fn check(&self, (x, y): (usize, usize)) -> bool {
for &b in &self.opens[x] {
if b {
return false;
}
}
for i in 0..self.opens.len() {
if self.opens[i][y] {
return false;
}
}
return true;
}
fn insert(&mut self, n: u64, coords: (usize, usize)) -> () {
self.numbers.insert(n, coords);
self.score += n;
}
}
fn main(){ fn main(){
println!("Advent of Code #4!\n");
let path = Path::new("./3.txt");
let display = path.display();
let file = match File::open(&path) {
Err(why) => panic!("Couldn't open {}: {}", display, why),
Ok(file) => file,
};
let lines = io::BufReader::new(file).lines();
let mut numbers = Vec::<u8>::new();
let mut bingo_cards = Vec::<Card>::new();
let mut c = Card::new();
c.insert(10, (1, 1));
let r = c.call(10);
match r {
None => println!("test failed"),
Some(t) => println!("test got: {}", t),
}
} }