day4 part2

This commit is contained in:
Candygoblen123 2023-12-05 15:26:58 -05:00
parent 89a46e88f5
commit b1bfc50625
No known key found for this signature in database
GPG Key ID: 577DA64EBEF10385
2 changed files with 66 additions and 13 deletions

View File

@ -1,4 +1,5 @@
use std::fs; use std::fs;
use std::collections::VecDeque;
fn main() { fn main() {
let input = fs::read_to_string("input.txt").unwrap(); let input = fs::read_to_string("input.txt").unwrap();
@ -20,27 +21,79 @@ fn main() {
}) })
.collect(); .collect();
let mut total_pts = 0; let mut queue = VecDeque::from((0..input.len()).collect::<Vec<usize>>());
for card in input {
let mut card_pts = 0; let mut total_cards = 0;
while !queue.is_empty() {
let card_num = queue.pop_front().unwrap();
let card = input.get(card_num).unwrap();
total_cards += 1;
let mut dup_cards = 0;
let winning_nums = card.first().unwrap(); let winning_nums = card.first().unwrap();
let our_nums = card.last().unwrap(); let our_nums = card.last().unwrap();
//dp would kill here, but im lazy
for num in our_nums { for num in our_nums {
if winning_nums.contains(num) { if winning_nums.contains(num) {
if card_pts == 0 { dup_cards += 1;
card_pts = 1; }
} else { }
card_pts *= 2; for card in (card_num + 1)..=(card_num + dup_cards) {
} if card < input.len() {
queue.push_back(card);
} }
} }
//println!("{}", card_pts);
total_pts += card_pts;
} }
println!("{:?}", total_pts); println!("{:?}", total_cards);
} }
// use std::fs;
// fn main() {
// let input = fs::read_to_string("input.txt").unwrap();
// // an array in this format [[card 1: [winning nums][ our nums]]]
// let input: Vec<_> = input.split('\n') // split days
// .map(|card| &card[(card.find(':').unwrap() + 1)..]) // remove day numbers
// .map(|card| card.trim()) // trim extra whitespace
// .map(|card| {
// card.split('|') // split winning/own numbers
// .map(|numbers| {
// numbers.trim() // trim whitespace
// .split(' ') // split into individual nums
// .filter(|x| !x.is_empty()) // remove empty strings
// .map(|x| {return x.parse::<i32>().unwrap()}) // convert to i32
// .collect::<Vec<_>>()
// })
// .collect::<Vec<_>>()
// })
// .collect();
// let mut total_pts = 0;
// for card in input {
// let mut card_pts = 0;
// let winning_nums = card.first().unwrap();
// let our_nums = card.last().unwrap();
// for num in our_nums {
// if winning_nums.contains(num) {
// if card_pts == 0 {
// card_pts = 1;
// } else {
// card_pts *= 2;
// }
// }
// }
// //println!("{}", card_pts);
// total_pts += card_pts;
// }
// println!("{:?}", total_pts);
// }