aboutsummaryrefslogtreecommitdiffstats
path: root/src/day5.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/day5.rs')
-rw-r--r--src/day5.rs96
1 files changed, 89 insertions, 7 deletions
diff --git a/src/day5.rs b/src/day5.rs
index ddd5be1..00a8779 100644
--- a/src/day5.rs
+++ b/src/day5.rs
@@ -26,10 +26,99 @@ pub fn part1() {
let mut polymer: Vec<_> = input.trim().chars().collect();
+ react_polymer(&mut polymer, None);
+
+ let number_of_units = polymer.len();
+
+ println!(
+ "the number of units remaining after fully reacting the polymer you scanned: {}",
+ number_of_units
+ );
+}
+
+/// Time to improve the polymer.
+///
+/// One of the unit types is causing problems; it's preventing the polymer from collapsing as much as it should. Your goal is to figure out which unit type is causing the most problems, remove all instances of it (regardless of polarity), fully react the remaining polymer, and measure its length.
+///
+/// For example, again using the polymer dabAcCaCBAcCcaDA from above:
+///
+/// Removing all A/a units produces dbcCCBcCcD. Fully reacting this polymer produces dbCBcD, which has length 6.
+/// Removing all B/b units produces daAcCaCAcCcaDA. Fully reacting this polymer produces daCAcaDA, which has length 8.
+/// Removing all C/c units produces dabAaBAaDA. Fully reacting this polymer produces daDA, which has length 4.
+/// Removing all D/d units produces abAcCaCBAcCcaA. Fully reacting this polymer produces abCBAc, which has length 6.
+///
+/// In this example, removing all C/c units was best, producing the answer 4.
+///
+/// What is the length of the shortest polymer you can produce by removing all units of exactly one type and fully reacting the result?
+pub fn part2() {
+ let input = ::common::read_stdin_to_string();
+
+ let polymer: Vec<_> = input.trim().chars().collect();
+ let mut shortest_polymer = polymer.len();
+
+ let drop_units = [
+ ('a', 'A'),
+ ('b', 'B'),
+ ('c', 'C'),
+ ('d', 'D'),
+ ('e', 'E'),
+ ('f', 'F'),
+ ('g', 'G'),
+ ('h', 'H'),
+ ('i', 'I'),
+ ('j', 'J'),
+ ('k', 'K'),
+ ('l', 'L'),
+ ('m', 'M'),
+ ('n', 'N'),
+ ('o', 'O'),
+ ('p', 'P'),
+ ('q', 'Q'),
+ ('r', 'R'),
+ ('s', 'S'),
+ ('t', 'T'),
+ ('u', 'U'),
+ ('v', 'V'),
+ ('w', 'W'),
+ ('x', 'X'),
+ ('y', 'Y'),
+ ('z', 'Z'),
+ ];
+
+ for drop_unit in drop_units.iter() {
+ let mut polymer = polymer.to_vec();
+ react_polymer(&mut polymer, *drop_unit);
+ if polymer.len() < shortest_polymer {
+ shortest_polymer = polymer.len();
+ }
+ }
+
+ println!("the length of the shortest polymer: {}", shortest_polymer);
+}
+
+fn react_polymer<T: Into<Option<(char, char)>>>(polymer: &mut Vec<char>, drop_unit: T) {
let mut i = 0;
+ let drop_unit = drop_unit.into();
+
while i < polymer.len() - 1 {
let unit = polymer[i];
let next_unit = polymer[i + 1];
+
+ if let Some(drop_unit) = drop_unit {
+ if unit == drop_unit.0 || unit == drop_unit.1 {
+ polymer.remove(i);
+
+ if i != 0 {
+ i -= 1;
+ }
+ continue;
+ }
+ if next_unit == drop_unit.0 || next_unit == drop_unit.1 {
+ polymer.remove(i + 1);
+ continue;
+ }
+ }
+
if test_unit_reaction(unit, next_unit) {
polymer.remove(i);
polymer.remove(i);
@@ -42,13 +131,6 @@ pub fn part1() {
i += 1;
}
-
- let number_of_units = polymer.len();
-
- println!(
- "the number of units remaining after fully reacting the polymer you scanned: {}",
- number_of_units
- );
}
fn test_unit_reaction(a: char, b: char) -> bool {