Day9
This commit is contained in:
496
day9/zig/input.txt
Normal file
496
day9/zig/input.txt
Normal file
@@ -0,0 +1,496 @@
|
|||||||
|
97975,50252
|
||||||
|
97975,51453
|
||||||
|
97479,51453
|
||||||
|
97479,52662
|
||||||
|
97539,52662
|
||||||
|
97539,53901
|
||||||
|
97902,53901
|
||||||
|
97902,55126
|
||||||
|
97907,55126
|
||||||
|
97907,56376
|
||||||
|
98042,56376
|
||||||
|
98042,57475
|
||||||
|
97138,57475
|
||||||
|
97138,58798
|
||||||
|
97645,58798
|
||||||
|
97645,59877
|
||||||
|
96811,59877
|
||||||
|
96811,61156
|
||||||
|
96953,61156
|
||||||
|
96953,62151
|
||||||
|
95934,62151
|
||||||
|
95934,63481
|
||||||
|
96194,63481
|
||||||
|
96194,64558
|
||||||
|
95558,64558
|
||||||
|
95558,65632
|
||||||
|
94958,65632
|
||||||
|
94958,67049
|
||||||
|
95301,67049
|
||||||
|
95301,67863
|
||||||
|
94045,67863
|
||||||
|
94045,69238
|
||||||
|
94186,69238
|
||||||
|
94186,70063
|
||||||
|
93066,70063
|
||||||
|
93066,71249
|
||||||
|
92749,71249
|
||||||
|
92749,72575
|
||||||
|
92669,72575
|
||||||
|
92669,73439
|
||||||
|
91709,73439
|
||||||
|
91709,74808
|
||||||
|
91639,74808
|
||||||
|
91639,75785
|
||||||
|
90887,75785
|
||||||
|
90887,76610
|
||||||
|
89917,76610
|
||||||
|
89917,77468
|
||||||
|
89024,77468
|
||||||
|
89024,78462
|
||||||
|
88335,78462
|
||||||
|
88335,79839
|
||||||
|
88132,79839
|
||||||
|
88132,80218
|
||||||
|
86664,80218
|
||||||
|
86664,81153
|
||||||
|
85904,81153
|
||||||
|
85904,82413
|
||||||
|
85498,82413
|
||||||
|
85498,83317
|
||||||
|
84682,83317
|
||||||
|
84682,83990
|
||||||
|
83634,83990
|
||||||
|
83634,84884
|
||||||
|
82812,84884
|
||||||
|
82812,85907
|
||||||
|
82098,85907
|
||||||
|
82098,86499
|
||||||
|
81001,86499
|
||||||
|
81001,87113
|
||||||
|
79938,87113
|
||||||
|
79938,88054
|
||||||
|
79136,88054
|
||||||
|
79136,88830
|
||||||
|
78199,88830
|
||||||
|
78199,89918
|
||||||
|
77473,89918
|
||||||
|
77473,90237
|
||||||
|
76216,90237
|
||||||
|
76216,90863
|
||||||
|
75172,90863
|
||||||
|
75172,91514
|
||||||
|
74144,91514
|
||||||
|
74144,91724
|
||||||
|
72872,91724
|
||||||
|
72872,92467
|
||||||
|
71899,92467
|
||||||
|
71899,93244
|
||||||
|
70930,93244
|
||||||
|
70930,93375
|
||||||
|
69653,93375
|
||||||
|
69653,94504
|
||||||
|
68821,94504
|
||||||
|
68821,94830
|
||||||
|
67634,94830
|
||||||
|
67634,95408
|
||||||
|
66545,95408
|
||||||
|
66545,95277
|
||||||
|
65210,95277
|
||||||
|
65210,95980
|
||||||
|
64160,95980
|
||||||
|
64160,96603
|
||||||
|
63069,96603
|
||||||
|
63069,96514
|
||||||
|
61782,96514
|
||||||
|
61782,97296
|
||||||
|
60713,97296
|
||||||
|
60713,97306
|
||||||
|
59462,97306
|
||||||
|
59462,97639
|
||||||
|
58279,97639
|
||||||
|
58279,97915
|
||||||
|
57082,97915
|
||||||
|
57082,97198
|
||||||
|
55758,97198
|
||||||
|
55758,98216
|
||||||
|
54646,98216
|
||||||
|
54646,97474
|
||||||
|
53363,97474
|
||||||
|
53363,97470
|
||||||
|
52156,97470
|
||||||
|
52156,98184
|
||||||
|
50967,98184
|
||||||
|
50967,97836
|
||||||
|
49748,97836
|
||||||
|
49748,98311
|
||||||
|
48521,98311
|
||||||
|
48521,98044
|
||||||
|
47309,98044
|
||||||
|
47309,97631
|
||||||
|
46120,97631
|
||||||
|
46120,97560
|
||||||
|
44910,97560
|
||||||
|
44910,98013
|
||||||
|
43627,98013
|
||||||
|
43627,97613
|
||||||
|
42448,97613
|
||||||
|
42448,97341
|
||||||
|
41257,97341
|
||||||
|
41257,96770
|
||||||
|
40131,96770
|
||||||
|
40131,96290
|
||||||
|
39000,96290
|
||||||
|
39000,96362
|
||||||
|
37734,96362
|
||||||
|
37734,96365
|
||||||
|
36468,96365
|
||||||
|
36468,96168
|
||||||
|
35246,96168
|
||||||
|
35246,95017
|
||||||
|
34347,95017
|
||||||
|
34347,94960
|
||||||
|
33078,94960
|
||||||
|
33078,94141
|
||||||
|
32097,94141
|
||||||
|
32097,94398
|
||||||
|
30669,94398
|
||||||
|
30669,93912
|
||||||
|
29542,93912
|
||||||
|
29542,93272
|
||||||
|
28490,93272
|
||||||
|
28490,92285
|
||||||
|
27627,92285
|
||||||
|
27627,92198
|
||||||
|
26286,92198
|
||||||
|
26286,90904
|
||||||
|
25629,90904
|
||||||
|
25629,90972
|
||||||
|
24161,90972
|
||||||
|
24161,89590
|
||||||
|
23607,89590
|
||||||
|
23607,89460
|
||||||
|
22225,89460
|
||||||
|
22225,88680
|
||||||
|
21281,88680
|
||||||
|
21281,87418
|
||||||
|
20719,87418
|
||||||
|
20719,87308
|
||||||
|
19250,87308
|
||||||
|
19250,86140
|
||||||
|
18642,86140
|
||||||
|
18642,85119
|
||||||
|
17933,85119
|
||||||
|
17933,84676
|
||||||
|
16688,84676
|
||||||
|
16688,83628
|
||||||
|
16014,83628
|
||||||
|
16014,82584
|
||||||
|
15357,82584
|
||||||
|
15357,81967
|
||||||
|
14239,81967
|
||||||
|
14239,81352
|
||||||
|
13086,81352
|
||||||
|
13086,80449
|
||||||
|
12252,80449
|
||||||
|
12252,78919
|
||||||
|
12230,78919
|
||||||
|
12230,78293
|
||||||
|
11040,78293
|
||||||
|
11040,77383
|
||||||
|
10211,77383
|
||||||
|
10211,76320
|
||||||
|
9601,76320
|
||||||
|
9601,75283
|
||||||
|
8956,75283
|
||||||
|
8956,74307
|
||||||
|
8205,74307
|
||||||
|
8205,73286
|
||||||
|
7520,73286
|
||||||
|
7520,72048
|
||||||
|
7243,72048
|
||||||
|
7243,70756
|
||||||
|
7114,70756
|
||||||
|
7114,69608
|
||||||
|
6724,69608
|
||||||
|
6724,68707
|
||||||
|
5765,68707
|
||||||
|
5765,67462
|
||||||
|
5605,67462
|
||||||
|
5605,66301
|
||||||
|
5261,66301
|
||||||
|
5261,65196
|
||||||
|
4763,65196
|
||||||
|
4763,64245
|
||||||
|
3744,64245
|
||||||
|
3744,62832
|
||||||
|
4241,62832
|
||||||
|
4241,61686
|
||||||
|
3864,61686
|
||||||
|
3864,60560
|
||||||
|
3375,60560
|
||||||
|
3375,59461
|
||||||
|
2695,59461
|
||||||
|
2695,58165
|
||||||
|
3014,58165
|
||||||
|
3014,57080
|
||||||
|
2096,57080
|
||||||
|
2096,55764
|
||||||
|
2757,55764
|
||||||
|
2757,54562
|
||||||
|
2657,54562
|
||||||
|
2657,53421
|
||||||
|
1718,53421
|
||||||
|
1718,52198
|
||||||
|
1610,52198
|
||||||
|
1610,50958
|
||||||
|
2235,50958
|
||||||
|
2235,50236
|
||||||
|
94851,50236
|
||||||
|
94851,48543
|
||||||
|
2411,48543
|
||||||
|
2411,47289
|
||||||
|
1596,47289
|
||||||
|
1596,46090
|
||||||
|
2006,46090
|
||||||
|
2006,44880
|
||||||
|
2161,44880
|
||||||
|
2161,43661
|
||||||
|
2238,43661
|
||||||
|
2238,42492
|
||||||
|
2662,42492
|
||||||
|
2662,41205
|
||||||
|
2380,41205
|
||||||
|
2380,40173
|
||||||
|
3427,40173
|
||||||
|
3427,38915
|
||||||
|
3350,38915
|
||||||
|
3350,37726
|
||||||
|
3605,37726
|
||||||
|
3605,36417
|
||||||
|
3458,36417
|
||||||
|
3458,35493
|
||||||
|
4603,35493
|
||||||
|
4603,34366
|
||||||
|
5037,34366
|
||||||
|
5037,32938
|
||||||
|
4667,32938
|
||||||
|
4667,31795
|
||||||
|
5114,31795
|
||||||
|
5114,30668
|
||||||
|
5600,30668
|
||||||
|
5600,29844
|
||||||
|
6734,29844
|
||||||
|
6734,28729
|
||||||
|
7207,28729
|
||||||
|
7207,27456
|
||||||
|
7392,27456
|
||||||
|
7392,26408
|
||||||
|
8019,26408
|
||||||
|
8019,25188
|
||||||
|
8354,25188
|
||||||
|
8354,24485
|
||||||
|
9541,24485
|
||||||
|
9541,23546
|
||||||
|
10318,23546
|
||||||
|
10318,22529
|
||||||
|
10972,22529
|
||||||
|
10972,21425
|
||||||
|
11513,21425
|
||||||
|
11513,20700
|
||||||
|
12557,20700
|
||||||
|
12557,19461
|
||||||
|
12947,19461
|
||||||
|
12947,18679
|
||||||
|
13902,18679
|
||||||
|
13902,17868
|
||||||
|
14809,17868
|
||||||
|
14809,16883
|
||||||
|
15527,16883
|
||||||
|
15527,15849
|
||||||
|
16207,15849
|
||||||
|
16207,15014
|
||||||
|
17093,15014
|
||||||
|
17093,14095
|
||||||
|
17903,14095
|
||||||
|
17903,13455
|
||||||
|
18960,13455
|
||||||
|
18960,12395
|
||||||
|
19665,12395
|
||||||
|
19665,12096
|
||||||
|
20978,12096
|
||||||
|
20978,11398
|
||||||
|
21965,11398
|
||||||
|
21965,10257
|
||||||
|
22647,10257
|
||||||
|
22647,9721
|
||||||
|
23757,9721
|
||||||
|
23757,8872
|
||||||
|
24664,8872
|
||||||
|
24664,8596
|
||||||
|
25919,8596
|
||||||
|
25919,7824
|
||||||
|
26880,7824
|
||||||
|
26880,7671
|
||||||
|
28172,7671
|
||||||
|
28172,7103
|
||||||
|
29237,7103
|
||||||
|
29237,5921
|
||||||
|
30027,5921
|
||||||
|
30027,6089
|
||||||
|
31429,6089
|
||||||
|
31429,5521
|
||||||
|
32504,5521
|
||||||
|
32504,4554
|
||||||
|
33440,4554
|
||||||
|
33440,4529
|
||||||
|
34724,4529
|
||||||
|
34724,3811
|
||||||
|
35774,3811
|
||||||
|
35774,3455
|
||||||
|
36946,3455
|
||||||
|
36946,3188
|
||||||
|
38142,3188
|
||||||
|
38142,2905
|
||||||
|
39332,2905
|
||||||
|
39332,3075
|
||||||
|
40614,3075
|
||||||
|
40614,3113
|
||||||
|
41851,3113
|
||||||
|
41851,2445
|
||||||
|
42971,2445
|
||||||
|
42971,2120
|
||||||
|
44158,2120
|
||||||
|
44158,2569
|
||||||
|
45429,2569
|
||||||
|
45429,1852
|
||||||
|
46588,1852
|
||||||
|
46588,1943
|
||||||
|
47816,1943
|
||||||
|
47816,2254
|
||||||
|
49041,2254
|
||||||
|
49041,1803
|
||||||
|
50253,1803
|
||||||
|
50253,2228
|
||||||
|
51462,2228
|
||||||
|
51462,1909
|
||||||
|
52692,1909
|
||||||
|
52692,2119
|
||||||
|
53900,2119
|
||||||
|
53900,1826
|
||||||
|
55155,1826
|
||||||
|
55155,2730
|
||||||
|
56273,2730
|
||||||
|
56273,2449
|
||||||
|
57540,2449
|
||||||
|
57540,2339
|
||||||
|
58801,2339
|
||||||
|
58801,2901
|
||||||
|
59937,2901
|
||||||
|
59937,3035
|
||||||
|
61159,3035
|
||||||
|
61159,3459
|
||||||
|
62312,3459
|
||||||
|
62312,3791
|
||||||
|
63485,3791
|
||||||
|
63485,4174
|
||||||
|
64643,4174
|
||||||
|
64643,4536
|
||||||
|
65807,4536
|
||||||
|
65807,5066
|
||||||
|
66911,5066
|
||||||
|
66911,5915
|
||||||
|
67879,5915
|
||||||
|
67879,5928
|
||||||
|
69188,5928
|
||||||
|
69188,6764
|
||||||
|
70141,6764
|
||||||
|
70141,7203
|
||||||
|
71273,7203
|
||||||
|
71273,7294
|
||||||
|
72594,7294
|
||||||
|
72594,7751
|
||||||
|
73741,7751
|
||||||
|
73741,8749
|
||||||
|
74576,8749
|
||||||
|
74576,9047
|
||||||
|
75826,9047
|
||||||
|
75826,10217
|
||||||
|
76520,10217
|
||||||
|
76520,10745
|
||||||
|
77629,10745
|
||||||
|
77629,11725
|
||||||
|
78417,11725
|
||||||
|
78417,12212
|
||||||
|
79569,12212
|
||||||
|
79569,12742
|
||||||
|
80707,12742
|
||||||
|
80707,13918
|
||||||
|
81306,13918
|
||||||
|
81306,14507
|
||||||
|
82407,14507
|
||||||
|
82407,15516
|
||||||
|
83127,15516
|
||||||
|
83127,16462
|
||||||
|
83892,16462
|
||||||
|
83892,16920
|
||||||
|
85168,16920
|
||||||
|
85168,18095
|
||||||
|
85689,18095
|
||||||
|
85689,18999
|
||||||
|
86499,18999
|
||||||
|
86499,19958
|
||||||
|
87241,19958
|
||||||
|
87241,20793
|
||||||
|
88145,20793
|
||||||
|
88145,21697
|
||||||
|
88970,21697
|
||||||
|
88970,22535
|
||||||
|
89906,22535
|
||||||
|
89906,23729
|
||||||
|
90321,23729
|
||||||
|
90321,24836
|
||||||
|
90848,24836
|
||||||
|
90848,25720
|
||||||
|
91745,25720
|
||||||
|
91745,26940
|
||||||
|
92065,26940
|
||||||
|
92065,28028
|
||||||
|
92606,28028
|
||||||
|
92606,29038
|
||||||
|
93307,29038
|
||||||
|
93307,30315
|
||||||
|
93443,30315
|
||||||
|
93443,31116
|
||||||
|
94650,31116
|
||||||
|
94650,32401
|
||||||
|
94739,32401
|
||||||
|
94739,33654
|
||||||
|
94859,33654
|
||||||
|
94859,34617
|
||||||
|
95791,34617
|
||||||
|
95791,35849
|
||||||
|
95946,35849
|
||||||
|
95946,36924
|
||||||
|
96624,36924
|
||||||
|
96624,38269
|
||||||
|
96308,38269
|
||||||
|
96308,39322
|
||||||
|
97138,39322
|
||||||
|
97138,40541
|
||||||
|
97288,40541
|
||||||
|
97288,41855
|
||||||
|
96860,41855
|
||||||
|
96860,42997
|
||||||
|
97372,42997
|
||||||
|
97372,44221
|
||||||
|
97358,44221
|
||||||
|
97358,45361
|
||||||
|
98131,45361
|
||||||
|
98131,46582
|
||||||
|
98238,46582
|
||||||
|
98238,47836
|
||||||
|
97623,47836
|
||||||
|
97623,49040
|
||||||
|
97819,49040
|
||||||
|
97819,50252
|
||||||
131
day9/zig/main.zig
Normal file
131
day9/zig/main.zig
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const Point = struct { x: i64, y: i64 };
|
||||||
|
|
||||||
|
const Tile = enum { RED, GREEN, EMPTY };
|
||||||
|
|
||||||
|
pub fn main() !void {
|
||||||
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
|
defer _ = gpa.deinit();
|
||||||
|
|
||||||
|
var stdin_buffer: [1024]u8 = undefined;
|
||||||
|
var stdin_reader = std.fs.File.stdin().reader(&stdin_buffer);
|
||||||
|
|
||||||
|
const stdin = &stdin_reader.interface;
|
||||||
|
|
||||||
|
const allocator = gpa.allocator();
|
||||||
|
var red_tiles = try std.ArrayList(Point).initCapacity(allocator, 1);
|
||||||
|
defer red_tiles.deinit(allocator);
|
||||||
|
|
||||||
|
while (try stdin.takeDelimiter('\n')) |line| {
|
||||||
|
const line_trimmed = std.mem.trim(u8, line, "\n");
|
||||||
|
|
||||||
|
var it = std.mem.splitScalar(u8, line_trimmed, ',');
|
||||||
|
|
||||||
|
const x = try std.fmt.parseInt(i64, it.next().?, 10);
|
||||||
|
const y = try std.fmt.parseInt(i64, it.next().?, 10);
|
||||||
|
|
||||||
|
try red_tiles.append(allocator, Point{ .x = x, .y = y });
|
||||||
|
}
|
||||||
|
|
||||||
|
var task1: i64 = 0;
|
||||||
|
var task2: i64 = 0;
|
||||||
|
|
||||||
|
for (red_tiles.items, 0..) |tile1, i| {
|
||||||
|
for (red_tiles.items[i + 1 ..]) |tile2| {
|
||||||
|
const minx = @min(tile1.x, tile2.x);
|
||||||
|
const maxx = @max(tile1.x, tile2.x);
|
||||||
|
const miny = @min(tile1.y, tile2.y);
|
||||||
|
const maxy = @max(tile1.y, tile2.y);
|
||||||
|
const area = (maxx - minx + 1) * (maxy - miny + 1);
|
||||||
|
|
||||||
|
if (area > task1) {
|
||||||
|
task1 = area;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (area <= task2) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Task2
|
||||||
|
// Test if they can form a rectangle
|
||||||
|
var formable = true;
|
||||||
|
|
||||||
|
for (red_tiles.items) |tile| {
|
||||||
|
if (tile.x < maxx and tile.x > minx and tile.y < maxy and tile.y > miny) {
|
||||||
|
formable = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const uminx: u64 = @intCast(minx);
|
||||||
|
const umaxx: u64 = @intCast(maxx);
|
||||||
|
|
||||||
|
// Check diagonal
|
||||||
|
const dx: f64 = @floatFromInt(maxx - minx);
|
||||||
|
const dy: f64 = @floatFromInt(maxy - miny);
|
||||||
|
const fminy: f64 = @floatFromInt(miny);
|
||||||
|
const m = dy / dx;
|
||||||
|
for (0..umaxx - uminx + 1) |ix| {
|
||||||
|
const iix: i64 = @intCast(ix + uminx);
|
||||||
|
const fix: f64 = @floatFromInt(ix);
|
||||||
|
const iiy: i64 = @intFromFloat(fminy + m * fix);
|
||||||
|
if (!isPointInPolygon(Point{ .x = iix, .y = iiy }, red_tiles)) {
|
||||||
|
formable = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (formable) {
|
||||||
|
task2 = area;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std.debug.print("{d}\n", .{task1});
|
||||||
|
std.debug.print("{d}\n", .{task2});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn isPointInPolygon(p: Point, polygon: std.ArrayList(Point)) bool {
|
||||||
|
var instersection_count: usize = 0;
|
||||||
|
var j = polygon.items.len - 1;
|
||||||
|
|
||||||
|
for (0..polygon.items.len) |i| {
|
||||||
|
const start = polygon.items[i];
|
||||||
|
const end = polygon.items[j];
|
||||||
|
|
||||||
|
const ymin = @min(start.y, end.y);
|
||||||
|
const ymax = @max(start.y, end.y);
|
||||||
|
|
||||||
|
// Horiztonal
|
||||||
|
if (ymin == ymax) {
|
||||||
|
const xmin = @min(start.x, end.x);
|
||||||
|
const xmax = @max(start.x, end.x);
|
||||||
|
|
||||||
|
if (p.y == ymin and p.x <= xmax and p.x >= xmin) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Vertical
|
||||||
|
else {
|
||||||
|
const xedge = start.x;
|
||||||
|
|
||||||
|
if (p.y > ymax or p.y < ymin) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// On edge
|
||||||
|
if (p.x == xedge and p.y <= ymax and p.y >= ymin) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p.y >= ymin and p.y < ymax and p.x > xedge) {
|
||||||
|
instersection_count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
j = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return instersection_count % 2 == 1;
|
||||||
|
}
|
||||||
8
day9/zig/sample.txt
Normal file
8
day9/zig/sample.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
7,1
|
||||||
|
11,1
|
||||||
|
11,7
|
||||||
|
9,7
|
||||||
|
9,5
|
||||||
|
2,5
|
||||||
|
2,3
|
||||||
|
7,3
|
||||||
Reference in New Issue
Block a user