Skip to content

Commit

Permalink
2023 day 3 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
taw10 committed Dec 4, 2023
1 parent 0b50f05 commit eff1420
Showing 1 changed file with 42 additions and 11 deletions.
53 changes: 42 additions & 11 deletions 2023/day03-gear-ratios/day03.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,45 @@ function findnum(line)
end


function gears(instars)

out = []
stars = sort(instars)
for idx in 1:length(stars)-1
if stars[idx][1] == stars[idx+1][1]
push!(out, stars[idx][2]*stars[idx+1][2])
end
end

out
end


issymbol(ch) = (ch != '.') && !isdigit(ch)

function symboladj(grid, xr, y)
function adjacent(xr, y, w, h)

coords = []

# Row above
if y>1
for x in max(xr.start-1, 1):min(xr.stop+1,size(grid)[1])
issymbol(grid[x,y-1]) && return true
push!(coords, (x,y-1))
end
end

# Row below
if y<size(grid)[2]
for x in max(xr.start-1, 1):min(xr.stop+1,size(grid)[1])
issymbol(grid[x,y+1]) && return true
push!(coords, (x,y+1))
end
end

# To the left and right
xr.start>1 && issymbol(grid[xr.start-1,y]) && return true
xr.stop<size(grid)[1] && issymbol(grid[xr.stop+1,y]) && return true
xr.start>1 && push!(coords, (xr.start-1,y))
xr.stop<size(grid)[1] && push!(coords, (xr.stop+1,y))

return false
return coords

end

Expand All @@ -71,14 +87,29 @@ grid = read2d("input")
let total = 0
for y in 1:size(grid)[2]
while (s = findnum(view(grid, :,y))) != false
print("Number at ", s)
if symboladj(grid, s[2], y)
if any(c->issymbol(grid[c...]), adjacent(s[2], y, size(grid)...))
total += s[1]
println(" valid")
else
println("")
end
end
end
println("Part 1: ", total)
end


grid = read2d("input")

let stars = []
for y in 1:size(grid)[2]
while (s = findnum(view(grid, :,y))) != false
let adj = adjacent(s[2], y, size(grid)...)
l = findall(c->grid[c...]=='*', adj)
for lx in l
push!(stars, (adj[lx], s[1]))
end
end
end
end

println("Part 2: ", sum(gears(stars)))

end

0 comments on commit eff1420

Please sign in to comment.