aoc2023/day-18/util.go

74 lines
1.4 KiB
Go
Raw Normal View History

2023-12-22 21:57:00 +00:00
package main
import (
"bufio"
"log"
"math"
2023-12-22 21:57:00 +00:00
"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
2023-12-22 21:57:00 +00:00
// 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
}