Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2020
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.55 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5.  
  6. fn main() {
  7.     let testinput = vec![1,9,10,3,2,3,11,0,99,30,40,50];
  8.     runp1(testinput) ;
  9. }
  10.    
  11.  
  12.  
  13. fn runp1(input: Vec<i16>) {
  14.  
  15.     let mut instruction_list = input.clone();
  16.     let mut index = 0;
  17.  
  18.     loop {
  19.          
  20.         let curr_ins = instruction_list[index];
  21.        
  22.         //println!("curr index: {} ", index );
  23.         //println!("curr list: {:?} ", instruction_list );
  24.  
  25.         match curr_ins {
  26.              
  27.             1 => {
  28.                 let x = instruction_list[ (instruction_list[index+1] as usize) ];
  29.                 let y = instruction_list[ (instruction_list[index+2] as usize) ];
  30.                 let dest = instruction_list[ (instruction_list[index+3] as usize) ];
  31.                 instruction_list[dest as usize] = x+y;
  32.                 index+=4;
  33.             }
  34.  
  35.  
  36.             2 => {
  37.                 let x = instruction_list[ (instruction_list[index+1] as usize) ];
  38.                 let y = instruction_list[ (instruction_list[index+2] as usize) ];
  39.                 let dest = instruction_list[ (instruction_list[index+3] as usize) ];
  40.                 instruction_list[dest as usize] = x*y;
  41.                 index+=4;
  42.             }
  43.             99 => {
  44.                 break;
  45.             }
  46.             _ => {
  47.                 println!("this shouldnt happen ...");
  48.             }
  49.         }
  50.     }
  51.     println!("{:?}", instruction_list);
  52. }
  53.  
  54. /*
  55.  
  56. #[cfg(test)]
  57. mod tests {
  58.     use super::*;
  59.  
  60.     #[test]
  61.     fn test_run() {
  62.             let input = [1,9,10,3,2,3,11,0,99,30,40,50];
  63.         runp1
  64.  
  65.     }
  66. }
  67. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement