day 9 complete
This commit is contained in:
BIN
9/__debug_bin3099938667
Executable file
BIN
9/__debug_bin3099938667
Executable file
Binary file not shown.
110
9/main.go
110
9/main.go
@@ -9,8 +9,8 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
file, _ := os.Open("test")
|
||||
// file, _ := os.Open("input")
|
||||
// file, _ := os.Open("test")
|
||||
file, _ := os.Open("input")
|
||||
defer file.Close()
|
||||
scanner := bufio.NewScanner(file)
|
||||
diskMap := []string{}
|
||||
@@ -40,89 +40,38 @@ func main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
// for i := range blocks {
|
||||
// if blocks[i] == "." {
|
||||
// for j:=len(blocks)-1;j>i;j-- {
|
||||
// if blocks[j] != "." {
|
||||
// blocks[i] = blocks[j]
|
||||
// blocks[j] = "."
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// checksum := 0
|
||||
// for i := range blocks {
|
||||
// blockID, _ := strconv.Atoi(blocks[i])
|
||||
// checksum = checksum + i * blockID
|
||||
// }
|
||||
// fmt.Println(checksum)
|
||||
fmt.Println(blocks)
|
||||
// moved := []bool{}
|
||||
// for i:=0;i<fileID;i++ {
|
||||
// moved = append(moved, false)
|
||||
// }
|
||||
// for id:=fileID-1;id>=0;id-- {
|
||||
// for j:=0;j<id;j++{
|
||||
// fmt.Println("1.", blockSizes[id], "<=", freeSpaces[j])
|
||||
// if blockSizes[id] <= freeSpaces[j] {
|
||||
// for k:=len(blocks)-1;k>=0;k-- {
|
||||
// if blocks[k] == strconv.Itoa(j) {
|
||||
// fmt.Println("2. k:", k, "blocks[k]:", blocks[k])
|
||||
// changed := []int{}
|
||||
// for l:=0;l<blockSizes[id];l++ {
|
||||
// emptyPosition := 0
|
||||
// found := false
|
||||
// for ! found {
|
||||
// if blocks[k+emptyPosition+1+l] == "." {
|
||||
// found = true
|
||||
// break
|
||||
// } else {
|
||||
// emptyPosition++
|
||||
// }
|
||||
// }
|
||||
// blocks[k+emptyPosition+1+l] = strconv.Itoa(id)
|
||||
// fmt.Println(blocks)
|
||||
// changed = append(changed, k+1+l)
|
||||
// }
|
||||
// for x1:=0;x1<len(blocks);x1++ {
|
||||
// if blocks[x1] == strconv.Itoa(id) && ! slices.Contains(changed, x1) {
|
||||
// blocks[x1] = "."
|
||||
// }
|
||||
// }
|
||||
// fmt.Println("3.1 before", freeSpaces[j])
|
||||
// freeSpaces[j] = freeSpaces[j]-blockSizes[id]
|
||||
// fmt.Println("3.2 after", freeSpaces[j])
|
||||
// moved[id] = true
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// if moved[id] {
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// fmt.Println(blocks)
|
||||
//TRY2
|
||||
for id:=fileID-1;id>=0;id-- {
|
||||
idPositions := findIDpositions(blocks, id)
|
||||
for i:=0;i<fileID-1;i++ {
|
||||
spacesPositionsAfterID := findSpacesAfterID(blocks, i)
|
||||
if len(idPositions) <= len(spacesPositionsAfterID) {
|
||||
for i:=0;i<len(spacesPositionsAfterID);i++ {
|
||||
blocks[spacesPositionsAfterID[i]] = strconv.Itoa(id)
|
||||
fmt.Println(blocks)
|
||||
if len(freeSpaces) < len(blockSizes) {
|
||||
freeSpaces = append(freeSpaces, 0)
|
||||
}
|
||||
for i:=0;i<len(idPositions);i++ {
|
||||
//TRY3
|
||||
for id:=fileID-1;id>=0;id-- {
|
||||
idPositions := findIDpositions(blocks,id)
|
||||
for moveTo:=0;moveTo<=id;moveTo++ {
|
||||
if moveTo==id {
|
||||
continue
|
||||
}
|
||||
if len(idPositions) <= freeSpaces[moveTo] {
|
||||
//find the spaces after the moveTo
|
||||
idSpacePositions := findSpacesAfterID(blocks,moveTo,freeSpaces[moveTo])
|
||||
//assign id to spaces for each len(idPositions)
|
||||
for i:=0;i<blockSizes[id];i++ {
|
||||
blocks[idSpacePositions[i]]=strconv.Itoa(id)
|
||||
blocks[idPositions[i]] = "."
|
||||
fmt.Println(blocks)
|
||||
freeSpaces[moveTo]--
|
||||
}
|
||||
break
|
||||
}
|
||||
//assign new id positions to idPositions
|
||||
//assign space to each idPositions
|
||||
}
|
||||
}
|
||||
fmt.Println(blocks)
|
||||
}
|
||||
checksum := 0
|
||||
for i := range blocks {
|
||||
blockID, _ := strconv.Atoi(blocks[i])
|
||||
checksum = checksum + i * blockID
|
||||
}
|
||||
fmt.Println(checksum)
|
||||
}
|
||||
|
||||
|
||||
@@ -136,13 +85,16 @@ func findIDpositions(blocks []string, id int) []int {
|
||||
return positions
|
||||
}
|
||||
|
||||
func findSpacesAfterID(blocks []string, id int) []int {
|
||||
func findSpacesAfterID(blocks []string, id, idSpaces int) []int {
|
||||
spaces := []int{}
|
||||
idPositions := findIDpositions(blocks,id)
|
||||
for i:=idPositions[len(idPositions)-1];i<len(blocks);i++ {
|
||||
if blocks[i] == "." {
|
||||
spaces = append(spaces, i)
|
||||
}
|
||||
if len(spaces) == idSpaces {
|
||||
break
|
||||
}
|
||||
}
|
||||
return spaces
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user