From 856b912c80dbebcef60d51fb210b849d94de5140 Mon Sep 17 00:00:00 2001 From: Myeongseon Choi Date: Thu, 29 Aug 2024 16:22:08 +0900 Subject: [PATCH] =?UTF-8?q?Update=20solution=20for=20=EB=B9=99=EA=B3=A0=20?= =?UTF-8?q?w/=20id=2023778889=20Time:=204ms=20MemUsage:=2013152KB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/빙고/solution_23778889.rs | 114 +++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 baekjoon/빙고/solution_23778889.rs diff --git a/baekjoon/빙고/solution_23778889.rs b/baekjoon/빙고/solution_23778889.rs new file mode 100644 index 0000000..1e36d9f --- /dev/null +++ b/baekjoon/빙고/solution_23778889.rs @@ -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 = 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 { + 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); + } +}