diff options
| author | nekineki <nekineki@nekineki.net> | 2022-12-15 08:34:46 +0100 |
|---|---|---|
| committer | nekineki <nekineki@nekineki.net> | 2022-12-15 08:34:46 +0100 |
| commit | 033bd6e17f84e73aaf1d5db16726a6bde76df146 (patch) | |
| tree | 436fba21f7bbb45dd604afae671ee1a1edf7b37d /2022 | |
| parent | e7f9dc040480cb459098c0c97baf38b92466b806 (diff) | |
day15
Diffstat (limited to '2022')
| -rwxr-xr-x | 2022/day15.py | 86 | ||||
| -rw-r--r-- | 2022/in/day15.pzl | 28 | ||||
| -rw-r--r-- | 2022/in/day15.ref | 14 |
3 files changed, 128 insertions, 0 deletions
diff --git a/2022/day15.py b/2022/day15.py new file mode 100755 index 0000000..cea0f0b --- /dev/null +++ b/2022/day15.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 + +# import numpy as np +from functools import reduce +from re import findall +from copy import deepcopy +import sys + +# filename = "in/day15.ref" +filename = "in/day15.pzl" +data = open(filename).read() +lines = [line for line in data.rstrip('\n').split('\n')] + +max_x = 0 +min_x = 99999999 + +B = dict() +D = list() +for line in lines: + w = line.split(' ') + a = list() + for i in [2,3,8,9]: + a.append(int(w[i].split('=')[-1].rstrip(',').rstrip(':'))) + sx = a[0] + sy = a[1] + bx = a[2] + by = a[3] + B[(by,bx)] = 1 + dx = bx - sx + dy = by - sy + r = abs(dx) + abs(dy) + 1 + D.append((sy,sx,dy,dx, r)) + max_x = max(max_x, sx, bx) + min_x = min(min_x, sx, bx) + +# print(D) + +def too_close(y, x): + if (y,x) in B.keys(): + return False + + is_to_close = list() + for sy,sx,dy,dx,r in D: + if abs(y-sy) + abs(x-sx) <= abs(dy) + abs(dx): + return True + return False + +# print('max_x', max_x) +# print('min_x', min_x) + +def part1(y, tol): + num = 0 + for x in range(min_x-tol, max_x+tol): + ret = too_close(y,x) + # if x % 10000 == 0: + # print(x, ret) + if ret == True: + num += 1 + return num + +def part2(therange): + counter = 0 + for (sy,sx,dy,dx,r) in D[::-1]: + # print('range', r) + for i in range(r): + counter += 1 + # if counter % 100000 == 0: + # print('count', counter) + for y,x in [(sy-i, sx+r-i), (sy-r+i, sx-i), (sy+i, sx-r+i), (sy+r-i, sx+i)]: + if 0 <= x <= therange and 0 <= y <= therange: + ret = too_close(y,x) + if ret == False: + if (y,x) not in B.keys(): + # print(y,x, ret, x*4000000+y, sy, sx) + return x*4000000+y + break + +# res1 = part1(y=10, tol=20) +# res2 = part2(therange=20) + +res1 = part1(y=2000000, tol=2000000) +res2 = part2(therange=4000000) + +print('res1:', res1) +print('res2:', res2) + diff --git a/2022/in/day15.pzl b/2022/in/day15.pzl new file mode 100644 index 0000000..315b0f4 --- /dev/null +++ b/2022/in/day15.pzl @@ -0,0 +1,28 @@ +Sensor at x=655450, y=2013424: closest beacon is at x=967194, y=2000000 +Sensor at x=1999258, y=1017714: closest beacon is at x=3332075, y=572515 +Sensor at x=2159800, y=3490958: closest beacon is at x=2145977, y=3551728 +Sensor at x=3990472, y=1891598: closest beacon is at x=3022851, y=2629972 +Sensor at x=188608, y=354698: closest beacon is at x=-1037755, y=-391680 +Sensor at x=286630, y=3999086: closest beacon is at x=-1202308, y=3569538 +Sensor at x=2022540, y=3401295: closest beacon is at x=2013531, y=3335868 +Sensor at x=65063, y=2648597: closest beacon is at x=967194, y=2000000 +Sensor at x=2533266, y=439414: closest beacon is at x=3332075, y=572515 +Sensor at x=1728594, y=2416005: closest beacon is at x=967194, y=2000000 +Sensor at x=1156357, y=1867331: closest beacon is at x=967194, y=2000000 +Sensor at x=825519, y=3323952: closest beacon is at x=2013531, y=3335868 +Sensor at x=3278267, y=201451: closest beacon is at x=3332075, y=572515 +Sensor at x=3679732, y=1213595: closest beacon is at x=3332075, y=572515 +Sensor at x=896808, y=1637672: closest beacon is at x=967194, y=2000000 +Sensor at x=2035362, y=3363480: closest beacon is at x=2013531, y=3335868 +Sensor at x=2056169, y=3442413: closest beacon is at x=2013531, y=3335868 +Sensor at x=2631999, y=1884495: closest beacon is at x=3022851, y=2629972 +Sensor at x=3149604, y=3870003: closest beacon is at x=3707835, y=4152776 +Sensor at x=3579002, y=1702: closest beacon is at x=3332075, y=572515 +Sensor at x=2306088, y=2605428: closest beacon is at x=3022851, y=2629972 +Sensor at x=2428132, y=3171598: closest beacon is at x=2013531, y=3335868 +Sensor at x=1447212, y=3938104: closest beacon is at x=2145977, y=3551728 +Sensor at x=3131240, y=3166665: closest beacon is at x=3022851, y=2629972 +Sensor at x=3865496, y=2980765: closest beacon is at x=3022851, y=2629972 +Sensor at x=2508598, y=3611761: closest beacon is at x=2145977, y=3551728 +Sensor at x=2144092, y=3514660: closest beacon is at x=2145977, y=3551728 +Sensor at x=3947251, y=469499: closest beacon is at x=3332075, y=572515 diff --git a/2022/in/day15.ref b/2022/in/day15.ref new file mode 100644 index 0000000..a612424 --- /dev/null +++ b/2022/in/day15.ref @@ -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 |
