-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.rs
61 lines (50 loc) · 1.56 KB
/
day02.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
use std::{fs, mem};
use itertools::Itertools;
pub(crate) fn day02() {
let input = parse(fs::read_to_string("input/2017/day02/input.txt").unwrap());
println!("{}", part_a(&input));
println!("{}", part_b(&input));
}
fn parse(input: String) -> Vec<Vec<i32>> {
input.lines().into_iter()
.map(|line| line.split_whitespace().map(|num| num.parse().unwrap()).collect::<Vec<i32>>())
.collect()
}
fn part_a(input: &Vec<Vec<i32>>) -> i32 {
input.iter().map(|line| {
let max = line.iter().max().unwrap();
let min = line.iter().min().unwrap();
max - min
}).sum()
}
fn part_b(input: &Vec<Vec<i32>>) -> i32 {
input.iter().map(|line| {
line.iter().combinations(2).map(|aha| {
let mut a = *aha[0];
let mut b = *aha[1];
if a < b { mem::swap(&mut a, &mut b) }
if a % b == 0 { a / b } else { 0 }
}).sum::<i32>()
}).sum()
}
#[cfg(test)]
mod day02_tests {
use std::fs;
use crate::y2017::day02::{parse, part_a, part_b};
#[test]
fn part_a_works() {
let input = parse(fs::read_to_string("input/2017/day02/test.txt").unwrap());
assert_eq!(18, part_a(&input));
}
#[test]
fn part_b_works() {
let input = parse(fs::read_to_string("input/2017/day02/test_2.txt").unwrap());
assert_eq!(9, part_b(&input));
}
#[test]
fn input_works() {
let input = parse(fs::read_to_string("input/2017/day02/input.txt").unwrap());
assert_eq!(32020, part_a(&input));
assert_eq!(236, part_b(&input));
}
}