diff --git a/day-18/go.mod b/day-18/go.mod new file mode 100644 index 0000000..c669279 --- /dev/null +++ b/day-18/go.mod @@ -0,0 +1,3 @@ +module github.com/onyx-and-iris/aoc2023/day-18 + +go 1.21.5 diff --git a/day-18/imager.go b/day-18/imager.go new file mode 100644 index 0000000..1a39aed --- /dev/null +++ b/day-18/imager.go @@ -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)) + } +} diff --git a/day-18/makefile b/day-18/makefile new file mode 100644 index 0000000..d1bbae3 --- /dev/null +++ b/day-18/makefile @@ -0,0 +1,10 @@ +TEST="test.txt" +INPUT="input.txt" + +test: + go run . < $(TEST) + +run: + go run . < $(INPUT) + +all: test diff --git a/day-18/one.go b/day-18/one.go new file mode 100644 index 0000000..19a1b79 --- /dev/null +++ b/day-18/one.go @@ -0,0 +1,39 @@ +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 +} diff --git a/day-18/solution.go b/day-18/solution.go new file mode 100644 index 0000000..6340f80 --- /dev/null +++ b/day-18/solution.go @@ -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) + +} diff --git a/day-18/two.go b/day-18/two.go new file mode 100644 index 0000000..0b68418 --- /dev/null +++ b/day-18/two.go @@ -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 +} diff --git a/day-18/util.go b/day-18/util.go new file mode 100644 index 0000000..09ad1c6 --- /dev/null +++ b/day-18/util.go @@ -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) +}