my code kinda blows...

stable
Penguin 3 years ago
parent 4db07684cc
commit 34d12093ef

@ -0,0 +1,40 @@
b 45
r
n
n
n
p u
n
n
n
n
n
p u.b.x
p u.b.x-u.a.x
q
q
b 62
r
p u
n
p u.b.y
p u
p grid
p grid[u.a.x]
p ind
n
n
p grid[u.a.x]
q
b 45
p grid
r
p grid
n
n
n
p u
p grid
c
q
q

@ -0,0 +1,9 @@
CC=gcc
CFLAGS=-std=gnu99 -O2 -g -Wall -Wextra -Wshadow
PROJECT=aoc_day4
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)
$(PROJECT): aoc_day5.o
$(CC) -o aoc_day5 aoc_day5.o

Binary file not shown.

@ -0,0 +1,251 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
/* #define DEBUG */
#define INPUT_FILE ("../input.txt")
#define NUM_EDGES (500)
#define MAX_DIM (1000)
/* #define INPUT_FILE ("../input_test.txt") */
/* #define NUM_EDGES (10) */
/* #define MAX_DIM (10) */
typedef struct vertex_t
{
int x;
int y;
}vertex_t;
typedef struct edge_t
{
vertex_t a;
vertex_t b;
}edge_t;
// takes an array of edges and the length
// fills a grid according to part 1 rules
// computes overlapping points and returns this
int d5_part1(edge_t* edges, int len);
// takes an array of edges and the length
// fills a grid according to part 1 rules
// computes overlapping points and returns this
int d5_part2(edge_t* edges, int len);
int calc_overlapping_points(int _grid[MAX_DIM][MAX_DIM], size_t dimension);
void print_grid(int _grid[MAX_DIM][MAX_DIM], size_t dimension);
int main(void)
{
edge_t edges[NUM_EDGES];
memset(edges, 0, sizeof(edge_t) * NUM_EDGES);
FILE* fp = fopen(INPUT_FILE, "r");
if(fp == NULL)
{
printf("open file error...\n");
exit(-1);
}
int edge_ct = 0;
size_t len = 0;
ssize_t rc = 0;
char* line = NULL;
while((rc = getline(&line, &len, fp)) != -1)
{
if (edge_ct >= NUM_EDGES)
{
printf("edges[] out of bounds -- edge count: %d\n", edge_ct);
}
sscanf(line,
"%d,%d -> %d,%d",
&edges[edge_ct].a.x, &edges[edge_ct].a.y,
&edges[edge_ct].b.x, &edges[edge_ct].b.y);
#ifdef DEBUG
printf("%d,%d -> %d,%d\n",
edges[edge_ct].a.x, edges[edge_ct].a.y,
edges[edge_ct].b.x, edges[edge_ct].b.y);
#endif
edge_ct++;
}
printf("Overlapping points (part 1): %d\n", d5_part1(edges, NUM_EDGES));
printf("Overlapping points (part 2): %d\n", d5_part2(edges, NUM_EDGES));
return 0;
}
// takes an array of edges and the length
// fills a grid according to part 1 rules
// computes overlapping points and returns this
int d5_part1(edge_t* edges, int len)
{
int grid[MAX_DIM][MAX_DIM];
memset(grid, 0, MAX_DIM * MAX_DIM * sizeof(int));
for(int edge = 0; edge < len; edge++)
{
if(edges[edge].b.x == edges[edge].a.x)
{
// from y2 to y1
if(edges[edge].a.y > edges[edge].b.y)
{
for(int ind = edges[edge].b.y; ind <= edges[edge].a.y; ind++)
{
grid[edges[edge].a.x][ind]++;
}
}
else // from y1 to y2
{
for(int ind = edges[edge].a.y; ind <= edges[edge].b.y; ind++)
{
grid[edges[edge].a.x][ind]++;
}
}
}
else if(edges[edge].b.y == edges[edge].a.y)
{
if(edges[edge].a.x > edges[edge].b.x)
{
//
for(int ind = edges[edge].b.x; ind <= edges[edge].a.x; ind++)
{
grid[ind][edges[edge].a.y]++;
}
}
else
{
for(int ind = edges[edge].a.x; ind <= edges[edge].b.x; ind++)
{
grid[ind][edges[edge].a.y]++;
}
}
}
}
#ifdef DEBUG
printf("Part 1 grid:\n");
print_grid(grid, MAX_DIM);
#endif
return calc_overlapping_points(grid, MAX_DIM);
}
// takes an array of edges and the length
// fills a grid according to part 1 rules
// computes overlapping points and returns this
int d5_part2(edge_t* edges, int len)
{
int grid[MAX_DIM][MAX_DIM];
memset(grid, 0, MAX_DIM * MAX_DIM * sizeof(int));
for(int edge = 0; edge < len; edge++)
{
// now we have to account for horizontal, vertical, and diagonal lines, or lines with a 45 degree angle
// this just means the slope must be 1. so y2-y1/x2-x1 needs to be 1
if(edges[edge].b.x == edges[edge].a.x)
{
// from y2 to y1
if(edges[edge].a.y > edges[edge].b.y)
{
for(int ind = edges[edge].b.y; ind <= edges[edge].a.y; ind++)
{
grid[edges[edge].a.x][ind]++;
}
}
else // from y1 to y2
{
for(int ind = edges[edge].a.y; ind <= edges[edge].b.y; ind++)
{
grid[edges[edge].a.x][ind]++;
}
}
}
else if(edges[edge].b.y == edges[edge].a.y)
{
if(edges[edge].a.x > edges[edge].b.x)
{
//
for(int ind = edges[edge].b.x; ind <= edges[edge].a.x; ind++)
{
grid[ind][edges[edge].a.y]++;
}
}
else
{
for(int ind = edges[edge].a.x; ind <= edges[edge].b.x; ind++)
{
grid[ind][edges[edge].a.y]++;
}
}
}
else if(abs(edges[edge].b.y - edges[edge].a.y) ==
abs(edges[edge].b.x - edges[edge].a.x))
{
int inc_x = (edges[edge].b.x - edges[edge].a.x) /
abs(edges[edge].b.x - edges[edge].a.x);
int inc_y = (edges[edge].b.y - edges[edge].a.y) /
abs(edges[edge].b.y - abs(edges[edge].a.y));
int distance = abs(edges[edge].b.x - edges[edge].a.x);
vertex_t start =
{
.x = edges[edge].a.x,
.y = edges[edge].a.y
};
for(int ind = 0; ind <= distance; ind++)
{
grid[start.x][start.y]++;
start.x += inc_x;
start.y += inc_y;
}
}
}
#ifdef DEBUG
print_grid(grid, MAX_DIM);
#endif
return calc_overlapping_points(grid, MAX_DIM);
}
int calc_overlapping_points(int _grid[MAX_DIM][MAX_DIM], size_t dimension)
{
int ret = 0;
for(int i = 0; i < (int)dimension; i++)
{
for(int j = 0; j < (int)dimension; j++)
{
if(_grid[i][j] > 1)
{
ret++;
}
}
}
return ret;
}
void print_grid(int _grid[MAX_DIM][MAX_DIM], size_t dimension)
{
printf(" |");
for(int i = 0; i < (int)dimension; i++)
{
printf("%2d ", i);
}
printf("\n");
for(int i = 0; i < (int)dimension; i++)
{
printf("%d | ", i);
for(int j = 0; j < (int)dimension; j++)
{
if(_grid[j][i] < 1)
{
printf("%-2s ", ".");
}
else
{
printf("%-2d ", _grid[j][i]);
}
}
printf("\n");
}
}

Binary file not shown.

@ -0,0 +1,500 @@
432,708 -> 432,160
579,594 -> 579,448
351,791 -> 351,595
520,836 -> 564,880
30,443 -> 666,443
868,157 -> 563,157
112,186 -> 853,927
493,387 -> 456,424
846,165 -> 160,165
811,805 -> 292,805
454,333 -> 885,333
32,407 -> 32,933
907,590 -> 368,51
117,904 -> 375,904
913,145 -> 913,798
245,402 -> 245,755
855,910 -> 427,482
677,252 -> 189,252
65,30 -> 956,921
808,114 -> 808,76
62,946 -> 92,946
478,756 -> 581,756
387,812 -> 495,812
197,606 -> 413,390
173,640 -> 515,640
456,26 -> 456,345
888,69 -> 888,436
157,959 -> 824,959
832,519 -> 43,519
891,258 -> 660,258
929,176 -> 815,176
309,893 -> 309,831
841,825 -> 914,825
168,260 -> 168,389
264,969 -> 763,969
527,553 -> 118,962
251,413 -> 228,413
163,759 -> 550,759
776,650 -> 914,650
178,99 -> 887,808
368,92 -> 646,92
705,583 -> 394,583
547,278 -> 597,328
24,941 -> 264,941
599,588 -> 597,588
308,668 -> 308,369
462,335 -> 462,251
109,343 -> 594,343
928,980 -> 727,980
955,955 -> 100,100
655,716 -> 688,716
797,64 -> 354,64
100,633 -> 100,39
639,757 -> 630,757
22,962 -> 878,106
236,687 -> 130,687
117,881 -> 404,881
590,334 -> 590,984
864,888 -> 864,126
648,164 -> 396,164
749,446 -> 191,446
41,973 -> 955,59
247,725 -> 193,725
736,136 -> 736,545
919,33 -> 122,830
66,986 -> 735,317
262,989 -> 262,440
25,86 -> 59,86
329,151 -> 953,151
387,105 -> 310,105
216,197 -> 307,106
568,574 -> 681,574
982,801 -> 982,853
965,606 -> 634,606
901,854 -> 72,25
875,278 -> 569,584
500,240 -> 355,240
790,448 -> 438,96
922,863 -> 559,863
772,727 -> 129,84
25,11 -> 897,883
568,609 -> 318,859
876,875 -> 453,452
181,471 -> 544,834
323,377 -> 655,377
418,20 -> 418,963
449,392 -> 401,440
601,958 -> 296,653
527,343 -> 519,343
763,328 -> 290,328
889,262 -> 499,262
390,478 -> 879,478
298,764 -> 25,764
753,86 -> 40,799
311,350 -> 353,350
132,817 -> 504,817
34,121 -> 837,924
535,27 -> 535,539
932,257 -> 932,983
44,833 -> 596,833
141,34 -> 910,803
781,61 -> 453,389
929,28 -> 449,508
679,958 -> 679,618
894,273 -> 894,778
735,697 -> 735,395
266,693 -> 561,988
402,586 -> 402,983
141,459 -> 368,459
721,723 -> 721,914
845,69 -> 871,95
196,370 -> 728,902
110,725 -> 110,391
901,670 -> 664,433
542,601 -> 244,899
365,386 -> 365,655
647,738 -> 197,738
292,250 -> 292,29
441,603 -> 571,603
847,146 -> 847,941
629,282 -> 629,700
132,44 -> 805,44
972,31 -> 17,986
437,303 -> 186,303
982,416 -> 245,416
498,258 -> 752,258
300,815 -> 228,743
10,69 -> 925,984
26,275 -> 227,275
969,358 -> 969,833
19,593 -> 136,593
436,451 -> 436,364
181,847 -> 199,865
559,409 -> 902,409
664,570 -> 749,485
170,176 -> 170,254
469,908 -> 635,742
94,496 -> 341,743
142,913 -> 142,987
968,143 -> 552,559
577,847 -> 629,847
888,354 -> 214,354
756,769 -> 756,835
117,696 -> 756,696
966,297 -> 699,297
187,800 -> 504,800
467,739 -> 264,942
952,83 -> 85,950
167,478 -> 167,480
846,257 -> 814,225
427,787 -> 867,787
287,886 -> 287,256
538,594 -> 838,594
987,989 -> 11,13
205,825 -> 876,154
433,437 -> 29,437
954,943 -> 105,94
53,143 -> 615,143
116,438 -> 116,136
31,48 -> 826,843
522,637 -> 522,976
201,322 -> 146,322
873,214 -> 873,564
719,460 -> 719,295
879,886 -> 12,19
688,723 -> 688,677
769,779 -> 962,586
109,887 -> 940,56
359,57 -> 147,57
809,857 -> 22,857
239,860 -> 239,523
329,907 -> 67,907
796,165 -> 979,348
661,385 -> 661,606
638,685 -> 53,100
727,787 -> 883,943
324,414 -> 229,414
323,363 -> 323,357
201,440 -> 201,316
778,54 -> 68,764
528,151 -> 528,549
909,970 -> 20,81
978,12 -> 17,973
944,574 -> 944,499
625,85 -> 452,258
708,618 -> 148,618
40,957 -> 40,31
113,288 -> 113,424
262,109 -> 262,982
90,359 -> 319,359
635,21 -> 635,433
337,310 -> 918,891
512,701 -> 100,289
830,946 -> 60,176
233,959 -> 257,959
460,702 -> 156,702
587,96 -> 508,17
954,552 -> 906,552
359,268 -> 324,268
571,416 -> 422,416
147,684 -> 379,684
817,274 -> 631,88
722,85 -> 527,85
845,457 -> 288,457
50,687 -> 282,919
368,310 -> 704,310
156,20 -> 920,784
888,89 -> 347,89
173,27 -> 173,984
659,450 -> 507,298
144,857 -> 671,857
575,976 -> 455,856
452,531 -> 452,67
858,506 -> 858,205
903,486 -> 610,193
936,165 -> 498,165
864,336 -> 875,336
113,15 -> 113,492
153,892 -> 153,20
65,401 -> 669,401
261,743 -> 261,693
511,401 -> 241,401
272,683 -> 875,683
630,871 -> 630,868
249,68 -> 249,500
242,532 -> 111,532
140,125 -> 220,125
878,457 -> 435,457
18,723 -> 693,48
853,234 -> 853,953
285,507 -> 155,507
139,873 -> 139,339
965,327 -> 965,696
68,616 -> 68,484
886,659 -> 701,659
162,836 -> 886,112
119,281 -> 119,776
445,706 -> 430,706
420,705 -> 420,263
941,764 -> 941,442
910,932 -> 910,289
24,11 -> 249,11
476,296 -> 564,296
960,822 -> 450,312
802,852 -> 272,852
527,127 -> 527,408
928,132 -> 382,678
102,297 -> 109,297
308,586 -> 308,894
35,913 -> 99,977
950,392 -> 950,405
96,531 -> 892,531
700,451 -> 291,42
219,197 -> 219,201
863,861 -> 267,265
104,55 -> 944,895
309,909 -> 165,909
732,458 -> 790,458
877,959 -> 45,959
30,902 -> 907,25
314,36 -> 974,36
665,580 -> 665,882
526,288 -> 865,288
312,166 -> 312,464
556,380 -> 820,644
870,959 -> 602,959
273,69 -> 726,69
161,885 -> 882,164
251,332 -> 251,989
853,962 -> 348,962
523,47 -> 577,47
559,503 -> 128,934
625,681 -> 477,533
179,352 -> 800,973
609,847 -> 609,218
249,521 -> 249,930
237,903 -> 299,903
59,373 -> 59,127
788,558 -> 377,147
242,792 -> 421,792
449,874 -> 449,901
821,263 -> 964,263
162,121 -> 561,520
31,35 -> 600,604
622,649 -> 622,97
33,967 -> 985,15
877,745 -> 56,745
688,688 -> 688,385
227,137 -> 728,638
839,54 -> 593,54
662,36 -> 88,610
845,500 -> 163,500
131,579 -> 131,592
632,28 -> 632,387
335,79 -> 979,79
33,95 -> 302,95
981,13 -> 28,966
737,165 -> 170,732
869,751 -> 14,751
420,367 -> 420,949
623,618 -> 558,553
964,34 -> 15,983
567,75 -> 553,75
674,835 -> 674,886
374,727 -> 199,552
880,953 -> 579,953
320,664 -> 777,207
290,198 -> 290,689
405,778 -> 405,253
801,164 -> 801,766
722,572 -> 722,721
704,890 -> 905,890
80,909 -> 974,909
50,643 -> 371,964
903,810 -> 42,810
451,102 -> 13,102
944,151 -> 337,758
781,514 -> 417,514
875,828 -> 77,30
551,210 -> 728,210
280,447 -> 484,447
934,926 -> 144,136
341,182 -> 485,182
611,748 -> 321,458
135,483 -> 22,483
590,577 -> 590,307
962,48 -> 962,95
285,770 -> 395,880
740,804 -> 362,426
942,72 -> 530,484
750,893 -> 750,725
707,916 -> 769,978
424,344 -> 47,344
267,891 -> 267,955
718,112 -> 367,463
917,148 -> 80,985
679,682 -> 51,682
72,641 -> 72,729
717,760 -> 153,196
600,341 -> 600,312
530,258 -> 747,258
703,434 -> 703,266
381,291 -> 34,291
889,645 -> 640,645
488,877 -> 958,407
263,375 -> 43,155
865,33 -> 408,33
373,337 -> 412,337
584,15 -> 76,15
431,447 -> 263,447
176,644 -> 176,875
603,487 -> 257,487
857,45 -> 144,758
833,108 -> 64,877
23,173 -> 23,156
175,434 -> 330,434
988,22 -> 28,982
26,793 -> 26,680
164,221 -> 901,958
832,672 -> 250,90
805,537 -> 805,600
393,302 -> 871,302
633,709 -> 171,247
833,118 -> 990,118
806,943 -> 806,447
348,345 -> 967,964
825,747 -> 399,321
976,339 -> 371,339
857,26 -> 857,737
348,474 -> 715,474
260,678 -> 820,678
735,301 -> 347,689
150,591 -> 150,838
538,285 -> 19,804
845,904 -> 60,119
440,963 -> 440,586
500,72 -> 500,819
772,742 -> 772,828
737,452 -> 113,452
238,818 -> 941,115
123,343 -> 123,639
927,370 -> 635,78
373,745 -> 235,745
140,77 -> 140,262
517,375 -> 178,714
72,423 -> 821,423
437,251 -> 210,251
170,51 -> 941,822
135,467 -> 515,467
886,920 -> 89,123
374,302 -> 550,478
289,592 -> 289,557
79,112 -> 944,977
303,831 -> 419,831
369,32 -> 348,53
404,13 -> 890,499
377,781 -> 260,781
288,769 -> 981,76
924,188 -> 628,188
399,313 -> 959,313
970,52 -> 196,826
623,984 -> 169,984
572,644 -> 749,644
426,533 -> 343,533
94,155 -> 721,782
328,268 -> 544,268
878,124 -> 89,913
966,170 -> 610,526
108,841 -> 766,183
115,808 -> 688,235
45,835 -> 806,74
152,932 -> 152,938
229,814 -> 634,409
649,647 -> 649,850
437,904 -> 321,904
115,116 -> 784,785
19,657 -> 19,415
831,833 -> 616,833
88,954 -> 970,72
11,981 -> 707,285
261,255 -> 909,903
883,875 -> 97,89
872,21 -> 872,820
34,229 -> 34,690
10,10 -> 989,989
986,178 -> 986,963
413,933 -> 413,907
897,756 -> 897,88
771,408 -> 771,468
327,198 -> 327,421
938,900 -> 938,721
533,629 -> 533,661
177,701 -> 489,389
601,839 -> 40,278
969,38 -> 70,937
797,698 -> 140,41
794,107 -> 156,745
730,410 -> 730,727
115,14 -> 358,14
608,382 -> 608,959
457,788 -> 393,788
592,729 -> 363,500
742,964 -> 87,309
230,790 -> 433,587
302,878 -> 725,455
353,722 -> 353,512
219,856 -> 824,856
92,44 -> 938,890
547,179 -> 458,90
953,368 -> 193,368
767,26 -> 932,26
883,96 -> 883,311
679,136 -> 679,656
32,117 -> 393,478
619,704 -> 260,704
101,96 -> 985,980
169,61 -> 800,692
545,902 -> 306,902
948,16 -> 52,912
546,691 -> 546,887
611,294 -> 611,324
942,89 -> 942,688
34,838 -> 34,734
68,98 -> 68,876
485,73 -> 485,779
573,976 -> 573,364
331,233 -> 729,631
616,116 -> 616,432
909,947 -> 904,947
901,139 -> 864,102
289,511 -> 289,371
269,845 -> 269,597
934,86 -> 80,940
18,883 -> 790,111
154,436 -> 154,464
628,901 -> 96,901
573,154 -> 159,154
118,475 -> 118,339
12,12 -> 989,989
44,760 -> 512,760
45,859 -> 888,16
103,826 -> 827,102
41,22 -> 945,926
769,339 -> 188,339
136,658 -> 748,46
297,37 -> 297,160
261,575 -> 293,543
910,724 -> 910,634
30,31 -> 962,963
747,285 -> 122,910
451,976 -> 32,976
558,524 -> 165,524
631,395 -> 631,48
584,382 -> 874,382
246,838 -> 246,980
750,460 -> 577,460
736,747 -> 560,747
604,889 -> 604,150
206,819 -> 18,819
989,974 -> 116,101

@ -0,0 +1,10 @@
0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2
Loading…
Cancel
Save