Mercurial > lbo > hg > aoc22
changeset 25:c5879c425b94
day 15 part 1
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sat, 17 Dec 2022 12:33:38 +0100 |
parents | e7ce9cd36033 |
children | 7db9666c6c77 |
files | 15/15.jl 15/input.txt 15/test_input.txt |
diffstat | 3 files changed, 99 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/15/15.jl Sat Dec 17 12:33:38 2022 +0100 @@ -0,0 +1,58 @@ + +using ParserCombinator + +const Line = E"Sensor at x=" + PInt64() + E", y=" + PInt64() + E": closest beacon is at x=" + PInt64() + E", y=" + PInt64() + Eos(); + +struct Point + x::Int + y::Int +end + +function distance(p::Point, q::Point)::Int + abs(p.x-q.x) + abs(p.y-q.y) +end + +struct Beacon + sensor::Point + beacon::Point + dist::Int +end + +function parse_line(s::String)::Beacon + es = parse_one(s, Line); + p, q = Point(es[1]::Int, es[2]::Int), Point(es[3]::Int, es[4]::Int) + Beacon(p, q, distance(p, q)) +end + +function parse_lines(f::String)::Vector{Beacon} + v = Vector{Beacon}(); + open(f; read=true) do fh + for l in eachline(fh) + push!(v, parse_line(l)); + end + end + v +end + +function point_is_within_closest(p::Point, b::Beacon)::Bool + distance(p, b.sensor) <= b.dist && p != b.beacon +end + +function point_is_covered(p::Point, bs::Vector{Beacon})::Bool + any(b -> point_is_within_closest(p, b), bs) +end + +function n_covered_points(bs::Vector{Beacon}, y::Int)::Int + minx = minimum(min(b.sensor.x, b.beacon.x)-b.dist for b in bs); + maxx = maximum(max(b.sensor.x, b.beacon.x)+b.dist for b in bs); + + count = sum(point_is_covered(Point(x, y), bs) for x = minx:maxx); + count +end + +println(" === PART 1 === "); +bs = parse_lines("15/input.txt"); +println(n_covered_points(bs, 2000000)); + +bs = parse_lines("15/test_input.txt"); +println(n_covered_points(bs, 10));
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/15/input.txt Sat Dec 17 12:33:38 2022 +0100 @@ -0,0 +1,27 @@ +Sensor at x=3391837, y=2528277: closest beacon is at x=3448416, y=2478759 +Sensor at x=399473, y=1167503: closest beacon is at x=1188862, y=2000000 +Sensor at x=3769110, y=2896086: closest beacon is at x=4076658, y=2478123 +Sensor at x=900438, y=3835648: closest beacon is at x=-435606, y=3506717 +Sensor at x=2913762, y=3937542: closest beacon is at x=2964244, y=3612685 +Sensor at x=3646459, y=3446878: closest beacon is at x=3264675, y=3635510 +Sensor at x=1182092, y=2135147: closest beacon is at x=1188862, y=2000000 +Sensor at x=3213897, y=2710772: closest beacon is at x=3448416, y=2478759 +Sensor at x=3242113, y=3984214: closest beacon is at x=3264675, y=3635510 +Sensor at x=2809237, y=3782833: closest beacon is at x=2872059, y=3592616 +Sensor at x=2962421, y=37354: closest beacon is at x=3358601, y=-1111474 +Sensor at x=3456740, y=2458922: closest beacon is at x=3448416, y=2478759 +Sensor at x=1799203, y=3569221: closest beacon is at x=2872059, y=3592616 +Sensor at x=3907873, y=3898376: closest beacon is at x=3264675, y=3635510 +Sensor at x=3481951, y=2453964: closest beacon is at x=3448416, y=2478759 +Sensor at x=1120077, y=2963237: closest beacon is at x=1188862, y=2000000 +Sensor at x=2901181, y=3029961: closest beacon is at x=2872059, y=3592616 +Sensor at x=3111105, y=3361570: closest beacon is at x=2964244, y=3612685 +Sensor at x=2533601, y=3956413: closest beacon is at x=2872059, y=3592616 +Sensor at x=108898, y=2275290: closest beacon is at x=1188862, y=2000000 +Sensor at x=3501591, y=2414995: closest beacon is at x=3448416, y=2478759 +Sensor at x=3035657, y=3700769: closest beacon is at x=2964244, y=3612685 +Sensor at x=1286795, y=298997: closest beacon is at x=308571, y=-434280 +Sensor at x=200812, y=3470019: closest beacon is at x=-435606, y=3506717 +Sensor at x=2550124, y=1556776: closest beacon is at x=1188862, y=2000000 +Sensor at x=3955070, y=601908: closest beacon is at x=4076658, y=2478123 +Sensor at x=3565419, y=2355172: closest beacon is at x=3448416, y=2478759
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/15/test_input.txt Sat Dec 17 12:33:38 2022 +0100 @@ -0,0 +1,14 @@ +Sensor at x=2, y=18: closest beacon is at x=-2, y=15 +Sensor at x=9, y=16: closest beacon is at x=10, y=16 +Sensor at x=13, y=2: closest beacon is at x=15, y=3 +Sensor at x=12, y=14: closest beacon is at x=10, y=16 +Sensor at x=10, y=20: closest beacon is at x=10, y=16 +Sensor at x=14, y=17: closest beacon is at x=10, y=16 +Sensor at x=8, y=7: closest beacon is at x=2, y=10 +Sensor at x=2, y=0: closest beacon is at x=2, y=10 +Sensor at x=0, y=11: closest beacon is at x=2, y=10 +Sensor at x=20, y=14: closest beacon is at x=25, y=17 +Sensor at x=17, y=20: closest beacon is at x=21, y=22 +Sensor at x=16, y=7: closest beacon is at x=15, y=3 +Sensor at x=14, y=3: closest beacon is at x=15, y=3 +Sensor at x=20, y=1: closest beacon is at x=15, y=3