-
-
Save KristofferC/31e81a0d3fa59347b80a6dea4e9f5617 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Point | |
| id::Int | |
| x::Int | |
| y::Int | |
| end | |
| function run_6() | |
| data = readlines(joinpath(@__DIR__, "../../inputs/input_6")) | |
| points = Point[] | |
| min_x, min_y, max_x, max_y = typemax(Int), typemax(Int), 0, 0 | |
| for (i, l) in enumerate(data) | |
| s = split(l, ", ") | |
| x, y = parse.(Int, (s[1], s[2])) | |
| min_x, max_x, min_y, max_y = min(min_x, x), max(max_x, x), min(min_y, y), max(max_y, y) | |
| push!(points, Point(i, x, y)) | |
| end | |
| n_points = length(points) | |
| # The area contains (time, id) | |
| area = fill((0,0), max_y-min_y+1, max_x-min_x+1) | |
| # Shift coords to our origin (we are only interested in areas anyway) | |
| for i in eachindex(points) | |
| point = points[i] | |
| shifted_point = Point(point.id, point.x - min_x + 1, point.y - min_y + 1) | |
| points[i] = shifted_point | |
| area[shifted_point.y, shifted_point.x] = (0, shifted_point.id) | |
| end | |
| # Do the flood filling | |
| t = 0 | |
| new_points = Point[] | |
| while t == 0 || !isempty(points) | |
| resize!(new_points, 0) | |
| t += 1 | |
| for point in points | |
| for (dx, dy) in ((-1, 0), (1, 0), (0, -1), (0, 1)) | |
| flood_fill!(area, new_points, point.x + dx, point.y + dy, point.id, t) | |
| end | |
| end | |
| new_points, points = points, new_points | |
| end | |
| area_ids = [v[2] for v in area] | |
| # Each id on the edge will be infinite, discared them | |
| infinites = unique(vcat(area_ids[:, 1], area_ids[1, :], area_ids[end, :], area_ids[:, end])) | |
| replace!(x -> x in infinites ? -1 : x, area_ids) | |
| # Count the areas in each | |
| count = Dict{Int, Int}() | |
| foreach(i -> count[i] = 0, -1:n_points) | |
| for i in area_ids | |
| count[i] += 1 | |
| end | |
| delete!(count, -1) | |
| println("Part 1: ", findmax(count)[1]) | |
| return | |
| end | |
| function flood_fill!(area, new_coords, x, y, id, t) | |
| 0 < x <= size(area, 2) || return | |
| 0 < y <= size(area, 1) || return | |
| t0, v = area[y, x] | |
| if v == 0 | |
| area[y, x] = (t, id) | |
| push!(new_coords, Point(id, x, y)) | |
| elseif v != id && t0 == t | |
| # Use -1 as a sentinel for equal distance | |
| push!(new_coords, Point(-1, x, y)) | |
| area[y, x] = (t, -1) | |
| end | |
| return | |
| end | |
| run_6() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment