97 lines
2.1 KiB
Go
97 lines
2.1 KiB
Go
package day10
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
"tools"
|
|
)
|
|
|
|
type ChipTarget struct {
|
|
output bool
|
|
targetNumber int
|
|
}
|
|
|
|
type Instruction struct {
|
|
lowTarget ChipTarget
|
|
highTarget ChipTarget
|
|
}
|
|
|
|
var bots = make(map[int][]int)
|
|
var outputs = make(map[int]int)
|
|
var instructions = make(map[int]Instruction)
|
|
|
|
func setup(input []string) {
|
|
for _, line := range input {
|
|
parts := strings.Split(line, " ")
|
|
if parts[0] == "value" {
|
|
value, _ := strconv.Atoi(parts[1])
|
|
botno, _ := strconv.Atoi(parts[5])
|
|
bots[botno] = append(bots[botno], value)
|
|
} else {
|
|
botno, _ := strconv.Atoi(parts[1])
|
|
lowTargetNumber, _ := strconv.Atoi(parts[6])
|
|
highTargetNumber, _ := strconv.Atoi(parts[11])
|
|
lowTarget := ChipTarget{
|
|
output: parts[5] == "output",
|
|
targetNumber: lowTargetNumber,
|
|
}
|
|
highTarget := ChipTarget{
|
|
output: parts[10] == "output",
|
|
targetNumber: highTargetNumber,
|
|
}
|
|
instructions[botno] = Instruction{
|
|
lowTarget: lowTarget,
|
|
highTarget: highTarget,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func act(part1 bool) int {
|
|
for botno, inv := range bots {
|
|
if len(inv) == 2 {
|
|
low := tools.Min(inv...)
|
|
high := tools.Max(inv...)
|
|
// part 1 solution
|
|
if part1 && low == 17 && high == 61 {
|
|
return botno
|
|
}
|
|
if instructions[botno].lowTarget.output {
|
|
outputs[instructions[botno].lowTarget.targetNumber] = low
|
|
} else {
|
|
bots[instructions[botno].lowTarget.targetNumber] = append(bots[instructions[botno].lowTarget.targetNumber], low)
|
|
}
|
|
if instructions[botno].highTarget.output {
|
|
outputs[instructions[botno].highTarget.targetNumber] = high
|
|
} else {
|
|
bots[instructions[botno].highTarget.targetNumber] = append(bots[instructions[botno].highTarget.targetNumber], high)
|
|
}
|
|
bots[botno] = []int{}
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func Part1(puzzle tools.AoCPuzzle) interface{} {
|
|
setup(puzzle.GetInputArray())
|
|
|
|
i := 0
|
|
for i == 0 {
|
|
i = act(true)
|
|
}
|
|
|
|
return i
|
|
}
|
|
|
|
func Part2(puzzle tools.AoCPuzzle) interface{} {
|
|
// reset bots in case you run both parts together
|
|
bots = make(map[int][]int)
|
|
setup(puzzle.GetInputArray())
|
|
|
|
for outputs[0] == 0 || outputs[1] == 0 || outputs[2] == 0 {
|
|
act(false)
|
|
}
|
|
|
|
return outputs[0] * outputs[1] * outputs[2]
|
|
}
|