From 2641a35d5473beb6889b299b54de294fbf782b1f Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 7 Jul 2025 11:47:29 +0200 Subject: Day 21 2022 --- 2022/21/common.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 2022/21/common.rs (limited to '2022/21/common.rs') diff --git a/2022/21/common.rs b/2022/21/common.rs new file mode 100644 index 0000000..24dc37b --- /dev/null +++ b/2022/21/common.rs @@ -0,0 +1,51 @@ +#[derive(Debug)] +pub struct Operation { + pub m1: String, + pub m2: String, + pub op: char +} + +#[derive(Debug)] +pub enum MonkeyType { + Num(i64), + Op(Operation) +} + +#[derive(Debug)] +pub struct Monkey { + pub name: String, + pub kind: MonkeyType +} + +pub fn read_monkeys_from_stdin() -> Vec { + let mut v = vec![]; + let mut line = String::new(); + while std::io::stdin().read_line(&mut line).unwrap() > 0 { + let name = String::from(&line[0..4]); + let monkey = match line.chars().nth(6).unwrap() { + '0'..='9' => { + let n = line[6..line.len()-1].parse::().unwrap(); + Monkey { name, kind: MonkeyType::Num(n) } + }, + _ => { + let m1 = String::from(&line[6..10]); + let m2 = String::from(&line[13..17]); + let op = Operation { m1, m2, op: line.chars().nth(11).unwrap() }; + Monkey { name, kind: MonkeyType::Op(op) } + } + }; + v.push(monkey); + line.clear(); + } + v +} + +pub fn apply_op(n1: i64, n2: i64, op: &Operation) -> i64 { + match op.op { + '+' => n1 + n2, + '-' => n1 - n2, + '*' => n1 * n2, + '/' => n1 / n2, + _ => panic!("invalid operator") + } +} -- cgit v1.3