This commit is contained in:
kageru 2022-12-15 20:50:38 +01:00
parent 606f433196
commit d303f55a4e
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
5 changed files with 21 additions and 20 deletions

@ -28,8 +28,8 @@ fn is_visible_1d<'a>(iter: impl IntoIterator<Item = &'a u8>, rev: bool) -> Vec<b
v
}
fn transpose<T: Copy>(v: &Vec<Vec<T>>) -> Vec<Vec<T>> {
let mut v = v.clone();
fn transpose<T: Copy>(v: &[Vec<T>]) -> Vec<Vec<T>> {
let mut v = v.to_vec();
let size = v.len();
for i in 0..size {
for j in i + 1..size {

@ -61,21 +61,22 @@ fn monkey_business<const ITERATIONS: usize, const STRESS_REDUCTION: usize>(parse
for _ in 0..ITERATIONS {
let mut i = 0;
while i < monkeys.len() {
let monkey = monkeys.get_mut(i).unwrap();
monkey.inspection_count += monkey.items.len();
while !monkey.items.is_empty() {
moved.extend(monkey.items.drain(..).map(|mut stress| {
stress = match monkey.op {
MonkeyOp::Add(x) => stress + x.unwrap_or(stress),
MonkeyOp::Mul(x) => stress * x.unwrap_or(stress),
} / STRESS_REDUCTION;
if stress > lcm {
stress %= lcm;
}
(stress, if stress % monkey.div_test == 0 { monkey.true_dst } else { monkey.false_dst })
}));
{
let monkey = monkeys.get_mut(i).unwrap();
monkey.inspection_count += monkey.items.len();
while !monkey.items.is_empty() {
moved.extend(monkey.items.drain(..).map(|mut stress| {
stress = match monkey.op {
MonkeyOp::Add(x) => stress + x.unwrap_or(stress),
MonkeyOp::Mul(x) => stress * x.unwrap_or(stress),
} / STRESS_REDUCTION;
if stress > lcm {
stress %= lcm;
}
(stress, if stress % monkey.div_test == 0 { monkey.true_dst } else { monkey.false_dst })
}));
}
}
drop(monkey);
for (item, dst) in moved.drain(..) {
monkeys.get_mut(dst).unwrap().items.push(item);
}

@ -11,7 +11,7 @@ use aoc2022::{
const DAY: usize = 12;
type Parsed = (Position2D, Position2D, VecGrid<u8>);
fn find_and_modify(grid: &mut Vec<Vec<u8>>, needle: u8, replacement: u8) -> (usize, usize) {
fn find_and_modify(grid: &mut [Vec<u8>], needle: u8, replacement: u8) -> (usize, usize) {
grid.iter_mut()
.enumerate()
.find_map(|(x, line)| {

@ -44,14 +44,14 @@ fn simulate((x, y): (usize, usize), cave: &Cave) -> Option<(usize, usize)> {
// abusing scan here to simulate a stateful for-loop with a counter
fn part1((cave, _): &Parsed) -> usize {
repeat(()).scan(cave.to_owned(), |cave, _| simulate(SAND_SOURCE, &cave).map(|(x, y)| cave[x][y] = true)).count()
repeat(()).scan(cave.to_owned(), |cave, _| simulate(SAND_SOURCE, cave).map(|(x, y)| cave[x][y] = true)).count()
}
fn part2((cave, max_y): &Parsed) -> usize {
let mut cave = cave.to_owned();
cave.iter_mut().for_each(|row| row[max_y + 2] = true);
1 + repeat(())
.scan(cave, |cave, _| simulate(SAND_SOURCE, &cave).map(|(x, y)| cave[x][y] = true).filter(|_| !cave[SAND_SOURCE.0][SAND_SOURCE.1]))
.scan(cave, |cave, _| simulate(SAND_SOURCE, cave).map(|(x, y)| cave[x][y] = true).filter(|_| !cave[SAND_SOURCE.0][SAND_SOURCE.1]))
.count()
}

@ -1,5 +1,5 @@
pub fn read_file(day: usize) -> String {
std::fs::read_to_string(std::env::var("AOC_INPUT").unwrap_or_else(|_| format!("inputs/day{:0>2}", day))).unwrap()
std::fs::read_to_string(std::env::var("AOC_INPUT").unwrap_or_else(|_| format!("inputs/day{day:0>2}"))).unwrap()
}
pub fn parse_nums(l: &str) -> Vec<usize> {