day19/2: optimized

This commit is contained in:
Ishan Jain 2023-12-19 14:52:07 +05:30
parent e156c2d7aa
commit dfdc7ba3c7
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27

View File

@ -8,47 +8,37 @@ extern crate test;
const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")];
#[derive(Debug)] #[derive(Debug, Clone)]
struct Rule { struct Rule<'a> {
jump_to: String, jump_to: &'a [u8],
variable: char, variable: u8,
condition: char, condition: u8,
num: u64, num: u64,
} }
fn process(data: &str) -> u64 { fn process(data: &str) -> u64 {
let mut data = data.split("\n\n"); let data = data.as_bytes();
let mut workflows = HashMap::new(); let mut workflows = HashMap::new();
for workflow in data.next().unwrap().lines() { for workflow in data.split(|&x| x == b'\n') {
let (name, remain) = workflow.split_once(|x| x == '{').unwrap(); if workflow.is_empty() {
let mut rules = vec![]; break;
for branch in remain.split(',') { }
let rule = if branch.contains(|x| x == '<' || x == '>') { let (name, remain) = workflow.split_once(|&x| x == b'{').unwrap();
let branch: Vec<char> = branch.chars().collect();
let mut rules: Vec<Rule> = vec![];
for branch in remain.split(|&x| x == b',') {
let rule = if branch.contains(&b'<') || branch.contains(&b'>') {
let variable = branch[0]; let variable = branch[0];
let condition = branch[1]; let condition = branch[1];
let num: String = branch[2..] let num: String = branch[2..]
.iter() .iter()
.map(|&x| x as char)
.take_while(|&x| x.is_ascii_digit()) .take_while(|&x| x.is_ascii_digit())
.collect(); .collect();
let l = num.len();
let num: u64 = num.parse::<u64>().unwrap(); let num: u64 = num.parse::<u64>().unwrap();
let jump_to = &branch[3 + l..branch.len()];
let mut jump_to = String::new();
for &c in branch.iter().rev() {
if c == '}' {
continue;
}
if c == ':' {
break;
}
jump_to.push(c);
}
let jump_to: String = jump_to.chars().rev().collect();
Rule { Rule {
jump_to, jump_to,
@ -57,28 +47,13 @@ fn process(data: &str) -> u64 {
num, num,
} }
} else { } else {
let branch: Vec<char> = branch.chars().collect(); let jump_to = &branch[0..branch.len() - 1];
let mut jump_to = String::new();
for &c in branch.iter().rev() {
if c == '}' {
continue;
}
if c == ':' {
break;
}
jump_to.push(c);
}
let jump_to: String = jump_to.chars().rev().collect();
Rule { Rule {
jump_to, jump_to,
condition: ' ', condition: 0,
num: 0, num: 0,
variable: ' ', variable: 0,
} }
}; };
rules.push(rule); rules.push(rule);
@ -88,154 +63,78 @@ fn process(data: &str) -> u64 {
} }
let mut answer = 0; let mut answer = 0;
let mut set = Vec::new(); let mut set = Vec::new();
set.push(("in".to_string(), 1..=4000, 1..=4000, 1..=4000, 1..=4000)); let start = vec![b'i', b'n'];
while !set.is_empty() { set.push((start.as_slice(), 1..4001, 1..4001, 1..4001, 1..4001));
let mut next = Vec::new();
for set_ip in set.drain(..) { while let Some((wname, mut x, mut m, mut a, mut s)) = set.pop() {
let (ref wname, mut x, mut m, mut a, mut s) = set_ip.clone(); let rules = workflows.get(wname.as_ref()).unwrap();
let rules = workflows.get(wname.as_str()).unwrap();
for rule in rules { for rule in rules {
match (rule.variable, rule.condition) { let (x, m, a, s) = match (rule.variable, rule.condition) {
('x', '>') if x.contains(&rule.num) => { (b'x', b'>') if x.contains(&rule.num) => {
let new_x = rule.num + 1..=*x.end(); let new_x = rule.num + 1..x.end;
x = x.start..rule.num + 1;
let remain_x = *x.start()..=rule.num; (new_x, m.clone(), a.clone(), s.clone())
x = remain_x; }
(b'x', b'<') if x.contains(&rule.num) => {
let new_x = x.start..rule.num;
x = rule.num..x.end;
if !new_x.is_empty() { (new_x, m.clone(), a.clone(), s.clone())
next.push((
rule.jump_to.clone(),
new_x,
m.clone(),
a.clone(),
s.clone(),
));
} }
} (b'm', b'>') if m.contains(&rule.num) => {
('x', '<') if x.contains(&rule.num) => { let new_m = rule.num + 1..m.end;
let new_x = *x.start()..=rule.num - 1; m = m.start..rule.num + 1;
x = rule.num..=*x.end();
if !new_x.is_empty() {
next.push((
rule.jump_to.clone(),
new_x,
m.clone(),
a.clone(),
s.clone(),
));
}
}
('m', '>') if m.contains(&rule.num) => {
let new_m = rule.num + 1..=*m.end();
m = *m.start()..=rule.num;
if !new_m.is_empty() { (x.clone(), new_m, a.clone(), s.clone())
next.push((
rule.jump_to.clone(),
x.clone(),
new_m,
a.clone(),
s.clone(),
));
} }
} (b'm', b'<') if m.contains(&rule.num) => {
('m', '<') if m.contains(&rule.num) => { let new_m = m.start..rule.num;
let new_m = *m.start()..=rule.num - 1; m = rule.num..m.end;
m = rule.num..=*m.end();
if !new_m.is_empty() { (x.clone(), new_m, a.clone(), s.clone())
next.push((
rule.jump_to.clone(),
x.clone(),
new_m,
a.clone(),
s.clone(),
));
} }
} (b'a', b'>') if a.contains(&rule.num) => {
('a', '>') if a.contains(&rule.num) => { let new_a = rule.num + 1..a.end;
let new_a = rule.num + 1..=*a.end(); a = a.start..rule.num + 1;
a = *a.start()..=rule.num;
if !new_a.is_empty() {
next.push((
rule.jump_to.clone(),
x.clone(),
m.clone(),
new_a,
s.clone(),
));
}
}
('a', '<') if a.contains(&rule.num) => {
let new_a = *a.start()..=rule.num - 1;
a = rule.num..=*a.end();
if !new_a.is_empty() { (x.clone(), m.clone(), new_a, s.clone())
next.push((
rule.jump_to.clone(),
x.clone(),
m.clone(),
new_a,
s.clone(),
));
} }
} (b'a', b'<') if a.contains(&rule.num) => {
('s', '<') if s.contains(&rule.num) => { let new_a = a.start..rule.num;
let new_s = *s.start()..=rule.num - 1; a = rule.num..a.end;
s = rule.num..=*s.end();
if !new_s.is_empty() { (x.clone(), m.clone(), new_a, s.clone())
next.push((
rule.jump_to.clone(),
x.clone(),
m.clone(),
a.clone(),
new_s,
));
} }
(b's', b'>') if s.contains(&rule.num) => {
let new_s = rule.num + 1..s.end;
s = s.start..rule.num + 1;
(x.clone(), m.clone(), a.clone(), new_s)
} }
('s', '>') if s.contains(&rule.num) => { (b's', b'<') if s.contains(&rule.num) => {
let new_s = rule.num + 1..=*s.end(); let new_s = s.start..rule.num;
s = *s.start()..=rule.num; s = rule.num..s.end;
if !new_s.is_empty() {
next.push(( (x.clone(), m.clone(), a.clone(), new_s)
rule.jump_to.clone(),
x.clone(),
m.clone(),
a.clone(),
new_s,
));
}
}
(' ', ' ') => {
next.push((
rule.jump_to.clone(),
x.clone(),
m.clone(),
a.clone(),
s.clone(),
));
} }
(0, 0) => (x.clone(), m.clone(), a.clone(), s.clone()),
_ => unreachable!(), _ => unreachable!(),
} };
}
}
next.retain(|(wname, _, _, _, _)| wname != "R"); if rule.jump_to == [b'R'] {
continue;
for (_, x, m, a, s) in next.extract_if(|(wname, _, _, _, _)| wname == "A") { }
answer += (x.end() - x.start() + 1) if rule.jump_to == [b'A'] {
* (m.end() - m.start() + 1) answer +=
* (a.end() - a.start() + 1) (x.end - x.start) * (m.end - m.start) * (a.end - a.start) * (s.end - s.start);
* (s.end() - s.start() + 1); continue;
}
set.push((&rule.jump_to, x, m, a, s));
} }
set = next;
} }
answer answer