summary refs log tree commit diff
path: root/22.15
diff options
context:
space:
mode:
authordzwdz2022-12-15 13:15:48 +0100
committerdzwdz2022-12-15 13:15:48 +0100
commitcc806faf8b45058ca50f497bfa670a9dec683fcb (patch)
tree684ca0033834c932869ae73a265c681787efc755 /22.15
parent37bd4d9050b8268ba12f7e0ee6cd3a262eba1c00 (diff)
day 15 part 2: shitty bruteforce version HEAD asdf
ugh. also, gives the wrong answer. it's enough to recover the correct one, though.
Diffstat (limited to '22.15')
-rw-r--r--22.15/main.py48
1 files changed, 42 insertions, 6 deletions
diff --git a/22.15/main.py b/22.15/main.py
index c366999..9b21981 100644
--- a/22.15/main.py
+++ b/22.15/main.py
@@ -1,4 +1,4 @@
-import sys, re
+import sys, re, itertools
 # ranges are inclusive from both ends
 
 def range_intersect(a, b):
@@ -24,18 +24,54 @@ class RangeSet:
 
 data = []
 for line in sys.stdin:
-	data.append([int(n) for n in re.findall('\d+', line)])
+	data.append([int(n) for n in re.findall('-?\d+', line)])
 
-manhattan = [abs(a[0] - a[2]) + abs(a[1] - a[3]) for a in data]
+def manhattan(a):
+	return abs(a[0] - a[2]) + abs(a[1] - a[3])
 
 def partOne(y):
 	r = RangeSet()
-	for l, m in zip(data, manhattan):
+	for l in data:
+		m = manhattan(l)
 		w = m - abs(l[1] - y)
 		if w < 0: continue
-		#print(l, w, l[0] - w, l[0] + w)
 		r.add(l[0] - w, l[0] + w)
 	beacons = set([(a[2], a[3]) for a in data if a[3] == y])
 	return r.size() - len(beacons)
 
-print(partOne(2000000))
+def vis(size):
+	s = ''
+	for y in range(size):
+		for x in range(size):
+			c = ' '
+			for l in data:
+				m = abs(l[0] - l[2]) + abs(l[1] - l[3])
+				if abs(l[0] - x) + abs(l[1] - y) <= m:
+					c = '#'
+					break
+			if (x, y) in [(a[0], a[1]) for a in data]:
+				c = 'S'
+			if (x, y) in [(a[2], a[3]) for a in data]:
+				c = 'B'
+			s += c
+		s += '\n'
+	return s
+
+def partTwo():
+	for y in range(4000000):
+		if y % 4000 == 0: print(y / 40000)
+		r = RangeSet()
+		for l in data:
+			m = manhattan(l)
+			w = m - abs(l[1] - y)
+			if w < 0: continue
+			r.add(l[0] - w, l[0] + w)
+		l = len(r.rs)
+		if l == 0: break
+		if l != 1:
+			print(r.rs, y)
+			x = r.rs[0][1] + 1
+			return 4000000 * x + y
+
+#print(partOne(2000000))
+print(partTwo())