From e5639f8622045046adef539a3e8f29eb2ff00522 Mon Sep 17 00:00:00 2001 From: Zac Brown Date: Sat, 7 Dec 2019 22:16:44 -0800 Subject: [PATCH] Day 8, part 2 complete. --- cmd/day8/main.go | 49 ++++++++++++++++++++++++++++++--- cmd/day8/simple_image_data2.txt | 1 + 2 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 cmd/day8/simple_image_data2.txt diff --git a/cmd/day8/main.go b/cmd/day8/main.go index 8da01be..283e2aa 100644 --- a/cmd/day8/main.go +++ b/cmd/day8/main.go @@ -28,10 +28,12 @@ func loadImageData(filename string) string { } const ( - imageWidth = 25 - imageHeight = 6 - testImageWidth = 3 - testImageHeight = 2 + imageWidth = 25 + imageHeight = 6 + testImageWidth = 3 + testImageHeight = 2 + testImage2Width = 2 + testImage2Height = 2 ) type Layer [imageHeight][imageWidth]int @@ -94,6 +96,41 @@ func findImageChecksum(layer Layer) int { return ones * twos } +const ( + BLACK = 0 + WHITE = 1 + TRANSPARENT = 2 +) + +func renderFinalImage(layers []Layer) Layer { + final := layers[0] + + for _, l := range layers[1:] { + for ii := 0; ii < len(l); ii++ { + for jj := 0; jj < len(l[ii]); jj++ { + if final[ii][jj] == TRANSPARENT { + final[ii][jj] = l[ii][jj] + } + } + } + } + + return final +} + +func displayRenderedImage(layer Layer) { + for ii := 0; ii < len(layer); ii++ { + for jj := 0; jj < len(layer[ii]); jj++ { + if layer[ii][jj] == BLACK { + print("@@@") + } else { + print(" ") + } + } + println() + } +} + func main() { imageData := loadImageData(os.Args[1]) println("imageData: ", imageData) @@ -103,4 +140,8 @@ func main() { fmt.Printf("lowestZeroLayer: %v\n", lowestZeroLayer) fmt.Printf("[Part 1] Answer: %v\n", findImageChecksum(lowestZeroLayer)) + println() + println("[Part 2] Answer:") + renderedImage := renderFinalImage(layers) + displayRenderedImage(renderedImage) } diff --git a/cmd/day8/simple_image_data2.txt b/cmd/day8/simple_image_data2.txt new file mode 100644 index 0000000..adee405 --- /dev/null +++ b/cmd/day8/simple_image_data2.txt @@ -0,0 +1 @@ +0222112222120000 \ No newline at end of file -- 2.45.2