aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar alecdwm 2018-12-10 00:46:25 +1000
committerGravatar alecdwm 2018-12-10 00:46:25 +1000
commitb67458443411c459c19b47d92c77ff259d27118f (patch)
treee2fa5b0eb33d3629053c0d94a2247c1d1aac4c17
parentbfd489a2aa151beea3456d2812ab67253712c80b (diff)
added day5 part2 solution
-rw-r--r--src/day5.rs96
-rw-r--r--src/main.rs1
2 files changed, 90 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 {
diff --git a/src/main.rs b/src/main.rs
index ad183fe..dbdbc7f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -19,6 +19,7 @@ fn main() {
puzzle_solution_map.insert("day4::part1", advent_of_code_2018::day4::part1);
puzzle_solution_map.insert("day4::part2", advent_of_code_2018::day4::part2);
puzzle_solution_map.insert("day5::part1", advent_of_code_2018::day5::part1);
+ puzzle_solution_map.insert("day5::part2", advent_of_code_2018::day5::part2);
let command = args[1].as_str();
if command == "list" {