Add day 21

This commit is contained in:
Sebastian Knackstedt 2022-12-21 11:49:59 +01:00
parent a264e1e6d3
commit 195ef4651f
Signed by: sebastian
GPG Key ID: CDCD9AF904D93EF7
6 changed files with 1929 additions and 0 deletions

View File

@ -88,4 +88,9 @@ path = "src/day19/main.rs"
[[bin]]
name = "day20"
path = "src/day20/main.rs"
[[bin]]
name = "day21"
path = "src/day21/main.rs"
[dependencies]

0
day18.txt Normal file
View File

0
day18_test.txt Normal file
View File

1819
day21.txt Normal file

File diff suppressed because it is too large Load Diff

15
day21_test.txt Normal file
View File

@ -0,0 +1,15 @@
root: pppw + sjmn
dbpl: 5
cczh: sllz + lgvd
zczc: 2
ptdq: humn - dvpt
dvpt: 3
lfqf: 4
humn: 5
ljgn: 2
sjmn: drzm * dbpl
sllz: 4
pppw: cczh / lfqf
lgvd: ljgn * ptdq
drzm: hmdt - zczc
hmdt: 32

90
src/day21/main.rs Normal file
View File

@ -0,0 +1,90 @@
use std::collections::HashMap;
use base::read_file;
#[derive(Clone, Debug)]
enum UnparsedOp {
Constant(i64),
Add(String, String),
Sub(String, String),
Mul(String, String),
Div(String, String),
}
fn solve_op(op: &UnparsedOp, data: &HashMap<String, UnparsedOp>) -> i64 {
return match op {
UnparsedOp::Constant(number) => *number,
UnparsedOp::Add(f, s) => solve_op(&data[f], data) + solve_op(&data[s], data),
UnparsedOp::Sub(f, s) => solve_op(&data[f], data) - solve_op(&data[s], data),
UnparsedOp::Mul(f, s) => solve_op(&data[f], data) * solve_op(&data[s], data),
UnparsedOp::Div(f, s) => solve_op(&data[f], data) / solve_op(&data[s], data),
};
}
fn solve(data: &HashMap<String, UnparsedOp>) -> i64 {
let root = &data["root"];
solve_op(root, data)
}
fn solve_eq(data: &mut HashMap<String, UnparsedOp>) -> i64 {
let root = data["root"].clone();
if let UnparsedOp::Add(f, s) = root {
let mut guess = 0;
let mut add = 100000000000;
loop {
let new_guess = guess + add;
data.insert("humn".to_owned(), UnparsedOp::Constant(new_guess));
let left = solve_op(&data[&f], data);
let right = solve_op(&data[&s], data);
if left < right {
add /= 10;
} else {
guess = new_guess;
}
if left == right {
return guess;
}
}
} else {
panic!("Can't handle this");
}
}
fn main() {
let lines = read_file("day21.txt");
let mut data = HashMap::new();
for line in lines {
let splits = line.split(": ").collect::<Vec<_>>();
let name = splits[0];
if let Ok(number) = splits[1].parse::<i64>() {
data.insert(name.to_owned(), UnparsedOp::Constant(number));
} else {
let split_op = splits[1].split_whitespace().collect::<Vec<_>>();
let first = split_op[0].to_owned();
let second = split_op[2].to_owned();
data.insert(name.to_owned(), match split_op[1].chars().next().unwrap() {
'+' => UnparsedOp::Add(first, second),
'-' => UnparsedOp::Sub(first, second),
'*' => UnparsedOp::Mul(first, second),
'/' => UnparsedOp::Div(first, second),
_ => {
panic!("Unknown operation");
}
});
}
}
let result = solve(&data);
println!("Task1: {}", result);
let result = solve_eq(&mut data);
println!("Task2: {}", result);
}