day 9 complete
This commit is contained in:
BIN
9/__debug_bin3099938667
Executable file
BIN
9/__debug_bin3099938667
Executable file
Binary file not shown.
106
9/main.go
106
9/main.go
@@ -9,8 +9,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
file, _ := os.Open("test")
|
// file, _ := os.Open("test")
|
||||||
// file, _ := os.Open("input")
|
file, _ := os.Open("input")
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
diskMap := []string{}
|
diskMap := []string{}
|
||||||
@@ -40,90 +40,39 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// for i := range blocks {
|
if len(freeSpaces) < len(blockSizes) {
|
||||||
// if blocks[i] == "." {
|
freeSpaces = append(freeSpaces, 0)
|
||||||
// for j:=len(blocks)-1;j>i;j-- {
|
}
|
||||||
// if blocks[j] != "." {
|
//TRY3
|
||||||
// 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-- {
|
for id:=fileID-1;id>=0;id-- {
|
||||||
idPositions := findIDpositions(blocks,id)
|
idPositions := findIDpositions(blocks,id)
|
||||||
for i:=0;i<fileID-1;i++ {
|
for moveTo:=0;moveTo<=id;moveTo++ {
|
||||||
spacesPositionsAfterID := findSpacesAfterID(blocks, i)
|
if moveTo==id {
|
||||||
if len(idPositions) <= len(spacesPositionsAfterID) {
|
continue
|
||||||
for i:=0;i<len(spacesPositionsAfterID);i++ {
|
|
||||||
blocks[spacesPositionsAfterID[i]] = strconv.Itoa(id)
|
|
||||||
fmt.Println(blocks)
|
|
||||||
}
|
}
|
||||||
for i:=0;i<len(idPositions);i++ {
|
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]] = "."
|
blocks[idPositions[i]] = "."
|
||||||
fmt.Println(blocks)
|
freeSpaces[moveTo]--
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
//assign new id positions to idPositions
|
||||||
|
//assign space to each idPositions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Println(blocks)
|
fmt.Println(blocks)
|
||||||
}
|
}
|
||||||
|
checksum := 0
|
||||||
|
for i := range blocks {
|
||||||
|
blockID, _ := strconv.Atoi(blocks[i])
|
||||||
|
checksum = checksum + i * blockID
|
||||||
|
}
|
||||||
|
fmt.Println(checksum)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func findIDpositions(blocks []string, id int) []int {
|
func findIDpositions(blocks []string, id int) []int {
|
||||||
@@ -136,13 +85,16 @@ func findIDpositions(blocks []string, id int) []int {
|
|||||||
return positions
|
return positions
|
||||||
}
|
}
|
||||||
|
|
||||||
func findSpacesAfterID(blocks []string, id int) []int {
|
func findSpacesAfterID(blocks []string, id, idSpaces int) []int {
|
||||||
spaces := []int{}
|
spaces := []int{}
|
||||||
idPositions := findIDpositions(blocks,id)
|
idPositions := findIDpositions(blocks,id)
|
||||||
for i:=idPositions[len(idPositions)-1];i<len(blocks);i++ {
|
for i:=idPositions[len(idPositions)-1];i<len(blocks);i++ {
|
||||||
if blocks[i] == "." {
|
if blocks[i] == "." {
|
||||||
spaces = append(spaces, i)
|
spaces = append(spaces, i)
|
||||||
}
|
}
|
||||||
|
if len(spaces) == idSpaces {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return spaces
|
return spaces
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user