a few days
This commit is contained in:
109
d21/src/a1.rs
109
d21/src/a1.rs
@ -1,4 +1,113 @@
|
||||
use std::{collections::HashMap, hash::Hash};
|
||||
|
||||
|
||||
enum Operand {
|
||||
Ind(String),
|
||||
Dir(i64),
|
||||
}
|
||||
|
||||
impl Operand {
|
||||
fn from(op :&str) -> Self {
|
||||
|
||||
match op.parse::<i64>() {
|
||||
Ok(i) => { Operand::Dir(i) }
|
||||
Err(_) => { Operand::Ind(op.to_owned()) }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
enum Operation {
|
||||
ADD,
|
||||
SUB,
|
||||
MUL,
|
||||
DIV,
|
||||
}
|
||||
|
||||
impl Operation {
|
||||
fn from(op :&str) -> Self {
|
||||
match op {
|
||||
"+" => { Operation::ADD }
|
||||
"-" => { Operation::SUB }
|
||||
"*" => { Operation::MUL }
|
||||
"/" => { Operation::DIV }
|
||||
|
||||
_ => { std::process::exit(1) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Monkey {
|
||||
op1 :Operand,
|
||||
op2 :Operand,
|
||||
operation :Operation,
|
||||
}
|
||||
|
||||
enum MonkeyExcl {
|
||||
Monkey(Monkey),
|
||||
Num(i64),
|
||||
}
|
||||
|
||||
impl Monkey {
|
||||
fn from(operation :Operation, op1 :Operand, op2 :Operand) -> Self {
|
||||
Monkey { op1: op1, op2: op2, operation: operation }
|
||||
}
|
||||
}
|
||||
|
||||
fn find_result(goal :String, map :&HashMap<String, MonkeyExcl>) -> i64 {
|
||||
|
||||
let monk = map.get(&goal).unwrap();
|
||||
|
||||
match monk {
|
||||
MonkeyExcl::Num(i) => { *i }
|
||||
MonkeyExcl::Monkey(monk) => {
|
||||
|
||||
let op1 = {
|
||||
match &monk.op1 {
|
||||
Operand::Dir(i) => { *i }
|
||||
Operand::Ind(s) => { find_result(s.to_string(), map) }
|
||||
}
|
||||
};
|
||||
|
||||
let op2 = {
|
||||
match &monk.op2 {
|
||||
Operand::Dir(i) => { *i }
|
||||
Operand::Ind(s) => { find_result(s.to_string(), map) }
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
match monk.operation {
|
||||
Operation::ADD => { op1 + op2 }
|
||||
Operation::SUB => { op1 - op2 }
|
||||
Operation::MUL => { op1 * op2 }
|
||||
Operation::DIV => { op1 / op2 }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn run(inp :Vec<String>) {
|
||||
|
||||
let map :HashMap<String, MonkeyExcl> = inp.iter().map(|elem| {
|
||||
let repl = elem.replace(":", "");
|
||||
let tmp = repl.split(' ').collect::<Vec<&str>>();
|
||||
|
||||
if tmp.len() == 2 {
|
||||
(tmp[0].to_owned(), MonkeyExcl::Num(tmp[1].parse::<i64>().unwrap()))
|
||||
} else {
|
||||
|
||||
let monk = Monkey::from(Operation::from(tmp[2]), Operand::from(tmp[1]), Operand::from(tmp[3]));
|
||||
(tmp[0].to_owned(), MonkeyExcl::Monkey(monk))
|
||||
|
||||
}
|
||||
|
||||
|
||||
}).collect();
|
||||
|
||||
let result = find_result("root".to_owned(), &map);
|
||||
|
||||
println!("a1: {}", result);
|
||||
}
|
108
d21/src/a2.rs
108
d21/src/a2.rs
@ -1,5 +1,113 @@
|
||||
use std::{collections::HashMap, hash::Hash};
|
||||
|
||||
|
||||
enum Operand {
|
||||
Ind(String),
|
||||
Dir(i64),
|
||||
}
|
||||
|
||||
impl Operand {
|
||||
fn from(op :&str) -> Self {
|
||||
|
||||
match op.parse::<i64>() {
|
||||
Ok(i) => { Operand::Dir(i) }
|
||||
Err(_) => { Operand::Ind(op.to_owned()) }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
enum Operation {
|
||||
ADD,
|
||||
SUB,
|
||||
MUL,
|
||||
DIV,
|
||||
}
|
||||
|
||||
impl Operation {
|
||||
fn from(op :&str) -> Self {
|
||||
match op {
|
||||
"+" => { Operation::ADD }
|
||||
"-" => { Operation::SUB }
|
||||
"*" => { Operation::MUL }
|
||||
"/" => { Operation::DIV }
|
||||
|
||||
_ => { std::process::exit(1) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Monkey {
|
||||
op1 :Operand,
|
||||
op2 :Operand,
|
||||
operation :Operation,
|
||||
}
|
||||
|
||||
enum MonkeyExcl {
|
||||
Monkey(Monkey),
|
||||
Num(i64),
|
||||
}
|
||||
|
||||
impl Monkey {
|
||||
fn from(operation :Operation, op1 :Operand, op2 :Operand) -> Self {
|
||||
Monkey { op1: op1, op2: op2, operation: operation }
|
||||
}
|
||||
}
|
||||
|
||||
fn find_result(goal :String, map :&HashMap<String, MonkeyExcl>) -> i64 {
|
||||
|
||||
let monk = map.get(&goal).unwrap();
|
||||
|
||||
match monk {
|
||||
MonkeyExcl::Num(i) => { *i }
|
||||
MonkeyExcl::Monkey(monk) => {
|
||||
|
||||
let op1 = {
|
||||
match &monk.op1 {
|
||||
Operand::Dir(i) => { *i }
|
||||
Operand::Ind(s) => { find_result(s.to_string(), map) }
|
||||
}
|
||||
};
|
||||
|
||||
let op2 = {
|
||||
match &monk.op2 {
|
||||
Operand::Dir(i) => { *i }
|
||||
Operand::Ind(s) => { find_result(s.to_string(), map) }
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
match monk.operation {
|
||||
Operation::ADD => { op1 + op2 }
|
||||
Operation::SUB => { op1 - op2 }
|
||||
Operation::MUL => { op1 * op2 }
|
||||
Operation::DIV => { op1 / op2 }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn run(inp :Vec<String>) {
|
||||
|
||||
let map :HashMap<String, MonkeyExcl> = inp.iter().map(|elem| {
|
||||
let repl = elem.replace(":", "");
|
||||
let tmp = repl.split(' ').collect::<Vec<&str>>();
|
||||
|
||||
if tmp.len() == 2 {
|
||||
(tmp[0].to_owned(), MonkeyExcl::Num(tmp[1].parse::<i64>().unwrap()))
|
||||
} else {
|
||||
|
||||
let monk = Monkey::from(Operation::from(tmp[2]), Operand::from(tmp[1]), Operand::from(tmp[3]));
|
||||
(tmp[0].to_owned(), MonkeyExcl::Monkey(monk))
|
||||
|
||||
}
|
||||
|
||||
|
||||
}).collect();
|
||||
|
||||
let result = find_result("root".to_owned(), &map);
|
||||
|
||||
println!("a1: {}", result);
|
||||
}
|
@ -32,7 +32,7 @@ fn read_file(path :&str) -> Vec<String> {
|
||||
|
||||
fn main() {
|
||||
|
||||
let inp :Vec<String> = read_file("input.txt");
|
||||
let inp :Vec<String> = read_file("test_input.txt");
|
||||
|
||||
a1::run(inp.clone());
|
||||
|
||||
|
Reference in New Issue
Block a user