Optimized day 11 part 2
This commit is contained in:
parent
3b92d6f3ed
commit
6c0413adf4
22
src/main.rs
22
src/main.rs
|
@ -46,7 +46,7 @@ fn parse(input: &'static str) -> (Vec<Monkey>, usize) {
|
||||||
.split(',')
|
.split(',')
|
||||||
.map(|c| {
|
.map(|c| {
|
||||||
c.bytes()
|
c.bytes()
|
||||||
.filter(|c| (b'0'..=b'9').contains(c))
|
.filter(|&c| c != b' ')
|
||||||
.fold(0, |a, x| (a * 10) + (x - b'0') as usize)
|
.fold(0, |a, x| (a * 10) + (x - b'0') as usize)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
@ -103,24 +103,26 @@ fn main() {
|
||||||
|
|
||||||
fn solution(mut input: Vec<Monkey>, lcm: usize) -> usize {
|
fn solution(mut input: Vec<Monkey>, lcm: usize) -> usize {
|
||||||
let mlen = input.len();
|
let mlen = input.len();
|
||||||
let mut activity = vec![0; mlen];
|
let mut activity = [0; 8];
|
||||||
|
|
||||||
for _ in 0..10000 {
|
for _ in 0..10000 {
|
||||||
for i in 0..mlen {
|
for i in 0..mlen {
|
||||||
let monkey = input[i].clone();
|
activity[i] += input[i].items.len();
|
||||||
activity[i] += monkey.items.len();
|
let if_true = input[i].if_true;
|
||||||
|
let if_false = input[i].if_false;
|
||||||
|
let operation = input[i].operation;
|
||||||
|
let div_by_test = input[i].div_by_test;
|
||||||
|
|
||||||
for &item in monkey.items.iter() {
|
while let Some(item) = input[i].items.pop() {
|
||||||
let newwlevel = monkey.operation.apply(item);
|
let newwlevel = operation.apply(item);
|
||||||
let newwlevel = newwlevel % lcm;
|
let newwlevel = newwlevel % lcm;
|
||||||
|
|
||||||
if newwlevel % monkey.div_by_test == 0 {
|
if newwlevel % div_by_test == 0 {
|
||||||
input[monkey.if_true].items.push(newwlevel);
|
input[if_true].items.push(newwlevel);
|
||||||
} else {
|
} else {
|
||||||
input[monkey.if_false].items.push(newwlevel);
|
input[if_false].items.push(newwlevel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
input[i].items.clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user