aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar alecdwm 2018-12-10 00:09:15 +1000
committerGravatar alecdwm 2018-12-10 00:09:15 +1000
commitbfd489a2aa151beea3456d2812ab67253712c80b (patch)
tree8008f15ebc8e713ab579b5bc7e8acb91f55a9612
parentbdc6fe26ed4eeeca810c7475d01c876b011ecad4 (diff)
added day5 part1 solution
-rw-r--r--src/day5.rs60
-rw-r--r--src/lib.rs1
-rw-r--r--src/main.rs1
3 files changed, 62 insertions, 0 deletions
diff --git a/src/day5.rs b/src/day5.rs
new file mode 100644
index 0000000..ddd5be1
--- /dev/null
+++ b/src/day5.rs
@@ -0,0 +1,60 @@
+//! You've managed to sneak in to the prototype suit manufacturing lab. The Elves are making decent progress, but are still struggling with the suit's size reduction capabilities.
+
+/// While the very latest in 1518 alchemical technology might have solved their problem eventually, you can do better. You scan the chemical composition of the suit's material and discover that it is formed by extremely long polymers (one of which is available as your puzzle input).
+///
+/// The polymer is formed by smaller units which, when triggered, react with each other such that two adjacent units of the same type and opposite polarity are destroyed. Units' types are represented by letters; units' polarity is represented by capitalization. For instance, r and R are units with the same type but opposite polarity, whereas r and s are entirely different types and do not react.
+///
+/// For example:
+///
+/// In aA, a and A react, leaving nothing behind.
+/// In abBA, bB destroys itself, leaving aA. As above, this then destroys itself, leaving nothing.
+/// In abAB, no two adjacent units are of the same type, and so nothing happens.
+/// In aabAAB, even though aa and AA are of the same type, their polarities match, and so nothing happens.
+///
+/// Now, consider a larger example, dabAcCaCBAcCcaDA:
+///
+/// dabAcCaCBAcCcaDA The first 'cC' is removed.
+/// dabAaCBAcCcaDA This creates 'Aa', which is removed.
+/// dabCBAcCcaDA Either 'cC' or 'Cc' are removed (the result is the same).
+/// dabCBAcaDA No further actions can be taken.
+///
+/// After all possible reactions, the resulting polymer contains 10 units.
+///
+/// How many units remain after fully reacting the polymer you scanned?
+pub fn part1() {
+ let input = ::common::read_stdin_to_string();
+
+ let mut polymer: Vec<_> = input.trim().chars().collect();
+
+ let mut i = 0;
+ while i < polymer.len() - 1 {
+ let unit = polymer[i];
+ let next_unit = polymer[i + 1];
+ if test_unit_reaction(unit, next_unit) {
+ polymer.remove(i);
+ polymer.remove(i);
+
+ if i != 0 {
+ i -= 1;
+ }
+ continue;
+ }
+
+ 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 {
+ match (a.is_lowercase(), b.is_lowercase()) {
+ (true, true) | (false, false) => false,
+ (true, false) => a == b.to_lowercase().next().unwrap(),
+ (false, true) => a.to_lowercase().next().unwrap() == b,
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index dd7b852..08caa90 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -3,6 +3,7 @@ pub mod day1;
pub mod day2;
pub mod day3;
pub mod day4;
+pub mod day5;
#[cfg(test)]
mod tests {
diff --git a/src/main.rs b/src/main.rs
index 440bdcd..ad183fe 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -18,6 +18,7 @@ fn main() {
puzzle_solution_map.insert("day3::part2", advent_of_code_2018::day3::part2);
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);
let command = args[1].as_str();
if command == "list" {