Skip to content

Commit 6a545d1

Browse files
committed
2018 Day 6
1 parent 1ad28d6 commit 6a545d1

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

2018/6.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
194, 200
2+
299, 244
3+
269, 329
4+
292, 55
5+
211, 63
6+
123, 311
7+
212, 90
8+
292, 169
9+
359, 177
10+
354, 95
11+
101, 47
12+
95, 79
13+
95, 287
14+
294, 126
15+
81, 267
16+
330, 78
17+
202, 165
18+
225, 178
19+
266, 272
20+
351, 326
21+
180, 62
22+
102, 178
23+
151, 101
24+
343, 145
25+
205, 312
26+
74, 193
27+
221, 56
28+
89, 89
29+
242, 172
30+
59, 138
31+
83, 179
32+
223, 88
33+
297, 234
34+
147, 351
35+
226, 320
36+
358, 338
37+
321, 172
38+
54, 122
39+
263, 165
40+
126, 341
41+
64, 132
42+
264, 306
43+
72, 202
44+
98, 49
45+
238, 67
46+
310, 303
47+
277, 281
48+
222, 318
49+
357, 169
50+
123, 225

2018/6a.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from collections import defaultdict
2+
3+
4+
def mhd(p1, p2, q1, q2):
5+
return abs(p1 - q1) + abs(p2 - q2)
6+
7+
8+
with open("6.txt") as f:
9+
l = f.readlines()
10+
l = map(lambda x: x.rstrip(), l)
11+
points = []
12+
code = {}
13+
for i, s in enumerate(l):
14+
splited = s.split(", ")
15+
point = (int(splited[0]), int(splited[1]))
16+
code[point] = i
17+
points.append(point)
18+
min_x = min(map(lambda x: x[0], points))
19+
min_y = min(map(lambda x: x[1], points))
20+
max_x = max(map(lambda x: x[0], points))
21+
max_y = max(map(lambda x: x[1], points))
22+
23+
# {point: area}
24+
area = defaultdict(int)
25+
26+
# {coordinate tuple: point or None}
27+
board = {}
28+
29+
# helper set
30+
points_set = set(points)
31+
32+
# add points to the board
33+
for p in points:
34+
board[p] = p
35+
36+
for x in range(min_x, max_x + 1):
37+
for y in range(min_y, max_y + 1):
38+
if (x, y) in points_set:
39+
continue
40+
min_dist = 999999
41+
min_point = None
42+
double_minimum = False
43+
for p in points:
44+
dist = mhd(x, y, *p)
45+
if dist == min_dist:
46+
double_minimum = True
47+
elif dist < min_dist:
48+
double_minimum = False
49+
min_dist = dist
50+
min_point = p
51+
else:
52+
if double_minimum:
53+
board[(x, y)] = None
54+
else:
55+
board[(x, y)] = min_point
56+
area[min_point] += 1
57+
58+
# Check edges for infinite
59+
infinity_set = set()
60+
for x in range(min_x, max_x + 1):
61+
infinity_set.add(board[(x, min_y)])
62+
infinity_set.add(board[(x, max_y)])
63+
for y in range(min_y, max_y + 1):
64+
infinity_set.add(board[(min_x, y)])
65+
infinity_set.add(board[(max_x, y)])
66+
67+
areas = []
68+
for point, area in area.items():
69+
if point in infinity_set:
70+
continue
71+
areas.append(area)
72+
73+
# Treba pripocitat este jednotku, lebo neberiem do uvahy samotny bod, od
74+
# ktoreho pocitam vzdialenost
75+
#
76+
# Add one because the actual point from which the distance is computed was
77+
# not included
78+
print(max(areas) + 1)
79+
80+
# print()
81+
# for x in range(min_x, max_x + 1):
82+
# for y in range(min_y, max_y + 1):
83+
# point = board[(x, y)]
84+
# if point:
85+
# print(code[point], end='')
86+
# else:
87+
# print('.', end='')
88+
# print()

2018/6b.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from collections import defaultdict
2+
3+
4+
def mhd(p1, p2, q1, q2):
5+
return abs(p1 - q1) + abs(p2 - q2)
6+
7+
8+
with open("6.txt") as f:
9+
l = f.readlines()
10+
l = map(lambda x: x.rstrip(), l)
11+
points = []
12+
code = {}
13+
for i, s in enumerate(l):
14+
splited = s.split(", ")
15+
point = (int(splited[0]), int(splited[1]))
16+
code[point] = i
17+
points.append(point)
18+
min_x = min(map(lambda x: x[0], points))
19+
min_y = min(map(lambda x: x[1], points))
20+
max_x = max(map(lambda x: x[0], points))
21+
max_y = max(map(lambda x: x[1], points))
22+
23+
# helper set
24+
points_set = set(points)
25+
26+
result = 0
27+
28+
for x in range(min_x - 100, max_x + 100):
29+
for y in range(min_y - 100, max_y + 100):
30+
dist_sum = sum((mhd(x, y, *p) for p in points))
31+
if dist_sum < 10000:
32+
result += 1
33+
34+
print(result)

0 commit comments

Comments
 (0)