-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.rs
49 lines (40 loc) · 1.18 KB
/
solution.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
use std::fs;
use std::io::*;
fn solve_stupid() {
let input = fs::read_to_string("./input")
.expect("To read the file")
.lines()
.map(|x| x.parse::<u16>().unwrap())
.collect::<Vec<u16>>();
let mut part1: u16 = 0;
let mut part2: u16 = 0;
for i in 1..input.len() {
part1 += (input[i - 1] < input[i]) as u16;
if i < input.len() - 2 {
part2 += (input[i - 1] < input[i + 2]) as u16;
}
}
println!("part 1: {}, part 2: {}", part1, part2);
}
fn solve_not_so_stupid() {
let reader = BufReader::new(fs::File::open("./input").unwrap())
.lines()
.map(|l| l.expect("To be a number line").parse::<u16>().unwrap());
let mut xs: Vec<u16> = vec![9000, 9000, 9000, 9000];
let mut part1: u16 = 0;
let mut part2: u16 = 0;
for m in reader {
xs[0] = xs[1];
xs[1] = xs[2];
xs[2] = xs[3];
xs[3] = m;
part1 += (xs[0] < xs[1]) as u16;
part2 += (xs[0] < xs[3]) as u16;
}
part1 += (xs[1] < xs[2]) as u16 + (xs[2] < xs[3]) as u16;
println!("part 1: {}, part 2: {}", part1, part2);
}
fn main() {
solve_stupid();
solve_not_so_stupid();
}