This commit is contained in:
onyx-and-iris 2023-12-22 21:57:00 +00:00
parent 497da642b2
commit 038dd531d9
7 changed files with 205 additions and 0 deletions

3
day-18/go.mod Normal file
View File

@ -0,0 +1,3 @@
module github.com/onyx-and-iris/aoc2023/day-18
go 1.21.5

37
day-18/imager.go Normal file
View File

@ -0,0 +1,37 @@
package main
type coords struct {
X int
Y int
}
func newCoords(x, y int) coords {
return coords{X: x, Y: y}
}
type imager struct {
point coords
space []coords
}
// newImager returns an imager type
func newImager() *imager {
return &imager{point: newCoords(0, 0), space: make([]coords, 0)}
}
// add appends new coordinates to all points that describe the polygon
func (i *imager) add(direction string, count int) {
for j := 0; j < count; j++ {
switch direction {
case "U":
i.point.Y--
case "D":
i.point.Y++
case "L":
i.point.X--
case "R":
i.point.X++
}
i.space = append(i.space, newCoords(i.point.X, i.point.Y))
}
}

10
day-18/makefile Normal file
View File

@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

39
day-18/one.go Normal file
View File

@ -0,0 +1,39 @@
package main
import (
"math"
"regexp"
)
var r = regexp.MustCompile(`(?P<direction>[A-Z]) (?P<count>[0-9]+) \((?P<colour>.*)\)`)
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
}

22
day-18/solution.go Normal file
View File

@ -0,0 +1,22 @@
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetLevel(log.InfoLevel)
}
func main() {
lines := readlines()
ans := one(lines)
fmt.Printf("solution one: %d\n", ans)
ans = two(lines)
fmt.Printf("solution two: %d\n", ans)
}

36
day-18/two.go Normal file
View File

@ -0,0 +1,36 @@
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
}

58
day-18/util.go Normal file
View File

@ -0,0 +1,58 @@
package main
import (
"bufio"
"log"
"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
}
// mustConv converts 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)
}