-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
639ac6e
commit 14849e5
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use std::fs::File; | ||
use std::io::prelude::*; | ||
use std::path::Path; | ||
|
||
fn parse_ints(line: String) -> Vec<i32> { | ||
line.trim().split_whitespace().map(|p| p.parse::<i32>().unwrap()).collect::<Vec<_>>() | ||
} | ||
|
||
fn main() { | ||
let path = Path::new("input.txt"); | ||
let display = path.display(); | ||
|
||
let mut file = match File::open(&path) { | ||
Err(why) => panic!("couldn't open {}: {}", display, why), | ||
Ok(file) => file, | ||
}; | ||
|
||
let mut s = String::new(); | ||
file.read_to_string(&mut s).unwrap(); | ||
|
||
let lines = s.trim().split("\n"); | ||
let mut valid_triangles = 0; | ||
|
||
for line in lines.clone() { | ||
let mut parts = parse_ints(line.to_string()); | ||
parts.sort(); | ||
|
||
if parts[0] + parts[1] > parts[2] { | ||
valid_triangles += 1; | ||
} | ||
} | ||
|
||
println!("part 1 {}", valid_triangles); | ||
|
||
valid_triangles = 0; | ||
|
||
for line_group in lines.collect::<Vec<_>>().chunks(3) { | ||
let mut triangles = vec!(Vec::new(), Vec::new(), Vec::new()); | ||
|
||
for line in line_group { | ||
let parts = parse_ints(line.to_string()); | ||
|
||
for i in 0..3 { | ||
triangles[i].push(parts[i]); | ||
} | ||
} | ||
|
||
for i in 0..3 { | ||
triangles[i].sort(); | ||
|
||
if triangles[i][0] + triangles[i][1] > triangles[i][2] { | ||
valid_triangles += 1; | ||
} | ||
} | ||
} | ||
|
||
println!("part 2 {}", valid_triangles); | ||
} |