mirror of
https://gitlab.com/MisterBiggs/aoc_2015-rust.git
synced 2025-06-15 22:46:43 +00:00
finish day 2
This commit is contained in:
parent
b3e1772c8f
commit
21ed9f18b2
1000
inputs/day2.txt
Normal file
1000
inputs/day2.txt
Normal file
File diff suppressed because it is too large
Load Diff
90
src/day2.rs
Normal file
90
src/day2.rs
Normal file
@ -0,0 +1,90 @@
|
||||
use std::cmp;
|
||||
use std::fs;
|
||||
|
||||
pub fn run() {
|
||||
println!("Day 2:");
|
||||
let input = fs::read_to_string("./inputs/day2.txt").expect("Could not read file");
|
||||
|
||||
// let input = "1x1x10\n1x1x10".to_string();
|
||||
let dimensions_list = input
|
||||
.split_whitespace()
|
||||
.map(|c| {
|
||||
c.split('x')
|
||||
.map(|n| n.parse::<usize>().unwrap())
|
||||
.collect::<Vec<usize>>()
|
||||
})
|
||||
.collect::<Vec<Vec<usize>>>();
|
||||
|
||||
println!("\tPart 1: {}", part1(&dimensions_list));
|
||||
println!("\tPart 2: {}", part2(&dimensions_list));
|
||||
}
|
||||
|
||||
fn part1(dimensions: &Vec<Vec<usize>>) -> usize {
|
||||
dimensions
|
||||
.into_iter()
|
||||
.map(|x| {
|
||||
let (l, w, h) = (x[0], x[1], x[2]);
|
||||
let slack = { cmp::min(l * w, cmp::min(w * h, l * h)) };
|
||||
2 * l * w + 2 * w * h + 2 * h * l + slack
|
||||
})
|
||||
.sum::<usize>()
|
||||
}
|
||||
|
||||
fn part2(dimensions: &Vec<Vec<usize>>) -> usize {
|
||||
dimensions
|
||||
.into_iter()
|
||||
.map(|x| {
|
||||
let (l, w, h) = (x[0], x[1], x[2]);
|
||||
let volume = l * w * h;
|
||||
let wrap = {
|
||||
let mut y = x.clone();
|
||||
y.sort();
|
||||
2 * (y[0] + y[1])
|
||||
};
|
||||
|
||||
volume + wrap
|
||||
})
|
||||
.sum::<usize>()
|
||||
}
|
||||
// fn part2(directions: &String) -> usize {
|
||||
// let mut floor: isize = 0;
|
||||
|
||||
// for (num, dir) in directions.chars().enumerate() {
|
||||
// floor += match dir {
|
||||
// '(' => 1,
|
||||
// ')' => -1,
|
||||
// _ => 0,
|
||||
// };
|
||||
|
||||
// if floor == -1 {
|
||||
// return num + 1;
|
||||
// }
|
||||
// }
|
||||
|
||||
// return directions.len() + 1;
|
||||
// }
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
// use super::*;
|
||||
|
||||
// #[test]
|
||||
// fn test_1() {
|
||||
// let mut input: String;
|
||||
|
||||
// let input = "2x3x4";
|
||||
// assert_eq!(part1(&input), 0);
|
||||
|
||||
// input = "()()".to_string();
|
||||
// assert_eq!(part1(&input), 0);
|
||||
// }
|
||||
|
||||
// // #[test]
|
||||
// // fn test_2() {
|
||||
// // let mut input = ")".to_string();
|
||||
// // assert_eq!(part2(&input), 1);
|
||||
|
||||
// // input = "()())".to_string();
|
||||
// // assert_eq!(part2(&input), 5);
|
||||
// // }
|
||||
// }
|
@ -1,6 +1,8 @@
|
||||
mod day1;
|
||||
mod day2;
|
||||
|
||||
fn main() {
|
||||
println!("Running Advent of Code 2021");
|
||||
day1::run();
|
||||
day2::run();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user