aboutsummaryrefslogtreecommitdiff
path: root/2022/21/common.rs
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-07-07 11:47:29 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2025-07-07 11:47:29 +0200
commit2641a35d5473beb6889b299b54de294fbf782b1f (patch)
tree7fc7dcdb3f8750c19385cb3d9de87029d2fadb1b /2022/21/common.rs
parent2f0e522317972711b5e03ad25b38bf03af17e8d5 (diff)
downloadaoc-2641a35d5473beb6889b299b54de294fbf782b1f.tar.gz
aoc-2641a35d5473beb6889b299b54de294fbf782b1f.zip
Day 21 2022
Diffstat (limited to '2022/21/common.rs')
-rw-r--r--2022/21/common.rs51
1 files changed, 51 insertions, 0 deletions
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 @@
1#[derive(Debug)]
2pub struct Operation {
3 pub m1: String,
4 pub m2: String,
5 pub op: char
6}
7
8#[derive(Debug)]
9pub enum MonkeyType {
10 Num(i64),
11 Op(Operation)
12}
13
14#[derive(Debug)]
15pub struct Monkey {
16 pub name: String,
17 pub kind: MonkeyType
18}
19
20pub fn read_monkeys_from_stdin() -> Vec<Monkey> {
21 let mut v = vec![];
22 let mut line = String::new();
23 while std::io::stdin().read_line(&mut line).unwrap() > 0 {
24 let name = String::from(&line[0..4]);
25 let monkey = match line.chars().nth(6).unwrap() {
26 '0'..='9' => {
27 let n = line[6..line.len()-1].parse::<i64>().unwrap();
28 Monkey { name, kind: MonkeyType::Num(n) }
29 },
30 _ => {
31 let m1 = String::from(&line[6..10]);
32 let m2 = String::from(&line[13..17]);
33 let op = Operation { m1, m2, op: line.chars().nth(11).unwrap() };
34 Monkey { name, kind: MonkeyType::Op(op) }
35 }
36 };
37 v.push(monkey);
38 line.clear();
39 }
40 v
41}
42
43pub fn apply_op(n1: i64, n2: i64, op: &Operation) -> i64 {
44 match op.op {
45 '+' => n1 + n2,
46 '-' => n1 - n2,
47 '*' => n1 * n2,
48 '/' => n1 / n2,
49 _ => panic!("invalid operator")
50 }
51}

Generated with cgit - Back to sebastiano.tronto.net