Compare commits
No commits in common. "0dcd6750425e37d58a52ac62fb0489e4c52d7e37" and "c200e211c9f6d47e944d7ae9f3ef76ab7f44a805" have entirely different histories.
0dcd675042
...
c200e211c9
496
day09/input
496
day09/input
@ -1,496 +0,0 @@
|
||||
98292,50082
|
||||
98292,51299
|
||||
98041,51299
|
||||
98041,52512
|
||||
97933,52512
|
||||
97933,53729
|
||||
97891,53729
|
||||
97891,54958
|
||||
97948,54958
|
||||
97948,56123
|
||||
97437,56123
|
||||
97437,57349
|
||||
97434,57349
|
||||
97434,58625
|
||||
97658,58625
|
||||
97658,59815
|
||||
97354,59815
|
||||
97354,61007
|
||||
97068,61007
|
||||
97068,62109
|
||||
96444,62109
|
||||
96444,63237
|
||||
95967,63237
|
||||
95967,64422
|
||||
95696,64422
|
||||
95696,65722
|
||||
95742,65722
|
||||
95742,66911
|
||||
95424,66911
|
||||
95424,67730
|
||||
94169,67730
|
||||
94169,69128
|
||||
94366,69128
|
||||
94366,70025
|
||||
93389,70025
|
||||
93389,71338
|
||||
93314,71338
|
||||
93314,72103
|
||||
92139,72103
|
||||
92139,73287
|
||||
91787,73287
|
||||
91787,74514
|
||||
91481,74514
|
||||
91481,75422
|
||||
90632,75422
|
||||
90632,76704
|
||||
90368,76704
|
||||
90368,77568
|
||||
89464,77568
|
||||
89464,78483
|
||||
88650,78483
|
||||
88650,79236
|
||||
87637,79236
|
||||
87637,80655
|
||||
87465,80655
|
||||
87465,81115
|
||||
86119,81115
|
||||
86119,82571
|
||||
85928,82571
|
||||
85928,83239
|
||||
84848,83239
|
||||
84848,84347
|
||||
84231,84347
|
||||
84231,84722
|
||||
82893,84722
|
||||
82893,85694
|
||||
82138,85694
|
||||
82138,86215
|
||||
80982,86215
|
||||
80982,87023
|
||||
80083,87023
|
||||
80083,87874
|
||||
79213,87874
|
||||
79213,88861
|
||||
78434,88861
|
||||
78434,89490
|
||||
77386,89490
|
||||
77386,90035
|
||||
76288,90035
|
||||
76288,90659
|
||||
75247,90659
|
||||
75247,91834
|
||||
74530,91834
|
||||
74530,91663
|
||||
73032,91663
|
||||
73032,92769
|
||||
72247,92769
|
||||
72247,93129
|
||||
71065,93129
|
||||
71065,93719
|
||||
69997,93719
|
||||
69997,94601
|
||||
69050,94601
|
||||
69050,94483
|
||||
67680,94483
|
||||
67680,94626
|
||||
66441,94626
|
||||
66441,95786
|
||||
65563,95786
|
||||
65563,95363
|
||||
64148,95363
|
||||
64148,96153
|
||||
63120,96153
|
||||
63120,96752
|
||||
62020,96752
|
||||
62020,96319
|
||||
60665,96319
|
||||
60665,96704
|
||||
59514,96704
|
||||
59514,96954
|
||||
58333,96954
|
||||
58333,97717
|
||||
57226,97717
|
||||
57226,97419
|
||||
55957,97419
|
||||
55957,97683
|
||||
54766,97683
|
||||
54766,97392
|
||||
53527,97392
|
||||
53527,97506
|
||||
52328,97506
|
||||
52328,97750
|
||||
51128,97750
|
||||
51128,97884
|
||||
49918,97884
|
||||
49918,98455
|
||||
48689,98455
|
||||
48689,97950
|
||||
47486,97950
|
||||
47486,97626
|
||||
46291,97626
|
||||
46291,97900
|
||||
45046,97900
|
||||
45046,97981
|
||||
43806,97981
|
||||
43806,97693
|
||||
42610,97693
|
||||
42610,97475
|
||||
41407,97475
|
||||
41407,96948
|
||||
40268,96948
|
||||
40268,96341
|
||||
39162,96341
|
||||
39162,95987
|
||||
38009,95987
|
||||
38009,96196
|
||||
36696,96196
|
||||
36696,95887
|
||||
35516,95887
|
||||
35516,94958
|
||||
34547,94958
|
||||
34547,95161
|
||||
33186,95161
|
||||
33186,94474
|
||||
32146,94474
|
||||
32146,93686
|
||||
31164,93686
|
||||
31164,93591
|
||||
29880,93591
|
||||
29880,93365
|
||||
28636,93365
|
||||
28636,92868
|
||||
27514,92868
|
||||
27514,91996
|
||||
26595,91996
|
||||
26595,91248
|
||||
25623,91248
|
||||
25623,90405
|
||||
24719,90405
|
||||
24719,89965
|
||||
23562,89965
|
||||
23562,89216
|
||||
22605,89216
|
||||
22605,88794
|
||||
21410,88794
|
||||
21410,87854
|
||||
20595,87854
|
||||
20595,87135
|
||||
19614,87135
|
||||
19614,86044
|
||||
18949,86044
|
||||
18949,85497
|
||||
17819,85497
|
||||
17819,84807
|
||||
16800,84807
|
||||
16800,83553
|
||||
16331,83553
|
||||
16331,83087
|
||||
15072,83087
|
||||
15072,81979
|
||||
14481,81979
|
||||
14481,81230
|
||||
13494,81230
|
||||
13494,80364
|
||||
12631,80364
|
||||
12631,79438
|
||||
11834,79438
|
||||
11834,78320
|
||||
11294,78320
|
||||
11294,77350
|
||||
10561,77350
|
||||
10561,76275
|
||||
9983,76275
|
||||
9983,75427
|
||||
9049,75427
|
||||
9049,74517
|
||||
8187,74517
|
||||
8187,73207
|
||||
8020,73207
|
||||
8020,71987
|
||||
7731,71987
|
||||
7731,71263
|
||||
6464,71263
|
||||
6464,70095
|
||||
6066,70095
|
||||
6066,68916
|
||||
5711,68916
|
||||
5711,67727
|
||||
5399,67727
|
||||
5399,66691
|
||||
4694,66691
|
||||
4694,65298
|
||||
4992,65298
|
||||
4992,64223
|
||||
4395,64223
|
||||
4395,63008
|
||||
4243,63008
|
||||
4243,61887
|
||||
3762,61887
|
||||
3762,60810
|
||||
3049,60810
|
||||
3049,59627
|
||||
2745,59627
|
||||
2745,58451
|
||||
2377,58451
|
||||
2377,57232
|
||||
2245,57232
|
||||
2245,55998
|
||||
2255,55998
|
||||
2255,54788
|
||||
2103,54788
|
||||
2103,53599
|
||||
1645,53599
|
||||
1645,52353
|
||||
1980,52353
|
||||
1980,51122
|
||||
2484,51122
|
||||
2484,50076
|
||||
94607,50076
|
||||
94607,48713
|
||||
2419,48713
|
||||
2419,47502
|
||||
2367,47502
|
||||
2367,46305
|
||||
2556,46305
|
||||
2556,45069
|
||||
2319,45069
|
||||
2319,43868
|
||||
2503,43868
|
||||
2503,42604
|
||||
2271,42604
|
||||
2271,41378
|
||||
2366,41378
|
||||
2366,40243
|
||||
2932,40243
|
||||
2932,39158
|
||||
3637,39158
|
||||
3637,37993
|
||||
3952,37993
|
||||
3952,36700
|
||||
3817,36700
|
||||
3817,35502
|
||||
4067,35502
|
||||
4067,34405
|
||||
4628,34405
|
||||
4628,33233
|
||||
4965,33233
|
||||
4965,32219
|
||||
5706,32219
|
||||
5706,30966
|
||||
5853,30966
|
||||
5853,30059
|
||||
6796,30059
|
||||
6796,28764
|
||||
6895,28764
|
||||
6895,27555
|
||||
7210,27555
|
||||
7210,26525
|
||||
7877,26525
|
||||
7877,25807
|
||||
9062,25807
|
||||
9062,24416
|
||||
9111,24416
|
||||
9111,23301
|
||||
9639,23301
|
||||
9639,22495
|
||||
10627,22495
|
||||
10627,21248
|
||||
10985,21248
|
||||
10985,20458
|
||||
11969,20458
|
||||
11969,19850
|
||||
13153,19850
|
||||
13153,18527
|
||||
13465,18527
|
||||
13465,17780
|
||||
14460,17780
|
||||
14460,16736
|
||||
15125,16736
|
||||
15125,16113
|
||||
16229,16113
|
||||
16229,15375
|
||||
17198,15375
|
||||
17198,14067
|
||||
17646,14067
|
||||
17646,13204
|
||||
18520,13204
|
||||
18520,12766
|
||||
19745,12766
|
||||
19745,11706
|
||||
20463,11706
|
||||
20463,11476
|
||||
21812,11476
|
||||
21812,10306
|
||||
22472,10306
|
||||
22472,9799
|
||||
23603,9799
|
||||
23603,9481
|
||||
24840,9481
|
||||
24840,8594
|
||||
25721,8594
|
||||
25721,8321
|
||||
26959,8321
|
||||
26959,7306
|
||||
27791,7306
|
||||
27791,6632
|
||||
28818,6632
|
||||
28818,6289
|
||||
30006,6289
|
||||
30006,5752
|
||||
31101,5752
|
||||
31101,5616
|
||||
32358,5616
|
||||
32358,4838
|
||||
33361,4838
|
||||
33361,4326
|
||||
34475,4326
|
||||
34475,4414
|
||||
35782,4414
|
||||
35782,3495
|
||||
36779,3495
|
||||
36779,3373
|
||||
38012,3373
|
||||
38012,2815
|
||||
39135,2815
|
||||
39135,3440
|
||||
40514,3440
|
||||
40514,2709
|
||||
41607,2709
|
||||
41607,2818
|
||||
42854,2818
|
||||
42854,1922
|
||||
43960,1922
|
||||
43960,1884
|
||||
45189,1884
|
||||
45189,2138
|
||||
46437,2138
|
||||
46437,2264
|
||||
47660,2264
|
||||
47660,1956
|
||||
48864,1956
|
||||
48864,1579
|
||||
50082,1579
|
||||
50082,1956
|
||||
51299,1956
|
||||
51299,1669
|
||||
52533,1669
|
||||
52533,1656
|
||||
53764,1656
|
||||
53764,2349
|
||||
54927,2349
|
||||
54927,2193
|
||||
56171,2193
|
||||
56171,3042
|
||||
57275,3042
|
||||
57275,2822
|
||||
58538,2822
|
||||
58538,2858
|
||||
59771,2858
|
||||
59771,3154
|
||||
60954,3154
|
||||
60954,3953
|
||||
62006,3953
|
||||
62006,4034
|
||||
63237,4034
|
||||
63237,4422
|
||||
64385,4422
|
||||
64385,4471
|
||||
65649,4471
|
||||
65649,5053
|
||||
66733,5053
|
||||
66733,5422
|
||||
67894,5422
|
||||
67894,5800
|
||||
69056,5800
|
||||
69056,6313
|
||||
70162,6313
|
||||
70162,6949
|
||||
71208,6949
|
||||
71208,7281
|
||||
72406,7281
|
||||
72406,8118
|
||||
73339,8118
|
||||
73339,8878
|
||||
74301,8878
|
||||
74301,9110
|
||||
75583,9110
|
||||
75583,9760
|
||||
76618,9760
|
||||
76618,10542
|
||||
77562,10542
|
||||
77562,11459
|
||||
78402,11459
|
||||
78402,11839
|
||||
79642,11839
|
||||
79642,12591
|
||||
80609,12591
|
||||
80609,13867
|
||||
81126,13867
|
||||
81126,14279
|
||||
82382,14279
|
||||
82382,15350
|
||||
83049,15350
|
||||
83049,15940
|
||||
84176,15940
|
||||
84176,16870
|
||||
84971,16870
|
||||
84971,17882
|
||||
85671,17882
|
||||
85671,18636
|
||||
86660,18636
|
||||
86660,19524
|
||||
87505,19524
|
||||
87505,20842
|
||||
87801,20842
|
||||
87801,21454
|
||||
89013,21454
|
||||
89013,22634
|
||||
89459,22634
|
||||
89459,23916
|
||||
89724,23916
|
||||
89724,24675
|
||||
90783,24675
|
||||
90783,25871
|
||||
91149,25871
|
||||
91149,26864
|
||||
91850,26864
|
||||
91850,27747
|
||||
92777,27747
|
||||
92777,29088
|
||||
92815,29088
|
||||
92815,29845
|
||||
94062,29845
|
||||
94062,31020
|
||||
94436,31020
|
||||
94436,32266
|
||||
94616,32266
|
||||
94616,33501
|
||||
94782,33501
|
||||
94782,34499
|
||||
95601,34499
|
||||
95601,35767
|
||||
95634,35767
|
||||
95634,36965
|
||||
95849,36965
|
||||
95849,38021
|
||||
96591,38021
|
||||
96591,39160
|
||||
97075,39160
|
||||
97075,40461
|
||||
96820,40461
|
||||
96820,41594
|
||||
97362,41594
|
||||
97362,42761
|
||||
97798,42761
|
||||
97798,44061
|
||||
97274,44061
|
||||
97274,45271
|
||||
97302,45271
|
||||
97302,46441
|
||||
97806,46441
|
||||
97806,47663
|
||||
97683,47663
|
||||
97683,48874
|
||||
97612,48874
|
||||
97612,50082
|
||||
@ -1,8 +0,0 @@
|
||||
7,1
|
||||
11,1
|
||||
11,7
|
||||
9,7
|
||||
9,5
|
||||
2,5
|
||||
2,3
|
||||
7,3
|
||||
99
day09/sol.py
99
day09/sol.py
@ -1,99 +0,0 @@
|
||||
from math import dist
|
||||
|
||||
def solve(input):
|
||||
points = []
|
||||
for line in open(input):
|
||||
x,y = map(int, line.strip().split(","))
|
||||
points.append((x,y))
|
||||
|
||||
# Part 1:
|
||||
# find the largest rectangle created by two points
|
||||
|
||||
def areas():
|
||||
for p in points:
|
||||
for q in points:
|
||||
if p != q:
|
||||
yield area(p,q)
|
||||
|
||||
# answer 1
|
||||
print(max(areas()))
|
||||
|
||||
# Part 2:
|
||||
# find the largest rectangle formed by a pair of points
|
||||
# which is contained within the polygon defined by the list of points.
|
||||
|
||||
lines = list(zip(points, points[1:]+[points[0]]))
|
||||
# keep only vertical lines, and sort so the uppermost point (lowest y coord) is the first of the pair
|
||||
lines = [(min(p,q),max(p,q)) for p,q in lines if p[1] != q[1]]
|
||||
# sort lines by y coord
|
||||
lines.sort(key=lambda l: (l[0][1],l[1][1],l[0][0],l[1][0]))
|
||||
|
||||
#print(lines)
|
||||
|
||||
# we want to know if the rectangle formed by a pair of points
|
||||
# is completely contained within an axis-aligned polygon.
|
||||
#
|
||||
# we can do that with a pseudo-scanline algorithm.
|
||||
#
|
||||
# we make a couple simplifying assumptions:
|
||||
# 1. first, that the polygon is not self-intersecting
|
||||
# 2. second, that the polygon is not "U shaped" --
|
||||
# that is, it has a definite, single width in every horizontal slice
|
||||
# and it doesn't curve around at all -- so we don't have to worry
|
||||
# about voids
|
||||
#
|
||||
# for each y position, we first calculate the horizontal bounds of the
|
||||
# polygon at that line.
|
||||
#
|
||||
# then, to test if a rectangle is contained within the polygon,
|
||||
# we just need to iterate through the y positions which fall inside the rectangle
|
||||
# and check whether the rectangle would exceed the previously computed bounds
|
||||
# of the polygon at any of those points.
|
||||
|
||||
bounds = {}
|
||||
ys = sorted(set(p[1] for l in lines for p in l))
|
||||
for y in ys:
|
||||
# select lines which intersect with the scanline at y
|
||||
mylines = []
|
||||
for p,q in lines:
|
||||
if p[1] <= y <= q[1]:
|
||||
mylines.append(p[0]) # only keep the x coord
|
||||
# since we are assuming no self-intersection and no voids,
|
||||
# the bounds on this line are just the leftmost and rightmost
|
||||
# x coordinate of the intersecting lines
|
||||
bounds[y] = (min(mylines),max(mylines))
|
||||
|
||||
#print(ys)
|
||||
#print(bounds)
|
||||
|
||||
def inbounds(p,q):
|
||||
y0 = min(p[1],q[1])
|
||||
y1 = max(p[1],q[1])
|
||||
x0 = min(p[0],q[0])
|
||||
x1 = max(p[0],q[0])
|
||||
#print(y0, y1, ys)
|
||||
start = ys.index(y0)
|
||||
for i in range(start,len(ys)):
|
||||
y = ys[i]
|
||||
if y > y1:
|
||||
break
|
||||
if not bounds[y][0] <= x0 <= x1 <= bounds[y][1]:
|
||||
return False
|
||||
return True
|
||||
|
||||
def inbound_areas():
|
||||
for p in points:
|
||||
for q in points:
|
||||
if p != q and inbounds(p,q):
|
||||
yield area(p,q)
|
||||
|
||||
# answer 2
|
||||
print(max(inbound_areas()))
|
||||
|
||||
def area(p,q):
|
||||
dx = abs(p[0] - q[0]) + 1
|
||||
dy = abs(p[1] - q[1]) + 1
|
||||
return dx*dy
|
||||
|
||||
solve("sample")
|
||||
solve("input")
|
||||
Loading…
x
Reference in New Issue
Block a user