Changed advent_of_code rust stuff to be good cargo packages instead

This commit is contained in:
2021-12-17 16:17:45 +01:00
parent 6533d3ccca
commit 7a8d39f5ca
16 changed files with 82 additions and 14 deletions

View File

@@ -0,0 +1,98 @@
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<u64, (usize, usize)>,
opens : [[bool; 5]; 5],
score : u64,
}
impl Card {
fn new() -> Card {
Card { numbers: HashMap::new(), opens: [[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 {
let mut result_x = true;
let mut result_y = true;
for b in self.opens[x] {
if b {
result_x = false;
}
}
for i in 0..self.opens.len() {
if self.opens[i][y] {
result_y = false;
}
}
return result_x || result_y;
}
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("./4.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(1, (0, 1));
// c.insert(2, (1, 1));
// c.insert(3, (2, 1));
// c.insert(4, (3, 1));
// c.insert(5, (4, 1));
// assert_eq!(c.call(1), None);
// assert_eq!(c.call(2), None);
// assert_eq!(c.call(3), None);
// assert_eq!(c.call(4), None);
// assert_eq!(c.call(5), Some(0));
}