2023 day4/rust: add solution
This commit is contained in:
parent
a4568c199b
commit
3109623176
4 changed files with 86 additions and 0 deletions
9
2023/day4/rust/Cargo.toml
Normal file
9
2023/day4/rust/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
name = "rust_2023_04"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
aoc = { path = "../../../common/rust" }
|
69
2023/day4/rust/src/main.rs
Normal file
69
2023/day4/rust/src/main.rs
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
#![warn(clippy::pedantic)]
|
||||||
|
|
||||||
|
use std::{io::stdin, num::NonZeroU32};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
struct Card {
|
||||||
|
winning: Vec<usize>,
|
||||||
|
found: Vec<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Card {
|
||||||
|
pub fn parse(s: &str) -> Self {
|
||||||
|
fn get_numbers(s: &str) -> Vec<usize> {
|
||||||
|
s.split(' ')
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.map(|n| n.parse().unwrap())
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
let (_id, numbers) = s.split_once(": ").unwrap();
|
||||||
|
let (winning, found) = numbers.split_once(" | ").unwrap();
|
||||||
|
let winning = get_numbers(winning);
|
||||||
|
let found = get_numbers(found);
|
||||||
|
|
||||||
|
Self { winning, found }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn num_matching(&self) -> usize {
|
||||||
|
self.found
|
||||||
|
.iter()
|
||||||
|
.filter(|n| self.winning.contains(n))
|
||||||
|
.count()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn exp_score(&self) -> usize {
|
||||||
|
if let Some(matching) = NonZeroU32::new(u32::try_from(self.num_matching()).unwrap()) {
|
||||||
|
2_usize.pow(matching.get() - 1)
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn recursive_score(&self, previous: &[usize]) -> usize {
|
||||||
|
1 + previous
|
||||||
|
.iter()
|
||||||
|
.rev()
|
||||||
|
.take(self.num_matching())
|
||||||
|
.sum::<usize>()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let cards: Vec<Card> = stdin().lines().map(|l| Card::parse(&l.unwrap())).collect();
|
||||||
|
|
||||||
|
println!("{}", cards.iter().map(Card::exp_score).sum::<usize>());
|
||||||
|
|
||||||
|
let recursive_score: usize = cards
|
||||||
|
.iter()
|
||||||
|
.rev()
|
||||||
|
.fold(Vec::new(), |mut previous, card| {
|
||||||
|
let score = card.recursive_score(&previous);
|
||||||
|
previous.push(score);
|
||||||
|
previous
|
||||||
|
})
|
||||||
|
.iter()
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
println!("{recursive_score}");
|
||||||
|
}
|
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -663,6 +663,13 @@ dependencies = [
|
||||||
"enum-map",
|
"enum-map",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rust_2023_04"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"aoc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rust_2023_05"
|
name = "rust_2023_05"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
|
@ -29,5 +29,6 @@ members = [
|
||||||
"2022/day18/rust",
|
"2022/day18/rust",
|
||||||
"2023/day1/rust",
|
"2023/day1/rust",
|
||||||
"2023/day2/rust",
|
"2023/day2/rust",
|
||||||
|
"2023/day4/rust",
|
||||||
"2023/day5/rust",
|
"2023/day5/rust",
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in a new issue