diff --git a/Cargo.lock b/Cargo.lock index 42f652b..92a3092 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 564cf56..fe841b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,6 @@ members = [ "d02t2", "d03t1", "d03t2", + "d04t1", + "d04t2", ] diff --git a/d04t1/Cargo.toml b/d04t1/Cargo.toml new file mode 100644 index 0000000..6173496 --- /dev/null +++ b/d04t1/Cargo.toml @@ -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] diff --git a/d04t1/demo_input.txt b/d04t1/demo_input.txt new file mode 100644 index 0000000..71f208a --- /dev/null +++ b/d04t1/demo_input.txt @@ -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 \ No newline at end of file diff --git a/d04t1/src/main.rs b/d04t1/src/main.rs new file mode 100644 index 0000000..d8e08ec --- /dev/null +++ b/d04t1/src/main.rs @@ -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::().unwrap()) + .collect::>(), + b.split_whitespace() + .map(|e| e.parse::().unwrap()) + .collect::>(), + ) + }) + .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); + } +} diff --git a/d04t2/Cargo.toml b/d04t2/Cargo.toml new file mode 100644 index 0000000..1b7456b --- /dev/null +++ b/d04t2/Cargo.toml @@ -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] diff --git a/d04t2/demo_input.txt b/d04t2/demo_input.txt new file mode 100644 index 0000000..71f208a --- /dev/null +++ b/d04t2/demo_input.txt @@ -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 \ No newline at end of file diff --git a/d04t2/src/main.rs b/d04t2/src/main.rs new file mode 100644 index 0000000..6bbabb0 --- /dev/null +++ b/d04t2/src/main.rs @@ -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 = input + .lines() + .map(|line| { + let (card_id, card_data) = line + .split_once(": ") + .map(|(cid, cd)| { + ( + cid.split_once(" ") + .unwrap() + .1 + .trim() + .parse::() + .unwrap(), + cd, + ) + }) + .unwrap(); + let (winning_numbers, actual_numbers) = card_data + .split_once(" | ") + .map(|(a, b)| { + ( + a.split_whitespace() + .map(|e| e.parse::().unwrap()) + .collect::>(), + b.split_whitespace() + .map(|e| e.parse::().unwrap()) + .collect::>(), + ) + }) + .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) -> 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); + } +}