mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-15 15:10:49 +00:00
37 lines
924 B
Go
37 lines
924 B
Go
package main
|
|
|
|
import (
|
|
"math"
|
|
"strings"
|
|
)
|
|
|
|
func fromHex(imager *imager, lines []string) {
|
|
for _, line := range lines {
|
|
direction, count := func() (string, int) {
|
|
var dirs = []string{"R", "D", "L", "U"}
|
|
m := getParams(r, line)
|
|
code := strings.TrimLeft(m["colour"], "#")
|
|
return dirs[mustConv(string(code[len(code)-1]))], mustConvHex(code[:len(code)-1])
|
|
}()
|
|
imager.add(direction, count)
|
|
}
|
|
}
|
|
|
|
// two returns the area of the polygon described by imager.space
|
|
// it uses the hex codes to define points
|
|
func two(lines []string) int {
|
|
imager := newImager()
|
|
buildImage(fromHex, 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
|
|
}
|