-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
66 lines (54 loc) · 1.59 KB
/
main.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
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
use std::str::SplitWhitespace;
use itertools::{Chunk, Itertools};
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one(data));
println!("Part 2: {}", part_two(data));
}
fn part_one(data: &str) -> usize {
data.lines().map(possible_line).filter(|&v| v).count()
}
fn part_two(data: &str) -> usize {
data.split_whitespace()
.chunks(9)
.into_iter()
.map(possible_chunk_count)
.sum()
}
fn possible_chunk_count(c: Chunk<SplitWhitespace>) -> usize {
c.tuple_windows()
.map(|(a, _, _, b, _, _, c)| (a.parse().unwrap(), b.parse().unwrap(), c.parse().unwrap()))
.map(possible)
.filter(|&v| v)
.count()
}
fn possible_line(line: &str) -> bool {
let mut nums = line.split_whitespace().map(|v| v.parse().unwrap());
let (Some(a), Some(b), Some(c), None) = (nums.next(), nums.next(), nums.next(), nums.next()) else {
unreachable!();
};
possible((a, b, c))
}
fn possible(dims: (u16, u16, u16)) -> bool {
let mut max = std::cmp::max(dims.0, dims.1);
max = std::cmp::max(max, dims.2);
2 * max < dims.0 + dims.1 + dims.2
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!(0, part_one(data));
assert_eq!(0, part_one("1 1 2"));
assert_eq!(0, part_one("1 1 3"));
assert_eq!(1, part_one("1 1 1"));
}
#[test]
fn two() {
let data = include_str!("test.txt");
assert_eq!(2, part_two(data));
}
}