Malerwerkst.at

Rust - Advent of Code 2015 - Tag 2

Introduction

R.


Rust AoC-2015

Rust - Advent of Code 2015 - Tag 2

Posted by R. on .
Featured

Rust AoC-2015

Rust - Advent of Code 2015 - Tag 2

Posted by R. on .

Machen wir gleich mit Tag 2 weiter. Auch hier gilt, bitte wieder kommentieren, kritisieren und gerne verbessern.

Aufgabenstellung:

The elves are running low on wrapping paper, and so they need to submit an order for more. They have a list of the dimensions (length l, width w, and height h) of each present, and only want to order exactly as much as they need.

Fortunately, every present is a box (a perfect right rectangular prism), which makes calculating the required wrapping paper for each gift a little easier: find the surface area of the box, which is 2lw + 2wh + 2hl. The elves also need a little extra paper for each present: the area of the smallest side.

For example:

A present with dimensions 2x3x4 requires 26 + 212 + 28 = 52 square feet of wrapping paper plus 6 square feet of slack, for a total of 58 square feet. A present with dimensions 1x1x10 requires 21 + 210 + 210 = 42 square feet of wrapping paper plus 1 square foot of slack, for a total of 43 square feet.
All numbers in the elves' list are in feet. How many total square feet of wrapping paper should they order?

Code:

use std::fs::File;  
use std::io::Read;

fn get_data(fname: &str) -> String {  
    let mut file = match File::open(fname) {
        Err(e) => panic!("Kann Datei nicht öffnen: {}",e),
        Ok(file) => file,
    };

    let mut data = String::new();

    match file.read_to_string(&mut data) {
        Err(e) => panic!("file error: {}", e),
        Ok(_) => {},
    }
    data
}

fn main() {  
    let input_str=get_data("input.txt");
    let mut area=0;

    for line in input_str.lines() {
        let mut dim=line.split('x')
                    .map(|x| x.trim().parse().unwrap())
                    .collect::<Vec<u32>>();
        dim.sort();

        let (l,b,h)=(dim[0],dim[1],dim[2]);

        area+=2*(l*b+l*h+b*h)+l*b;
    }

    println!("{}",area);
}

R.

View Comments...