Compare commits
4 Commits
6668e8ae1b
...
cdefd68320
| Author | SHA1 | Date | |
|---|---|---|---|
| cdefd68320 | |||
| f28611a7bf | |||
| f98034b00c | |||
| 375b756718 |
@@ -17,6 +17,7 @@ import (
|
|||||||
|
|
||||||
// 2018
|
// 2018
|
||||||
_ "advent-of-code/internal/2018/DayOne"
|
_ "advent-of-code/internal/2018/DayOne"
|
||||||
|
_ "advent-of-code/internal/2018/DayThree"
|
||||||
_ "advent-of-code/internal/2018/DayTwo"
|
_ "advent-of-code/internal/2018/DayTwo"
|
||||||
|
|
||||||
// 2020
|
// 2020
|
||||||
|
|||||||
101
internal/2018/DayThree/code.go
Normal file
101
internal/2018/DayThree/code.go
Normal 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
|
||||||
|
}
|
||||||
25
internal/2018/DayThree/code_test.go
Normal file
25
internal/2018/DayThree/code_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
1335
internal/data/2018/DayThree/input.txt
Normal file
1335
internal/data/2018/DayThree/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user