package main import ( "math" "regexp" ) var r = regexp.MustCompile(`(?P[A-Z]) (?P[0-9]+) \((?P.*)\)`) func fromRegex(imager *imager, lines []string) { for _, line := range lines { direction, count := func() (string, int) { x := getParams(r, line) return x["direction"], mustConv(x["count"]) }() imager.add(direction, count) } } func buildImage(withParser func(imager *imager, lines []string), imager *imager, lines []string) { withParser(imager, lines) } // one returns the area of the polygon described by imager.space func one(lines []string) int { imager := newImager() buildImage(fromRegex, imager, lines) area := 0 for i := 0; i < len(imager.space); i++ { next := imager.space[(i+1)%len(imager.space)] area += imager.space[i].X*next.Y - imager.space[i].Y*next.X } // add perimeter to area within perimeter area = len(imager.space) + (int(math.Abs(float64(area))) / 2) return area - len(imager.space)/2 + 1 }