aoc2023/day-18/util.go
2023-12-24 10:50:41 +00:00

74 lines
1.4 KiB
Go

package main
import (
"bufio"
"log"
"math"
"os"
"regexp"
"strconv"
)
// readlines reads lines from stdin.
// it returns them as an array of strings
func readlines() []string {
lines := []string{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return lines
}
func getParams(rexp *regexp.Regexp, url string) map[string]string {
match := rexp.FindStringSubmatch(url)
m := make(map[string]string)
for i, name := range rexp.SubexpNames() {
if i > 0 && i <= len(match) {
m[name] = match[i]
}
}
return m
}
// mustConv converts string to int
// it will panic if an error occurs
func mustConv(s string) int {
n, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return n
}
// mustConvHex converts a hex string to int
// it will panic if an error occurs
func mustConvHex(s string) int {
n, err := strconv.ParseInt(s, 16, 64)
if err != nil {
panic(err)
}
return int(n)
}
// calculateArea returns the area of the polygon described by imager.space
func calculateArea(space []coords) int {
area := 0
for i := 0; i < len(space); i++ {
next := space[(i+1)%len(space)]
area += space[i].X*next.Y - space[i].Y*next.X
}
// add perimeter to area within perimeter
area = len(space) + (int(math.Abs(float64(area))) / 2)
return area - len(space)/2 + 1
}