Day 9: Disk Fragmenter

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • Karmmah@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    3 days ago

    Julia

    Oh today was a struggle. First I did not get what exactly the task wanted me to do and then in part 2 I tried a few different ideas which all failed because I changed the disk while I was indexing into it. Finally now I reworked part 2 not moving the blocks at all, just using indexes and it works.

    I feel that there is definitely something to learn here and that’s what I like about AoC so far. This is my first AoC but I hope that I won’t have to put this much thought into the rest, since I should definitely use my time differently.

    Code
    function readInput(inputFile::String)
    	f = open(inputFile,"r"); diskMap::String = readline(f); close(f)
    	disk::Vector{String} = []
    	id::Int = 0
    	for (i,c) in enumerate(diskMap)
    		if i%2 != 0 #used space
    			for j=1 : parse(Int,c)
    				push!(disk,string(id))
    			end
    			id += 1
    		else #free space
    			for j=1 : parse(Int,c)
    				push!(disk,".")
    			end
    		end
    	end
    	return disk
    end
    
    function getDiscBlocks(disk::Vector{String})::Vector{Vector{Int}}
    	diskBlocks::Vector{Vector{Int}} = []
    	currBlock::Int = parse(Int,disk[1]) #-1 for free space
    	blockLength::Int = 0; blockStartIndex::Int = 0
    	for (i,b) in enumerate(map(x->(x=="." ? -1 : parse(Int,x)),disk))
    		if b == currBlock
    			blockLength += 1
    		else #b!=currBlock
    			push!(diskBlocks,[currBlock,blockLength,blockStartIndex,i-2])
    			currBlock = b
    			blockLength = 1
    			blockStartIndex = i-1 #start of next block
    		end
    	end
    	push!(diskBlocks,[currBlock,blockLength,blockStartIndex,length(disk)-1])
    	return diskBlocks
    end
    
    function compressDisk(disk::Vector{String})::Vector{Int} #part 1
    	compressedDisk::Vector{Int} = []
    	startPtr::Int=1; endPtr::Int=length(disk)
    	while endPtr >= startPtr
    		while endPtr>startPtr && disk[endPtr]=="."
    			endPtr -= 1
    		end
    		while startPtr<endPtr && disk[startPtr]!="."
    			push!(compressedDisk,parse(Int,disk[startPtr])) about AoC
    			startPtr += 1
    		end
    		push!(compressedDisk,parse(Int,disk[endPtr]))
    		startPtr+=1;endPtr-=1
    	end
    	return compressedDisk
    end
    
    function compressBlocks(diskBlocks::Vector{Vector{Int}})
    	for i=length(diskBlocks) : -1 : 1 #go through all blocks, starting from end
    		diskBlocks[i][1] == -1 ? continue : nothing
    		for j=1 : i-1 #look for large enough empty space
    			diskBlocks[j][1]!=-1 || diskBlocks[j][2]<diskBlocks[i][2] ? continue : nothing #skip occupied blocks and empty blocks that are too short
    			diskBlocks[i][3] = diskBlocks[j][3] #set start index
    			diskBlocks[i][4] = diskBlocks[j][3]+diskBlocks[i][2]-1 #set end index
    			diskBlocks[j][3] += diskBlocks[i][2] #move start of empty block
    			diskBlocks[j][2] -= diskBlocks[i][2] #adjust length of empty block
    			break
    		end
    	end
    	return diskBlocks
    end
    
    function calcChecksum(compressedDisk::Vector{Int})::Int
    	checksum::Int = 0
    	for (i,n) in enumerate(compressedDisk)
    		checksum += n*(i-1)
    	end
    	return checksum
    end
    
    function calcChecksumBlocks(diskBlocks::Vector{Vector{Int}})::Int
    	checksum::Int = 0
    	for b in diskBlocks
    		b[1]==-1 ? continue : nothing
    		for i=b[3] : b[4]
    			checksum += b[1]*i
    		end
    	end
    	return checksum
    end
    
    disk::Vector{String} = readInput("input/day09Input")
    @info "Part 1"
    println("checksum: $(calcChecksum(compressDisk(disk)))")
    @info "Part 2"
    println("checksum: $(calcChecksumBlocks(compressBlocks(getDiscBlocks(disk)))")