1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
| use std::{collections::HashMap};
type MonkeyId = u32;
static ROOT_ID: MonkeyId = 0x746F6F72; static HUMN_ID: MonkeyId = 0x6E6D7568;
#[derive(Debug, PartialEq, Clone, Copy)] pub enum Op { Add, Sub, Mul, Div, }
impl Op { fn perform_op(&self, a: i64, b: i64) -> i64 { match self { Op::Add => a + b, Op::Sub => a - b, Op::Mul => a * b, Op::Div => a / b, } } }
#[derive(Debug, PartialEq, Clone)] pub enum Monkey { Yell(i64), Parent(Box<(Monkey, Op, Monkey)>), Human(i64), }
impl Monkey { fn from_str(id: MonkeyId, known_values: &HashMap<MonkeyId, String>) -> Self { let s = known_values.get(&id).unwrap(); if s.chars().filter(|c| c.is_numeric()).count() > 0 { let n = s.parse::<i64>().unwrap(); if id == HUMN_ID { Monkey::Human(n) } else { Monkey::Yell(n) } } else { let mut args = s.splitn(3, ' '); let lhs = Monkey::from_str(str_id_to_monkey_id(args.next().unwrap()), known_values); let op = match args.next().unwrap() { "+" => Op::Add, "-" => Op::Sub, "*" => Op::Mul, "/" => Op::Div, _ => panic!("\"{}\" is not a valid math operator", args.next().unwrap()), }; let rhs = Monkey::from_str(str_id_to_monkey_id(args.next().unwrap()), known_values); Monkey::Parent(Box::new((lhs, op, rhs))) } }
fn into_children(self) -> (Monkey, Op, Monkey) { if let Monkey::Parent(b) = self { *b } else { panic!("attempted to get children of a non-parent") } }
fn value(&self) -> i64 { match self { Monkey::Yell(n) | Monkey::Human(n) => *n, Monkey::Parent(b) => { let (lhs, op, rhs) = b.as_ref(); op.perform_op(lhs.value(), rhs.value()) } } }
fn make_constant(&mut self) { if let Monkey::Parent(b) = self { let (lhs, op, rhs) = b.as_mut(); lhs.make_constant(); rhs.make_constant(); if let (Monkey::Yell(a), Monkey::Yell(b)) = (lhs, rhs) { *self = Monkey::Yell(op.perform_op(*a, *b)); } } }
fn undo_op(self, h: &mut i64) -> Monkey { let (lhs, op, rhs) = self.into_children(); match op { Op::Add => { let (cons, var) = if matches!(lhs, Monkey::Yell(..)) { (lhs, rhs) } else { (rhs, lhs) }; *h -= cons.value(); var } Op::Sub => { if let Monkey::Yell(n) = lhs { *h = n - *h; rhs } else if let Monkey::Yell(n) = rhs { *h += n; lhs } else { unreachable!() } } Op::Mul => { let (cons, var) = if matches!(lhs, Monkey::Yell(..)) { (lhs, rhs) } else { (rhs, lhs) }; *h /= cons.value(); var } Op::Div => { if let Monkey::Yell(n) = lhs { *h = n / *h; rhs } else if let Monkey::Yell(n) = rhs { *h *= n; lhs } else { unreachable!() } } } }
fn solve_for_humn(mut self) -> i64 { self.make_constant(); let (lhs, _, rhs) = self.into_children(); let (constant_side, mut human_side) = if matches!(lhs, Monkey::Yell(..)) { (lhs, rhs) } else { (rhs, lhs) }; let mut h = constant_side.value(); while !matches!(human_side, Monkey::Human(..)) { human_side = human_side.undo_op(&mut h); } h } }
type Input = Monkey;
fn parse(input: &str) -> Input { let known_values = input.lines().filter_map(|line| { let (id, yell_str) = line.split_once(": ")?; Some((str_id_to_monkey_id(id), yell_str.to_string())) }).collect();
Monkey::from_str(ROOT_ID, &known_values) }
fn str_id_to_monkey_id(id: &str) -> MonkeyId { let id = id.as_bytes(); u32::from_le_bytes([id[0], id[1], id[2], id[3]]) }
pub fn part_one(input: Input) -> Option<i64> { Some(input.value()) }
pub fn part_two(input: Input) -> Option<i64> { Some(input.solve_for_humn()) }
|