-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution_2017_24.rs
126 lines (105 loc) · 3.03 KB
/
solution_2017_24.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use std::collections::BTreeSet;
use advent_of_code_common::pairing::Pairing;
use advent_of_code_common::parsing::{
Error, parse_lines_to_btreeset, parse_str, split_into_two_strings,
};
use pathfinding::prelude::bfs_reach;
type Port = u32;
type Component = Pairing<Port>;
#[derive(Clone, Eq, PartialEq, Hash)]
struct State {
last: Port,
remaining: BTreeSet<Component>,
}
impl State {
fn new(components: &BTreeSet<Component>) -> Self {
Self {
last: 0,
remaining: components.clone(),
}
}
fn strength(&self, original: &BTreeSet<Component>) -> u32 {
strength(original) - strength(&self.remaining)
}
fn successors(&self) -> Vec<State> {
self.remaining
.iter()
.filter(|x| x.contains(&self.last))
.map(|x| {
let mut new_remaining = self.remaining.clone();
new_remaining.remove(x);
let new_last = x.find_other_side(&self.last).unwrap();
Self {
last: *new_last,
remaining: new_remaining,
}
})
.collect()
}
}
fn strength(components: &BTreeSet<Component>) -> u32 {
components.iter().map(Pairing::sum).sum()
}
fn solve<F, G: Ord>(components: &BTreeSet<Component>, compare_by: F) -> Result<u32, Error>
where
F: Fn(&State) -> G,
{
let start = State::new(components);
bfs_reach(start, State::successors)
.min_by_key(|x| compare_by(x))
.ok_or("Failed to find".to_string())
.map(|x| x.strength(components))
}
fn parse(input: &str) -> Result<BTreeSet<Component>, Error> {
let lines: BTreeSet<String> = parse_lines_to_btreeset(input)?;
lines
.into_iter()
.map(|s| {
let (a, b) = split_into_two_strings(&s, "/")?;
let a: u32 = parse_str(&a)?;
let b: u32 = parse_str(&b)?;
Ok(Pairing::new(a, b))
})
.collect()
}
fn part_1(input: &str) -> Result<u32, Error> {
let components = parse(input)?;
solve(&components, |state| strength(&state.remaining))
}
fn part_2(input: &str) -> Result<u32, Error> {
let components = parse(input)?;
solve(&components, |state| {
(state.remaining.len(), strength(&state.remaining))
})
}
const DATA: &str = include_str!("../../resources/24.txt");
fn main() -> Result<(), Error> {
let result_1 = part_1(DATA)?;
println!("Part 1: {result_1}");
let result_2 = part_2(DATA)?;
println!("Part 2: {result_2}");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
const TEST: &str = include_str!("../../resources/24-test.txt");
#[test]
fn test_solve_1_test() {
assert_eq!(part_1(TEST), Ok(31));
}
#[test]
#[ignore]
fn test_solve_1_real() {
assert_eq!(part_1(DATA), Ok(1906));
}
#[test]
fn test_solve_2_test() {
assert_eq!(part_2(TEST), Ok(19));
}
#[test]
#[ignore]
fn test_solve_2_real() {
assert_eq!(part_2(DATA), Ok(1824));
}
}