-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.rs
40 lines (34 loc) · 790 Bytes
/
day10.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
use std::fs;
fn part1(xs: &Vec<i32>) -> i32 {
let mut counts = [0; 4];
for i in 1..xs.len() {
counts[(xs[i] - xs[i-1]) as usize] += 1;
}
counts[1] * counts[3]
}
fn part2(xs: &Vec<i32>) -> i64 {
let n = xs.len();
let mut dp = vec![0; n];
dp[0] = 1;
for i in 1..n {
let mut j: i32 = (i-1) as i32;
while j >= 0 && xs[j as usize] >= xs[i]-3 {
dp[i] += dp[j as usize];
j -= 1
}
}
dp[n-1]
}
fn main() {
let mut adaptors: Vec<i32> =
fs::read_to_string("input/input10.txt")
.unwrap()
.lines()
.map(|x| x.parse().unwrap())
.collect();
adaptors.push(0);
adaptors.push(*adaptors.iter().max().unwrap()+3);
adaptors.sort();
println!("Part 1: {}", part1(&adaptors));
println!("Part 2: {}", part2(&adaptors));
}