2018 Day07 Complete

This commit is contained in:
Brian Buller 2018-12-07 09:41:03 -06:00
parent 5f920c039d
commit b42d14780d
6 changed files with 446 additions and 0 deletions

227
2018/day07/day07.go Normal file
View File

@ -0,0 +1,227 @@
package main
import (
"bufio"
"errors"
"fmt"
"os"
"strings"
"time"
)
const ClearScreen = "\033[H\033[2J"
var manager *StepManager
var TotalWorkers int
func main() {
inp := StdinToStringSlice()
signTheContract(inp)
part1()
manager.resetJobs()
part2()
}
func signTheContract(inp []string) {
TotalWorkers = 5
manager = &StepManager{workers: TotalWorkers}
for _, v := range inp {
manager.addOrUpdateStep(v[36], v[5])
}
}
func part1() {
fmt.Println("= Part 1 =")
for !manager.done() {
manager.part1Act()
}
fmt.Println("")
}
func part2() {
var seconds int
realtime := true
for !manager.done() {
seconds++
manager.part2Act()
fmt.Print(ClearScreen)
fmt.Println(manager.string())
fmt.Println("Total Seconds: ", seconds)
if realtime {
time.Sleep(time.Millisecond * 125)
}
}
}
type StepManager struct {
steps []*Step
workers int
}
// In part 1, all steps complete immediately
func (m *StepManager) part1Act() {
// Find the next step that should run
var nextStep *Step
for i := range m.steps {
if !m.steps[i].done() && m.steps[i].ready() && (nextStep == nil || nextStep.id > m.steps[i].id) {
nextStep = m.steps[i]
}
}
nextStep.timeSpent = int(nextStep.id) + 5
fmt.Print(string(nextStep.id))
}
func (m *StepManager) part2Act() {
// Clear all done jobs
for i := range m.steps {
if m.steps[i].active && m.steps[i].done() {
m.steps[i].active = false
m.workers++
fmt.Println(string(m.steps[i].id))
}
}
// Find all steps that are ready for work
var waitingSteps []*Step
for i := range m.steps {
if m.steps[i].ready() && !m.steps[i].done() && !m.steps[i].active {
waitingSteps = append(waitingSteps, m.steps[i])
}
}
// If we have any available workers, get them working
for m.workers > 0 {
var nextStep *Step
for i := range waitingSteps {
if !waitingSteps[i].active && (nextStep == nil || waitingSteps[i].id < nextStep.id) {
nextStep = waitingSteps[i]
}
}
if nextStep != nil {
nextStep.active = true
m.workers--
} else {
break
}
}
// Increment all active steps
for i := range m.steps {
if m.steps[i].active {
m.steps[i].timeSpent++
}
}
}
func (m *StepManager) getStep(id byte) (*Step, error) {
for i := range m.steps {
if m.steps[i].id == id {
return m.steps[i], nil
}
}
return nil, errors.New("No step with id " + string(id) + " found.")
}
func (m *StepManager) addOrUpdateStep(id, reqId byte) {
s, err := m.getStep(id)
if err != nil {
s = &Step{id: id}
}
r, reqErr := m.getStep(reqId)
if reqErr != nil {
r = &Step{id: reqId}
m.steps = append(m.steps, r)
}
s.addRequirement(r)
if err != nil {
m.steps = append(m.steps, s)
}
}
func (m *StepManager) resetJobs() {
for i := range manager.steps {
manager.steps[i].timeSpent = 0
}
}
func (m *StepManager) string() string {
var ret string
var stepsDone int
for i := range manager.steps {
ret += (manager.steps[i].string() + "\n")
if manager.steps[i].done() {
stepsDone++
}
}
pct := 100 * (float64(stepsDone) / float64(len(manager.steps)))
filled := int(pct / 10)
progBar := strings.Repeat("\u2588", filled)
progBar += strings.Repeat(" ", (10 - filled))
ret += fmt.Sprintf("Progress: [%s] %d\n", progBar, int(pct))
ret += fmt.Sprintf("Idle Workers %d/%d", m.workers, TotalWorkers)
return ret
}
func (m *StepManager) done() bool {
for i := range m.steps {
if !m.steps[i].done() {
return false
}
}
return true
}
type Step struct {
id byte
req []*Step
timeSpent int
active bool
}
func (s *Step) done() bool {
return s.timeSpent >= int(s.id)-4
}
func (s *Step) addRequirement(r *Step) {
for i := range s.req {
if s.req[i] == r {
return
}
}
s.req = append(s.req, r)
}
func (s *Step) ready() bool {
for i := range s.req {
if !s.req[i].done() {
return false
}
}
return true
}
func (s *Step) string() string {
var ret string
if s.done() {
ret += "[✔️ ] "
} else {
ret += "[✖️ ] "
}
ret += fmt.Sprintf("%s ( %d ): [", string(s.id), s.id)
for i := range s.req {
ret += string(s.req[i].id) + " "
}
ret += "] "
if s.active {
ret += "▶️ "
}
return ret
}
func StdinToStringSlice() []string {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}

BIN
2018/day07/day07.mp4 Normal file

Binary file not shown.

1
2018/day07/go.mod Normal file
View File

@ -0,0 +1 @@
module day07

101
2018/day07/input Normal file
View File

@ -0,0 +1,101 @@
Step W must be finished before step B can begin.
Step G must be finished before step T can begin.
Step B must be finished before step P can begin.
Step R must be finished before step M can begin.
Step K must be finished before step Q can begin.
Step Z must be finished before step X can begin.
Step V must be finished before step S can begin.
Step D must be finished before step U can begin.
Step Y must be finished before step J can begin.
Step A must be finished before step C can begin.
Step M must be finished before step U can begin.
Step E must be finished before step X can begin.
Step T must be finished before step F can begin.
Step U must be finished before step C can begin.
Step C must be finished before step Q can begin.
Step S must be finished before step N can begin.
Step X must be finished before step H can begin.
Step F must be finished before step L can begin.
Step Q must be finished before step J can begin.
Step P must be finished before step J can begin.
Step I must be finished before step L can begin.
Step J must be finished before step L can begin.
Step L must be finished before step N can begin.
Step H must be finished before step O can begin.
Step N must be finished before step O can begin.
Step B must be finished before step S can begin.
Step A must be finished before step T can begin.
Step G must be finished before step K can begin.
Step Z must be finished before step N can begin.
Step V must be finished before step I can begin.
Step Z must be finished before step Q can begin.
Step I must be finished before step J can begin.
Step S must be finished before step I can begin.
Step P must be finished before step I can begin.
Step B must be finished before step C can begin.
Step M must be finished before step L can begin.
Step G must be finished before step Z can begin.
Step M must be finished before step C can begin.
Step U must be finished before step F can begin.
Step B must be finished before step Y can begin.
Step W must be finished before step U can begin.
Step G must be finished before step M can begin.
Step M must be finished before step J can begin.
Step C must be finished before step L can begin.
Step K must be finished before step D can begin.
Step S must be finished before step X can begin.
Step Q must be finished before step N can begin.
Step V must be finished before step N can begin.
Step R must be finished before step C can begin.
Step E must be finished before step H can begin.
Step D must be finished before step P can begin.
Step H must be finished before step N can begin.
Step X must be finished before step O can begin.
Step K must be finished before step Y can begin.
Step R must be finished before step F can begin.
Step L must be finished before step O can begin.
Step Y must be finished before step M can begin.
Step T must be finished before step I can begin.
Step T must be finished before step Q can begin.
Step B must be finished before step F can begin.
Step C must be finished before step N can begin.
Step V must be finished before step M can begin.
Step T must be finished before step N can begin.
Step S must be finished before step L can begin.
Step P must be finished before step H can begin.
Step X must be finished before step Q can begin.
Step Z must be finished before step I can begin.
Step Q must be finished before step O can begin.
Step I must be finished before step N can begin.
Step E must be finished before step P can begin.
Step R must be finished before step L can begin.
Step P must be finished before step L can begin.
Step T must be finished before step H can begin.
Step G must be finished before step X can begin.
Step J must be finished before step H can begin.
Step G must be finished before step V can begin.
Step K must be finished before step N can begin.
Step R must be finished before step Q can begin.
Step Z must be finished before step T can begin.
Step E must be finished before step F can begin.
Step Y must be finished before step H can begin.
Step P must be finished before step N can begin.
Step S must be finished before step O can begin.
Step L must be finished before step H can begin.
Step W must be finished before step E can begin.
Step X must be finished before step N can begin.
Step Z must be finished before step D can begin.
Step A must be finished before step H can begin.
Step T must be finished before step X can begin.
Step E must be finished before step Q can begin.
Step K must be finished before step U can begin.
Step M must be finished before step T can begin.
Step J must be finished before step O can begin.
Step D must be finished before step N can begin.
Step K must be finished before step A can begin.
Step G must be finished before step E can begin.
Step R must be finished before step H can begin.
Step W must be finished before step M can begin.
Step U must be finished before step N can begin.
Step Q must be finished before step H can begin.
Step Y must be finished before step A can begin.

110
2018/day07/problem Normal file
View File

@ -0,0 +1,110 @@
Advent of Code
--- Day 7: The Sum of Its Parts ---
You find yourself standing on a snow-covered coastline; apparently, you landed a little off course. The region is too hilly to see the North Pole from here, but you do spot some Elves that seem to be trying to unpack
something that washed ashore. It's quite cold out, so you decide to risk creating a paradox by asking them for directions.
"Oh, are you the search party?" Somehow, you can understand whatever Elves from the year 1018 speak; you assume it's Ancient Nordic Elvish. Could the device on your wrist also be a translator? "Those clothes don't look
very warm; take this." They hand you a heavy coat.
"We do need to find our way back to the North Pole, but we have higher priorities at the moment. You see, believe it or not, this box contains something that will solve all of Santa's transportation problems - at
least, that's what it looks like from the pictures in the instructions." It doesn't seem like they can read whatever language it's in, but you can: "Sleigh kit. Some assembly required."
"'Sleigh'? What a wonderful name! You must help us assemble this 'sleigh' at once!" They start excitedly pulling more parts out of the box.
The instructions specify a series of steps and requirements about which steps must be finished before others can begin (your puzzle input). Each step is designated by a single letter. For example, suppose you have the
following instructions:
Step C must be finished before step A can begin.
Step C must be finished before step F can begin.
Step A must be finished before step B can begin.
Step A must be finished before step D can begin.
Step B must be finished before step E can begin.
Step D must be finished before step E can begin.
Step F must be finished before step E can begin.
Visually, these requirements look like this:
-->A--->B--
/ \ \
C -->D----->E
\ /
---->F-----
Your first goal is to determine the order in which the steps should be completed. If more than one step is ready, choose the step which is first alphabetically. In this example, the steps would be completed as follows:
 Only C is available, and so it is done first.
 Next, both A and F are available. A is first alphabetically, so it is done next.
 Then, even though F was available earlier, steps B and D are now also available, and B is the first alphabetically of the three.
 After that, only D and F are available. E is not available because only some of its prerequisites are complete. Therefore, D is completed next.
 F is the only choice, so it is done next.
 Finally, E is completed.
So, in this example, the correct order is CABDFE.
In what order should the steps in your instructions be completed?
Your puzzle answer was GKRVWBESYAMZDPTIUCFXQJLHNO.
--- Part Two ---
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?
Your puzzle answer was 903.
Both parts of this puzzle are complete! They provide two gold stars: **
References
Visible links
. https://adventofcode.com/
. https://adventofcode.com/2018/about
. https://adventofcode.com/2018/events
. https://adventofcode.com/2018/settings
. https://adventofcode.com/2018/auth/logout
. Advent of Code Supporter
https://adventofcode.com/2018/support
. https://adventofcode.com/2018
. https://adventofcode.com/2018
. https://adventofcode.com/2018/support
. https://adventofcode.com/2018/sponsors
. https://adventofcode.com/2018/leaderboard
. https://adventofcode.com/2018/stats
. https://adventofcode.com/2018/sponsors
. https://adventofcode.com/2015/day/6
. https://adventofcode.com/2018
. https://adventofcode.com/2018/day/7/input

7
2018/day07/testinput Normal file
View File

@ -0,0 +1,7 @@
Step C must be finished before step A can begin.
Step C must be finished before step F can begin.
Step A must be finished before step B can begin.
Step A must be finished before step D can begin.
Step B must be finished before step E can begin.
Step D must be finished before step E can begin.
Step F must be finished before step E can begin.