main
henine 4 years ago
parent 75746ade18
commit ca275c703e

@ -0,0 +1,9 @@
[package]
name = "day10"
version = "0.1.0"
authors = ["henine <why@no.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -0,0 +1,106 @@
66
7
73
162
62
165
157
158
137
125
138
59
36
40
94
95
13
35
136
96
156
155
24
84
42
171
142
3
104
149
83
129
19
122
68
103
74
118
20
110
54
127
88
31
135
26
126
2
51
91
16
65
128
119
67
48
111
29
49
12
132
17
41
166
75
146
50
30
1
164
112
34
18
72
97
145
11
117
58
78
152
90
172
163
89
107
45
37
79
159
141
105
10
115
69
170
25
100
80
4
85
169
106
57
116
23

@ -0,0 +1,52 @@
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::string::String;
fn main() {
let file = match File::open("input") {
Ok(file) => file,
Err(e) => panic!(e)
};
let input_buffer = BufReader::new(&file);
let lines = input_buffer.lines();
let mut adapters: Vec<u32> = lines
.map(Result::unwrap)
.map(|x: String| x.trim().to_string())
.map(|x| x.parse::<u32>().unwrap())
.collect();
adapters.sort();
let mut hist = [0, 0, 1];
let mut prev = 0;
for adapter in adapters.iter() {
hist[(adapter - prev - 1) as usize] += 1;
prev = *adapter;
}
println!("{:?}", hist);
println!("{}", hist[0] * hist[2]);
adapters.reverse();
let mut path_counts = Vec::with_capacity(adapters.len());
path_counts.push(1 as u64);
for _i in 1..adapters.len() { path_counts.push(0 as u64) };
for adapter_i in 1..adapters.len() {
let adapter = adapters[adapter_i];
path_counts[adapter_i] =
if adapter_i >= 1 && adapters[adapter_i - 1] - adapter <= 3 { path_counts[adapter_i - 1] } else { 0 } +
if adapter_i >= 2 && adapters[adapter_i - 2] - adapter <= 3 { path_counts[adapter_i - 2] } else { 0 } +
if adapter_i >= 3 && adapters[adapter_i - 3] - adapter <= 3 { path_counts[adapter_i - 3] } else { 0 }
}
let count =
if adapters[adapters.len() - 1] <= 3 { path_counts[adapters.len() - 1] } else { 0 } +
if adapters[adapters.len() - 2] <= 3 { path_counts[adapters.len() - 2] } else { 0 } +
if adapters[adapters.len() - 3] <= 3 { path_counts[adapters.len() - 3] } else { 0 };
println!("{}", count);
}

@ -0,0 +1,31 @@
28
33
18
42
31
14
46
20
48
47
24
23
49
45
19
38
39
11
1
32
25
35
8
17
7
9
4
2
34
10
3
Loading…
Cancel
Save