summaryrefslogtreecommitdiffstats
path: root/2023/rs/code/four/src/main.rs
diff options
context:
space:
mode:
authoralyx <alyx@aleteoryx.me>2023-12-04 01:21:54 -0500
committeralyx <alyx@aleteoryx.me>2023-12-04 01:21:54 -0500
commit3438b040ad18b362e5a4d8576d11be462d841432 (patch)
treec89a9a6d99c4d98478a05a16ebafe6924af9ce28 /2023/rs/code/four/src/main.rs
parent21880ee880d321e760b6b560db0696c8af4c98d9 (diff)
downloadadventofcode-3438b040ad18b362e5a4d8576d11be462d841432.tar.gz
adventofcode-3438b040ad18b362e5a4d8576d11be462d841432.tar.bz2
adventofcode-3438b040ad18b362e5a4d8576d11be462d841432.zip
2023.4
Diffstat (limited to '2023/rs/code/four/src/main.rs')
-rw-r--r--2023/rs/code/four/src/main.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/2023/rs/code/four/src/main.rs b/2023/rs/code/four/src/main.rs
new file mode 100644
index 0000000..7508e4a
--- /dev/null
+++ b/2023/rs/code/four/src/main.rs
@@ -0,0 +1,32 @@
+#![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::<u32>();
+ println!("Sum of Win Scores: {winsum}");
+
+ let mut nums = valueified.map(|n| (n as usize, 1)).collect::<Vec<(usize, u32)>>();
+ 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::<u32>();
+ println!("Sum of Recursive Card Counting: {cardsum}");
+}