GDD11JP: Go問題答案

この問題のためにGo言語初めて触るという泥縄モード。

はっきり言って言語仕様ちゃんと理解していなくて、とりあえず動きました状態です。

ロジック的には特に難しいところはありません。Goでは組み込みのデータ型としてmapが使えるのは便利です。

package main

import (
    "fmt"
    "io"
    "strings"
    "image/png"
)

func CountColor(pngfile io.Reader) int {
  image, _ := png.Decode(pngfile)
  bounds := image.Bounds()
  colors := map[string]bool {}

  for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
    for x := bounds.Min.X; x < bounds.Max.X; x++ {
      r, g, b, _ := image.At(x, y).RGBA()
      colors[fmt.Sprintf("%04x_%04x_%04x", r, g, b)] = true
    }
  }
  return len(colors)
}