Completed day 10
This commit is contained in:
parent
c2960e8ee1
commit
646539557f
14
day10/Cargo.lock
generated
Normal file
14
day10/Cargo.lock
generated
Normal file
@ -0,0 +1,14 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2c0df63cb2955042487fad3aefd2c6e3ae7389ac5dc1beb28921de0b69f779d4"
|
||||
|
||||
[[package]]
|
||||
name = "day10"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
]
|
10
day10/Cargo.toml
Normal file
10
day10/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "day10"
|
||||
version = "0.1.0"
|
||||
authors = ["Tyler Hallada <tyler@hallada.net>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
111
day10/input/input.txt
Executable file
111
day10/input/input.txt
Executable file
@ -0,0 +1,111 @@
|
||||
71
|
||||
183
|
||||
111
|
||||
89
|
||||
92
|
||||
142
|
||||
25
|
||||
101
|
||||
52
|
||||
86
|
||||
18
|
||||
22
|
||||
70
|
||||
2
|
||||
135
|
||||
163
|
||||
34
|
||||
143
|
||||
153
|
||||
35
|
||||
144
|
||||
24
|
||||
23
|
||||
94
|
||||
100
|
||||
102
|
||||
17
|
||||
57
|
||||
76
|
||||
182
|
||||
134
|
||||
38
|
||||
7
|
||||
103
|
||||
66
|
||||
31
|
||||
11
|
||||
121
|
||||
77
|
||||
113
|
||||
128
|
||||
82
|
||||
99
|
||||
148
|
||||
137
|
||||
41
|
||||
32
|
||||
48
|
||||
131
|
||||
60
|
||||
127
|
||||
138
|
||||
73
|
||||
28
|
||||
10
|
||||
84
|
||||
180
|
||||
63
|
||||
125
|
||||
53
|
||||
176
|
||||
165
|
||||
114
|
||||
145
|
||||
152
|
||||
72
|
||||
107
|
||||
167
|
||||
59
|
||||
164
|
||||
78
|
||||
126
|
||||
118
|
||||
136
|
||||
83
|
||||
79
|
||||
58
|
||||
14
|
||||
106
|
||||
69
|
||||
51
|
||||
39
|
||||
157
|
||||
42
|
||||
177
|
||||
173
|
||||
93
|
||||
141
|
||||
3
|
||||
33
|
||||
13
|
||||
19
|
||||
45
|
||||
154
|
||||
95
|
||||
170
|
||||
54
|
||||
181
|
||||
6
|
||||
151
|
||||
1
|
||||
112
|
||||
96
|
||||
115
|
||||
85
|
||||
108
|
||||
166
|
||||
160
|
||||
40
|
||||
122
|
||||
12
|
11
day10/input/test1.txt
Normal file
11
day10/input/test1.txt
Normal file
@ -0,0 +1,11 @@
|
||||
16
|
||||
10
|
||||
15
|
||||
5
|
||||
1
|
||||
11
|
||||
7
|
||||
19
|
||||
6
|
||||
12
|
||||
4
|
31
day10/input/test2.txt
Normal file
31
day10/input/test2.txt
Normal file
@ -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
|
114
day10/src/main.rs
Normal file
114
day10/src/main.rs
Normal file
@ -0,0 +1,114 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::io::BufReader;
|
||||
use std::time::Instant;
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
const INPUT: &str = "input/input.txt";
|
||||
|
||||
fn find_jolt_differences(
|
||||
adapters: &mut HashSet<usize>,
|
||||
input_jolt: usize,
|
||||
jolt_differences: &mut HashMap<usize, usize>,
|
||||
) {
|
||||
for i in 1..=3 {
|
||||
if adapters.remove(&(input_jolt + i)) {
|
||||
let entry = jolt_differences.entry(i).or_insert(0);
|
||||
*entry += 1;
|
||||
find_jolt_differences(adapters, input_jolt + i, jolt_differences)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn count_adapter_combinations(
|
||||
adapters: &HashSet<usize>,
|
||||
input_jolt: usize,
|
||||
target_jolt: usize,
|
||||
cache: &mut HashMap<usize, usize>,
|
||||
) -> usize {
|
||||
if let Some(count) = cache.get(&input_jolt) {
|
||||
*count
|
||||
} else {
|
||||
let count = if input_jolt == target_jolt {
|
||||
1
|
||||
} else {
|
||||
(1..=3)
|
||||
.map(|i| {
|
||||
if adapters.contains(&(input_jolt + i)) {
|
||||
count_adapter_combinations(adapters, input_jolt + i, target_jolt, cache)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
})
|
||||
.sum()
|
||||
};
|
||||
cache.insert(input_jolt, count);
|
||||
count
|
||||
}
|
||||
}
|
||||
|
||||
fn solve_part1(input_path: &str) -> Result<usize> {
|
||||
let file = File::open(input_path)?;
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
let mut adapters = reader
|
||||
.lines()
|
||||
.map(|line| Ok(line?.parse()?))
|
||||
.collect::<Result<HashSet<usize>>>()?;
|
||||
let mut differences = HashMap::new();
|
||||
differences.insert(1, 0);
|
||||
differences.insert(2, 0);
|
||||
differences.insert(3, 0);
|
||||
find_jolt_differences(&mut adapters, 0, &mut differences);
|
||||
Ok(differences.get(&1).unwrap() * (differences.get(&3).unwrap() + 1))
|
||||
}
|
||||
|
||||
fn solve_part2(input_path: &str) -> Result<usize> {
|
||||
let file = File::open(input_path)?;
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
let adapters = reader
|
||||
.lines()
|
||||
.map(|line| Ok(line?.parse()?))
|
||||
.collect::<Result<HashSet<usize>>>()?;
|
||||
let target_jolt = *adapters.iter().max().expect("non-empty input");
|
||||
let mut cache = HashMap::new();
|
||||
Ok(count_adapter_combinations(
|
||||
&adapters,
|
||||
0,
|
||||
target_jolt,
|
||||
&mut cache,
|
||||
))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut now = Instant::now();
|
||||
println!("Part 1: {}", solve_part1(INPUT).unwrap());
|
||||
println!("(elapsed: {:?})", now.elapsed());
|
||||
now = Instant::now();
|
||||
println!("");
|
||||
println!("Part 2: {}", solve_part2(INPUT).unwrap());
|
||||
println!("(elapsed: {:?})", now.elapsed());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const TEST_INPUT1: &str = "input/test1.txt";
|
||||
const TEST_INPUT2: &str = "input/test2.txt";
|
||||
|
||||
#[test]
|
||||
fn solves_part1() {
|
||||
assert_eq!(solve_part1(TEST_INPUT1).unwrap(), 35);
|
||||
assert_eq!(solve_part1(TEST_INPUT2).unwrap(), 220);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solves_part2() {
|
||||
assert_eq!(solve_part2(TEST_INPUT1).unwrap(), 8);
|
||||
assert_eq!(solve_part2(TEST_INPUT2).unwrap(), 19208);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user