From cf44a7171642c60266e30b2d5b7dea7054da769d Mon Sep 17 00:00:00 2001 From: Luciano Quercia Date: Mon, 18 Dec 2023 22:19:26 +0100 Subject: [PATCH] 2023 day 18, lucianoq --- 2023/18/Makefile | 17 ++ 2023/18/assignment | 113 +++++++++ 2023/18/common.go | 37 +++ 2023/18/input | 602 +++++++++++++++++++++++++++++++++++++++++++++ 2023/18/main1.go | 45 ++++ 2023/18/main2.go | 61 +++++ 2023/18/output1 | 1 + 2023/18/output2 | 1 + 8 files changed, 877 insertions(+) create mode 100644 2023/18/Makefile create mode 100644 2023/18/assignment create mode 100644 2023/18/common.go create mode 100644 2023/18/input create mode 100644 2023/18/main1.go create mode 100644 2023/18/main2.go create mode 100644 2023/18/output1 create mode 100644 2023/18/output2 diff --git a/2023/18/Makefile b/2023/18/Makefile new file mode 100644 index 0000000..27dfdc0 --- /dev/null +++ b/2023/18/Makefile @@ -0,0 +1,17 @@ +main1: + go build -o main1 main1.go common.go + +main2: + go build -o main2 main2.go common.go + +.PHONY: run1 run2 clean + +run1: main1 + ./main1 0 { + curr, toDo = toDo[0], toDo[1:] + + for _, d := range Delta { + next := P{curr.R + d.R, curr.C + d.C} + + if borders[next] > 0 { + continue + } + + if _, ok := visited[next]; ok { + continue + } + + visited[next] = struct{}{} + toDo = append(toDo, next) + } + } + + return len(visited) +} diff --git a/2023/18/main2.go b/2023/18/main2.go new file mode 100644 index 0000000..2900f7e --- /dev/null +++ b/2023/18/main2.go @@ -0,0 +1,61 @@ +package main + +import ( + "fmt" + "strconv" +) + +func main() { + curr := P{0, 0} + + var edges int + var vertices = []P{curr} + + for _, op := range parse() { + dir, steps := extract(op.Color) + + curr = P{ + curr.R + Delta[dir].R*steps, + curr.C + Delta[dir].C*steps, + } + vertices = append(vertices, curr) + + edges += steps + } + + fmt.Println(area(vertices) + edges/2 + 1) +} + +func extract(s string) (string, int) { + var dir string + switch s[5] { + case '0': + dir = "R" + case '1': + dir = "D" + case '2': + dir = "L" + case '3': + dir = "U" + } + steps, _ := strconv.ParseInt(s[:5], 16, 64) + return dir, int(steps) +} + +// Shoelace formula +func area(vertices []P) int { + var a int + for i := 0; i < len(vertices); i++ { + j := (i + 1) % len(vertices) + a += vertices[i].R * vertices[j].C + a -= vertices[i].C * vertices[j].R + } + return abs(a) / 2 +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} diff --git a/2023/18/output1 b/2023/18/output1 new file mode 100644 index 0000000..1e28a1a --- /dev/null +++ b/2023/18/output1 @@ -0,0 +1 @@ +62573 diff --git a/2023/18/output2 b/2023/18/output2 new file mode 100644 index 0000000..44339b0 --- /dev/null +++ b/2023/18/output2 @@ -0,0 +1 @@ +54662804037719