mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Add solutions for day 3.
This commit is contained in:
1635
2016/day-03/input.txt
Normal file
1635
2016/day-03/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
6
2016/day-03/sample.txt
Normal file
6
2016/day-03/sample.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
101 301 501
|
||||||
|
102 302 502
|
||||||
|
103 303 503
|
||||||
|
201 401 601
|
||||||
|
202 402 602
|
||||||
|
203 403 603
|
||||||
54
2016/day-03/solution.rs
Normal file
54
2016/day-03/solution.rs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
use std::env;
|
||||||
|
use std::io::prelude::*;
|
||||||
|
use std::io::BufReader;
|
||||||
|
use std::fs::File;
|
||||||
|
|
||||||
|
fn is_triangle(mut triangle: Vec<i32>) -> bool
|
||||||
|
{
|
||||||
|
triangle.sort();
|
||||||
|
|
||||||
|
return triangle[0] + triangle[1] > triangle[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
let f = File::open(&args[1]).expect("Could not open file");
|
||||||
|
let reader = BufReader::new(f);
|
||||||
|
|
||||||
|
let mut horizontal = 0;
|
||||||
|
let mut all_nums: Vec<i32> = Vec::new();
|
||||||
|
|
||||||
|
for line in reader.lines() {
|
||||||
|
let numbers: Vec<i32> = line.unwrap()
|
||||||
|
.split(' ')
|
||||||
|
.map(|s| s.trim())
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.map(|s| s.parse().unwrap())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
for num in &numbers {
|
||||||
|
all_nums.push(*num);
|
||||||
|
}
|
||||||
|
|
||||||
|
if is_triangle(numbers) {
|
||||||
|
horizontal += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut vertical = 0;
|
||||||
|
|
||||||
|
for i in 0..(all_nums.len() / 9) {
|
||||||
|
let offset = i * 9;
|
||||||
|
for j in 0..3 {
|
||||||
|
let triangle = vec![all_nums[offset + j + 0], all_nums[offset + j + 3], all_nums[offset + j + 6]];
|
||||||
|
|
||||||
|
if is_triangle(triangle) {
|
||||||
|
vertical += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{} triangles possible horizontally", horizontal);
|
||||||
|
println!("{} triangles possible vertically", vertical);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user