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

19
advent_of_code/d4/4.txt Normal file
View File

@@ -0,0 +1,19 @@
7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
3 15 0 2 22
9 18 13 17 5
19 8 7 25 23
20 11 10 24 4
14 21 16 12 6
14 21 17 24 4
10 16 15 9 19
18 8 23 26 20
22 11 13 6 5
2 0 12 3 7

7
advent_of_code/d4/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "d4"
version = "0.1.0"

View File

@@ -0,0 +1,8 @@
[package]
name = "d4"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

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));
}