Write me golang program that solves this task. The program should read its input from file input.txt. It should be concise and efficient. Skip comments.
code
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
--- 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?
Assistant
Here is a concise and efficient Golang program to solve the task from the file input.txt:
go
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
file, _ := os.Open("input.txt")
defer file.Close()
scanner := bufio.NewScanner(file)
fabric := make(map[string]int)
for scanner.Scan() {
text := scanner.Text()
if !strings.HasPrefix(text, "#") {
continue
}
parts := strings.Split(text, " ")
id := parts[0]
leftEdge, _ := strconv.Atoi(strings.Split(parts[2], ",")[0])
topEdge, _ := strconv.Atoi(strings.Split(parts[2], ",")[1][:-1])
width, _ := strconv.Atoi(strings.Split(parts[3], "x")[0])
height, _ := strconv.Atoi(strings.Split(parts[3], "x")[1])
for i := 0; i < width; i++ {
for j := 0; j < height; j++ {
key := fmt.Sprintf("%d,%d", leftEdge+i, topEdge+j)
fabric[key]++
}
}
}
overlaps := 0
for _, v := range fabric {
if v > 1 {
overlaps++
}
}
fmt.Println(overlaps)
}
This opens the input file, parses each claim into its components, tracks each square inch of fabric in a map, and counts the number of squares that have multiple claims. It skips unnecessary text and focuses only on the key parsing and logic to solve the problem efficiently.