You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.go 935B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/csmith/aoc-2019/common"
  5. "math"
  6. )
  7. const width = 25
  8. const height = 6
  9. func checkChecksum(layer string, fewestZeros *int, checksum *int) {
  10. counts := []int{0, 0, 0}
  11. for _, r := range layer {
  12. counts[r-'0']++
  13. }
  14. if counts[0] < *fewestZeros {
  15. *fewestZeros = counts[0]
  16. *checksum = counts[1] * counts[2]
  17. }
  18. }
  19. func main() {
  20. var (
  21. input = common.ReadFileAsStrings("08/input.txt")[0]
  22. fewestZeros = math.MaxInt64
  23. checksum = 0
  24. pixels [width * height]rune
  25. )
  26. for i := len(input) - width*height; i >= 0; i -= width * height {
  27. layer := input[i : i+width*height]
  28. checkChecksum(layer, &fewestZeros, &checksum)
  29. for i, r := range layer {
  30. if r == '0' {
  31. pixels[i] = ' '
  32. } else if r == '1' {
  33. pixels[i] = '█'
  34. }
  35. }
  36. }
  37. fmt.Printf("%d\n", checksum)
  38. for i, p := range pixels {
  39. fmt.Printf("%c", p)
  40. if i%width == width-1 {
  41. fmt.Printf("\n")
  42. }
  43. }
  44. }