up till 07/01/2024
This commit is contained in:
122
9/main.go
122
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{}
|
||||
@@ -20,37 +20,129 @@ func main() {
|
||||
}
|
||||
fileID := 0
|
||||
blockSize := 0
|
||||
blockSizes := []int{}
|
||||
freeSpace := 0
|
||||
freeSpaces := []int{}
|
||||
blocks := []string{}
|
||||
for i := range diskMap {
|
||||
if i % 2 == 0 {
|
||||
blockSize, _ = strconv.Atoi(diskMap[i])
|
||||
blockSizes = append(blockSizes, blockSize)
|
||||
for j:=0;j<blockSize;j++ {
|
||||
blocks = append(blocks, strconv.Itoa(fileID))
|
||||
}
|
||||
fileID++
|
||||
} else {
|
||||
freeSpace, _ = strconv.Atoi(diskMap[i])
|
||||
freeSpaces = append(freeSpaces, freeSpace)
|
||||
for j:=0;j<freeSpace;j++ {
|
||||
blocks = append(blocks, ".")
|
||||
}
|
||||
}
|
||||
}
|
||||
for i := range blocks {
|
||||
if blocks[i] == "." {
|
||||
for j:=len(blocks)-1;j>i;j-- {
|
||||
if blocks[j] != "." {
|
||||
blocks[i] = blocks[j]
|
||||
blocks[j] = "."
|
||||
// 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)
|
||||
}
|
||||
for i:=0;i<len(idPositions);i++ {
|
||||
blocks[idPositions[i]] = "."
|
||||
fmt.Println(blocks)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
checksum := 0
|
||||
for i := range blocks {
|
||||
blockID, _ := strconv.Atoi(blocks[i])
|
||||
checksum = checksum + i * blockID
|
||||
}
|
||||
fmt.Println(checksum)
|
||||
fmt.Println(blocks)
|
||||
}
|
||||
|
||||
|
||||
func findIDpositions(blocks []string, id int) []int {
|
||||
positions := []int{}
|
||||
for i := range blocks {
|
||||
if blocks[i] == strconv.Itoa(id) {
|
||||
positions = append(positions, i)
|
||||
}
|
||||
}
|
||||
return positions
|
||||
}
|
||||
|
||||
func findSpacesAfterID(blocks []string, id 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)
|
||||
}
|
||||
}
|
||||
return spaces
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user