149 lines
3.8 KiB
Go
149 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
file, _ := os.Open("test")
|
|
// file, _ := os.Open("input")
|
|
defer file.Close()
|
|
scanner := bufio.NewScanner(file)
|
|
diskMap := []string{}
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
diskMap = append(diskMap, strings.Split(line, "")...)
|
|
}
|
|
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] = "."
|
|
// 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
|
|
}
|
|
}
|
|
}
|
|
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
|
|
}
|