mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-18 19:13:27 +02:00
cleanup effort pt 1
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/cast"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var part int
|
||||
flag.IntVar(&part, "part", 1, "part 1 or 2")
|
||||
flag.Parse()
|
||||
fmt.Println("Running part", part)
|
||||
|
||||
if part == 1 {
|
||||
ans := part1(util.ReadFile("./input.txt"))
|
||||
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
||||
fmt.Println("Output:", ans)
|
||||
} else {
|
||||
ans := part2(util.ReadFile("./input.txt"))
|
||||
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
||||
fmt.Println("Output:", ans)
|
||||
}
|
||||
}
|
||||
|
||||
func part1(input string) int {
|
||||
lines := strings.Split(input, "\n")
|
||||
|
||||
seen := make(map[string]bool)
|
||||
counted := make(map[string]bool)
|
||||
var overlap int
|
||||
|
||||
for _, line := range lines {
|
||||
row := cast.ToInt(line[strings.Index(line, "@")+2 : strings.Index(line, ",")])
|
||||
col := cast.ToInt(line[strings.Index(line, ",")+1 : strings.Index(line, ":")])
|
||||
width := cast.ToInt(line[strings.Index(line, ":")+2 : strings.Index(line, "x")])
|
||||
height := cast.ToInt(line[strings.Index(line, "x")+1:])
|
||||
|
||||
for i := 0; i < width; i++ {
|
||||
for j := 0; j < height; j++ {
|
||||
coords := fmt.Sprintf("%vx%v", row+i, col+j)
|
||||
if seen[coords] && !counted[coords] {
|
||||
overlap++
|
||||
counted[coords] = true
|
||||
}
|
||||
seen[coords] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return overlap
|
||||
}
|
||||
|
||||
func part2(input string) int {
|
||||
lines := strings.Split(input, "\n")
|
||||
|
||||
coords := makeMapOfCoordinates(lines)
|
||||
|
||||
for _, line := range lines {
|
||||
uniqueID := hasNoOverlap(coords, line)
|
||||
if uniqueID != -1 {
|
||||
return uniqueID
|
||||
}
|
||||
}
|
||||
panic("expect return from loop")
|
||||
}
|
||||
|
||||
func makeMapOfCoordinates(lines []string) map[string]int {
|
||||
seen := make(map[string]int)
|
||||
for _, line := range lines {
|
||||
// ID := line[:strings.Index(line, " @")]
|
||||
row := cast.ToInt(line[strings.Index(line, "@")+2 : strings.Index(line, ",")])
|
||||
col := cast.ToInt(line[strings.Index(line, ",")+1 : strings.Index(line, ":")])
|
||||
width := cast.ToInt(line[strings.Index(line, ":")+2 : strings.Index(line, "x")])
|
||||
height := cast.ToInt(line[strings.Index(line, "x")+1:])
|
||||
|
||||
for i := 0; i < width; i++ {
|
||||
for j := 0; j < height; j++ {
|
||||
coords := fmt.Sprintf("%vx%v", row+i, col+j)
|
||||
seen[coords]++
|
||||
}
|
||||
}
|
||||
}
|
||||
return seen
|
||||
}
|
||||
|
||||
// if cut is unique returns the ID, otherwise -1
|
||||
func hasNoOverlap(seen map[string]int, line string) int {
|
||||
ID := cast.ToInt(line[1:strings.Index(line, " @")])
|
||||
row := cast.ToInt(line[strings.Index(line, "@")+2 : strings.Index(line, ",")])
|
||||
col := cast.ToInt(line[strings.Index(line, ",")+1 : strings.Index(line, ":")])
|
||||
width := cast.ToInt(line[strings.Index(line, ":")+2 : strings.Index(line, "x")])
|
||||
height := cast.ToInt(line[strings.Index(line, "x")+1:])
|
||||
|
||||
for i := 0; i < width; i++ {
|
||||
for j := 0; j < height; j++ {
|
||||
coords := fmt.Sprintf("%vx%v", row+i, col+j)
|
||||
if seen[coords] != 1 {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
}
|
||||
return ID
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
func Test_part1(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want int
|
||||
}{
|
||||
{"actual", util.ReadFile("input.txt"), 118223},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := part1(tt.input); got != tt.want {
|
||||
t.Errorf("part1() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_part2(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want int
|
||||
}{
|
||||
{"actual", util.ReadFile("input.txt"), 412},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := part2(tt.input); got != tt.want {
|
||||
t.Errorf("part2() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
func main() {
|
||||
input := util.ReadFile("../input.txt")
|
||||
lines := strings.Split(input, "\n")
|
||||
|
||||
seen := make(map[string]bool)
|
||||
counted := make(map[string]bool)
|
||||
var overlap int
|
||||
|
||||
for _, line := range lines {
|
||||
// ID := line[:strings.Index(line, " @")]
|
||||
row, _ := strconv.Atoi(line[strings.Index(line, "@")+2 : strings.Index(line, ",")])
|
||||
col, _ := strconv.Atoi(line[strings.Index(line, ",")+1 : strings.Index(line, ":")])
|
||||
width, _ := strconv.Atoi(line[strings.Index(line, ":")+2 : strings.Index(line, "x")])
|
||||
height, _ := strconv.Atoi(line[strings.Index(line, "x")+1:])
|
||||
|
||||
for i := 0; i < width; i++ {
|
||||
for j := 0; j < height; j++ {
|
||||
coords := fmt.Sprintf("%vx%v", row+i, col+j)
|
||||
if seen[coords] && !counted[coords] {
|
||||
overlap++
|
||||
counted[coords] = true
|
||||
}
|
||||
seen[coords] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("Overlapping cells:", overlap)
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
func main() {
|
||||
input := util.ReadFile("../input.txt")
|
||||
lines := strings.Split(input, "\n")
|
||||
|
||||
coords := makeMapOfCoordinates(lines)
|
||||
|
||||
for _, line := range lines {
|
||||
uniqueID := hasNoOverlap(coords, line)
|
||||
if uniqueID != -1 {
|
||||
fmt.Println("Unique Plan ID is:", uniqueID)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func makeMapOfCoordinates(lines []string) map[string]int {
|
||||
seen := make(map[string]int)
|
||||
for _, line := range lines {
|
||||
// ID := line[:strings.Index(line, " @")]
|
||||
row, _ := strconv.Atoi(line[strings.Index(line, "@")+2 : strings.Index(line, ",")])
|
||||
col, _ := strconv.Atoi(line[strings.Index(line, ",")+1 : strings.Index(line, ":")])
|
||||
width, _ := strconv.Atoi(line[strings.Index(line, ":")+2 : strings.Index(line, "x")])
|
||||
height, _ := strconv.Atoi(line[strings.Index(line, "x")+1:])
|
||||
|
||||
for i := 0; i < width; i++ {
|
||||
for j := 0; j < height; j++ {
|
||||
coords := fmt.Sprintf("%vx%v", row+i, col+j)
|
||||
seen[coords]++
|
||||
}
|
||||
}
|
||||
}
|
||||
return seen
|
||||
}
|
||||
|
||||
// if cut is unique returns the ID, otherwise -1
|
||||
func hasNoOverlap(seen map[string]int, line string) int {
|
||||
ID, _ := strconv.Atoi(line[1:strings.Index(line, " @")])
|
||||
row, _ := strconv.Atoi(line[strings.Index(line, "@")+2 : strings.Index(line, ",")])
|
||||
col, _ := strconv.Atoi(line[strings.Index(line, ",")+1 : strings.Index(line, ":")])
|
||||
width, _ := strconv.Atoi(line[strings.Index(line, ":")+2 : strings.Index(line, "x")])
|
||||
height, _ := strconv.Atoi(line[strings.Index(line, "x")+1:])
|
||||
|
||||
for i := 0; i < width; i++ {
|
||||
for j := 0; j < height; j++ {
|
||||
coords := fmt.Sprintf("%vx%v", row+i, col+j)
|
||||
if seen[coords] != 1 {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
}
|
||||
return ID
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
--- Day 3: No Matter How You Slice It ---
|
||||
The Elves managed to locate the chimney-squeeze prototype fabric for Santa's suit (thanks to someone who helpfully wrote its box IDs on the wall of the warehouse in the middle of the night). Unfortunately, anomalies are still affecting them - nobody can even agree on how to cut the fabric.
|
||||
|
||||
The whole piece of fabric they're working on is a very large square - at least 1000 inches on each side.
|
||||
|
||||
Each Elf has made a claim about which area of fabric would be ideal for Santa's suit. All claims have an ID and consist of a single rectangle with edges parallel to the edges of the fabric. Each claim's rectangle is defined as follows:
|
||||
|
||||
The number of inches between the left edge of the fabric and the left edge of the rectangle.
|
||||
The number of inches between the top edge of the fabric and the top edge of the rectangle.
|
||||
The width of the rectangle in inches.
|
||||
The height of the rectangle in inches.
|
||||
A claim like #123 @ 3,2: 5x4 means that claim ID 123 specifies a rectangle 3 inches from the left edge, 2 inches from the top edge, 5 inches wide, and 4 inches tall. Visually, it claims the square inches of fabric represented by # (and ignores the square inches of fabric represented by .) in the diagram below:
|
||||
|
||||
...........
|
||||
...........
|
||||
...#####...
|
||||
...#####...
|
||||
...#####...
|
||||
...#####...
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
The problem is that many of the claims overlap, causing two or more claims to cover part of the same areas. For example, consider the following claims:
|
||||
|
||||
#1 @ 1,3: 4x4
|
||||
#2 @ 3,1: 4x4
|
||||
#3 @ 5,5: 2x2
|
||||
Visually, these claim the following areas:
|
||||
|
||||
........
|
||||
...2222.
|
||||
...2222.
|
||||
.11XX22.
|
||||
.11XX22.
|
||||
.111133.
|
||||
.111133.
|
||||
........
|
||||
The four square inches marked with X are claimed by both 1 and 2. (Claim 3, while adjacent to the others, does not overlap either of them.)
|
||||
|
||||
If the Elves all proceed with their own plans, none of them will have enough fabric. How many square inches of fabric are within two or more claims?
|
||||
|
||||
Your puzzle answer was 118223.
|
||||
|
||||
The first half of this puzzle is complete! It provides one gold star: *
|
||||
|
||||
--- Part Two ---
|
||||
Amidst the chaos, you notice that exactly one claim doesn't overlap by even a single square inch of fabric with any other claim. If you can somehow draw attention to it, maybe the Elves will be able to make Santa's suit after all!
|
||||
|
||||
For example, in the claims above, only claim 3 is intact after all claims are made.
|
||||
|
||||
What is the ID of the only claim that doesn't overlap?
|
||||
Reference in New Issue
Block a user