Day 4
This commit is contained in:
parent
ac7a1925a5
commit
cec5519273
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -25,3 +25,11 @@ version = "0.1.0"
|
|||
[[package]]
|
||||
name = "d03t2"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "d04t1"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "d04t2"
|
||||
version = "0.1.0"
|
||||
|
|
|
@ -8,4 +8,6 @@ members = [
|
|||
"d02t2",
|
||||
"d03t1",
|
||||
"d03t2",
|
||||
"d04t1",
|
||||
"d04t2",
|
||||
]
|
||||
|
|
8
d04t1/Cargo.toml
Normal file
8
d04t1/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "d04t1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
6
d04t1/demo_input.txt
Normal file
6
d04t1/demo_input.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
|
||||
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
|
||||
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
|
||||
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
|
||||
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
|
||||
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
|
46
d04t1/src/main.rs
Normal file
46
d04t1/src/main.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
fn main() {
|
||||
let input = include_str!("../input.txt");
|
||||
println!("d04t1: {}", d04t1(input));
|
||||
}
|
||||
|
||||
pub fn d04t1(input: &str) -> usize {
|
||||
let mut result = 0;
|
||||
for line in input.lines() {
|
||||
let (_card_id, card_data) = line.split_once(": ").unwrap();
|
||||
let (winning_numbers, actual_numbers) = card_data
|
||||
.split_once(" | ")
|
||||
.map(|(a, b)| {
|
||||
(
|
||||
a.split_whitespace()
|
||||
.map(|e| e.parse::<usize>().unwrap())
|
||||
.collect::<Vec<_>>(),
|
||||
b.split_whitespace()
|
||||
.map(|e| e.parse::<usize>().unwrap())
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
})
|
||||
.unwrap();
|
||||
let mut card_value = 0;
|
||||
for num in actual_numbers {
|
||||
if winning_numbers.contains(&num) {
|
||||
if card_value == 0 {
|
||||
card_value = 1;
|
||||
} else {
|
||||
card_value *= 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
result += card_value;
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_d04t1() {
|
||||
let input = include_str!("../demo_input.txt");
|
||||
assert_eq!(d04t1(input), 13);
|
||||
}
|
||||
}
|
8
d04t2/Cargo.toml
Normal file
8
d04t2/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "d04t2"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
6
d04t2/demo_input.txt
Normal file
6
d04t2/demo_input.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
|
||||
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
|
||||
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
|
||||
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
|
||||
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
|
||||
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
|
86
d04t2/src/main.rs
Normal file
86
d04t2/src/main.rs
Normal file
|
@ -0,0 +1,86 @@
|
|||
fn main() {
|
||||
let input = include_str!("../input.txt");
|
||||
println!("d04t2: {}", d04t2(input));
|
||||
}
|
||||
|
||||
struct Card {
|
||||
card_id: usize,
|
||||
winning_amount: usize,
|
||||
}
|
||||
|
||||
pub fn d04t2(input: &str) -> usize {
|
||||
let mut result = 0;
|
||||
let cards: Vec<Card> = input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let (card_id, card_data) = line
|
||||
.split_once(": ")
|
||||
.map(|(cid, cd)| {
|
||||
(
|
||||
cid.split_once(" ")
|
||||
.unwrap()
|
||||
.1
|
||||
.trim()
|
||||
.parse::<usize>()
|
||||
.unwrap(),
|
||||
cd,
|
||||
)
|
||||
})
|
||||
.unwrap();
|
||||
let (winning_numbers, actual_numbers) = card_data
|
||||
.split_once(" | ")
|
||||
.map(|(a, b)| {
|
||||
(
|
||||
a.split_whitespace()
|
||||
.map(|e| e.parse::<usize>().unwrap())
|
||||
.collect::<Vec<_>>(),
|
||||
b.split_whitespace()
|
||||
.map(|e| e.parse::<usize>().unwrap())
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
})
|
||||
.unwrap();
|
||||
let winning_amount = actual_numbers
|
||||
.iter()
|
||||
.filter(|num| winning_numbers.contains(&num))
|
||||
.count();
|
||||
Card {
|
||||
winning_amount,
|
||||
card_id,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
for card in &cards {
|
||||
result += 1;
|
||||
println!(
|
||||
"Checking card {} - {} winning",
|
||||
card.card_id, card.winning_amount
|
||||
);
|
||||
result += add_rec(card, &cards);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn add_rec(card: &Card, cards: &Vec<Card>) -> usize {
|
||||
let mut to_add = 0;
|
||||
for cid in 1..=card.winning_amount {
|
||||
let found_card = cards.iter().find(|c| c.card_id == card.card_id + cid);
|
||||
if let Some(found_card) = found_card {
|
||||
to_add += 1;
|
||||
to_add += add_rec(found_card, cards);
|
||||
} else {
|
||||
println!("\tdidn't find card {}", card.card_id + cid);
|
||||
}
|
||||
}
|
||||
to_add
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_d04t2() {
|
||||
let input = include_str!("../demo_input.txt");
|
||||
assert_eq!(d04t2(input), 30);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue