SHARE
TWEET

Untitled

a guest Dec 5th, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::env;
  2. use std::io::{self, prelude::*, BufReader};
  3. use std::fs::File;
  4.  
  5. fn main() {
  6.     let args: Vec<String> = env::args().collect();
  7.     let filename = &args[1];
  8.     day05(&filename).unwrap();
  9. }
  10.  
  11. fn reverse_digits_of(n: i64) -> Vec<i64> {
  12.     let mut digits = Vec::new();
  13.     if n == 0 { digits.push(0); return digits; }
  14.     let mut a = n;
  15.     while a > 0 {
  16.         digits.push(a % 10);
  17.         a /= 10;
  18.     }
  19.     digits
  20. }
  21.  
  22. fn get_if_exists(vec: &Vec<i64>, idx: i64) -> usize {
  23.     if vec.len() > idx as usize {
  24.         return vec[idx as usize] as usize;
  25.     } else {
  26.         return 0usize;
  27.     }
  28. }
  29.  
  30. fn process(code: &Vec<i64>, input: i64) -> i64 {
  31.     let mut ptr: usize = 0;
  32.     let mut intcode = code.clone();
  33.     let mut output = 0;
  34.     loop {
  35.         if intcode[ptr] == 99 { break; }
  36.         let parts = reverse_digits_of(intcode[ptr]);
  37.         let opcode = parts[0];
  38.         match opcode {
  39.             // Add
  40.             1 => {
  41.                 let a = if get_if_exists(&parts,2) == 1 { intcode[ptr+1] } else { intcode[intcode[ptr+1] as usize] };
  42.                 let b = if get_if_exists(&parts,3) == 1 { intcode[ptr+2] } else { intcode[intcode[ptr+2] as usize] };
  43.                 let d = intcode[ptr+3] as usize;
  44.                 intcode[d] = a + b;
  45.                 ptr += 4;
  46.             },
  47.            
  48.             // Multiply
  49.             2 => {
  50.                 let a = if get_if_exists(&parts,2) == 1 { intcode[ptr+1] } else { intcode[intcode[ptr+1] as usize] };
  51.                 let b = if get_if_exists(&parts,3) == 1 { intcode[ptr+2] } else { intcode[intcode[ptr+2] as usize] };
  52.                 let d = intcode[ptr+3] as usize;
  53.                 intcode[d] = a * b;
  54.                 ptr += 4;
  55.             },
  56.            
  57.             // Input
  58.             3 => {
  59.                 println!("Input: {}", input);
  60.                 let ix = intcode[ptr+1] as usize;
  61.                 intcode[ix] = input;
  62.                 ptr += 2;
  63.             },
  64.  
  65.             // Output
  66.             4 => {
  67.                 let ix = intcode[ptr+1] as usize;
  68.                 println!("Output: {}", intcode[ix]);
  69.                 output = intcode[ix];
  70.                 ptr += 2;
  71.             },
  72.  
  73.             // Jump if True
  74.             5 => {
  75.                 let a = if get_if_exists(&parts,2) == 1 { intcode[ptr+1] } else { intcode[intcode[ptr+1] as usize] };
  76.                 let b = if get_if_exists(&parts,3) == 1 { intcode[ptr+2] } else { intcode[intcode[ptr+2] as usize] };
  77.                 if a != 0 { ptr = b as usize } else { ptr += 3 }
  78.             },
  79.            
  80.             // Jump if False
  81.             6 => {
  82.                 let a = if get_if_exists(&parts,2) == 1 { intcode[ptr+1] } else { intcode[intcode[ptr+1] as usize] };
  83.                 let b = if get_if_exists(&parts,3) == 1 { intcode[ptr+2] } else { intcode[intcode[ptr+2] as usize] };
  84.                 if a == 0 { ptr = b as usize } else { ptr += 3 }
  85.             },
  86.            
  87.             // Less Than
  88.             7 => {
  89.                 let a = if get_if_exists(&parts,2) == 1 { intcode[ptr+1] } else { intcode[intcode[ptr+1] as usize] };
  90.                 let b = if get_if_exists(&parts,3) == 1 { intcode[ptr+2] } else { intcode[intcode[ptr+2] as usize] };
  91.                 let d = intcode[ptr+3] as usize;
  92.                 if a < b { intcode[d] = 1 } else { intcode[d] = 0 }
  93.                 ptr += 4;
  94.             },
  95.            
  96.             // Equal
  97.             8 => {
  98.                 let a = if get_if_exists(&parts,2) == 1 { intcode[ptr+1] } else { intcode[intcode[ptr+1] as usize] };
  99.                 let b = if get_if_exists(&parts,3) == 1 { intcode[ptr+2] } else { intcode[intcode[ptr+2] as usize] };
  100.                 let d = intcode[ptr+3] as usize;
  101.                 if a == b { intcode[d] = 1 } else { intcode[d] = 0 }
  102.                 ptr += 4;
  103.             },
  104.             other => panic!("ERROR: Unknown instruction: {}", other),
  105.         }
  106.     }
  107.     output
  108. }
  109.  
  110. fn day05(input: &str) -> io::Result<()> {
  111.     let file = File::open(input).expect("Input file not found.");
  112.     let reader = BufReader::new(file);
  113.  
  114.     let input: Vec<String> = match reader.lines().collect() {
  115.         Err(err) => panic!("Unknown error reading input: {}", err),
  116.         Ok(result) => result,
  117.     };
  118.     let input = input.concat();
  119.     let intcode_default = input
  120.         .split(",")
  121.         .map(|x| x.parse::<i64>().unwrap())
  122.         .collect::<Vec<i64>>();
  123.  
  124.     // Part 1
  125.     let intcode = intcode_default.clone();
  126.     let part1 = process(&intcode,1);
  127.  
  128.     // Part 1
  129.     let intcode = intcode_default.clone();
  130.     let part2 = process(&intcode,5);
  131.  
  132.     // Output
  133.     println!("Part 1: {}", part1); // 7265618
  134.     println!("Part 2: {}", part2); // 7731427
  135.  
  136.     Ok(())
  137. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top