From 6bd5db6251662abb5c09f59e06ad3f242405ad36 Mon Sep 17 00:00:00 2001 From: Andrew Coleman Date: Wed, 9 Jan 2019 12:28:51 -0500 Subject: [PATCH] cargo fmt --- 2018/day16/src/main.rs | 167 ++++++++++++++++++++++++++++------------- 1 file changed, 115 insertions(+), 52 deletions(-) diff --git a/2018/day16/src/main.rs b/2018/day16/src/main.rs index 4c42471..f96e4fd 100644 --- a/2018/day16/src/main.rs +++ b/2018/day16/src/main.rs @@ -45,7 +45,7 @@ fn read_samples_from_file(filename: &str) -> Vec { samples.push(OpSample { before, instruction, - after + after, }); } samples @@ -61,71 +61,109 @@ fn read_instructions_from_file(filename: &str) -> Vec { 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,15 +333,17 @@ fn reduce_decode_map(map: &mut HashMap>) -> 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); } for op in this_pass_found_opcodes.iter() { - map.retain(|k,_| k != op); + map.retain(|k, _| k != op); } for instr in this_pass_found_instr.iter() { - for (_k,v) in map.iter_mut() { + for (_k, v) in map.iter_mut() { v.retain(|i| i != instr); } } @@ -301,12 +359,19 @@ fn opcodes_part_one(samples: &Vec) -> (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| { - m.insert(op); - }).or_insert(HashSet::<&str>::new()); + decode_map + .entry(instruction) + .and_modify(|m| { + m.insert(op); + }) + .or_insert(HashSet::<&str>::new()); } } let final_decode_map = reduce_decode_map(&mut decode_map); @@ -321,26 +386,24 @@ fn execute_program(decode_table: &DecodeTable, instructions: &Vec) for instruction in instructions.iter() { debug!("{:?}", instruction); registers = match decode_table.get(&instruction[0]) { - Some(x) => { - match x.as_str() { - "addr" => addr(registers, *instruction), - "addi" => addi(registers, *instruction), - "mulr" => mulr(registers, *instruction), - "muli" => muli(registers, *instruction), - "banr" => banr(registers, *instruction), - "bani" => bani(registers, *instruction), - "borr" => borr(registers, *instruction), - "bori" => bori(registers, *instruction), - "setr" => setr(registers, *instruction), - "seti" => seti(registers, *instruction), - "gtir" => gtir(registers, *instruction), - "gtri" => gtri(registers, *instruction), - "gtrr" => gtrr(registers, *instruction), - "eqir" => eqir(registers, *instruction), - "eqri" => eqri(registers, *instruction), - "eqrr" => eqrr(registers, *instruction), - _ => panic!("No decoded instruction available for {}", x), - } + Some(x) => match x.as_str() { + "addr" => addr(registers, *instruction), + "addi" => addi(registers, *instruction), + "mulr" => mulr(registers, *instruction), + "muli" => muli(registers, *instruction), + "banr" => banr(registers, *instruction), + "bani" => bani(registers, *instruction), + "borr" => borr(registers, *instruction), + "bori" => bori(registers, *instruction), + "setr" => setr(registers, *instruction), + "seti" => seti(registers, *instruction), + "gtir" => gtir(registers, *instruction), + "gtri" => gtri(registers, *instruction), + "gtrr" => gtrr(registers, *instruction), + "eqir" => eqir(registers, *instruction), + "eqri" => eqri(registers, *instruction), + "eqrr" => eqrr(registers, *instruction), + _ => panic!("No decoded instruction available for {}", x), }, None => panic!("Unmapped instruction!"), }