Implement 2022 day 11 part 1

This commit is contained in:
2022-12-11 20:34:47 +01:00
parent a7188186c3
commit 9a63adc355
3 changed files with 157 additions and 19 deletions

View File

@@ -1,5 +1,7 @@
//! Common helper utilities to all days
use std::cmp::Ordering;
use anyhow::Result;
use nom::combinator::map;
use nom::error::ErrorKind;
@@ -116,3 +118,18 @@ where
(b, a)
}
}
/// Some magic to get two mutable references into the same slice
pub fn get_both<T>(slice: &mut [T], first: usize, second: usize) -> (&mut T, &mut T) {
match first.cmp(&second) {
Ordering::Greater => {
let (begin, end) = slice.split_at_mut(first);
(&mut end[0], &mut begin[second])
}
Ordering::Less => {
let (begin, end) = slice.split_at_mut(second);
(&mut begin[first], &mut end[0])
}
Ordering::Equal => panic!("Tried to get the same index twice {first}"),
}
}