diff --git a/days/17.go b/days/17.go index dfd0217..b9ef746 100644 --- a/days/17.go +++ b/days/17.go @@ -272,5 +272,65 @@ func (d *Day17) Part2() string { instructions = append(instructions, fmt.Sprintf("%d", numMoved)) } - return fmt.Sprintf("%s%s%s", u.TextBold, strings.Join(instructions, ","), u.TextReset) + workingInstructions := make([]string, len(instructions)) + copy(workingInstructions, instructions) + + instructionStr := strings.Join(workingInstructions, ",") + progs := make([][]string, 3) + for i := 0; i < 3; i++ { + numFound := 3 + subLen := 4 + for numFound >= 3 { + numFound = 0 + instructionSubset := strings.Join(workingInstructions[0:subLen], ",") + for x := 0; x <= len(instructionStr)-len(instructionSubset); x++ { + if instructionStr[x:x+len(instructionSubset)] == instructionSubset { + numFound++ + } + } + if numFound >= 3 { + subLen += 2 + } + } + if numFound < 3 { + subLen -= 2 + } + progs[i] = make([]string, subLen) + copy(progs[i], workingInstructions[0:subLen]) + + instructionStr = strings.ReplaceAll(instructionStr, strings.Join(progs[i], ","), "") + instructionStr = strings.TrimPrefix(strings.ReplaceAll(instructionStr, ",,", ","), ",") + + if len(instructionStr) == 0 { + workingInstructions = nil + } else { + workingInstructions = strings.Split(instructionStr, ",") + } + } + + if workingInstructions != nil { + panic("didn't empty instructions") + } + instructionStr = strings.Join(instructions, ",") + instructionStr = strings.ReplaceAll(instructionStr, strings.Join(progs[0], ","), "A") + instructionStr = strings.ReplaceAll(instructionStr, strings.Join(progs[1], ","), "B") + instructionStr = strings.ReplaceAll(instructionStr, strings.Join(progs[2], ","), "C") + + instructionStr = fmt.Sprintf("%s\n%s\n%s\n%s\nn\n", + instructionStr, + strings.Join(progs[0], ","), + strings.Join(progs[1], ","), + strings.Join(progs[2], ","), + ) + + d.program.Reset() + d.program.SetMemory(0, 2) + dustCollected := int64(0) + d.program.RunIn(func(inputStep int) int64 { + return int64(instructionStr[inputStep-1]) + }, func(val int64, state u.IntcodeProgramState) { + dustCollected = val + }) + + return fmt.Sprintf("Dust collected after traveling all paths: %s%d%s", u.TextBold, dustCollected, u.TextReset) }