#![feature(array_chunks)] #![feature(iter_array_chunks)] static INPUT: &[u8] = include_bytes!("input.txt"); fn parse_val(s: &[u8]) -> u8 { (if s[0].is_ascii_digit() { (s[0] - b'0') * 10 } else { 0 }) + s[1] - b'0' } fn main() { let valueified = INPUT.split(|b| *b == b'\n').filter(|s| !s.is_empty()) .map(|b| { let win: u128 = b[10..40].array_chunks::<3>().fold(0, |a, b| a | (1 << parse_val(&b[0..2]))); let valiter = b[42..].array_chunks::<3>(); let val: u128 = valiter.clone().fold(0, |a, b| a | (1 << parse_val(&b[0..2]))) | (1 << parse_val(valiter.remainder())); (win & val).count_ones() }); let winsum = valueified.clone().map(|c| if c == 0 { 0 } else { 2u32.pow(c - 1) }).sum::(); println!("Sum of Win Scores: {winsum}"); let mut nums = valueified.map(|n| (n as usize, 1)).collect::>(); for i in 0..nums.len() { let (m, c) = nums[i]; for n in i + 1 .. (i + m + 1).min(nums.len()) { nums[n].1 += c; } } let cardsum = nums.into_iter().map(|(_, c)| c).sum::(); println!("Sum of Recursive Card Counting: {cardsum}"); }