2023/day04.rs
2023-12-06 01:12:29 +01:00

42 lines
1.1 KiB
Rust

use std::collections::HashSet;
use std::io::{self, BufRead};
fn main() {
// Part 1
let mut part1 = 0;
let cards: Vec<usize> = io::stdin()
.lock()
.lines()
.map(|line| {
let line = line.unwrap();
let (_, card) = line.split_once(": ").unwrap();
let (winning, owned) = card.split_once(" | ").unwrap();
let winning: HashSet<i32> = winning
.split_whitespace()
.flat_map(str::parse)
.collect();
let owned: HashSet<i32> = owned
.split_whitespace()
.flat_map(str::parse)
.collect();
let count = winning.intersection(&owned).count();
part1 += 1 << (count - 1);
count
})
.collect();
// Part 2
let mut dp = vec![1; cards.len()];
let part2: i32 = (0..cards.len())
.map(|i| {
dp[i] += dp[i - cards[cards.len() - i - 1]..i].iter().sum::<i32>();
dp[i]
})
.sum();
println!("{} {}", part1, part2);
}