aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar alecdwm 2018-12-11 23:58:46 +1000
committerGravatar alecdwm 2018-12-11 23:58:46 +1000
commit834c84a1bddcbefbb383226f13e1d38043a773be (patch)
tree38524607096c09cc628e7fe24becf7e96c010e2b
parentf7dc1a18cab4a979aefbdda2b5cd4969c1b5a044 (diff)
added day7 part2 solution
-rw-r--r--src/day7.rs114
-rw-r--r--src/main.rs1
2 files changed, 107 insertions, 8 deletions
diff --git a/src/day7.rs b/src/day7.rs
index f28ef2e..1dffd94 100644
--- a/src/day7.rs
+++ b/src/day7.rs
@@ -47,7 +47,7 @@ pub fn part1() {
let mut step_graph = StepGraph::from_instructions(instructions);
let mut steps = Vec::new();
- while let Some(step) = step_graph.next_step() {
+ while let Some(step) = step_graph.next_step(true) {
steps.push(step);
}
@@ -57,6 +57,95 @@ pub fn part1() {
);
}
+/// As you're about to begin construction, four of the Elves offer to help. "The sun will set soon; it'll go faster if we work together." Now, you need to account for multiple people working on steps simultaneously. If multiple steps are available, workers should still begin them in alphabetical order.
+///
+/// Each step takes 60 seconds plus an amount corresponding to its letter: A=1, B=2, C=3, and so on. So, step A takes 60+1=61 seconds, while step Z takes 60+26=86 seconds. No time is required between steps.
+///
+/// To simplify things for the example, however, suppose you only have help from one Elf (a total of two workers) and that each step takes 60 fewer seconds (so that step A takes 1 second and step Z takes 26 seconds). Then, using the same instructions as above, this is how each second would be spent:
+///
+/// Second Worker 1 Worker 2 Done
+/// 0 C .
+/// 1 C .
+/// 2 C .
+/// 3 A F C
+/// 4 B F CA
+/// 5 B F CA
+/// 6 D F CAB
+/// 7 D F CAB
+/// 8 D F CAB
+/// 9 D . CABF
+/// 10 E . CABFD
+/// 11 E . CABFD
+/// 12 E . CABFD
+/// 13 E . CABFD
+/// 14 E . CABFD
+/// 15 . . CABFDE
+///
+/// Each row represents one second of time. The Second column identifies how many seconds have passed as of the beginning of that second. Each worker column shows the step that worker is currently doing (or . if they are idle). The Done column shows completed steps.
+///
+/// Note that the order of the steps has changed; this is because steps now take time to finish and multiple workers can begin multiple steps simultaneously.
+///
+/// In this example, it would take 15 seconds for two workers to complete these steps.
+///
+/// With 5 workers and the 60+ second step durations described above, how long will it take to complete all of the steps?
+pub fn part2() {
+ let input = crate::common::read_stdin_to_string();
+
+ let instructions = input_to_instructions(input);
+ let mut step_graph = StepGraph::from_instructions(instructions);
+
+ let mut seconds = 0;
+ let mut gnomes: Vec<(usize, char, usize)> = Vec::new();
+ loop {
+ while gnomes.len() < 5 {
+ let step = match step_graph.next_step(false) {
+ Some(step) => step,
+ None => break,
+ };
+
+ let step_completion_time =
+ 61 + ALPHABET.iter().position(|letter| *letter == step).unwrap();
+ let step_completed_at_time = seconds + step_completion_time;
+
+ let insert_at = match gnomes
+ .iter()
+ .position(|&gnome| step_completed_at_time > gnome.0)
+ {
+ Some(position) => position,
+ None => gnomes.len(),
+ };
+
+ gnomes.insert(
+ insert_at,
+ (step_completed_at_time, step, step_completion_time),
+ );
+ }
+
+ let gnome = match gnomes.pop() {
+ Some(gnome) => gnome,
+ None => break,
+ };
+ seconds += gnome.2;
+ step_graph.complete_step(gnome.1);
+
+ gnomes = gnomes
+ .iter()
+ .filter_map(|busy_gnome| {
+ if busy_gnome.2 < gnome.2 {
+ step_graph.complete_step(busy_gnome.1);
+ return None;
+ }
+ Some((busy_gnome.0 - gnome.2, busy_gnome.1, busy_gnome.2 - gnome.2))
+ })
+ .collect();
+ }
+
+ println!(
+ "seconds it will take to complete all of the steps: {}",
+ seconds
+ );
+}
+
fn input_to_instructions(input: String) -> Vec<(char, char)> {
let mut instructions = Vec::new();
for line in input.lines() {
@@ -105,7 +194,7 @@ impl StepGraph {
.push(from);
}
- fn next_step(&mut self) -> Option<char> {
+ fn next_step(&mut self, complete_step: bool) -> Option<char> {
if self.available_steps.len() < 1 {
return None;
}
@@ -116,13 +205,19 @@ impl StepGraph {
Some(step) => step,
None => return None,
};
- self.completed_steps.push(next_step);
+
+ if complete_step {
+ self.complete_step(next_step);
+ }
+
+ Some(next_step)
+ }
+
+ fn complete_step(&mut self, step: char) {
+ self.completed_steps.push(step);
let mut new_available_steps = Vec::new();
for (step, dependencies) in &self.step_dependencies {
- if self.completed_steps.contains(step) || self.available_steps.contains(step) {
- continue;
- }
if dependencies
.iter()
.all(|step_dependency| self.completed_steps.contains(step_dependency))
@@ -135,7 +230,10 @@ impl StepGraph {
self.step_dependencies.remove(&step);
self.available_steps.push(step);
}
-
- Some(next_step)
}
}
+
+const ALPHABET: [char; 26] = [
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', //
+ 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+];
diff --git a/src/main.rs b/src/main.rs
index 76e2daf..bfbae08 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -21,6 +21,7 @@ fn main() {
puzzle_solution_map.insert("day6::part1", advent_of_code_2018::day6::part1);
puzzle_solution_map.insert("day6::part2", advent_of_code_2018::day6::part2);
puzzle_solution_map.insert("day7::part1", advent_of_code_2018::day7::part1);
+ puzzle_solution_map.insert("day7::part2", advent_of_code_2018::day7::part2);
let command = args[1].as_str();
if command == "list" {