From cbaab27054f954f3397b7b6dd5f4ccb53fc1402b Mon Sep 17 00:00:00 2001 From: Parnic Date: Mon, 20 Jun 2022 08:58:58 -0500 Subject: [PATCH] Day 19 almost Part 1 works, part 2 is giving me trouble. I need to find a better way to validate whether 100x100 can fit, and then something faster to figure out the minimum position. In other words, the entire problem. --- days/19.go | 180 +++++++++++++++++++++++++++++++++++++++++++++++++ inputs/19p.txt | 1 + main.go | 1 + 3 files changed, 182 insertions(+) create mode 100644 days/19.go create mode 100644 inputs/19p.txt diff --git a/days/19.go b/days/19.go new file mode 100644 index 0000000..0f15b20 --- /dev/null +++ b/days/19.go @@ -0,0 +1,180 @@ +package days + +import ( + "fmt" + + u "parnic.com/aoc2019/utilities" +) + +type Day19 struct { + program u.IntcodeProgram +} + +func (d *Day19) Parse() { + d.program = u.LoadIntcodeProgram("19p") +} + +func (d Day19) Num() int { + return 19 +} + +func (d *Day19) Part1() string { + grid := make([][]bool, 50) + for y := 0; y < len(grid); y++ { + grid[y] = make([]bool, 50) + } + + count := int64(0) + + for y := 0; y < 50; y++ { + for x := 0; x < 50; x++ { + d.program.Reset() + d.program.RunIn(func(inputStep int) int64 { + if inputStep == 1 { + return int64(x) + } + + return int64(y) + }, func(val int64, state u.IntcodeProgramState) { + res := val == 1 + grid[y][x] = res + if res { + count++ + } + }) + } + } + + // fmt.Println("50x50 tractor view:") + // for y := 0; y < len(grid); y++ { + // for x := 0; x < len(grid[y]); x++ { + // if grid[y][x] { + // fmt.Print("█") + // } else { + // fmt.Print(" ") + // } + // } + // fmt.Println() + // } + + return fmt.Sprintf("Points affected in 50x50 area: %s%d%s", u.TextBold, count, u.TextReset) +} + +func (d *Day19) Part2() string { + grid := make([][]bool, 5000) + for y := 0; y < len(grid); y++ { + grid[y] = make([]bool, 5000) + } + + // find first hit + firstY := 0 + for y := 1; firstY == 0; y++ { + for x := 0; x < 10*y; x++ { + d.program.Reset() + d.program.RunIn(func(inputStep int) int64 { + if inputStep == 1 { + return int64(x) + } + return int64(y) + }, func(val int64, state u.IntcodeProgramState) { + if val == 1 { + if firstY == 0 { + firstY = y + d.program.Stop() + } + } + }) + } + } + + done := false + y := firstY + x := 0 + firstFoundX := 0 + firstFoundY := 0 + lastX := 0 + lastY := 0 + for y = firstY; !done; y++ { + lastFoundX := -1 + for x = firstFoundX; lastFoundX == -1 || lastFoundX == x-1; x++ { + d.program.Reset() + d.program.RunIn(func(inputStep int) int64 { + if inputStep == 1 { + return int64(x) + } + + return int64(y) + }, func(val int64, state u.IntcodeProgramState) { + res := val == 1 + grid[y][x] = res + if res { + if lastFoundX == -1 { + firstFoundX = x + } + lastFoundX = x + } + }) + } + + if lastFoundX-firstFoundX+1 >= 100 { + if firstFoundY == 0 { + firstFoundY = y + } + done = grid[y-100][firstFoundX+100] + if done { + for checkY := y - 100; checkY <= y; checkY++ { + if !grid[checkY][firstFoundX+100] || !grid[checkY][firstFoundX] { + done = false + break + } + } + + if done { + lastX = firstFoundX + 100 + lastY = y + } + } + } + + // if lastFoundX-firstFoundX+1 >= 100 { + // fmt.Printf("At y=%d, x reached %d for %d in a row\n", y, lastFoundX, lastFoundX-firstFoundX+1) + // } + } + + for y := firstFoundY; y <= lastY; y++ { + for x = 0; ; x++ { + if grid[y][x] { + break + } + } + + found := true + for y2 := y + 1; y2 < y+100; y2++ { + if !grid[y2][x] { + found = false + break + } + } + + if found { + fmt.Printf("100 down, 100 across starts at y=%d, x=%d\n", y, x) + } + } + + // fmt.Println("part 2 tractor view:") + // for y := 0; y < len(grid); y++ { + // for x := 0; x < len(grid[y]); x++ { + // if grid[y][x] { + // fmt.Print("█") + // } else { + // fmt.Print(" ") + // } + // } + // fmt.Println() + // } + + origX := lastX - 100 + origY := lastY - 100 + result := (origX * 10000) + origY + return fmt.Sprintf("x=%d, y=%d, orig x=%d, orig y=%d, %s%d%s", lastX, lastY, origX, origY, u.TextBold, result, u.TextReset) +} diff --git a/inputs/19p.txt b/inputs/19p.txt new file mode 100644 index 0000000..eb82c4a --- /dev/null +++ b/inputs/19p.txt @@ -0,0 +1 @@ +109,424,203,1,21102,11,1,0,1105,1,282,21102,18,1,0,1105,1,259,1201,1,0,221,203,1,21101,31,0,0,1105,1,282,21101,38,0,0,1106,0,259,21002,23,1,2,22101,0,1,3,21102,1,1,1,21101,0,57,0,1105,1,303,1201,1,0,222,20102,1,221,3,21001,221,0,2,21101,259,0,1,21101,0,80,0,1105,1,225,21102,1,76,2,21101,91,0,0,1106,0,303,1201,1,0,223,21001,222,0,4,21102,1,259,3,21101,0,225,2,21102,1,225,1,21102,1,118,0,1106,0,225,20101,0,222,3,21101,100,0,2,21102,1,133,0,1105,1,303,21202,1,-1,1,22001,223,1,1,21101,148,0,0,1105,1,259,2102,1,1,223,20102,1,221,4,21001,222,0,3,21101,0,17,2,1001,132,-2,224,1002,224,2,224,1001,224,3,224,1002,132,-1,132,1,224,132,224,21001,224,1,1,21101,0,195,0,106,0,109,20207,1,223,2,21002,23,1,1,21102,1,-1,3,21101,214,0,0,1105,1,303,22101,1,1,1,204,1,99,0,0,0,0,109,5,1201,-4,0,249,22101,0,-3,1,21201,-2,0,2,22102,1,-1,3,21101,0,250,0,1106,0,225,22101,0,1,-4,109,-5,2105,1,0,109,3,22107,0,-2,-1,21202,-1,2,-1,21201,-1,-1,-1,22202,-1,-2,-2,109,-3,2105,1,0,109,3,21207,-2,0,-1,1206,-1,294,104,0,99,22101,0,-2,-2,109,-3,2105,1,0,109,5,22207,-3,-4,-1,1206,-1,346,22201,-4,-3,-4,21202,-3,-1,-1,22201,-4,-1,2,21202,2,-1,-1,22201,-4,-1,1,22101,0,-2,3,21102,1,343,0,1105,1,303,1106,0,415,22207,-2,-3,-1,1206,-1,387,22201,-3,-2,-3,21202,-2,-1,-1,22201,-3,-1,3,21202,3,-1,-1,22201,-3,-1,2,21201,-4,0,1,21102,1,384,0,1106,0,303,1105,1,415,21202,-4,-1,-4,22201,-4,-3,-4,22202,-3,-2,-2,22202,-2,-4,-4,22202,-3,-2,-3,21202,-4,-1,-2,22201,-3,-2,1,21201,1,0,-4,109,-5,2106,0,0 \ No newline at end of file diff --git a/main.go b/main.go index 1a1c0cc..f4b640b 100644 --- a/main.go +++ b/main.go @@ -52,6 +52,7 @@ var dayMap = []day{ &days.Day16{}, &days.Day17{}, &days.Day18{}, + &days.Day19{}, } func main() {