-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquest07.rs
116 lines (96 loc) · 3.41 KB
/
quest07.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
//! Replace "S" with "=" and move to the end to simplify pattern matching.
//! The tracks are constructed so that power never drops below zero.
//!
//! For part 3 this means that the power increases by the same amount every 11 laps
//! as LCM(11, 340) = 3470.
//!
//! 2024 divides evenly by 11 = 184, so we only need to race 11 laps instead the entire 2024 laps
//! to find the winning plans.
use std::collections::{BTreeMap, HashMap};
const TRACK1: &str = "=";
const TRACK2: &str = "\
-=++=-==++=++=-=+=-=+=+=--=-=++=-==++=-+=-=+=-=+=+=++=-+==++=++=-=-=---=++==--\
+++==++=+=--==++==+++=++=+++=--=+=-=+=-+=-+=-+-=+=-=+=-+++=+==++++==---=+=+=-=";
const TRACK3: &str = "\
+=+++===-+++++=-==+--+=+===-++=====+--===++=-==+=++====-==-===+=+=--==++=+========-==\
=====++--+++=-++=-+=+==-=++=--+=-====++--+=-==++======+=++=-+==+=-==++=-=-=---++=-=++\
==++===--==+===++===---+++==++=+=-=====+==++===--==-==+++==+++=++=+===--==++--===+===\
==-=++====-+=-+--=+++=-+-===++====+++--=++====+=-=+===+=====-+++=+==++++==----=+=+=-=";
pub fn part1(notes: &str) -> String {
let plans = parse(notes);
let mut result = BTreeMap::new();
for (name, plan) in plans {
let essence = score(TRACK1, 10, &plan);
result.insert(essence, name);
}
result.into_values().rev().collect()
}
pub fn part2(notes: &str) -> String {
let plans = parse(notes);
let mut result = BTreeMap::new();
for (name, plan) in plans {
let essence = score(TRACK2, 10, &plan);
result.insert(essence, name);
}
result.into_values().rev().collect()
}
pub fn part3(notes: &str) -> usize {
let rival_plan = parse(notes).into_values().next().unwrap();
let rival_score = score(TRACK3, 11, &rival_plan);
permutations().iter().filter(|&&essence| essence > rival_score).count()
}
fn parse(notes: &str) -> HashMap<String, String> {
let mut plans = HashMap::new();
for line in notes.lines() {
let (prefix, suffix) = line.split_once(':').unwrap();
let name = String::from(prefix);
let plan = suffix.replace(',', "");
plans.insert(name, plan);
}
plans
}
fn score(track: &str, laps: usize, plan: &str) -> u64 {
let first = track.bytes().cycle();
let second = plan.bytes().cycle();
let size = track.len() * laps;
let combined = first.zip(second).take(size);
let mut power = 10_u64;
let mut essence = 0_u64;
for (terrain, segment) in combined {
let next = if terrain == b'=' { segment } else { terrain };
match next {
b'+' => power += 1,
b'-' => power -= 1,
_ => (),
};
essence += power;
}
essence
}
fn permutations() -> Vec<u64> {
fn helper(scores: &mut Vec<u64>, plan: &mut String, plus: u8, minus: u8, equal: u8) {
if plus + minus + equal == 0 {
let essence = score(TRACK3, 11, plan);
scores.push(essence);
return;
}
if plus > 0 {
plan.push('+');
helper(scores, plan, plus - 1, minus, equal);
plan.pop();
}
if minus > 0 {
plan.push('-');
helper(scores, plan, plus, minus - 1, equal);
plan.pop();
}
if equal > 0 {
plan.push('=');
helper(scores, plan, plus, minus, equal - 1);
plan.pop();
}
}
let mut scores = Vec::new();
helper(&mut scores, &mut String::new(), 5, 3, 3);
scores
}