From 6533d3cccad569b66c622a687b1503a99952791c Mon Sep 17 00:00:00 2001 From: Philippe Zwietering Date: Wed, 15 Dec 2021 00:37:31 +0100 Subject: [PATCH] Bingo cards for AoC day 4 not functional yet --- advent_of_code/4.rs | 85 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) diff --git a/advent_of_code/4.rs b/advent_of_code/4.rs index 09290e8..35ff8b4 100644 --- a/advent_of_code/4.rs +++ b/advent_of_code/4.rs @@ -1,7 +1,90 @@ use std::io::{self, BufRead}; use std::path::Path; 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, + opens : Vec::>, + 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 { + 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 { + 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(){ - + 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::::new(); + let mut bingo_cards = Vec::::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), + } } \ No newline at end of file