Compare commits

..

4 Commits

Author SHA1 Message Date
cdefd68320 feat: add 2018D3 dataset 2025-12-01 20:09:37 +01:00
f28611a7bf test: add unit tests for p1/p2 2025-12-01 20:09:29 +01:00
f98034b00c feat: solve both parts 2025-12-01 20:09:20 +01:00
375b756718 feat: include 2018D3 2025-12-01 20:09:12 +01:00
4 changed files with 1462 additions and 0 deletions

View File

@@ -17,6 +17,7 @@ import (
// 2018
_ "advent-of-code/internal/2018/DayOne"
_ "advent-of-code/internal/2018/DayThree"
_ "advent-of-code/internal/2018/DayTwo"
// 2020

View File

@@ -0,0 +1,101 @@
package daythree
import (
"advent-of-code/internal/registry"
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
func init() {
registry.Register("2018D3", ParseInput, PartOne, PartTwo)
}
func ParseInput(filepath string) []string {
content, _ := os.ReadFile(filepath)
return strings.Split(string(content), "\n")
}
func PartOne(data []string) int {
coverage := make(map[string]int)
re := regexp.MustCompile(`#\d+ @ (\d+),(\d+): (\d+)x(\d+)`)
for _, line := range data {
matches := re.FindStringSubmatch(line)
left, _ := strconv.Atoi(matches[1])
top, _ := strconv.Atoi(matches[2])
width, _ := strconv.Atoi(matches[3])
height, _ := strconv.Atoi(matches[4])
for x := left; x < left+width; x++ {
for y := top; y < top+height; y++ {
key := fmt.Sprintf("%d,%d", x, y)
coverage[key]++
}
}
}
overlapCount := 0
for _, count := range coverage {
if count >= 2 {
overlapCount++
}
}
return overlapCount
}
func PartTwo(data []string) int {
coverage := make(map[string]int)
re := regexp.MustCompile(`#\d+ @ (\d+),(\d+): (\d+)x(\d+)`)
for _, line := range data {
matches := re.FindStringSubmatch(line)
left, _ := strconv.Atoi(matches[1])
top, _ := strconv.Atoi(matches[2])
width, _ := strconv.Atoi(matches[3])
height, _ := strconv.Atoi(matches[4])
for x := left; x < left+width; x++ {
for y := top; y < top+height; y++ {
key := fmt.Sprintf("%d,%d", x, y)
coverage[key]++
}
}
}
reWithID := regexp.MustCompile(`#(\d+) @ (\d+),(\d+): (\d+)x(\d+)`)
for _, line := range data {
matches := reWithID.FindStringSubmatch(line)
if len(matches) != 6 {
continue
}
id, _ := strconv.Atoi(matches[1])
left, _ := strconv.Atoi(matches[2])
top, _ := strconv.Atoi(matches[3])
width, _ := strconv.Atoi(matches[4])
height, _ := strconv.Atoi(matches[5])
overlaps := false
for x := left; x < left+width && !overlaps; x++ {
for y := top; y < top+height; y++ {
key := fmt.Sprintf("%d,%d", x, y)
if coverage[key] > 1 {
overlaps = true
break
}
}
}
if !overlaps {
return id
}
}
return 0
}

View File

@@ -0,0 +1,25 @@
package daythree
import "testing"
var testInput = []string{
"#1 @ 1,3: 4x4",
"#2 @ 3,1: 4x4",
"#3 @ 5,5: 2x2",
}
func TestPartOne(t *testing.T) {
expected := 4
got := PartOne(testInput)
if got != expected {
t.Errorf("PartOne() = %d, want %d", got, expected)
}
}
func TestPartTwo(t *testing.T) {
expected := 3
got := PartTwo(testInput)
if got != expected {
t.Errorf("PartTwo() = %d, want %d", got, expected)
}
}

File diff suppressed because it is too large Load Diff