day4: optimized

This commit is contained in:
Ishan Jain 2023-12-04 14:14:09 +05:30
parent 780171280f
commit 2b64d5de7c
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
2 changed files with 36 additions and 33 deletions

View File

@ -9,19 +9,28 @@ const INPUTS: [&[u8]; 2] = [
fn process(data: &[u8]) -> u64 { fn process(data: &[u8]) -> u64 {
let mut total = 0; let mut total = 0;
let mut colon_pos = 0;
let mut vert_tab_pos = 0;
for data in data.split(|&x| x == b'\n') { for data in data.split(|&x| x == b'\n') {
if data.is_empty() { if data.is_empty() {
continue; continue;
} }
if colon_pos == 0 || vert_tab_pos == 0 {
colon_pos = data.iter().position(|&x| x == b':').unwrap();
vert_tab_pos = data.iter().position(|&x| x == b'|').unwrap();
}
let (_, nums) = data.split_once(|&x| x == b':').unwrap(); let (_, nums) = data.split_at(colon_pos + 2);
let (nums, wins) = nums.split_once(|&x| x == b'|').unwrap(); let (nums, wins) = nums.split_at(vert_tab_pos + 1 - colon_pos - 2);
let nums = nums[..nums.len() - 2].split(|&x| x == b' ');
let wins = wins.split(|&x| x == b' ');
let mut bit_map = BitMap::new(); let mut bit_map = BitMap::new();
for win in wins.split(|&x| x == b' ') { for win in wins {
if win.is_empty() { if win.is_empty() {
continue; continue;
} }
@ -31,7 +40,7 @@ fn process(data: &[u8]) -> u64 {
} }
let mut val = 0; let mut val = 0;
for num in nums.split(|&x| x == b' ') { for num in nums {
if num.is_empty() { if num.is_empty() {
continue; continue;
} }

View File

@ -9,33 +9,31 @@ const INPUTS: [&[u8]; 2] = [
]; ];
fn process(data: &[u8]) -> usize { fn process(data: &[u8]) -> usize {
let mut cards = vec![]; let mut counter = vec![0];
let mut colon_pos = 0;
let mut vert_tab_pos = 0;
for data in data.split(|&x| x == b'\n') { for data in data.split(|&x| x == b'\n') {
if data.is_empty() { if data.is_empty() {
continue; continue;
} }
let (x, nums) = data.split_once(|&x| x == b':').unwrap(); if colon_pos == 0 || vert_tab_pos == 0 {
colon_pos = data.iter().position(|&x| x == b':').unwrap();
vert_tab_pos = data.iter().position(|&x| x == b'|').unwrap();
}
let id = parse(&x[5..]); let (x, nums) = data.split_at(colon_pos + 2);
let (nums, wins) = nums.split_once(|&x| x == b'|').unwrap(); let id = parse(&x[4..x.len() - 2]);
let nums = nums.split(|&x| x == b' ');
let (nums, wins) = nums.split_at(vert_tab_pos + 1 - colon_pos - 2);
let nums = nums[..nums.len() - 2].split(|&x| x == b' ');
let wins = wins.split(|&x| x == b' '); let wins = wins.split(|&x| x == b' ');
let mut card = BitMap::new();
let mut win = BitMap::new(); let mut win = BitMap::new();
for num in nums {
if num.is_empty() {
continue;
}
let num = parse(num);
card.set(num);
}
for w in wins { for w in wins {
if w.is_empty() { if w.is_empty() {
continue; continue;
@ -44,18 +42,14 @@ fn process(data: &[u8]) -> usize {
win.set(num); win.set(num);
} }
cards.push((id, card, win)); let sum = nums
} .into_iter()
.filter(|x| !x.is_empty())
.map(parse)
.fold(0, |a, x| a + win.get(x) as usize);
let mut counter = vec![1; cards.len() + 1]; if id + 1 + sum >= counter.len() {
counter[0] = 0; counter.extend(std::iter::repeat(1).take(id + 1 + sum - counter.len()));
for (id, card, wins) in cards.into_iter() {
let mut sum = 0;
for c in 0..100 {
if card.get(c) && wins.get(c) {
sum += 1;
}
} }
let cid = counter[id]; let cid = counter[id];
@ -67,7 +61,7 @@ fn process(data: &[u8]) -> usize {
counter.into_iter().sum::<usize>() counter.into_iter().sum::<usize>()
} }
#[derive(Clone)] #[derive(Debug, Clone)]
struct BitMap(u128); struct BitMap(u128);
impl BitMap { impl BitMap {
#[inline] #[inline]
@ -93,7 +87,7 @@ fn parse(b: &[u8]) -> usize {
let mut pow = 1; let mut pow = 1;
for c in b.iter().rev() { for c in b.iter().rev() {
if !c.is_ascii_digit() { if !c.is_ascii_digit() {
continue; break;
} }
out += (c - b'0') as usize * pow; out += (c - b'0') as usize * pow;
pow *= 10; pow *= 10;