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)))")
Julia
Quite happy that today went a lot smoother than yesterday even though I am not really familiar with recursion. Normally I never use recursion but I felt like today could be solved by it (or using trees, but I’m even less familiar with them). Surprisingly my solution actually worked and for part 2 only small modifications were needed to count peaks reached by each trail.
Code
function readInput(inputFile::String) f = open(inputFile,"r") lines::Vector{String} = readlines(f) close(f) topoMap = Matrix{Int}(undef,length(lines),length(lines[1])) for (i,l) in enumerate(lines) topoMap[i,:] = map(x->parse(Int,x),collect(l)) end return topoMap end function getTrailheads(topoMap::Matrix{Int}) trailheads::Vector{Vector{Int}} = [] for (i,r) in enumerate(eachrow(topoMap)) for (j,c) in enumerate(r) c==0 ? push!(trailheads,[i,j]) : nothing end end return trailheads end function getReachablePeaks(topoMap::Matrix{Int},trailheads::Vector{Vector{Int}}) reachablePeaks = Dict{Int,Vector{Vector{Int}}}() function getPossibleMoves(topoMap::Matrix{Int},pos::Vector{Int}) possibleMoves::Vector{Vector{Int}} = [] pos[1]-1 in 1:size(topoMap)[1] && topoMap[pos[1]-1,pos[2]]==topoMap[pos[1],pos[2]]+1 ? push!(possibleMoves,[pos[1]-1,pos[2]]) : nothing #up? pos[1]+1 in 1:size(topoMap)[1] && topoMap[pos[1]+1,pos[2]]==topoMap[pos[1],pos[2]]+1 ? push!(possibleMoves,[pos[1]+1,pos[2]]) : nothing #down? pos[2]-1 in 1:size(topoMap)[2] && topoMap[pos[1],pos[2]-1]==topoMap[pos[1],pos[2]]+1 ? push!(possibleMoves,[pos[1],pos[2]-1]) : nothing #left? pos[2]+1 in 1:size(topoMap)[2] && topoMap[pos[1],pos[2]+1]==topoMap[pos[1],pos[2]]+1 ? push!(possibleMoves,[pos[1],pos[2]+1]) : nothing #right? return possibleMoves end function walkPossMoves(topoMap::Matrix{Int},pos::Vector{Int},reachedPeaks::Matrix{Bool},trailId::Int) possMoves::Vector{Vector{Int}} = getPossibleMoves(topoMap,pos) for m in possMoves if topoMap[m[1],m[2]]==9 reachedPeaks[m[1],m[2]]=1 trailId += 1 continue end reachedPeaks,trailId = walkPossMoves(topoMap,m,reachedPeaks,trailId) end return reachedPeaks, trailId end peaksScore::Int = 0; trailsScore::Int = 0 trailId::Int = 0 for (i,t) in enumerate(trailheads) if !haskey(reachablePeaks,i); reachablePeaks[i]=[]; end reachedPeaks::Matrix{Bool} = zeros(size(topoMap)) trailId = 0 reachedPeaks,trailId = walkPossMoves(topoMap,t,reachedPeaks,trailId) trailPeaksScore = sum(reachedPeaks) peaksScore += trailPeaksScore trailsScore += trailId end return peaksScore,trailsScore end #getReachablePeaks topoMap::Matrix{Int} = readInput("input/day10Input") trailheads::Vector{Vector{Int}} = getTrailheads(topoMap) @info "Part 1" reachablePeaks = getReachablePeaks(topoMap,trailheads)[1] println("reachable peaks: ",reachablePeaks) @info "Part 2" trailsScore::Int = getReachablePeaks(topoMap,trailheads)[2] println("trails score: $trailsScore")