day 4 part 2 (in Rust)
This commit is contained in:
parent
7d234e0fc2
commit
9e92b66cbe
1 changed files with 20 additions and 1 deletions
21
src/main.rs
21
src/main.rs
|
@ -1,9 +1,15 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use std::{collections::HashSet, io::stdin};
|
use std::{collections::HashSet, io::stdin};
|
||||||
|
|
||||||
|
struct Row {
|
||||||
|
cards: usize,
|
||||||
|
matches: usize,
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let mut sum = 0;
|
let mut sum = 0;
|
||||||
|
|
||||||
|
let mut rows = vec![];
|
||||||
for line in stdin().lines().map_while(Result::ok) {
|
for line in stdin().lines().map_while(Result::ok) {
|
||||||
let p: Vec<_> = line.split(':').collect();
|
let p: Vec<_> = line.split(':').collect();
|
||||||
let nums: Vec<_> = p[1].split('|').collect();
|
let nums: Vec<_> = p[1].split('|').collect();
|
||||||
|
@ -22,8 +28,21 @@ fn main() -> Result<()> {
|
||||||
|
|
||||||
let both = winning.intersection(&mine);
|
let both = winning.intersection(&mine);
|
||||||
let c = both.count();
|
let c = both.count();
|
||||||
sum += if c > 0 { 2i32.pow(c as u32 - 1) } else { 0 };
|
rows.push(Row {
|
||||||
|
cards: 1,
|
||||||
|
matches: c,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
for i in 0..rows.len() {
|
||||||
|
// let row = &rows[i];
|
||||||
|
let (front, back) = rows.split_at_mut(i + 1);
|
||||||
|
let row = front.last().unwrap();
|
||||||
|
for r in back.iter_mut().take(row.matches) {
|
||||||
|
r.cards += row.cards;
|
||||||
|
}
|
||||||
|
sum += row.cards;
|
||||||
|
}
|
||||||
|
|
||||||
println!("{sum}");
|
println!("{sum}");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue