71 lines
1.1 KiB
Go
71 lines
1.1 KiB
Go
package day15
|
|
|
|
import (
|
|
"regexp"
|
|
"strconv"
|
|
"tools"
|
|
)
|
|
|
|
type Disc struct {
|
|
posCount int
|
|
initPos int
|
|
}
|
|
|
|
func parseInput(inp []string, part2 bool) map[int]Disc {
|
|
discs := make(map[int]Disc)
|
|
parserRegex := regexp.MustCompile(".*has ([0-9]+) positions.*at position ([0-9]+).")
|
|
for i, s := range inp {
|
|
parts := parserRegex.FindStringSubmatch(s)
|
|
pc, _ := strconv.Atoi(parts[1])
|
|
ip, _ := strconv.Atoi(parts[2])
|
|
discs[i] = Disc{pc, ip}
|
|
if part2 {
|
|
discs[i+1] = Disc{11, 0}
|
|
}
|
|
}
|
|
|
|
return discs
|
|
}
|
|
|
|
func Part1(puzzle tools.AoCPuzzle) interface{} {
|
|
discs := parseInput(puzzle.GetInputArray(), false)
|
|
|
|
tIndex := -1
|
|
for {
|
|
tIndex++
|
|
failed := false
|
|
for i, d := range discs {
|
|
if !((d.initPos+tIndex+i+1)%d.posCount == 0) {
|
|
failed = true
|
|
break
|
|
}
|
|
}
|
|
if !failed {
|
|
break
|
|
}
|
|
}
|
|
|
|
return tIndex
|
|
}
|
|
|
|
func Part2(puzzle tools.AoCPuzzle) interface{} {
|
|
discs := parseInput(puzzle.GetInputArray(), true)
|
|
|
|
tIndex := -1
|
|
for {
|
|
tIndex++
|
|
failed := false
|
|
for i, d := range discs {
|
|
if !((d.initPos+tIndex+i+1)%d.posCount == 0) {
|
|
failed = true
|
|
break
|
|
}
|
|
}
|
|
if !failed {
|
|
break
|
|
}
|
|
}
|
|
|
|
return tIndex
|
|
}
|