1
0
Fork 0

cargo fmt

main
Andrew Coleman 2019-01-09 12:28:51 -05:00
parent 921e892ca8
commit 6bd5db6251
1 changed files with 115 additions and 52 deletions

View File

@ -45,7 +45,7 @@ fn read_samples_from_file(filename: &str) -> Vec<OpSample> {
samples.push(OpSample {
before,
instruction,
after
after,
});
}
samples
@ -61,71 +61,109 @@ fn read_instructions_from_file(filename: &str) -> Vec<Instruction> {
fn addr(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize] + input_registers[instruction[2] as usize];
debug!("addr: input {:?} output {:?}", input_registers, output_registers);
output_registers[instruction[3] as usize] =
input_registers[instruction[1] as usize] + input_registers[instruction[2] as usize];
debug!(
"addr: input {:?} output {:?}",
input_registers, output_registers
);
output_registers
}
fn addi(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize] + instruction[2];
debug!("addi: input {:?} output {:?}", input_registers, output_registers);
output_registers[instruction[3] as usize] =
input_registers[instruction[1] as usize] + instruction[2];
debug!(
"addi: input {:?} output {:?}",
input_registers, output_registers
);
output_registers
}
fn mulr(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize] * input_registers[instruction[2] as usize];
debug!("mulr: input {:?} output {:?}", input_registers, output_registers);
output_registers[instruction[3] as usize] =
input_registers[instruction[1] as usize] * input_registers[instruction[2] as usize];
debug!(
"mulr: input {:?} output {:?}",
input_registers, output_registers
);
output_registers
}
fn muli(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize] * instruction[2];
debug!("muli: input {:?} output {:?}", input_registers, output_registers);
output_registers[instruction[3] as usize] =
input_registers[instruction[1] as usize] * instruction[2];
debug!(
"muli: input {:?} output {:?}",
input_registers, output_registers
);
output_registers
}
fn banr(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize] & input_registers[instruction[2] as usize];
debug!("banr: input {:?} output {:?}", input_registers, output_registers);
output_registers[instruction[3] as usize] =
input_registers[instruction[1] as usize] & input_registers[instruction[2] as usize];
debug!(
"banr: input {:?} output {:?}",
input_registers, output_registers
);
output_registers
}
fn bani(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize] & instruction[2];
debug!("bani: input {:?} output {:?}", input_registers, output_registers);
output_registers[instruction[3] as usize] =
input_registers[instruction[1] as usize] & instruction[2];
debug!(
"bani: input {:?} output {:?}",
input_registers, output_registers
);
output_registers
}
fn borr(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize] | input_registers[instruction[2] as usize];
debug!("borr: input {:?} output {:?}", input_registers, output_registers);
output_registers[instruction[3] as usize] =
input_registers[instruction[1] as usize] | input_registers[instruction[2] as usize];
debug!(
"borr: input {:?} output {:?}",
input_registers, output_registers
);
output_registers
}
fn bori(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize] | instruction[2];
debug!("bori: input {:?} output {:?}", input_registers, output_registers);
output_registers[instruction[3] as usize] =
input_registers[instruction[1] as usize] | instruction[2];
debug!(
"bori: input {:?} output {:?}",
input_registers, output_registers
);
output_registers
}
fn setr(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = input_registers[instruction[1] as usize];
debug!("setr: input {:?} output {:?}", input_registers, output_registers);
debug!(
"setr: input {:?} output {:?}",
input_registers, output_registers
);
output_registers
}
fn seti(input_registers: Registers, instruction: Instruction) -> Registers {
let mut output_registers = input_registers.clone();
output_registers[instruction[3] as usize] = instruction[1];
debug!("seti: input {:?} output {:?}", input_registers, output_registers);
debug!(
"seti: input {:?} output {:?}",
input_registers, output_registers
);
output_registers
}
@ -136,7 +174,10 @@ fn gtir(input_registers: Registers, instruction: Instruction) -> Registers {
} else {
output_registers[instruction[3] as usize] = 0;
}
debug!("gtir: input {:?} output {:?}", input_registers, output_registers);
debug!(
"gtir: input {:?} output {:?}",
input_registers, output_registers
);
output_registers
}
@ -147,7 +188,10 @@ fn gtri(input_registers: Registers, instruction: Instruction) -> Registers {
} else {
output_registers[instruction[3] as usize] = 0;
}
debug!("gtri: input {:?} output {:?}", input_registers, output_registers);
debug!(
"gtri: input {:?} output {:?}",
input_registers, output_registers
);
output_registers
}
@ -158,7 +202,10 @@ fn gtrr(input_registers: Registers, instruction: Instruction) -> Registers {
} else {
output_registers[instruction[3] as usize] = 0;
}
debug!("gtrr: input {:?} output {:?}", input_registers, output_registers);
debug!(
"gtrr: input {:?} output {:?}",
input_registers, output_registers
);
output_registers
}
@ -169,7 +216,10 @@ fn eqir(input_registers: Registers, instruction: Instruction) -> Registers {
} else {
output_registers[instruction[3] as usize] = 0;
}
debug!("eqir: input {:?} output {:?}", input_registers, output_registers);
debug!(
"eqir: input {:?} output {:?}",
input_registers, output_registers
);
output_registers
}
@ -180,7 +230,10 @@ fn eqri(input_registers: Registers, instruction: Instruction) -> Registers {
} else {
output_registers[instruction[3] as usize] = 0;
}
debug!("eqri: input {:?} output {:?}", input_registers, output_registers);
debug!(
"eqri: input {:?} output {:?}",
input_registers, output_registers
);
output_registers
}
@ -191,7 +244,10 @@ fn eqrr(input_registers: Registers, instruction: Instruction) -> Registers {
} else {
output_registers[instruction[3] as usize] = 0;
}
debug!("eqrr: input {:?} output {:?}", input_registers, output_registers);
debug!(
"eqrr: input {:?} output {:?}",
input_registers, output_registers
);
output_registers
}
@ -277,7 +333,9 @@ fn reduce_decode_map(map: &mut HashMap<u64, HashSet<&str>>) -> DecodeTable {
}
removed_one = true;
let confirmed_instruction = confirmed_instructions.iter().nth(0).unwrap();
results.entry(*confirmed_op).or_insert(confirmed_instruction.to_string());
results
.entry(*confirmed_op)
.or_insert(confirmed_instruction.to_string());
this_pass_found_opcodes.insert(*confirmed_op);
this_pass_found_instr.insert(*confirmed_instruction);
}
@ -301,12 +359,19 @@ fn opcodes_part_one(samples: &Vec<OpSample>) -> (u16, DecodeTable) {
if matched_ops.len() >= 3 {
i += 1;
}
debug!("{:?} matches {} opcodes", sample.instruction, matched_ops.len());
debug!(
"{:?} matches {} opcodes",
sample.instruction,
matched_ops.len()
);
let instruction = sample.instruction[0];
for op in matched_ops.iter() {
decode_map.entry(instruction).and_modify(|m| {
decode_map
.entry(instruction)
.and_modify(|m| {
m.insert(op);
}).or_insert(HashSet::<&str>::new());
})
.or_insert(HashSet::<&str>::new());
}
}
let final_decode_map = reduce_decode_map(&mut decode_map);
@ -321,8 +386,7 @@ fn execute_program(decode_table: &DecodeTable, instructions: &Vec<Instruction>)
for instruction in instructions.iter() {
debug!("{:?}", instruction);
registers = match decode_table.get(&instruction[0]) {
Some(x) => {
match x.as_str() {
Some(x) => match x.as_str() {
"addr" => addr(registers, *instruction),
"addi" => addi(registers, *instruction),
"mulr" => mulr(registers, *instruction),
@ -340,7 +404,6 @@ fn execute_program(decode_table: &DecodeTable, instructions: &Vec<Instruction>)
"eqri" => eqri(registers, *instruction),
"eqrr" => eqrr(registers, *instruction),
_ => panic!("No decoded instruction available for {}", x),
}
},
None => panic!("Unmapped instruction!"),
}