Puh, that was a tough one for me today. I started out by counting the number of claims for each square on the "canvas" which gave me a correct aswer. However, I later misunderstand part 2 thinking I should find completely unclaimed squares that could be filled out by one claim...
After cheating a little and getting inspirations for solutions I have implemented a Golang solution for today. At least I am happy that I could implement it in Golang without any prior experience using Golang:
Part 1 and 2 together:
packagemainimport("bufio""fmt""os""regexp""strconv")typecoordstruct{linttint}// readLines reads a whole file into memory// and returns a slice of its lines.funcreadLines(pathstring)([]string,error){file,err:=os.Open(path)iferr!=nil{returnnil,err}deferfile.Close()varlines[]stringscanner:=bufio.NewScanner(file)forscanner.Scan(){lines=append(lines,scanner.Text())}returnlines,scanner.Err()}funcmapClaims(data[]string)(map[coord][]int,map[int][]int){m:=make(map[coord][]int)claims:=make([][]int,len(data))overlaps:=make(map[int][]int)r,_:=regexp.Compile("-?[0-9]+")fori,d:=rangedata{claimsVal:=r.FindAllString(d,-1)for_,valStr:=rangeclaimsVal{val,_:=strconv.Atoi(valStr)claims[i]=append(claims[i],val)}id:=claims[i][0]startX:=claims[i][1]startY:=claims[i][2]width:=claims[i][3]height:=claims[i][4]overlaps[id]=[]int{}forl:=startX;l<startX+width;l++{fort:=startY;t<startY+height;t++{claimSet:=m[coord{l,t}]for_,number:=rangeclaimSet{overlaps[number]=append(overlaps[number],id)overlaps[id]=append(overlaps[id],number)}claimSet=append(claimSet,id)m[coord{l,t}]=claimSet}}}returnm,overlaps}funcmain(){start:=time.Now()data,err:=readLines("input")iferr!=nil{panic(err)}m,overlaps:=mapClaims(data)partA:=0for_,v:=rangem{iflen(v)>=2{partA++}}fmt.Println(partA)partB:=[]int{}fork,v:=rangeoverlaps{iflen(v)==0{partB=append(partB,k)}}fmt.Println(partB[0])elapsed:=time.Since(start)}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Puh, that was a tough one for me today. I started out by counting the number of claims for each square on the "canvas" which gave me a correct aswer. However, I later misunderstand part 2 thinking I should find completely unclaimed squares that could be filled out by one claim...
After cheating a little and getting inspirations for solutions I have implemented a Golang solution for today. At least I am happy that I could implement it in Golang without any prior experience using Golang:
Part 1 and 2 together: