Part 2
This commit is contained in:
parent
d80a8ce32f
commit
327aaf4fd7
2 changed files with 570 additions and 37 deletions
504
input/day09
504
input/day09
|
|
@ -1,8 +1,496 @@
|
||||||
7,1
|
98107,50192
|
||||||
11,1
|
98107,51403
|
||||||
11,7
|
97816,51403
|
||||||
9,7
|
97816,52648
|
||||||
9,5
|
98404,52648
|
||||||
2,5
|
98404,53825
|
||||||
2,3
|
97715,53825
|
||||||
7,3
|
97715,55038
|
||||||
|
97649,55038
|
||||||
|
97649,56313
|
||||||
|
98041,56313
|
||||||
|
98041,57420
|
||||||
|
97175,57420
|
||||||
|
97175,58579
|
||||||
|
96790,58579
|
||||||
|
96790,59854
|
||||||
|
97000,59854
|
||||||
|
97000,60991
|
||||||
|
96518,60991
|
||||||
|
96518,62292
|
||||||
|
96707,62292
|
||||||
|
96707,63299
|
||||||
|
95787,63299
|
||||||
|
95787,64633
|
||||||
|
95996,64633
|
||||||
|
95996,65671
|
||||||
|
95257,65671
|
||||||
|
95257,66805
|
||||||
|
94824,66805
|
||||||
|
94824,68064
|
||||||
|
94703,68064
|
||||||
|
94703,69040
|
||||||
|
93885,69040
|
||||||
|
93885,70033
|
||||||
|
93144,70033
|
||||||
|
93144,71442
|
||||||
|
93273,71442
|
||||||
|
93273,72223
|
||||||
|
92132,72223
|
||||||
|
92132,73626
|
||||||
|
92168,73626
|
||||||
|
92168,74530
|
||||||
|
91292,74530
|
||||||
|
91292,75470
|
||||||
|
90502,75470
|
||||||
|
90502,76416
|
||||||
|
89735,76416
|
||||||
|
89735,77334
|
||||||
|
88939,77334
|
||||||
|
88939,78456
|
||||||
|
88429,78456
|
||||||
|
88429,79755
|
||||||
|
88124,79755
|
||||||
|
88124,80206
|
||||||
|
86743,80206
|
||||||
|
86743,81651
|
||||||
|
86572,81651
|
||||||
|
86572,82637
|
||||||
|
85835,82637
|
||||||
|
85835,83550
|
||||||
|
85013,83550
|
||||||
|
85013,84208
|
||||||
|
83935,84208
|
||||||
|
83935,85223
|
||||||
|
83215,85223
|
||||||
|
83215,85505
|
||||||
|
81820,85505
|
||||||
|
81820,86396
|
||||||
|
80992,86396
|
||||||
|
80992,87581
|
||||||
|
80394,87581
|
||||||
|
80394,87810
|
||||||
|
79026,87810
|
||||||
|
79026,88858
|
||||||
|
78295,88858
|
||||||
|
78295,89626
|
||||||
|
77345,89626
|
||||||
|
77345,90479
|
||||||
|
76446,90479
|
||||||
|
76446,90760
|
||||||
|
75180,90760
|
||||||
|
75180,91580
|
||||||
|
74253,91580
|
||||||
|
74253,91749
|
||||||
|
72954,91749
|
||||||
|
72954,92987
|
||||||
|
72236,92987
|
||||||
|
72236,92764
|
||||||
|
70765,92764
|
||||||
|
70765,93388
|
||||||
|
69726,93388
|
||||||
|
69726,94574
|
||||||
|
68917,94574
|
||||||
|
68917,94680
|
||||||
|
67640,94680
|
||||||
|
67640,95382
|
||||||
|
66601,95382
|
||||||
|
66601,95646
|
||||||
|
65398,95646
|
||||||
|
65398,95949
|
||||||
|
64215,95949
|
||||||
|
64215,95773
|
||||||
|
62899,95773
|
||||||
|
62899,96533
|
||||||
|
61849,96533
|
||||||
|
61849,96742
|
||||||
|
60649,96742
|
||||||
|
60649,96781
|
||||||
|
59418,96781
|
||||||
|
59418,97093
|
||||||
|
58246,97093
|
||||||
|
58246,97486
|
||||||
|
57080,97486
|
||||||
|
57080,97991
|
||||||
|
55917,97991
|
||||||
|
55917,97935
|
||||||
|
54680,97935
|
||||||
|
54680,97953
|
||||||
|
53458,97953
|
||||||
|
53458,98064
|
||||||
|
52244,98064
|
||||||
|
52244,97870
|
||||||
|
51021,97870
|
||||||
|
51021,98482
|
||||||
|
49806,98482
|
||||||
|
49806,97943
|
||||||
|
48593,97943
|
||||||
|
48593,97538
|
||||||
|
47398,97538
|
||||||
|
47398,98218
|
||||||
|
46133,98218
|
||||||
|
46133,97704
|
||||||
|
44956,97704
|
||||||
|
44956,97150
|
||||||
|
43803,97150
|
||||||
|
43803,97022
|
||||||
|
42603,97022
|
||||||
|
42603,97296
|
||||||
|
41327,97296
|
||||||
|
41327,97133
|
||||||
|
40117,97133
|
||||||
|
40117,97002
|
||||||
|
38894,97002
|
||||||
|
38894,96124
|
||||||
|
37860,96124
|
||||||
|
37860,96328
|
||||||
|
36542,96328
|
||||||
|
36542,95555
|
||||||
|
35506,95555
|
||||||
|
35506,95187
|
||||||
|
34352,95187
|
||||||
|
34352,95215
|
||||||
|
33048,95215
|
||||||
|
33048,94803
|
||||||
|
31894,94803
|
||||||
|
31894,93668
|
||||||
|
31053,93668
|
||||||
|
31053,93616
|
||||||
|
29747,93616
|
||||||
|
29747,92979
|
||||||
|
28703,92979
|
||||||
|
28703,92337
|
||||||
|
27668,92337
|
||||||
|
27668,92289
|
||||||
|
26305,92289
|
||||||
|
26305,91414
|
||||||
|
25397,91414
|
||||||
|
25397,90393
|
||||||
|
24597,90393
|
||||||
|
24597,89829
|
||||||
|
23521,89829
|
||||||
|
23521,88999
|
||||||
|
22623,88999
|
||||||
|
22623,88490
|
||||||
|
21498,88490
|
||||||
|
21498,87464
|
||||||
|
20759,87464
|
||||||
|
20759,87311
|
||||||
|
19326,87311
|
||||||
|
19326,86286
|
||||||
|
18596,86286
|
||||||
|
18596,85462
|
||||||
|
17702,85462
|
||||||
|
17702,84371
|
||||||
|
17064,84371
|
||||||
|
17064,83881
|
||||||
|
15846,83881
|
||||||
|
15846,83261
|
||||||
|
14727,83261
|
||||||
|
14727,82278
|
||||||
|
13983,82278
|
||||||
|
13983,80939
|
||||||
|
13667,80939
|
||||||
|
13667,80408
|
||||||
|
12401,80408
|
||||||
|
12401,79459
|
||||||
|
11625,79459
|
||||||
|
11625,78483
|
||||||
|
10883,78483
|
||||||
|
10883,77211
|
||||||
|
10568,77211
|
||||||
|
10568,76057
|
||||||
|
10115,76057
|
||||||
|
10115,75452
|
||||||
|
8799,75452
|
||||||
|
8799,74402
|
||||||
|
8164,74402
|
||||||
|
8164,73202
|
||||||
|
7800,73202
|
||||||
|
7800,72060
|
||||||
|
7352,72060
|
||||||
|
7352,70898
|
||||||
|
6961,70898
|
||||||
|
6961,69773
|
||||||
|
6506,69773
|
||||||
|
6506,68930
|
||||||
|
5395,68930
|
||||||
|
5395,67756
|
||||||
|
5026,67756
|
||||||
|
5026,66423
|
||||||
|
5105,66423
|
||||||
|
5105,65220
|
||||||
|
4882,65220
|
||||||
|
4882,64148
|
||||||
|
4266,64148
|
||||||
|
4266,62985
|
||||||
|
3923,62985
|
||||||
|
3923,61919
|
||||||
|
3194,61919
|
||||||
|
3194,60554
|
||||||
|
3675,60554
|
||||||
|
3675,59474
|
||||||
|
2942,59474
|
||||||
|
2942,58214
|
||||||
|
3088,58214
|
||||||
|
3088,57150
|
||||||
|
2042,57150
|
||||||
|
2042,55879
|
||||||
|
2316,55879
|
||||||
|
2316,54706
|
||||||
|
1807,54706
|
||||||
|
1807,53447
|
||||||
|
2203,53447
|
||||||
|
2203,52232
|
||||||
|
2206,52232
|
||||||
|
2206,51032
|
||||||
|
1605,51032
|
||||||
|
1605,50179
|
||||||
|
94768,50179
|
||||||
|
94768,48588
|
||||||
|
1883,48588
|
||||||
|
1883,47399
|
||||||
|
2484,47399
|
||||||
|
2484,46162
|
||||||
|
2141,46162
|
||||||
|
2141,44991
|
||||||
|
2634,44991
|
||||||
|
2634,43719
|
||||||
|
2213,43719
|
||||||
|
2213,42544
|
||||||
|
2602,42544
|
||||||
|
2602,41302
|
||||||
|
2568,41302
|
||||||
|
2568,40219
|
||||||
|
3352,40219
|
||||||
|
3352,38855
|
||||||
|
2831,38855
|
||||||
|
2831,37871
|
||||||
|
3915,37871
|
||||||
|
3915,36668
|
||||||
|
4103,36668
|
||||||
|
4103,35421
|
||||||
|
4176,35421
|
||||||
|
4176,34389
|
||||||
|
4920,34389
|
||||||
|
4920,32992
|
||||||
|
4637,32992
|
||||||
|
4637,32018
|
||||||
|
5501,32018
|
||||||
|
5501,31051
|
||||||
|
6327,31051
|
||||||
|
6327,29991
|
||||||
|
6908,29991
|
||||||
|
6908,28766
|
||||||
|
7147,28766
|
||||||
|
7147,27503
|
||||||
|
7349,27503
|
||||||
|
7349,26770
|
||||||
|
8540,26770
|
||||||
|
8540,25571
|
||||||
|
8879,25571
|
||||||
|
8879,24466
|
||||||
|
9397,24466
|
||||||
|
9397,23172
|
||||||
|
9646,23172
|
||||||
|
9646,22147
|
||||||
|
10322,22147
|
||||||
|
10322,21212
|
||||||
|
11124,21212
|
||||||
|
11124,20416
|
||||||
|
12095,20416
|
||||||
|
12095,19808
|
||||||
|
13273,19808
|
||||||
|
13273,18827
|
||||||
|
13980,18827
|
||||||
|
13980,17941
|
||||||
|
14800,17941
|
||||||
|
14800,17112
|
||||||
|
15678,17112
|
||||||
|
15678,15995
|
||||||
|
16266,15995
|
||||||
|
16266,14744
|
||||||
|
16754,14744
|
||||||
|
16754,14559
|
||||||
|
18236,14559
|
||||||
|
18236,13457
|
||||||
|
18882,13457
|
||||||
|
18882,12473
|
||||||
|
19650,12473
|
||||||
|
19650,12285
|
||||||
|
21047,12285
|
||||||
|
21047,11216
|
||||||
|
21759,11216
|
||||||
|
21759,10724
|
||||||
|
22896,10724
|
||||||
|
22896,9656
|
||||||
|
23642,9656
|
||||||
|
23642,8817
|
||||||
|
24558,8817
|
||||||
|
24558,8283
|
||||||
|
25667,8283
|
||||||
|
25667,7673
|
||||||
|
26727,7673
|
||||||
|
26727,7670
|
||||||
|
28104,7670
|
||||||
|
28104,7116
|
||||||
|
29177,7116
|
||||||
|
29177,5920
|
||||||
|
29959,5920
|
||||||
|
29959,5795
|
||||||
|
31239,5795
|
||||||
|
31239,5601
|
||||||
|
32470,5601
|
||||||
|
32470,4729
|
||||||
|
33439,4729
|
||||||
|
33439,4688
|
||||||
|
34714,4688
|
||||||
|
34714,4325
|
||||||
|
35869,4325
|
||||||
|
35869,3410
|
||||||
|
36870,3410
|
||||||
|
36870,3699
|
||||||
|
38209,3699
|
||||||
|
38209,3271
|
||||||
|
39353,3271
|
||||||
|
39353,2503
|
||||||
|
40437,2503
|
||||||
|
40437,2545
|
||||||
|
41690,2545
|
||||||
|
41690,2153
|
||||||
|
42865,2153
|
||||||
|
42865,2431
|
||||||
|
44134,2431
|
||||||
|
44134,2128
|
||||||
|
45325,2128
|
||||||
|
45325,1863
|
||||||
|
46527,1863
|
||||||
|
46527,1715
|
||||||
|
47744,1715
|
||||||
|
47744,2370
|
||||||
|
48983,2370
|
||||||
|
48983,2208
|
||||||
|
50191,2208
|
||||||
|
50191,1596
|
||||||
|
51420,1596
|
||||||
|
51420,1777
|
||||||
|
52638,1777
|
||||||
|
52638,2426
|
||||||
|
53814,2426
|
||||||
|
53814,2323
|
||||||
|
55040,2323
|
||||||
|
55040,1948
|
||||||
|
56315,1948
|
||||||
|
56315,3050
|
||||||
|
57384,3050
|
||||||
|
57384,2961
|
||||||
|
58625,2961
|
||||||
|
58625,3074
|
||||||
|
59839,3074
|
||||||
|
59839,3595
|
||||||
|
60964,3595
|
||||||
|
60964,3677
|
||||||
|
62191,3677
|
||||||
|
62191,3720
|
||||||
|
63442,3720
|
||||||
|
63442,4125
|
||||||
|
64595,4125
|
||||||
|
64595,4172
|
||||||
|
65869,4172
|
||||||
|
65869,5434
|
||||||
|
66708,5434
|
||||||
|
66708,5266
|
||||||
|
68076,5266
|
||||||
|
68076,5977
|
||||||
|
69100,5977
|
||||||
|
69100,6618
|
||||||
|
70143,6618
|
||||||
|
70143,6746
|
||||||
|
71431,6746
|
||||||
|
71431,7832
|
||||||
|
72241,7832
|
||||||
|
72241,8034
|
||||||
|
73512,8034
|
||||||
|
73512,9162
|
||||||
|
74260,9162
|
||||||
|
74260,9514
|
||||||
|
75460,9514
|
||||||
|
75460,9627
|
||||||
|
76840,9627
|
||||||
|
76840,10908
|
||||||
|
77440,10908
|
||||||
|
77440,11672
|
||||||
|
78380,11672
|
||||||
|
78380,11972
|
||||||
|
79679,11972
|
||||||
|
79679,13184
|
||||||
|
80265,13184
|
||||||
|
80265,13479
|
||||||
|
81606,13479
|
||||||
|
81606,14622
|
||||||
|
82220,14622
|
||||||
|
82220,15218
|
||||||
|
83328,15218
|
||||||
|
83328,15880
|
||||||
|
84393,15880
|
||||||
|
84393,16884
|
||||||
|
85117,16884
|
||||||
|
85117,17746
|
||||||
|
85987,17746
|
||||||
|
85987,18854
|
||||||
|
86575,18854
|
||||||
|
86575,19853
|
||||||
|
87275,19853
|
||||||
|
87275,20481
|
||||||
|
88452,20481
|
||||||
|
88452,21548
|
||||||
|
89071,21548
|
||||||
|
89071,22598
|
||||||
|
89707,22598
|
||||||
|
89707,23486
|
||||||
|
90581,23486
|
||||||
|
90581,24670
|
||||||
|
91002,24670
|
||||||
|
91002,25846
|
||||||
|
91409,25846
|
||||||
|
91409,26790
|
||||||
|
92213,26790
|
||||||
|
92213,27909
|
||||||
|
92705,27909
|
||||||
|
92705,29085
|
||||||
|
93072,29085
|
||||||
|
93072,30099
|
||||||
|
93771,30099
|
||||||
|
93771,31076
|
||||||
|
94587,31076
|
||||||
|
94587,32364
|
||||||
|
94666,32364
|
||||||
|
94666,33666
|
||||||
|
94648,33666
|
||||||
|
94648,34775
|
||||||
|
95128,34775
|
||||||
|
95128,35721
|
||||||
|
96154,35721
|
||||||
|
96154,36872
|
||||||
|
96583,36872
|
||||||
|
96583,38043
|
||||||
|
96950,38043
|
||||||
|
96950,39313
|
||||||
|
96900,39313
|
||||||
|
96900,40470
|
||||||
|
97329,40470
|
||||||
|
97329,41656
|
||||||
|
97648,41656
|
||||||
|
97648,42909
|
||||||
|
97551,42909
|
||||||
|
97551,44185
|
||||||
|
97161,44185
|
||||||
|
97161,45335
|
||||||
|
97772,45335
|
||||||
|
97772,46568
|
||||||
|
97572,46568
|
||||||
|
97572,47781
|
||||||
|
97508,47781
|
||||||
|
97508,48975
|
||||||
|
97989,48975
|
||||||
|
97989,50192
|
||||||
|
|
|
||||||
103
src/day09.scala
103
src/day09.scala
|
|
@ -20,45 +20,90 @@ def part1(input: String): Long =
|
||||||
((a.x - b.x + 1) * (a.y - b.y + 1)).abs
|
((a.x - b.x + 1) * (a.y - b.y + 1)).abs
|
||||||
}.max
|
}.max
|
||||||
|
|
||||||
def part2(input: String): Long =
|
enum Edge:
|
||||||
val points = input.split('\n').map { case s"$x,$y" => (x = x.toLong, y = y.toLong) }
|
case H(fixedY: Int, range: Range)
|
||||||
val polygon = points
|
case V(fixedX: Int, range: Range)
|
||||||
|
|
||||||
def corners(ax: Long, ay: Long, bx: Long, by: Long) =
|
def contains(x: Int, y: Int): Boolean =
|
||||||
List((ax, ay), (ax, by), (ay, bx), (bx, by))
|
this match
|
||||||
|
case H(fixedY, range) => fixedY == y && range.contains(x)
|
||||||
|
case V(fixedX, range) => fixedX == x && range.contains(y)
|
||||||
|
|
||||||
def isInside(x: Long, y: Long) =
|
object Edge:
|
||||||
val counter = polygon.sliding(2).foldLeft(0) { case (counter, Array(a, b)) =>
|
def apply(a: Point, b: Point): Edge =
|
||||||
if y > min(a.y, b.y) && y <= max(a.y, b.y) && x <= max(a.x, b.x) then
|
if a.y == b.y then Edge.H(a.y, min(a.x, b.x) to max(a.x, b.x))
|
||||||
val xinters = (y - a.y) * (b.x - a.x) / (b.y - a.y) + a.x
|
else if a.x == b.x then Edge.V(a.x, min(a.y, b.y) to max(a.y, b.y))
|
||||||
counter + (if a.x == b.x || x <= xinters then 1 else 0)
|
else ???
|
||||||
else counter
|
|
||||||
|
case class Polygon(edges: List[Edge]):
|
||||||
|
val minX = edges.map {
|
||||||
|
case Edge.H(_, range) => range.min
|
||||||
|
case Edge.V(fixedX, _) => fixedX
|
||||||
|
}.min - 1
|
||||||
|
|
||||||
|
def contains(p: Point) =
|
||||||
|
if edges.exists(_.contains(p.x, p.y)) then true
|
||||||
|
else
|
||||||
|
// println(p)
|
||||||
|
val (count, _) = (minX to p.x).foldLeft((0, false)) { case ((count, switched), x) =>
|
||||||
|
// println(x)
|
||||||
|
// print("edge contains ")
|
||||||
|
// println(edges.exists(_.contains(x, p.y)))
|
||||||
|
// print("switched ")
|
||||||
|
// println(switched)
|
||||||
|
if edges.exists(_.contains(x, p.y)) && !switched then (count + 1, true)
|
||||||
|
else (count, false)
|
||||||
}
|
}
|
||||||
counter % 2 == 1
|
// println("count")
|
||||||
|
// println(count)
|
||||||
|
count % 2 == 1
|
||||||
|
|
||||||
println(isInside(7, 1))
|
case class Point(x: Int, y: Int)
|
||||||
|
|
||||||
println(isInside(7, 2))
|
def part2(input: String): Long =
|
||||||
println(isInside(0, 0))
|
val points = input.split('\n').map { case s"$x,$y" => Point(x.toInt, y.toInt) }
|
||||||
|
val polygon = Polygon(
|
||||||
|
(points :+ points.head).sliding(2).map { case Array(a, b) => Edge(a, b) }.toList
|
||||||
|
)
|
||||||
|
|
||||||
|
def corners(a: Point, b: Point) =
|
||||||
|
List(
|
||||||
|
Point(min(a.x, b.x), min(a.y, b.y)),
|
||||||
|
Point(min(a.x, b.x), max(a.y, b.y)),
|
||||||
|
Point(max(a.x, b.x), min(a.y, b.y)),
|
||||||
|
Point(max(a.x, b.x), max(a.y, b.y))
|
||||||
|
).distinct
|
||||||
|
|
||||||
|
// println(polygon.contains(Point(9, 3)))
|
||||||
|
|
||||||
|
// println(polygon.edges.mkString("\n"))
|
||||||
|
|
||||||
|
// println(polygon.edges.exists(_.contains(9, 3)))
|
||||||
|
// println(polygon.edges.exists(_.contains(9, 5)))
|
||||||
|
// println(polygon.contains(Point(11, 1)))
|
||||||
|
// println(polygon.contains(Point(11, 7)))
|
||||||
|
// println(polygon.contains(Point(11, 8)))
|
||||||
|
// println(polygon.contains(Point(11, 9)))
|
||||||
|
|
||||||
|
// corners(Point(9, 5), Point(2, 3)).foreach(p =>
|
||||||
|
// println(p)
|
||||||
|
// println(polygon.contains(p))
|
||||||
|
// )
|
||||||
|
|
||||||
|
println(
|
||||||
|
points
|
||||||
|
.combinations(2)
|
||||||
|
.map { case Array(a, b) =>
|
||||||
|
corners(a, b)
|
||||||
|
}.distinct.size
|
||||||
|
)
|
||||||
points
|
points
|
||||||
.combinations(2)
|
.combinations(2)
|
||||||
.collect {
|
.collect {
|
||||||
case Array(a, b) if a != b && corners(a.x, a.y, b.x, b.y).forall((x, y) => isInside(x, y)) =>
|
case Array(a, b) if a != b && corners(a, b).forall(polygon.contains) =>
|
||||||
println(corners(a.x, a.y, b.x, b.y))
|
println(corners(a, b))
|
||||||
(a, b)
|
(a, b)
|
||||||
}
|
}
|
||||||
.map((a, b) => ((a.x - b.x + 1) * (a.y - b.y + 1)).abs)
|
.map((a, b) => ((a.x - b.x + 1) * (a.y - b.y + 1)).abs)
|
||||||
.max
|
.max
|
||||||
|
|
||||||
// if (p.y > MIN(p1.y,p2.y)) {
|
|
||||||
// if (p.y <= MAX(p1.y,p2.y)) {
|
|
||||||
// if (p.x <= MAX(p1.x,p2.x)) {
|
|
||||||
// if (p1.y != p2.y) {
|
|
||||||
// xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
|
|
||||||
// if (p1.x == p2.x || p.x <= xinters)
|
|
||||||
// counter++;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
end part2
|
end part2
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue