From 3ff5e90e8192aedcc867bfe04b32ec8950a16058 Mon Sep 17 00:00:00 2001 From: Parnic Date: Tue, 14 Jun 2022 00:28:14 -0500 Subject: [PATCH] Day 17 part 2...sort of It works for one of my account's inputs, but not the other. At least the foundation is in place...just need to fix the heuristics for determining which subsets to hang onto. I also desperately need to clean up this code as I was writing it just to get stuff working and not thinking about actual structure. --- days/17.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) 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) }