From 50f8acaa66eb75a869a40da756717b634e80114d Mon Sep 17 00:00:00 2001 From: Sebastian Seedorf Date: Thu, 15 Dec 2022 12:57:19 +0100 Subject: [PATCH] Day 15 --- day15/input.txt | 22 ++++++++++++++++++++++ day15/part1.py | 29 +++++++++++++++++++++++++++++ day15/part2.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 day15/input.txt create mode 100644 day15/part1.py create mode 100644 day15/part2.py diff --git a/day15/input.txt b/day15/input.txt new file mode 100644 index 0000000..49e9e5e --- /dev/null +++ b/day15/input.txt @@ -0,0 +1,22 @@ +Sensor at x=1384790, y=3850432: closest beacon is at x=2674241, y=4192888 +Sensor at x=2825953, y=288046: closest beacon is at x=2154954, y=-342775 +Sensor at x=3553843, y=2822363: closest beacon is at x=3444765, y=2347460 +Sensor at x=2495377, y=3130491: closest beacon is at x=2761496, y=2831113 +Sensor at x=1329263, y=1778185: closest beacon is at x=2729595, y=2000000 +Sensor at x=2882039, y=2206085: closest beacon is at x=2729595, y=2000000 +Sensor at x=3903141, y=2510440: closest beacon is at x=4006219, y=3011198 +Sensor at x=3403454, y=3996578: closest beacon is at x=3754119, y=4475047 +Sensor at x=3630476, y=1048796: closest beacon is at x=3444765, y=2347460 +Sensor at x=16252, y=2089672: closest beacon is at x=-276514, y=2995794 +Sensor at x=428672, y=1150723: closest beacon is at x=-281319, y=668868 +Sensor at x=2939101, y=3624676: closest beacon is at x=2674241, y=4192888 +Sensor at x=3166958, y=2890076: closest beacon is at x=2761496, y=2831113 +Sensor at x=3758241, y=3546895: closest beacon is at x=4006219, y=3011198 +Sensor at x=218942, y=3011070: closest beacon is at x=-276514, y=2995794 +Sensor at x=52656, y=3484635: closest beacon is at x=-276514, y=2995794 +Sensor at x=2057106, y=405314: closest beacon is at x=2154954, y=-342775 +Sensor at x=1966905, y=2495701: closest beacon is at x=2761496, y=2831113 +Sensor at x=511976, y=2696731: closest beacon is at x=-276514, y=2995794 +Sensor at x=3094465, y=2478570: closest beacon is at x=3444765, y=2347460 +Sensor at x=806671, y=228252: closest beacon is at x=-281319, y=668868 +Sensor at x=3011731, y=1976307: closest beacon is at x=2729595, y=2000000 diff --git a/day15/part1.py b/day15/part1.py new file mode 100644 index 0000000..b89ec82 --- /dev/null +++ b/day15/part1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +import re + +lines = (x.strip() for x in open("input.txt")) +target = 2000000 +intervals = [] +known_targets = set() + +for line in lines: + x, y, a, b = map(int, re.search(r"(-?\d+).+?(-?\d+).+?(-?\d+).+?(-?\d+)", line).groups()) + dist = abs(x-a) + abs(y-b) + width = dist-abs(y-target) + if width >= 0: + intervals.append((x-width, x+width)) + if b == target: + known_targets.add(a) + if y == target: + known_targets.add(x) + + +intervals.sort(key=lambda x: x[0]) +merged = [intervals[0]] +for i in intervals[1:]: + if merged[-1][1] >= i[0]: + merged[-1] = (merged[-1][0], max(merged[-1][1], i[1])) + else: + merged.append(i) + +print(sum(b-a+1 for a, b in merged)-len(known_targets)) diff --git a/day15/part2.py b/day15/part2.py new file mode 100644 index 0000000..e478d14 --- /dev/null +++ b/day15/part2.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +import re + +lines = (x.strip() for x in open("input.txt")) +lines = [tuple(map(int, re.search(r"(-?\d+).+?(-?\d+).+?(-?\d+).+?(-?\d+)", line).groups())) for line in lines] + +for target in range(0, 4000000): #3186981 + if target % 40000 == 0: + print(target/4000000) + intervals = [] + known_targets = set() + + for line in lines: + x, y, a, b = line + dist = abs(x-a) + abs(y-b) + width = dist-abs(y-target) + if width >= 0: + intervals.append((x-width, x+width)) + if b == target: + known_targets.add(a) + if y == target: + known_targets.add(x) + + + intervals.sort(key=lambda x: x[0]) + merged = [intervals[0]] + for i in intervals[1:]: + if merged[-1][1] >= i[0]: + merged[-1] = (merged[-1][0], max(merged[-1][1], i[1])) + else: + merged.append(i) + + if len(merged) > 1: + print(target, merged, (merged[0][1]+1)*4000000+target) + break