Advertisement
lillie_

aoc2015day2_rs

Nov 7th, 2019
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.94 KB | None | 0 0
  1. use std::fs;
  2. use std::cmp::min;
  3.  
  4. fn ft(l: u64, w: u64, h: u64) -> u64 {
  5.     l * w * h + min(min(l + w, l + h), min(l + w, w + h)) * 2
  6. }
  7.  
  8. fn sqft(l: u64, w: u64, h: u64) -> u64 {
  9.     2 * l * w + 2 * w * h + 2 * h * l + min(min(l * w, l * h), min(l * w, w * h))
  10. }
  11.  
  12. fn main() {
  13.     println!("AoC 2015, Day 2");
  14.  
  15.     let file = fs::read_to_string("/home/michelle/AoC_2015/Day_2/input.txt")
  16.         .expect("Failed to open file!");
  17.     let mut feet = 0u64;
  18.     let mut square_feet = 0u64;
  19.  
  20.     for i in file.split_terminator('\n') {
  21.         if i == "" {
  22.             break;
  23.         }
  24.  
  25.         let mut params: Vec<u64> = Vec::new();
  26.  
  27.         for j in i.split('x') {
  28.             params.push(j.parse::<u64>().unwrap());
  29.         }
  30.  
  31.         feet += ft(params[0], params[1], params[2]);
  32.         square_feet += sqft(params[0], params[1], params[2]);
  33.     }
  34.  
  35.     println!("Part 1: {}", square_feet);
  36.     println!("Part 2: {}", feet);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement