day 4 completed

This commit is contained in:
simon
2024-10-07 12:54:01 +02:00
parent c0d1a77f5b
commit e7461baa77
3 changed files with 80 additions and 10 deletions

View File

@@ -1,21 +1,22 @@
use std::fs;
use aochelpers;
use regex::Regex;
#[macro_use(c)]
extern crate cute;
fn main() {
/* battleplan
/* battleplan part 1
1. create a list for all the cards
1.1 parse input
1.1.1split on '|' and parse via regex both parts or whole thing via regex
1.2 build list
1.2.1 each value of the list represents a card
1.2.2 representation of a card = 2 lists, winning numbers, and drawn numbers
1.3 map the list to convert card info into the amount of points
1.4 sum the points*/
let content = fs::read_to_string("input").expect("Should have been able to read the file");
2. map the list to convert card info into the amount of points
3. sum the points
*/
let content = fs::read_to_string("input").expect("Could not read the file");
let lines_array = content.split("\n").collect::<Vec<_>>();
let number_regex = Regex::new(r"\d+").unwrap();
let mut all_cards: Vec<(Vec<i32>, Vec<i32>)> = Vec::new();
@@ -46,9 +47,31 @@ fn main() {
.collect();
let part1: i32 = part1_array
.iter()
.map(|x| i32::pow(2, x.clone().len() as u32 -1))
.map(|x| i32::pow(2, x.clone().len() as u32 - 1))
.collect::<Vec<i32>>()
.iter()
.sum();
println!("Part 1: {part1}");
/*
battleplan part 2
1. get the intersection between similar numbers left and right
2. add the amount of current card to subsequent cards
3. sum the newly created list of amount of cards
*/
let mut part2_array: Vec<i32> = c! [1, for _x in 0..all_cards.len()];
for (card_index, card) in all_cards.iter().enumerate() {
let similar_amount = card
.1
.clone()
.into_iter()
.filter(|y| card.0.contains(y))
.collect::<Vec<i32>>()
.len() as i32;
for i in 1..similar_amount+1 {
part2_array[i32::min(card_index as i32 + i, 189) as usize] += part2_array[card_index];
}
}
let part2 = part2_array.iter().sum::<i32>();
println!("Part 2: {part2}");
}