y2016d3
This commit is contained in:
parent
d12175371e
commit
a09f89896d
1992
y2016/resources/3_input.txt
Normal file
1992
y2016/resources/3_input.txt
Normal file
File diff suppressed because it is too large
Load Diff
20
y2016/src/bin/d3.rs
Normal file
20
y2016/src/bin/d3.rs
Normal file
@ -0,0 +1,20 @@
|
||||
use std::fs;
|
||||
|
||||
use y2016::days::d3;
|
||||
|
||||
fn main() {
|
||||
part1();
|
||||
part2();
|
||||
}
|
||||
|
||||
fn part1() {
|
||||
let root = env!("CARGO_MANIFEST_DIR");
|
||||
let content = fs::read_to_string(format!("{root}/resources/3_input.txt")).unwrap();
|
||||
println!("{}", d3::process_part1(&content));
|
||||
}
|
||||
|
||||
fn part2() {
|
||||
let root = env!("CARGO_MANIFEST_DIR");
|
||||
let content = fs::read_to_string(format!("{root}/resources/3_input.txt")).unwrap();
|
||||
println!("{}", d3::process_part2(&content));
|
||||
}
|
60
y2016/src/days/d3.rs
Normal file
60
y2016/src/days/d3.rs
Normal file
@ -0,0 +1,60 @@
|
||||
pub fn process_part1(input: &str) -> i32 {
|
||||
input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let sides = line
|
||||
.split_whitespace()
|
||||
.map(|side| side.parse::<u32>().unwrap())
|
||||
.collect::<Vec<u32>>();
|
||||
if sides[0] + sides[1] > sides[2]
|
||||
&& sides[0] + sides[2] > sides[1]
|
||||
&& sides[1] + sides[2] > sides[0]
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
0
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
pub fn process_part2(input: &str) -> u32 {
|
||||
input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
line.split_whitespace()
|
||||
.map(|side| side.parse::<u32>().unwrap())
|
||||
.collect::<Vec<u32>>()
|
||||
})
|
||||
.collect::<Vec<Vec<u32>>>()
|
||||
.chunks(3)
|
||||
.map(|line_triple| {
|
||||
(0..3)
|
||||
.map(|triangle_idx| {
|
||||
if line_triple[0][triangle_idx] + line_triple[1][triangle_idx]
|
||||
> line_triple[2][triangle_idx]
|
||||
&& line_triple[0][triangle_idx] + line_triple[2][triangle_idx]
|
||||
> line_triple[1][triangle_idx]
|
||||
&& line_triple[1][triangle_idx] + line_triple[2][triangle_idx]
|
||||
> line_triple[0][triangle_idx]
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
0
|
||||
})
|
||||
.sum::<u32>()
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const INPUT: &str = " 5 10 25";
|
||||
|
||||
#[test]
|
||||
fn part1() {
|
||||
let result = process_part1(INPUT);
|
||||
assert_eq!(result, 0);
|
||||
}
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
pub mod d1;
|
||||
|
||||
pub mod d2;
|
||||
|
||||
pub mod d3;
|
||||
|
Loading…
Reference in New Issue
Block a user