Skip to content

Instantly share code, notes, and snippets.

@blackakula
Created March 22, 2022 07:16
Show Gist options
  • Select an option

  • Save blackakula/edc701820b3ea11204ae8ef5da59c56d to your computer and use it in GitHub Desktop.

Select an option

Save blackakula/edc701820b3ea11204ae8ef5da59c56d to your computer and use it in GitHub Desktop.
1D SPREADSHEET
use std::io;
macro_rules! parse_input {
($x:expr, $t:ident) => ($x.trim().parse::<$t>().unwrap())
}
#[derive(Debug)]
enum Val1 {
Val(i32),
Ref(usize),
None
}
impl Clone for Val1 {
fn clone(&self) -> Self {
match self {
Val1::Val(x) => Val1::Val(x.clone()),
Val1::Ref(index) => Val1::Ref(index.clone()),
Val1::None => Val1::None,
}
}
}
#[derive(Debug)]
enum Op1 {
V,
A,
S,
M
}
impl Clone for Op1 {
fn clone(&self) -> Self {
match self {
Op1::V => Op1::V,
Op1::A => Op1::A,
Op1::S => Op1::S,
Op1::M => Op1::M,
}
}
}
fn parse_value1(arg: String) -> Val1 {
if arg.starts_with('$') {
Val1::Ref(arg[1..].parse().unwrap())
} else if arg.starts_with('_') {
Val1::None
} else {
Val1::Val(arg.parse().unwrap())
}
}
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
fn main() {
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let mut data: Vec<(Op1, Val1, Val1)> = vec![];
let n = parse_input!(input_line, i32);
for _i in 0..n as usize {
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let inputs = input_line.split(" ").collect::<Vec<_>>();
let operation = inputs[0].trim().to_string();
let arg_1 = inputs[1].trim().to_string();
let arg_2 = inputs[2].trim().to_string();
let val_11 = parse_value1(arg_1);
if operation == "VALUE" {
data.push((Op1::V, val_11, Val1::None));
} else {
let val_22 = parse_value1(arg_2);
if operation == "ADD" {
data.push((Op1::A, val_11, val_22));
} else if operation == "SUB" {
data.push((Op1::S, val_11, val_22));
} else if operation == "MULT" {
data.push((Op1::M, val_11, val_22));
} else {
panic!(format!("Operation: {}", operation));
}
};
}
while data.iter().filter(|(op, a, _)| match op {
Op1::V => match a {
Val1::Val(_) => false,
_ => true,
},
_ => true,
}).count() > 0 {
for i in 0..n as usize {
let (op, a, b) = data[i].clone();
let deref_a = match a {
Val1::None => Val1::None,
Val1::Val(x) => Val1::Val(x),
Val1::Ref(index) => {
let (op, a, _) = data[index].clone();
if let Op1::V = op {
if let Val1::Val(x) = a {
Val1::Val(x)
} else {
Val1::Ref(index)
}
} else {
Val1::Ref(index)
}
}
};
let deref_b = match b {
Val1::None => Val1::None,
Val1::Val(x) => Val1::Val(x),
Val1::Ref(index) => {
let (op, a, _) = data[index].clone();
if let Op1::V = op {
if let Val1::Val(x) = a {
Val1::Val(x)
} else {
Val1::Ref(index)
}
} else {
Val1::Ref(index)
}
}
};
data[i] = (op, deref_a, deref_b);
}
for i in 0..n as usize {
let (op, a, b) = data[i].clone();
if let Val1::Val(x) = a {
if let Val1::Val(y) = b {
match op {
Op1::A => {
data[i] = (Op1::V, Val1::Val(x + y), Val1::None);
},
Op1::S => {
data[i] = (Op1::V, Val1::Val(x - y), Val1::None);
},
Op1::M => {
data[i] = (Op1::V, Val1::Val(x * y), Val1::None);
},
_ => ()
}
}
}
}
}
for i in 0..n as usize {
// Write an answer using println!("message...");
// To debug: eprintln!("Debug message...");
// eprintln!("value in cell: {:?}", data[i]);
let (_, v, _) = &data[i];
if let Val1::Val(deref_v) = v {
println!("{}", deref_v);
} else {
println!("1");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment