wooo day7
This commit is contained in:
parent
7922c5add8
commit
8e4fbf44bc
2 changed files with 165 additions and 0 deletions
64
day7.exs
Normal file
64
day7.exs
Normal file
|
@ -0,0 +1,64 @@
|
|||
defmodule DaySeven do
|
||||
def part_one(input) do
|
||||
deps = parse_input(input)
|
||||
letters = Enum.map(?A..?Z, &(<<&1>>))
|
||||
Enum.reverse resolve_instructions(deps, letters, [])
|
||||
end
|
||||
|
||||
def part_two(input) do
|
||||
deps = parse_input(input)
|
||||
letters = Enum.map(?A..?Z, &(<<&1>>))
|
||||
resolve_workers(deps, letters, [], 5, 0) - 1
|
||||
end
|
||||
|
||||
def parse_input(input) do
|
||||
regex = ~r/Step (\w) must be finished before step (\w) can begin\./
|
||||
String.split(input, "\n")
|
||||
|> Enum.filter(&(&1 != ""))
|
||||
|> Enum.map(fn line ->
|
||||
[[_, on, dep]] = Regex.scan(regex, line)
|
||||
{on, dep}
|
||||
end)
|
||||
end
|
||||
|
||||
def resolve_instructions([], [ letter ], acc), do: [letter | acc ]
|
||||
def resolve_instructions(deps, remletters, acc) do
|
||||
firsts = Enum.filter(remletters, fn letter -> not Enum.member?(Enum.map(deps, fn {_on, dep} -> dep end), letter) end)
|
||||
[first | _ ] = firsts |> Enum.sort
|
||||
rem = Enum.filter(deps, fn {on, _dep} -> on != first end)
|
||||
rletters = Enum.filter(remletters, fn letter -> letter != first end)
|
||||
resolve_instructions(rem, rletters, [first | acc])
|
||||
end
|
||||
|
||||
def resolve_workers(_deps, [], _workers, _, acc), do: acc
|
||||
def resolve_workers(deps, remletters, workers, maxworkers, acc) do
|
||||
sworkers = Enum.map(workers, fn {task, time} -> {task, time - 1} end)
|
||||
step_workers = Enum.filter(sworkers, fn {_, time} -> time != 0 end)
|
||||
completed = Enum.filter(sworkers, fn {_, time} -> time == 0 end)
|
||||
|> Enum.map(fn {task, _} -> task end)
|
||||
uremletters = Enum.filter(remletters, &(not Enum.member?(completed, &1)))
|
||||
udeps = Enum.filter(deps, fn {on, _dep} -> not Enum.member?(completed, on) end)
|
||||
canwork = Enum.filter(uremletters, fn letter -> not Enum.member?(Enum.map(udeps, fn {_on, dep} -> dep end), letter) end) |> Enum.filter(fn letter -> not Enum.member?(Enum.map(step_workers, fn {task, _} -> task end), letter) end)
|
||||
uworkers = if length(step_workers) < maxworkers and length(canwork) > 0 do
|
||||
schedule_work(canwork, maxworkers - length(step_workers), step_workers)
|
||||
else
|
||||
step_workers
|
||||
end
|
||||
|
||||
# IO.puts "Move #{acc}, now working on #{Enum.map(uworkers, fn {task, time} -> "(#{task}: #{time})" end) |> Enum.join(", ")}"
|
||||
resolve_workers(udeps, uremletters, uworkers, maxworkers, acc + 1)
|
||||
end
|
||||
|
||||
def schedule_work([], _, workers), do: workers
|
||||
def schedule_work(_, 0, workers), do: workers
|
||||
def schedule_work([work | tail], numworkers, workers) do
|
||||
schedule_work(tail, numworkers - 1, [{work, time_taken(work)} | workers])
|
||||
end
|
||||
|
||||
def time_taken(<< letter >>), do: letter - 4
|
||||
end
|
||||
|
||||
{:ok, content} = File.read("input7.txt")
|
||||
|
||||
IO.puts("Part 1: #{DaySeven.part_one(content)}")
|
||||
IO.puts("Part 2: #{DaySeven.part_two(content)}")
|
101
input7.txt
Normal file
101
input7.txt
Normal file
|
@ -0,0 +1,101 @@
|
|||
Step X must be finished before step C can begin.
|
||||
Step C must be finished before step G can begin.
|
||||
Step F must be finished before step G can begin.
|
||||
Step U must be finished before step Y can begin.
|
||||
Step O must be finished before step S can begin.
|
||||
Step D must be finished before step N can begin.
|
||||
Step M must be finished before step H can begin.
|
||||
Step J must be finished before step Q can begin.
|
||||
Step G must be finished before step R can begin.
|
||||
Step I must be finished before step N can begin.
|
||||
Step R must be finished before step K can begin.
|
||||
Step A must be finished before step Z can begin.
|
||||
Step Y must be finished before step L can begin.
|
||||
Step H must be finished before step P can begin.
|
||||
Step K must be finished before step S can begin.
|
||||
Step Z must be finished before step P can begin.
|
||||
Step T must be finished before step S can begin.
|
||||
Step N must be finished before step P can begin.
|
||||
Step E must be finished before step S can begin.
|
||||
Step S must be finished before step W can begin.
|
||||
Step W must be finished before step V can begin.
|
||||
Step L must be finished before step V can begin.
|
||||
Step P must be finished before step B can begin.
|
||||
Step Q must be finished before step V can begin.
|
||||
Step B must be finished before step V can begin.
|
||||
Step P must be finished before step Q can begin.
|
||||
Step S must be finished before step V can begin.
|
||||
Step C must be finished before step Q can begin.
|
||||
Step I must be finished before step H can begin.
|
||||
Step A must be finished before step E can begin.
|
||||
Step H must be finished before step Q can begin.
|
||||
Step G must be finished before step V can begin.
|
||||
Step N must be finished before step L can begin.
|
||||
Step R must be finished before step Q can begin.
|
||||
Step W must be finished before step L can begin.
|
||||
Step X must be finished before step L can begin.
|
||||
Step X must be finished before step J can begin.
|
||||
Step W must be finished before step P can begin.
|
||||
Step U must be finished before step B can begin.
|
||||
Step P must be finished before step V can begin.
|
||||
Step O must be finished before step P can begin.
|
||||
Step W must be finished before step Q can begin.
|
||||
Step S must be finished before step Q can begin.
|
||||
Step U must be finished before step Z can begin.
|
||||
Step Z must be finished before step T can begin.
|
||||
Step M must be finished before step T can begin.
|
||||
Step A must be finished before step P can begin.
|
||||
Step Z must be finished before step B can begin.
|
||||
Step N must be finished before step S can begin.
|
||||
Step H must be finished before step N can begin.
|
||||
Step J must be finished before step E can begin.
|
||||
Step M must be finished before step J can begin.
|
||||
Step R must be finished before step A can begin.
|
||||
Step A must be finished before step Y can begin.
|
||||
Step F must be finished before step V can begin.
|
||||
Step L must be finished before step P can begin.
|
||||
Step K must be finished before step L can begin.
|
||||
Step F must be finished before step P can begin.
|
||||
Step G must be finished before step L can begin.
|
||||
Step I must be finished before step Q can begin.
|
||||
Step C must be finished before step L can begin.
|
||||
Step I must be finished before step Y can begin.
|
||||
Step G must be finished before step B can begin.
|
||||
Step H must be finished before step L can begin.
|
||||
Step X must be finished before step U can begin.
|
||||
Step I must be finished before step K can begin.
|
||||
Step R must be finished before step N can begin.
|
||||
Step I must be finished before step L can begin.
|
||||
Step M must be finished before step I can begin.
|
||||
Step K must be finished before step V can begin.
|
||||
Step G must be finished before step E can begin.
|
||||
Step F must be finished before step B can begin.
|
||||
Step O must be finished before step Y can begin.
|
||||
Step Y must be finished before step Q can begin.
|
||||
Step F must be finished before step K can begin.
|
||||
Step N must be finished before step W can begin.
|
||||
Step O must be finished before step R can begin.
|
||||
Step N must be finished before step E can begin.
|
||||
Step M must be finished before step V can begin.
|
||||
Step H must be finished before step T can begin.
|
||||
Step Y must be finished before step T can begin.
|
||||
Step F must be finished before step J can begin.
|
||||
Step F must be finished before step O can begin.
|
||||
Step W must be finished before step B can begin.
|
||||
Step T must be finished before step E can begin.
|
||||
Step T must be finished before step P can begin.
|
||||
Step F must be finished before step M can begin.
|
||||
Step U must be finished before step I can begin.
|
||||
Step H must be finished before step S can begin.
|
||||
Step S must be finished before step P can begin.
|
||||
Step T must be finished before step W can begin.
|
||||
Step A must be finished before step N can begin.
|
||||
Step O must be finished before step N can begin.
|
||||
Step L must be finished before step B can begin.
|
||||
Step U must be finished before step K can begin.
|
||||
Step Z must be finished before step W can begin.
|
||||
Step X must be finished before step D can begin.
|
||||
Step Z must be finished before step L can begin.
|
||||
Step I must be finished before step T can begin.
|
||||
Step O must be finished before step W can begin.
|
||||
Step I must be finished before step B can begin.
|
Loading…
Reference in a new issue