Mercurial > lbo > hg > aoc22
changeset 26:7db9666c6c77
day 15 part 2 - solve inefficiently
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sat, 17 Dec 2022 13:11:15 +0100 |
parents | c5879c425b94 |
children | 457820e3609e |
files | 15/15.jl |
diffstat | 1 files changed, 20 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/15/15.jl Sat Dec 17 12:33:38 2022 +0100 +++ b/15/15.jl Sat Dec 17 13:11:15 2022 +0100 @@ -34,12 +34,12 @@ v end -function point_is_within_closest(p::Point, b::Beacon)::Bool - distance(p, b.sensor) <= b.dist && p != b.beacon +function point_is_within_closest(p::Point, b::Beacon; ignorebeacon=false)::Bool + distance(p, b.sensor) <= b.dist && (ignorebeacon || p != b.beacon) end -function point_is_covered(p::Point, bs::Vector{Beacon})::Bool - any(b -> point_is_within_closest(p, b), bs) +function point_is_covered(p::Point, bs::Vector{Beacon}; ignorebeacon=false)::Bool + any(b -> point_is_within_closest(p, b; ignorebeacon=ignorebeacon), bs) end function n_covered_points(bs::Vector{Beacon}, y::Int)::Int @@ -50,9 +50,24 @@ count end -println(" === PART 1 === "); +function find_distress_beacon(bs::Vector{Beacon}, minc=0, maxc=20)::Vector{Point} + reachable = Set{Point}(); + for b in bs + @show b + for x = b.sensor.x-b.dist:b.sensor.x+b.dist + for y = b.sensor.y-abs(b.dist-abs(x-b.sensor.x)):b.sensor.y+abs(b.dist-abs(x-b.sensor.x)) + push!(reachable, Point(x,y)); + end + end + end + possible = [Point(x, y) for x = minc:maxc for y = minc:maxc if !in(Point(x,y), reachable)]; + possible +end + bs = parse_lines("15/input.txt"); println(n_covered_points(bs, 2000000)); +#println(find_distress_beacon(bs, 0, 4000000)); bs = parse_lines("15/test_input.txt"); println(n_covered_points(bs, 10)); +println(find_distress_beacon(bs));