Skip to content

Commit

Permalink
Fix the optimization for cancel_opposed_instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
Sellig6792 committed Dec 27, 2022
1 parent ac087d6 commit d1e09d2
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 99 deletions.
10 changes: 5 additions & 5 deletions src/ast/instructions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#[derive(Debug, Clone, PartialEq)]
pub enum InstructionType {
Increment,
Expand All @@ -21,7 +20,6 @@ pub enum InstructionType {
Random,
}


pub trait InstructionTrait<T> {
fn new(instruction_type: InstructionType, content: Option<Vec<T>>) -> Self;
fn get_instruction_type(&self) -> InstructionType;
Expand All @@ -37,9 +35,11 @@ pub struct Instruction {
content: Option<Vec<Instruction>>,
}


impl Instruction {
pub fn new(instruction_type: InstructionType, content: Option<Vec<Instruction>>) -> Instruction {
pub fn new(
instruction_type: InstructionType,
content: Option<Vec<Instruction>>,
) -> Instruction {
Instruction {
instruction_type,
content,
Expand Down Expand Up @@ -79,4 +79,4 @@ impl InstructionTrait<Instruction> for Instruction {
fn get_amount(&self) -> u32 {
1
}
}
}
10 changes: 8 additions & 2 deletions src/ast/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ impl Parser {

'[' => {
let (loop_instructions, new_index) = self._parse(Some(index + 1), Some(']'));
instructions.push(Instruction::new(InstructionType::Loop, Some(loop_instructions)));
instructions.push(Instruction::new(
InstructionType::Loop,
Some(loop_instructions),
));
index = new_index;
}
'{' => {
let (function_instructions, new_index) =
self._parse(Some(index + 1), Some('}'));
instructions.push(Instruction::new(InstructionType::Function, Some(function_instructions)));
instructions.push(Instruction::new(
InstructionType::Function,
Some(function_instructions),
));
index = new_index;
}

Expand Down
2 changes: 1 addition & 1 deletion src/evaluation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod cell;
mod evaluator;
mod scope;
mod cell;

pub use evaluator::Evaluator;

Expand Down
1 change: 0 additions & 1 deletion src/evaluation/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ impl PartialEq<u8> for Cell {
}
}


#[cfg(test)]
mod tests {
use super::*;
Expand Down
42 changes: 27 additions & 15 deletions src/evaluation/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::ast::instructions::{InstructionTrait, InstructionType};
use crate::evaluation::{Cell, Scopes};

pub struct Evaluator<T: InstructionTrait<T>>
where
T: Clone,
where
T: Clone,
{
program: Vec<T>,

Expand All @@ -19,8 +19,8 @@ pub struct Evaluator<T: InstructionTrait<T>>
}

impl<T: InstructionTrait<T> + 'static> Evaluator<T>
where
T: Clone,
where
T: Clone,
{
pub fn new(instructions: Vec<T>) -> Evaluator<T> {
Evaluator {
Expand All @@ -46,10 +46,14 @@ impl<T: InstructionTrait<T> + 'static> Evaluator<T>
for instruction in instructions.iter() {
match &instruction.get_instruction_type() {
InstructionType::Increment => {
self.scopes.get_current_cell_mut().add(instruction.get_amount());
self.scopes
.get_current_cell_mut()
.add(instruction.get_amount());
}
InstructionType::Decrement => {
self.scopes.get_current_cell_mut().sub(instruction.get_amount());
self.scopes
.get_current_cell_mut()
.sub(instruction.get_amount());
}

InstructionType::MoveLeft => {
Expand All @@ -66,10 +70,13 @@ impl<T: InstructionTrait<T> + 'static> Evaluator<T>
// Convert the input to a vector of u8
self.input = input.trim().bytes().collect();
}
self.scopes.get_current_cell_mut().set_value(self.input.remove(0));
self.scopes
.get_current_cell_mut()
.set_value(self.input.remove(0));
}
InstructionType::Output => {
self.output_buffer.push(self.scopes.get_current_cell().get_value());
self.output_buffer
.push(self.scopes.get_current_cell().get_value());
}

InstructionType::Loop => {
Expand All @@ -87,7 +94,8 @@ impl<T: InstructionTrait<T> + 'static> Evaluator<T>
self.evaluate(
Some(
self.scopes
.get_scope_at(self.scopes.get_scope_index() - 1).unwrap()
.get_scope_at(self.scopes.get_scope_index() - 1)
.unwrap()
.get_function(self.scopes.get_index())
.clone(),
),
Expand All @@ -97,7 +105,8 @@ impl<T: InstructionTrait<T> + 'static> Evaluator<T>
}

InstructionType::MoveLeftScope => {
self.scopes.move_left_scope(instruction.get_amount() as usize);
self.scopes
.move_left_scope(instruction.get_amount() as usize);
}
InstructionType::MoveRightScope => {
if self.scope_pointer != self.scopes.len() - 1 {
Expand All @@ -112,10 +121,14 @@ impl<T: InstructionTrait<T> + 'static> Evaluator<T>
If the left cell's value is greater than the right cell's value,
generate a random number between the left cell's value and 255 and the right cell's value and 0
*/
let left: &Cell =
self.scopes.get_cell_at(self.scopes.get_index() - 1).unwrap();
let right: &Cell =
self.scopes.get_cell_at(self.scopes.get_index() + 1).unwrap();
let left: &Cell = self
.scopes
.get_cell_at(self.scopes.get_index() - 1)
.unwrap();
let right: &Cell = self
.scopes
.get_cell_at(self.scopes.get_index() + 1)
.unwrap();

if right > left {
let r = rand::thread_rng().gen_range(left.get_value()..=right.get_value());
Expand All @@ -142,4 +155,3 @@ impl<T: InstructionTrait<T> + 'static> Evaluator<T>
}
}
}

12 changes: 5 additions & 7 deletions src/evaluation/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ where
scope_index: usize,
}

impl<T> Scopes<T> where T: InstructionTrait<T> {
impl<T> Scopes<T>
where
T: InstructionTrait<T>,
{
pub fn new() -> Scopes<T> {
Scopes {
index: 0,
Expand Down Expand Up @@ -128,11 +131,7 @@ impl<T> Scopes<T> where T: InstructionTrait<T> {
// If the index is less than 0, set the index to the first scope
let sub: isize = self.scope_index as isize - amount as isize;

self.scope_index = if sub < 0 {
0
} else {
sub as usize
};
self.scope_index = if sub < 0 { 0 } else { sub as usize };
}

pub fn get_current_cell(&self) -> &Cell {
Expand Down Expand Up @@ -167,7 +166,6 @@ impl<T> Scopes<T> where T: InstructionTrait<T> {
self.get_current_scope_mut().function_memory.get_mut(index)
}


pub fn push(&mut self) {
self.scopes.push(Scope::new());
self.scope_index += 1;
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

mod ast;
mod evaluation;
mod optimization;
Expand Down
2 changes: 1 addition & 1 deletion src/optimization.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod optimized_instructions;
mod optimizer;

pub use optimizer::Optimizer;
pub use optimizer::Optimizer;
91 changes: 49 additions & 42 deletions src/optimization/optimized_instructions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ast::instructions::{InstructionType, InstructionTrait};
use crate::ast::instructions::{InstructionTrait, InstructionType};

#[derive(Clone)]
pub struct OptimizedInstruction {
Expand All @@ -7,8 +7,11 @@ pub struct OptimizedInstruction {
pub amount: u32,
}

impl OptimizedInstruction{
pub fn new(instruction_type: InstructionType, content: Option<Vec<OptimizedInstruction>>) -> OptimizedInstruction {
impl OptimizedInstruction {
pub fn new(
instruction_type: InstructionType,
content: Option<Vec<OptimizedInstruction>>,
) -> OptimizedInstruction {
OptimizedInstruction {
instruction_type,
content,
Expand All @@ -24,52 +27,47 @@ impl OptimizedInstruction{
self.amount -= amount
}

pub fn set_amount(&mut self, amount: u32) {
self.amount = amount
}

pub fn is_opposed(&self, other: &OptimizedInstruction) -> bool {
match self.get_instruction_type() {
InstructionType::Increment => {
match other.get_instruction_type() {
InstructionType::Decrement => true,
_ => false,
}
}
InstructionType::Decrement => {
match other.get_instruction_type() {
InstructionType::Increment => true,
_ => false,
}
}
InstructionType::MoveLeft => {
match other.get_instruction_type() {
InstructionType::MoveRight => true,
_ => false,
}
}
InstructionType::MoveRight => {
match other.get_instruction_type() {
InstructionType::MoveLeft => true,
_ => false,
}
}
InstructionType::MoveLeftScope => {
match other.get_instruction_type() {
InstructionType::MoveRightScope => true,
_ => false,
}
}
InstructionType::MoveRightScope => {
match other.get_instruction_type() {
InstructionType::MoveLeftScope => true,
_ => false,
}
}
InstructionType::Increment => match other.get_instruction_type() {
InstructionType::Decrement => true,
_ => false,
},
InstructionType::Decrement => match other.get_instruction_type() {
InstructionType::Increment => true,
_ => false,
},
InstructionType::MoveLeft => match other.get_instruction_type() {
InstructionType::MoveRight => true,
_ => false,
},
InstructionType::MoveRight => match other.get_instruction_type() {
InstructionType::MoveLeft => true,
_ => false,
},
InstructionType::MoveLeftScope => match other.get_instruction_type() {
InstructionType::MoveRightScope => true,
_ => false,
},
InstructionType::MoveRightScope => match other.get_instruction_type() {
InstructionType::MoveLeftScope => true,
_ => false,
},

_ => false
_ => false,
}
}
}

impl InstructionTrait<OptimizedInstruction> for OptimizedInstruction {
fn new(instruction_type: InstructionType, content: Option<Vec<OptimizedInstruction>>) -> OptimizedInstruction {
fn new(
instruction_type: InstructionType,
content: Option<Vec<OptimizedInstruction>>,
) -> OptimizedInstruction {
OptimizedInstruction {
instruction_type,
content,
Expand Down Expand Up @@ -102,4 +100,13 @@ impl InstructionTrait<OptimizedInstruction> for OptimizedInstruction {
fn get_amount(&self) -> u32 {
self.amount
}
}
}

impl std::fmt::Debug for OptimizedInstruction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
match &self.amount {
1 => write!(f, "{:?}", self.instruction_type),
_ => write!(f, "{:?}({})", self.instruction_type, self.amount),
}
}
}
Loading

0 comments on commit d1e09d2

Please sign in to comment.