Day 8: Resonant Collinearity

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
    2
    ·
    16 days ago

    Julia

    I was surprised when my solution worked for part 2 since I thought you also had to include fractions of antenna distances, but apparently not.

    Code
    function readInput(inputFile::String)::Matrix{Char}
    	f = open(inputFile,"r")
    	lines::Vector{String} = readlines(f)
    	close(f)
    	cityMap = Matrix{Char}(undef,length(lines),length(lines[1]))
    	for (i,l) in enumerate(lines)
    		cityMap[i,:] = collect(l)
    	end
    	return cityMap
    end
    
    function getAntennaLocations(cityMap::Matrix{Char})::Dict
    	antennaLocations = Dict{Char,Vector{Vector{Int}}}()
    	for l=1 : size(cityMap)[1]
    		for c=1 : size(cityMap)[2]
    			cityMap[l,c]=='.' ? continue : nothing
    			if !haskey(antennaLocations,cityMap[l,c])
    				antennaLocations[cityMap[l,c]] = []
    			end
    			push!(antennaLocations[cityMap[l,c]],[l,c])
    		end
    	end
    	return antennaLocations
    end
    
    function countAntinodes(cityMap::Matrix{Char},antLoc::Dict{Char,Vector{Vector{Int}}},withHarmonics::Bool)::Int #antLoc: antenna locations
    	lBounds = 1:size(cityMap)[1]; cBounds = 1:size(cityMap)[2]
    	anodeLocs::Matrix{Bool} = zeros(size(cityMap))
    	for key in keys(antLoc)
    		for i=1 : length(antLoc[key])
    			withHarmonics&&length(antLoc[key])>1 ? anodeLocs[antLoc[key][i][1],antLoc[key][i][2]]=1 : nothing #add antenna locations as antinodes
    			#should also add fractions of antenna distances, but works without
    			for j=i+1 : length(antLoc[key])
    				harmonic::Int = 1
    				while true
    					n1l = antLoc[key][i][1]+harmonic*(antLoc[key][i][1]-antLoc[key][j][1])
    					n1c = antLoc[key][i][2]+harmonic*(antLoc[key][i][2]-antLoc[key][j][2])
    					n2l = antLoc[key][j][1]+harmonic*(antLoc[key][j][1]-antLoc[key][i][1])
    					n2c = antLoc[key][j][2]+harmonic*(antLoc[key][j][2]-antLoc[key][i][2])
    					if n1l in lBounds && n1c in cBounds
    						anodeLocs[n1l,n1c] = 1
    					end
    					if n2l in lBounds && n2c in cBounds
    						anodeLocs[n2l,n2c] = 1
    					end
    					withHarmonics ? nothing : break
    					!(n1l in lBounds) && !(n1c in cBounds) && !(n2l in lBounds) && !(n2c in cBounds) ? break : harmonic+=1
    				end
    			end
    		end
    	end
    	return sum(anodeLocs)
    end
    
    @info "Part 1"
    println("antinode count $(countAntinodes(getAntennaLocations(readInput("day08Input"))),false)")
    @info "Part 2"
    println("antinode count $(countAntinodes(getAntennaLocations(readInput("day08Input"))),faltrue)")