-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.rs
72 lines (58 loc) · 1.67 KB
/
part1.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// adventofcode - day 2
// part 1
use std::io::prelude::*;
use std::fs::File;
use std::vec::Vec;
fn main(){
println!("Adventofcode - day 2 | part 1");
let content = import_data();
let mut paper = 0u32;
for line in content.lines(){
let dimensions: Vec<&str> = line.split('x').collect();
let l = str_to_u32(dimensions[0]);
let w = str_to_u32(dimensions[1]);
let h = str_to_u32(dimensions[2]);
paper += calc_paper( l, w, h );
}
println!("Total: {} square feet of wrapping paper", paper);
}
fn calc_paper(l: u32, w: u32, h: u32) -> u32 {
// the extra paper we need is calculated by multiplying the two smallest
// numbers with each other. Therefore, we look for the greatest number and
// simply multiply the others.
let extra = if l > w {
if l > h {
h * w
} else {
l * w
}
} else {
if w > h {
l * h
} else {
l * w
}
};
// the rest is simply multiplying and adding stuff
return extra + 2*l*w + 2*w*h + 2*l*h;
}
// converts a String to an unsigned int
fn str_to_u32(string: &str) -> u32 {
match string.parse::<u32>(){
Ok(x) => x,
Err(_) => panic!("Failed to convert String to u32!"),
}
}
// This function simply imports the data set from a file called input.txt
fn import_data() -> String {
let mut file = match File::open("../../inputs/02.txt") {
Ok(f) => f,
Err(e) => panic!("file error: {}", e),
};
let mut data = String::new();
match file.read_to_string(&mut data){
Ok(_) => {},
Err(e) => panic!("file error: {}", e),
};
data
}