diff options
Diffstat (limited to 'day07')
-rw-r--r-- | day07/Dockerfile | 7 | ||||
-rw-r--r-- | day07/Makefile | 17 | ||||
-rwxr-xr-x | day07/entrypoint | 13 | ||||
-rw-r--r-- | day07/input | 101 | ||||
-rwxr-xr-x | day07/part1 | 30 | ||||
-rwxr-xr-x | day07/part2 | 47 |
6 files changed, 215 insertions, 0 deletions
diff --git a/day07/Dockerfile b/day07/Dockerfile new file mode 100644 index 0000000..5e0a8b7 --- /dev/null +++ b/day07/Dockerfile @@ -0,0 +1,7 @@ +FROM ruby:2.5-slim + +WORKDIR /opt + +COPY . . + +ENTRYPOINT ["./entrypoint"] diff --git a/day07/Makefile b/day07/Makefile new file mode 100644 index 0000000..6efb4cd --- /dev/null +++ b/day07/Makefile @@ -0,0 +1,17 @@ +DAY = 07 + +.PHONY: run clean push + +run: build + docker run -it --rm aoc2018day$(DAY) + +build: part* input + docker build -t aoc2018day$(DAY) . + touch build + +clean: + rm -f build + +push: build + docker tag aoc2018day$(DAY) advent.4574.co.uk/aoc2018day$(DAY) + docker push advent.4574.co.uk/aoc2018day$(DAY) diff --git a/day07/entrypoint b/day07/entrypoint new file mode 100755 index 0000000..8982d21 --- /dev/null +++ b/day07/entrypoint @@ -0,0 +1,13 @@ +#!/bin/bash + +if [ -x part1 ] ; then + echo -ne "Part 1:\n\t" + time ./part1 < input +fi +if [ -x part1 -a -x part2 ] ; then + echo +fi +if [ -x part2 ] ; then + echo -ne "Part 2:\n\t" + time ./part2 < input +fi diff --git a/day07/input b/day07/input new file mode 100644 index 0000000..c77664d --- /dev/null +++ b/day07/input @@ -0,0 +1,101 @@ +Step M must be finished before step D can begin. +Step E must be finished before step Z can begin. +Step F must be finished before step W can begin. +Step T must be finished before step K can begin. +Step L must be finished before step Z can begin. +Step K must be finished before step Q can begin. +Step H must be finished before step V can begin. +Step Q must be finished before step P can begin. +Step B must be finished before step S can begin. +Step W must be finished before step P can begin. +Step P must be finished before step R can begin. +Step A must be finished before step D can begin. +Step G must be finished before step Z can begin. +Step I must be finished before step D can begin. +Step V must be finished before step D can begin. +Step Z must be finished before step R can begin. +Step X must be finished before step R can begin. +Step S must be finished before step U can begin. +Step J must be finished before step D can begin. +Step R must be finished before step U can begin. +Step U must be finished before step Y can begin. +Step D must be finished before step C can begin. +Step Y must be finished before step N can begin. +Step O must be finished before step C can begin. +Step N must be finished before step C can begin. +Step Q must be finished before step O can begin. +Step K must be finished before step Z can begin. +Step X must be finished before step N can begin. +Step F must be finished before step I can begin. +Step F must be finished before step O can begin. +Step V must be finished before step X can begin. +Step E must be finished before step N can begin. +Step V must be finished before step C can begin. +Step B must be finished before step P can begin. +Step J must be finished before step U can begin. +Step D must be finished before step Y can begin. +Step Z must be finished before step J can begin. +Step I must be finished before step U can begin. +Step E must be finished before step O can begin. +Step X must be finished before step U can begin. +Step G must be finished before step S can begin. +Step K must be finished before step X can begin. +Step G must be finished before step N can begin. +Step X must be finished before step O can begin. +Step P must be finished before step G can begin. +Step L must be finished before step G can begin. +Step B must be finished before step Y can begin. +Step W must be finished before step G can begin. +Step B must be finished before step A can begin. +Step T must be finished before step S can begin. +Step J must be finished before step C can begin. +Step A must be finished before step U can begin. +Step R must be finished before step D can begin. +Step U must be finished before step O can begin. +Step D must be finished before step N can begin. +Step O must be finished before step N can begin. +Step Q must be finished before step A can begin. +Step V must be finished before step J can begin. +Step W must be finished before step O can begin. +Step F must be finished before step R can begin. +Step A must be finished before step X can begin. +Step H must be finished before step O can begin. +Step P must be finished before step X can begin. +Step E must be finished before step Y can begin. +Step M must be finished before step U can begin. +Step T must be finished before step C can begin. +Step A must be finished before step S can begin. +Step P must be finished before step S can begin. +Step Q must be finished before step B can begin. +Step V must be finished before step S can begin. +Step S must be finished before step Y can begin. +Step X must be finished before step Y can begin. +Step H must be finished before step S can begin. +Step J must be finished before step R can begin. +Step V must be finished before step U can begin. +Step X must be finished before step C can begin. +Step I must be finished before step X can begin. +Step Y must be finished before step O can begin. +Step V must be finished before step O can begin. +Step F must be finished before step L can begin. +Step T must be finished before step Q can begin. +Step R must be finished before step O can begin. +Step E must be finished before step W can begin. +Step Q must be finished before step Y can begin. +Step E must be finished before step H can begin. +Step I must be finished before step R can begin. +Step B must be finished before step D can begin. +Step F must be finished before step A can begin. +Step J must be finished before step Y can begin. +Step R must be finished before step N can begin. +Step W must be finished before step A can begin. +Step D must be finished before step O can begin. +Step P must be finished before step N can begin. +Step E must be finished before step Q can begin. +Step G must be finished before step V can begin. +Step G must be finished before step I can begin. +Step X must be finished before step S can begin. +Step M must be finished before step C can begin. +Step G must be finished before step X can begin. +Step T must be finished before step P can begin. +Step Q must be finished before step Z can begin. diff --git a/day07/part1 b/day07/part1 new file mode 100755 index 0000000..bd92758 --- /dev/null +++ b/day07/part1 @@ -0,0 +1,30 @@ +#!/usr/bin/env ruby + +input = $stdin.readlines.map do |i| + i =~ /Step ([A-Z]) must be finished before step ([A-Z]) can begin./ + [$2, $1] +end + +steps = input.flatten.uniq.sort +prereqs = {} + +steps.each do |s| + prereqs[s] = [] +end + +input.each do |i| + prereqs[i.first] << i.last +end + +order = [] + +until prereqs.empty? do + next_step = prereqs.select { |k,v| v.empty? }.keys.sort.first + order << next_step + prereqs.delete(next_step) + prereqs.each_key do |k| + prereqs[k].delete(next_step) + end +end + +puts order.join diff --git a/day07/part2 b/day07/part2 new file mode 100755 index 0000000..eb71690 --- /dev/null +++ b/day07/part2 @@ -0,0 +1,47 @@ +#!/usr/bin/env ruby + +input = $stdin.readlines.map do |i| + i =~ /Step ([A-Z]) must be finished before step ([A-Z]) can begin./ + [$2, $1] +end + +steps = input.flatten.uniq.sort +prereqs = {} +workers = Array.new(5, nil) +now = 0 + +steps.each do |s| + prereqs[s] = [] +end + +input.each do |i| + prereqs[i.first] << i.last +end + +until prereqs.empty? && !workers.any? do + workers.each_index do |i| + next unless workers[i] + workers[i][1] -= 1 + if workers[i][1].zero? + ns = workers[i][0] + prereqs.each_key do |k| + prereqs[k].delete(ns) + end + workers[i] = nil + end + end + + next_steps = prereqs.select { |k,v| v.empty? }.keys.sort + workers.each_index do |i| + unless workers[i] || next_steps.empty? + ns = next_steps.shift + time = ns.ord - 64 + 60 + workers[i] = [ns, time] + prereqs.delete(ns) + end + end + + now += 1 +end + +puts now - 1 |