Update solution for 빙고 w/ id 23778889

Time: 4ms
MemUsage: 13152KB
This commit is contained in:
2024-08-29 16:22:08 +09:00
parent 9bacc34b04
commit 856b912c80

View File

@@ -0,0 +1,114 @@
use std::io::{self, BufRead};
macro_rules! parse_list {
($t: ty, $buff:ident) => {{
let mut line = String::new();
$buff.read_line(&mut line).unwrap();
let list: Vec<$t> = line
.split_whitespace()
.map(|w| w.parse::<$t>().unwrap())
.collect();
list
}};
}
fn main() {
let stdin = io::stdin();
let mut lock = stdin.lock();
let mut array: [usize; 25] = [0; 25];
let mut indices: [usize; 25] = [0; 25];
let mut call: Vec<usize> = Vec::new();
let mut bingos: usize = 0;
let mut index = 0;
for _i in 0..5 {
let input = parse_list!(usize, lock);
for x in input {
indices[x - 1] = index;
index += 1;
}
}
for _i in 0..5 {
let mut row = parse_list!(usize, lock);
call.append(&mut row);
}
for (idx, x) in call.iter().enumerate() {
let index = indices[x - 1];
array[index] = 1;
if let Some(n) = check_bingo(&array, index) {
bingos += n;
if bingos >= 3 {
println!("{}", idx + 1);
return;
}
}
}
}
fn check_bingo(array: &[usize; 25], change: usize) -> Option<usize> {
let mut index: usize;
let mut s: usize;
let mut count: usize = 0;
let row: usize = change / 5;
let col: usize = change % 5;
// check row
index = 5 * row;
s = 0;
for _i in 0..5 {
s += array[index];
index += 1;
}
if s == 5 {
count += 1;
// println!("{}", row);
}
// check col
index = col;
s = 0;
for _i in 0..5 {
s += array[index];
index += 5;
}
if s == 5 {
count += 1;
// println!("{}", col);
}
// check diag
if change % 6 == 0 {
index = 0;
s = 0;
for _i in 0..5 {
s += array[index];
index += 6;
}
if s == 5 {
count += 1;
// println!("d1");
}
}
if change % 4 == 0 && change != 24 {
index = 4;
s = 0;
for _i in 0..5 {
s += array[index];
index += 4;
}
if s == 5 {
count += 1;
// println!("d2");
}
}
if count == 0 {
return None;
} else {
return Some(count);
}
}