23

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

you are viewing a single comment's thread
view the rest of the comments
[-] Karmmah@lemmy.world 2 points 2 weeks 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)")

this post was submitted on 08 Dec 2024
23 points (96.0% liked)

Advent Of Code

920 readers
25 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS