day four part two
This commit is contained in:
parent
7cb042c0ba
commit
7c50cd34c5
2 changed files with 44 additions and 0 deletions
1
four/output-two
Normal file
1
four/output-two
Normal file
|
@ -0,0 +1 @@
|
||||||
|
8736438
|
|
@ -3,4 +3,47 @@ use std::io::{self, Read};
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
io::stdin().read_to_string(&mut buf).unwrap();
|
io::stdin().read_to_string(&mut buf).unwrap();
|
||||||
|
|
||||||
|
let wins_by_card: Vec<_> = buf
|
||||||
|
.lines()
|
||||||
|
.map(|l| &l[5..])
|
||||||
|
.map(|l| [&l[5..34], &l[37..]])
|
||||||
|
.map(|[winners, numbers]| {
|
||||||
|
(
|
||||||
|
winners
|
||||||
|
.split(' ')
|
||||||
|
.filter(|str| str != &"")
|
||||||
|
.map(str::trim)
|
||||||
|
.flat_map(|num_str| num_str.parse::<usize>())
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
numbers
|
||||||
|
.split(' ')
|
||||||
|
.filter(|str| str != &"")
|
||||||
|
.map(str::trim)
|
||||||
|
.flat_map(|num_str| num_str.parse::<usize>())
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.map(|(winners, numbers)| {
|
||||||
|
numbers
|
||||||
|
.iter()
|
||||||
|
.filter(|number| winners.contains(number))
|
||||||
|
.count()
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let mut copies_by_card = vec![1; wins_by_card.len()];
|
||||||
|
|
||||||
|
wins_by_card.iter().enumerate().for_each(|(idx, wins)| {
|
||||||
|
let copies_of_winner = copies_by_card[idx];
|
||||||
|
copies_by_card
|
||||||
|
.iter_mut()
|
||||||
|
.skip(idx + 1)
|
||||||
|
.take(*wins)
|
||||||
|
.for_each(|copies| *copies += copies_of_winner)
|
||||||
|
});
|
||||||
|
|
||||||
|
let total: usize = copies_by_card.iter().sum();
|
||||||
|
|
||||||
|
println!("{total}");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue