aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

commit c97da9a75995e9cf14447aaf09c1a4867e29625b
parent 50b4179b397183815a89be8ca06cac750f3f02bb
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Fri, 27 Jun 2025 13:52:01 +0200

Day 11 2022

Diffstat:
A2022/11/a.rs | 7+++++++
A2022/11/b.rs | 7+++++++
A2022/11/common.rs | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/11/input | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/11/test | 27+++++++++++++++++++++++++++
5 files changed, 198 insertions(+), 0 deletions(-)

diff --git a/2022/11/a.rs b/2022/11/a.rs @@ -0,0 +1,7 @@ +mod common; +use common::*; + +fn main() { + let mut monkeys = read_input(); + println!("{}", res_after_n_rounds(&mut monkeys, 20, 3)); +} diff --git a/2022/11/b.rs b/2022/11/b.rs @@ -0,0 +1,7 @@ +mod common; +use common::*; + +fn main() { + let mut monkeys = read_input(); + println!("{}", res_after_n_rounds(&mut monkeys, 10000, 1)); +} diff --git a/2022/11/common.rs b/2022/11/common.rs @@ -0,0 +1,102 @@ +#[derive(Debug)] +pub struct Monkey { + items: Vec<i64>, + op: fn(i64, i64) -> i64, + oparg: i64, + div: i64, + iftrue: usize, + iffalse: usize, + pub inspected: usize +} + +fn read_monkey_from_stdin() -> Monkey { + let mut line = String::new(); + let stdin = std::io::stdin(); + + let _ = stdin.read_line(&mut line); + line.clear(); + + let _ = stdin.read_line(&mut line); + let mut l = 16; + let mut items = Vec::<i64>::new(); + while line.chars().nth(l).unwrap() != '\n' { + l += 2; + let end = line[l..].find(|c| c == ',' || c == '\n').unwrap() + l; + items.push(line[l..end].parse::<i64>().unwrap()); + l = end; + } + line.clear(); + + let _ = stdin.read_line(&mut line); + let old = line.chars().nth(25).unwrap() == 'o'; + let oparg = if old { 0 } else { + line[25..line.len()-1].parse::<i64>().unwrap() + }; + let op = match line.chars().nth(23).unwrap() { + '*' => if old { |x, _| x * x } else { |x, y| x * y }, + '+' => if old { |x, _| x + x } else { |x, y| x + y }, + c => panic!("Expected operator, got {}", c) + }; + line.clear(); + + let _ = stdin.read_line(&mut line); + let div = line[21..line.len()-1].parse::<i64>().unwrap(); + line.clear(); + + let _ = stdin.read_line(&mut line); + let iftrue = line[29..line.len()-1].parse::<usize>().unwrap(); + line.clear(); + + let _ = stdin.read_line(&mut line); + let iffalse = line[30..line.len()-1].parse::<usize>().unwrap(); + line.clear(); + + Monkey { items, op, oparg, div, iftrue, iffalse, inspected: 0 } +} + +pub fn read_input() -> Vec<Monkey> { + let mut v = Vec::<Monkey>::new(); + let mut line = String::new(); + loop { + v.push(read_monkey_from_stdin()); + if std::io::stdin().read_line(&mut line).unwrap() == 0 { break; } + } + v +} + +fn play_round(m: &mut Vec<Monkey>, div: i64, modulus: i64) { + for i in 0..m.len() { + for k in 0..m[i].items.len() { + let mut x = (m[i].op)(m[i].items[k], m[i].oparg) / div; + if div == 1 { x %= modulus; } + let j = if x % m[i].div == 0 { m[i].iftrue } else { m[i].iffalse }; + m[j].items.push(x); + } + m[i].inspected += m[i].items.len(); + m[i].items.clear(); + } +} + +fn get_modulus(monkeys: &Vec<Monkey>) -> i64 { + let mut modulus = 1; + for m in monkeys { modulus *= m.div; } + modulus +} + +pub fn res_after_n_rounds(m: &mut Vec<Monkey>, n: usize, div: i64) -> usize { + let modulus = get_modulus(&m); + for _ in 0..n { play_round(m, div, modulus); } + + let mut m1 = 0; + let mut m2 = 0; + for m in m { + if m.inspected >= m1 { + m2 = m1; + m1 = m.inspected; + } else if m.inspected >= m2 { + m2 = m.inspected; + } + } + + m1 * m2 +} diff --git a/2022/11/input b/2022/11/input @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 80 + Operation: new = old * 5 + Test: divisible by 2 + If true: throw to monkey 4 + If false: throw to monkey 3 + +Monkey 1: + Starting items: 75, 83, 74 + Operation: new = old + 7 + Test: divisible by 7 + If true: throw to monkey 5 + If false: throw to monkey 6 + +Monkey 2: + Starting items: 86, 67, 61, 96, 52, 63, 73 + Operation: new = old + 5 + Test: divisible by 3 + If true: throw to monkey 7 + If false: throw to monkey 0 + +Monkey 3: + Starting items: 85, 83, 55, 85, 57, 70, 85, 52 + Operation: new = old + 8 + Test: divisible by 17 + If true: throw to monkey 1 + If false: throw to monkey 5 + +Monkey 4: + Starting items: 67, 75, 91, 72, 89 + Operation: new = old + 4 + Test: divisible by 11 + If true: throw to monkey 3 + If false: throw to monkey 1 + +Monkey 5: + Starting items: 66, 64, 68, 92, 68, 77 + Operation: new = old * 2 + Test: divisible by 19 + If true: throw to monkey 6 + If false: throw to monkey 2 + +Monkey 6: + Starting items: 97, 94, 79, 88 + Operation: new = old * old + Test: divisible by 5 + If true: throw to monkey 2 + If false: throw to monkey 7 + +Monkey 7: + Starting items: 77, 85 + Operation: new = old + 6 + Test: divisible by 13 + If true: throw to monkey 4 + If false: throw to monkey 0 diff --git a/2022/11/test b/2022/11/test @@ -0,0 +1,27 @@ +Monkey 0: + Starting items: 79, 98 + Operation: new = old * 19 + Test: divisible by 23 + If true: throw to monkey 2 + If false: throw to monkey 3 + +Monkey 1: + Starting items: 54, 65, 75, 74 + Operation: new = old + 6 + Test: divisible by 19 + If true: throw to monkey 2 + If false: throw to monkey 0 + +Monkey 2: + Starting items: 79, 60, 97 + Operation: new = old * old + Test: divisible by 13 + If true: throw to monkey 1 + If false: throw to monkey 3 + +Monkey 3: + Starting items: 74 + Operation: new = old + 3 + Test: divisible by 17 + If true: throw to monkey 0 + If false: throw to monkey 1