2022 rust day 11 refactor

This commit is contained in:
Maciej Jur 2022-12-11 13:03:06 +01:00
parent 2a5da384ac
commit 1442254825

View file

@ -15,33 +15,37 @@ pub fn run() -> () {
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
enum Operation { enum Operation {
OldTimesOld, OldTimesOld,
OldPlus(i32), OldPlus(i64),
OldTimes(i32), OldTimes(i64),
} }
struct Monkey { struct Monkey {
items: Vec<i32>, items: Vec<i64>,
operation: Operation, operation: Operation,
test: i32, test: i64,
yeah: usize, yeah: usize,
nope: usize, nope: usize,
} }
#[inline(always)] #[inline(always)]
fn transfer_items(source: &mut VecDeque<i32>, destination: &mut VecDeque<i32>) { fn transfer_items(source: &mut VecDeque<i64>, destination: &mut VecDeque<i64>) {
while let Some(item) = source.pop_front() { while let Some(item) = source.pop_front() {
destination.push_back(item) destination.push_back(item)
} }
} }
fn solve1(data: &Vec<Monkey>) -> i32 { fn simplify_factor(n: i64) -> i64 {
let mut monkeys: Vec<VecDeque<i32>> = data.iter().map(|m| m.items.iter().copied().collect()).collect(); n
let mut inspections: HashMap<usize, i32> = HashMap::new(); }
let mut buffer_yeah: VecDeque<i32> = VecDeque::new(); fn find_inspects(data: &Vec<Monkey>, rounds: i32) -> BinaryHeap<i64> {
let mut buffer_nope: VecDeque<i32> = VecDeque::new(); let mut monkeys: Vec<VecDeque<i64>> = data.iter().map(|m| m.items.iter().copied().collect()).collect();
for _ in 0..20 { let mut inspections: HashMap<usize, i64> = HashMap::new();
let mut buffer_yeah: VecDeque<i64> = VecDeque::new();
let mut buffer_nope: VecDeque<i64> = VecDeque::new();
for _ in 0..rounds {
for i in 0..monkeys.len() { for i in 0..monkeys.len() {
while let Some(old) = monkeys[i].pop_front() { while let Some(old) = monkeys[i].pop_front() {
*inspections.entry(i).or_insert(0) += 1; *inspections.entry(i).or_insert(0) += 1;
@ -60,16 +64,22 @@ fn solve1(data: &Vec<Monkey>) -> i32 {
transfer_items(&mut buffer_nope, &mut monkeys[data[i].nope]); transfer_items(&mut buffer_nope, &mut monkeys[data[i].nope]);
} }
}; };
let mut heap = BinaryHeap::from_iter(inspections.iter().map(|(_, &b)| b));
BinaryHeap::from_iter(inspections.iter().map(|(_, &b)| b))
}
fn solve1(data: &Vec<Monkey>) -> i64 {
let mut heap = find_inspects(&data, 20);
heap.pop().unwrap() * heap.pop().unwrap() heap.pop().unwrap() * heap.pop().unwrap()
} }
fn solve2(data: &Vec<Monkey>) -> i32 { fn solve2(data: &Vec<Monkey>) -> i64 {
2 let mut heap = find_inspects(&data, 10000);
heap.pop().unwrap() * heap.pop().unwrap()
} }
fn get_next_number(re: &Regex, chunk: &mut dyn Iterator<Item=&str>) -> i32 { fn get_next_number(re: &Regex, chunk: &mut dyn Iterator<Item=&str>) -> i64 {
re.find(chunk.next().unwrap()).unwrap() re.find(chunk.next().unwrap()).unwrap()
.as_str() .as_str()
.parse().unwrap() .parse().unwrap()
@ -153,7 +163,6 @@ mod tests {
#[test] #[test]
fn part2() { fn part2() {
let data = parse_data(DATA); assert_eq!(2713310158, solve2(&parse_data(DATA)));
assert_eq!(2, solve2(&data));
} }
} }