~zacbrown/aoc-2019-go

e5639f8622045046adef539a3e8f29eb2ff00522 — Zac Brown 3 years ago 247d6fc
Day 8, part 2 complete.
2 files changed, 46 insertions(+), 4 deletions(-)

M cmd/day8/main.go
A cmd/day8/simple_image_data2.txt
M cmd/day8/main.go => cmd/day8/main.go +45 -4
@@ 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)
}

A cmd/day8/simple_image_data2.txt => cmd/day8/simple_image_data2.txt +1 -0
@@ 0,0 1,1 @@
0222112222120000
\ No newline at end of file