Piotr Czajkowski 3 years ago
parent
commit
c3c516ab60
3 changed files with 1193 additions and 0 deletions
  1. 85 0
      day9/challenge.txt
  2. 108 0
      day9/day9.c
  3. 1000 0
      day9/input.txt

+ 85 - 0
day9/challenge.txt

@@ -0,0 +1,85 @@
+--- Day 9: Encoding Error ---
+
+With your neighbor happily enjoying their video game, you turn your attention to an open data port on the little screen in the seat in front of you.
+
+Though the port is non-standard, you manage to connect it to your computer through the clever use of several paperclips. Upon connection, the port outputs a series of numbers (your puzzle input).
+
+The data appears to be encrypted with the eXchange-Masking Addition System (XMAS) which, conveniently for you, is an old cypher with an important weakness.
+
+XMAS starts by transmitting a preamble of 25 numbers. After that, each number you receive should be the sum of any two of the 25 immediately previous numbers. The two numbers will have different values, and there might be more than one such pair.
+
+For example, suppose your preamble consists of the numbers 1 through 25 in a random order. To be valid, the next number must be the sum of two of those numbers:
+
+    26 would be a valid next number, as it could be 1 plus 25 (or many other pairs, like 2 and 24).
+    49 would be a valid next number, as it is the sum of 24 and 25.
+    100 would not be valid; no two of the previous 25 numbers sum to 100.
+    50 would also not be valid; although 25 appears in the previous 25 numbers, the two numbers in the pair must be different.
+
+Suppose the 26th number is 45, and the first number (no longer an option, as it is more than 25 numbers ago) was 20. Now, for the next number to be valid, there needs to be some pair of numbers among 1-19, 21-25, or 45 that add up to it:
+
+    26 would still be a valid next number, as 1 and 25 are still within the previous 25 numbers.
+    65 would not be valid, as no two of the available numbers sum to it.
+    64 and 66 would both be valid, as they are the result of 19+45 and 21+45 respectively.
+
+Here is a larger example which only considers the previous 5 numbers (and has a preamble of length 5):
+
+35
+20
+15
+25
+47
+40
+62
+55
+65
+95
+102
+117
+150
+182
+127
+219
+299
+277
+309
+576
+
+In this example, after the 5-number preamble, almost every number is the sum of two of the previous 5 numbers; the only number that does not follow this rule is 127.
+
+The first step of attacking the weakness in the XMAS data is to find the first number in the list (after the preamble) which is not the sum of two of the 25 numbers before it. What is the first number that does not have this property?
+
+Your puzzle answer was 27911108.
+--- Part Two ---
+
+The final step in breaking the XMAS encryption relies on the invalid number you just found: you must find a contiguous set of at least two numbers in your list which sum to the invalid number from step 1.
+
+Again consider the above example:
+
+35
+20
+15
+25
+47
+40
+62
+55
+65
+95
+102
+117
+150
+182
+127
+219
+299
+277
+309
+576
+
+In this list, adding up all of the numbers from 15 through 40 produces the invalid number from step 1, 127. (Of course, the contiguous set of numbers in your actual list might be much longer.)
+
+To find the encryption weakness, add together the smallest and largest number in this contiguous range; in this example, these are 15 and 47, producing 62.
+
+What is the encryption weakness in your XMAS-encrypted list of numbers?
+
+Your puzzle answer was 4023754.

+ 108 - 0
day9/day9.c

@@ -0,0 +1,108 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+int arraySize = 0;
+
+int checkNumber(int number, int **array, int last) {
+	for (int i = 0; i < last; i++)
+		for (int j = i + 1; j < last; j++) {
+			if ((*array[i] + *array[j]) == number) {
+				return 1;
+			}
+		}
+
+	return 0;
+}
+
+int findNumber(int **allNumbers, int last) {
+	int tempArray[last];
+	for (int i = 0; i < last; i++)
+		tempArray[i] = *allNumbers[i];
+
+	int start = 0;
+	int index = last;
+	while (index < arraySize) {
+		int number = *allNumbers[index];
+		if (!checkNumber(number, &allNumbers[start++], last)) return number;
+		index++;
+	}
+
+	return -1;
+}
+
+int **readAllNumbers(FILE *fp) {
+	int **allNumbers = NULL;
+	int index = 0;
+	while (1) {
+		int *p = malloc(sizeof(int));
+		if (!p) return NULL;
+
+		if (1 > fscanf(fp, "%d\n", p))
+			return NULL;
+
+		arraySize++;
+		int **newNumbers = realloc(allNumbers, sizeof(int*)*arraySize);
+		if (!newNumbers) return NULL;
+
+		allNumbers = newNumbers;
+		allNumbers[index] = p;
+		index++;
+
+		if (feof(fp)) break;
+	}
+
+	return allNumbers;
+}
+
+int encryptionWeakness(int badNumber, int **allNumbers) {
+	for (int i = 0; i < arraySize; i++) {
+		int sum = *allNumbers[i];
+		int smallest = *allNumbers[i];
+		int largest = *allNumbers[i];
+		for (int j = i + 1; j < arraySize; j++) {
+			sum += *allNumbers[j];
+			smallest = smallest < *allNumbers[j] ? smallest : *allNumbers[j];
+			largest = largest > *allNumbers[j] ? largest : *allNumbers[j];
+			if (sum == badNumber) return smallest + largest;
+		}
+	}
+
+	return -1;
+}
+
+void freeAllNumbers(int **array) {
+	for (int i = 0; i < arraySize; i++)
+		free(array[i]);
+
+	free(array);
+}
+
+int main(int argc, char **argv)
+{
+	if (argc < 3) {
+		puts("You need to specify path to file and preamble length!");
+		return -1;
+	}
+
+	int last = atoi(argv[2]);
+	FILE *fp = fopen(argv[1], "r");
+	if (!fp) {
+		puts("Can't read");
+		return -1;
+	}
+
+	int **allNumbers = readAllNumbers(fp);
+	if (!allNumbers) {
+		puts("Can't read numbers!");
+		fclose(fp);
+		return -1;
+	}
+
+	int badNumber = findNumber(allNumbers, last);
+	printf("Part1: %d\n", badNumber);
+
+	printf("Part2: %d\n", encryptionWeakness(badNumber, allNumbers));
+
+	freeAllNumbers(allNumbers);
+	fclose(fp);
+}

+ 1000 - 0
day9/input.txt

@@ -0,0 +1,1000 @@
+33
+38
+29
+45
+26
+35
+14
+5
+27
+44
+18
+8
+7
+23
+16
+10
+41
+21
+9
+30
+17
+42
+25
+31
+20
+12
+59
+13
+22
+40
+15
+47
+14
+33
+26
+19
+24
+49
+27
+28
+23
+29
+32
+21
+34
+30
+35
+36
+48
+55
+50
+37
+58
+38
+39
+43
+41
+45
+40
+98
+78
+57
+79
+44
+49
+51
+71
+106
+87
+66
+65
+72
+73
+124
+113
+75
+118
+77
+80
+81
+83
+130
+138
+84
+122
+117
+101
+93
+110
+147
+126
+150
+148
+190
+159
+137
+145
+152
+340
+194
+273
+157
+158
+176
+184
+177
+296
+185
+286
+203
+310
+211
+269
+247
+263
+271
+477
+282
+289
+294
+297
+302
+309
+473
+315
+333
+487
+645
+361
+467
+652
+454
+396
+497
+598
+458
+760
+510
+518
+534
+553
+586
+571
+1120
+1132
+868
+1006
+624
+648
+676
+694
+1048
+1095
+757
+906
+850
+854
+1604
+955
+976
+1096
+1228
+1028
+1529
+1762
+1139
+1318
+1421
+1554
+1272
+1300
+1381
+1324
+1342
+2224
+2276
+2409
+3548
+1607
+1704
+1805
+1809
+1983
+2560
+2294
+2167
+2256
+3311
+2411
+2693
+2618
+2572
+2624
+4005
+2596
+2642
+2666
+4264
+2949
+5478
+3412
+3509
+3416
+3513
+4065
+3614
+3976
+4150
+6572
+4461
+4578
+4667
+4983
+5007
+5168
+7417
+9168
+8621
+6054
+5238
+5308
+8821
+8240
+6361
+9561
+6828
+12390
+6929
+9072
+7590
+8075
+8126
+9816
+9039
+9128
+9674
+9650
+18729
+17558
+13778
+16315
+12237
+11669
+10546
+16502
+12136
+19795
+15956
+13290
+13757
+14418
+14519
+15004
+16629
+17203
+17891
+18802
+28476
+25065
+28297
+19324
+21319
+25994
+27047
+40643
+22215
+22682
+36693
+23836
+31633
+25426
+33552
+34328
+27708
+33321
+28937
+31148
+53541
+61758
+51158
+37215
+42638
+41539
+42006
+43534
+43160
+76223
+81249
+44897
+46518
+46051
+48108
+49262
+51544
+53134
+54363
+64700
+56645
+58856
+84689
+68363
+72687
+88431
+82112
+89211
+78754
+83545
+84699
+85166
+86694
+97523
+142401
+90948
+91415
+104753
+94159
+131543
+100806
+151441
+122726
+177642
+145856
+127219
+143555
+150475
+141050
+176277
+167185
+205973
+162299
+163920
+168244
+323498
+180853
+209420
+234503
+182363
+185107
+218634
+198912
+244361
+249945
+305089
+352292
+321638
+294404
+268269
+369109
+284605
+291525
+317327
+367156
+331105
+326219
+330543
+346283
+478864
+363216
+379765
+381275
+367470
+384019
+403741
+719762
+443273
+628380
+518214
+598812
+693689
+559794
+552874
+695328
+576130
+601932
+697699
+643546
+747235
+656762
+710308
+761040
+709499
+1458739
+730686
+1111961
+748745
+751489
+787760
+1078008
+1377125
+961487
+1071088
+1471348
+1269293
+1246563
+1112668
+1311431
+1219676
+1178062
+1353045
+1300308
+1353854
+1366261
+2096947
+1419807
+1440185
+1458244
+1479431
+1801774
+1500234
+2489493
+1749247
+1858848
+2845692
+2181163
+2032575
+2183756
+2290730
+2332344
+2478370
+2397738
+2519984
+3644584
+2531107
+2653353
+3844631
+2786068
+2859992
+3249481
+3241959
+2898429
+5647219
+3338279
+4269231
+4216331
+3930410
+4563682
+3891423
+4213738
+4323305
+4364919
+4474486
+4769100
+4852328
+6035549
+5183806
+5391099
+5429536
+5184460
+5439421
+9880180
+5646060
+5758421
+6140388
+6236708
+6789852
+7229702
+7268689
+8253715
+9904022
+7821833
+8105161
+13409077
+8982838
+14140710
+8839405
+10830520
+9621428
+10887877
+15050964
+12698225
+17067228
+10613996
+15051535
+15319601
+14058541
+11404481
+17172711
+13026560
+13466410
+23890940
+14498391
+18099209
+17236553
+15926994
+31508286
+45648996
+19727282
+21501873
+19453401
+29109505
+20235424
+25938841
+22018477
+25112387
+23640556
+24080406
+24431041
+24870891
+25463022
+25902872
+27331475
+26492970
+34968283
+33701834
+30425385
+40437232
+33163547
+36963835
+35380395
+27911108
+39180683
+39688825
+51991514
+50575409
+42253901
+43875980
+49103578
+45659033
+60194804
+51551664
+50573376
+63588932
+62866707
+51365894
+52395842
+53824445
+61873365
+58336493
+93619795
+61074655
+91500040
+63291503
+67599933
+104127266
+67091791
+142073416
+118643455
+86129881
+145171459
+145324485
+89535013
+107440071
+113239259
+108909869
+103947506
+114162308
+117413377
+103761736
+169984524
+106220287
+205364919
+119411148
+193569952
+185013310
+124366158
+262737862
+226851219
+134691724
+153221672
+170853527
+189891617
+205541029
+175664894
+193296749
+254234354
+208946161
+278293598
+209982023
+207709242
+381205923
+254102872
+252105101
+225631435
+230586445
+240912011
+243777306
+386796825
+379799688
+277587830
+383374136
+324075199
+287913396
+388794596
+328886566
+346518421
+418928184
+368961643
+417691265
+542016268
+416655403
+531690702
+433340677
+440568468
+438295687
+685445778
+471498456
+456217880
+612428260
+521365136
+484689317
+632571902
+565501226
+708686254
+606474396
+611988595
+717681162
+676707992
+982156629
+675404987
+715480064
+785617046
+888153859
+961933604
+1218724260
+849996080
+1052996728
+896786348
+1006054453
+977583016
+1146903443
+940907197
+1202370479
+1127839532
+1117261219
+1050190543
+1494628255
+1171975622
+1218462991
+1397605641
+1723735615
+1352112979
+1390885051
+2035153357
+1900186623
+1501097110
+1949783076
+1738149939
+1811929684
+1790903277
+2719560101
+1946976891
+2929190903
+2133893985
+1918490213
+3285531532
+2940520418
+2167451762
+2855411158
+2544818798
+4292633397
+2390438613
+4220657211
+2853210089
+2742998030
+3958355039
+4045915908
+2891982161
+3239247049
+3292000387
+3761712760
+4593561097
+3529053216
+3602832961
+3709393490
+3865467104
+4661488243
+4052384198
+7459904260
+4712270560
+7567681718
+6963655241
+4557890375
+4935257411
+5133436643
+5243648702
+8086943591
+5596208119
+5634980191
+5982245079
+6131229210
+6183982548
+6421035377
+6531247436
+7001393877
+7290765976
+9726997740
+7238446706
+8842830133
+7574860594
+7917851302
+8610274573
+8764654758
+10570237602
+9801539077
+9493147786
+9691327018
+16760681435
+11115681722
+11231188310
+10839856821
+11578453198
+17097926801
+11617225270
+12113474289
+21379992275
+14449098738
+12952282813
+13532641313
+16003101464
+14529212682
+19153313792
+14813307300
+24183471123
+15492711896
+16528125875
+18257802544
+18455981776
+19184474804
+24250637815
+20531183839
+20922515328
+33602412530
+24569508083
+22071045131
+23691927487
+23195678468
+23730699559
+28145351145
+40258825434
+27981740051
+34980282577
+26484924126
+43025489859
+29342519982
+34646025688
+30306019196
+34677186700
+40711596998
+59443300238
+47981337374
+46640553214
+37640456580
+44614442815
+48904255379
+41453699167
+42993560459
+45266723599
+45762972618
+45801744690
+46887605955
+83442201270
+50215623685
+86068141982
+57324260033
+56790943322
+55827444108
+113151704141
+72368009841
+105245044928
+64952044884
+74920462011
+72317643280
+78352053578
+89634113673
+79094155747
+80634017039
+82254899395
+84447259626
+86720422766
+87216671785
+97103229640
+91029696217
+91564717308
+92689350645
+163347339497
+106043067793
+114115203355
+186432846635
+112618387430
+138082343503
+120779488992
+137320054725
+242565099653
+163541415373
+137269688164
+147238105291
+150669696858
+157446209325
+159728172786
+161349055142
+177737246679
+173819616703
+171663931411
+308115906183
+273843376141
+182594413525
+184254067953
+197607785101
+205307738075
+249888075594
+218661455223
+226733590785
+305033556945
+233397876422
+271449185850
+426819167606
+274589742889
+408718874014
+342577426239
+503926481381
+308587160433
+451580622820
+466382612087
+387902151600
+333012986553
+345483548114
+354258344936
+638046543498
+415992289947
+381861853054
+366848481478
+389561806028
+416269240324
+656888360895
+501323333674
+445395046008
+855944418115
+504847062272
+604462172403
+732139232267
+920839352219
+696489312033
+675435641911
+641600146986
+820640957023
+678496534667
+846806881788
+687271331489
+743820150964
+921116302596
+771423659082
+834956852036
+748710334532
+1049857218411
+798131093378
+950242108280
+1467912971115
+861664286332
+1189215196972
+1132666377497
+1109309234675
+1146447209258
+1180282704183
+1353172506935
+1503264433318
+1374985846700
+1317035788897
+1320096681653
+1328871478475
+1365767866156
+1797048990068
+1917870868340
+1431091482453
+1520133993614
+1546841427910
+1569554752460
+1583667186568
+3037467723575
+1811906394612
+2059551342955
+2685864547809
+1970973521007
+1994330663829
+3629106095415
+2241975612172
+2462481741610
+3562815776273
+2727124132093
+5499949465185
+3237967549993
+2886590541357
+2648968160128
+2951225476067
+2977932910363
+2796859348609
+3000646234913
+4274388136222
+3579685336569
+3332040388226
+3116396180370
+3871457737567
+3395573581180
+6369508111801
+3782879915619
+3965304184836
+4212949133179
+4791190012438
+5700449291603
+8354005788711
+5678349608160
+5111449901738
+5376092292221
+5445827508737
+5535558701485
+5600193636195
+5774792258972
+5626901070491
+7178453496799
+5797505583522
+8841401089917
+6117042415283
+9498358808058
+7907586192808
+6448436568596
+6511969761550
+7360877766016
+11303159773988
+8574069928057
+9987741392151
+10647008603223
+14616193348889
+11795392023443
+10487542193959
+10557277410475
+10738350972229
+16513143231201
+12223228827568
+11894264077333
+16010328569608
+11374985895167
+11401693329463
+11424406654013
+11914547998805
+13158383349538
+12565478983879
+18243828592039
+27816303005189
+12960406330146
+15022506496653
+18099228738245
+21362727287318
+18561811320208
+20468334005390
+21385359575452
+21044819604434
+21225893166188
+27914836560664
+21295628382704
+21932263305642
+30785040147776
+22776679224630
+33608588403020
+26397492391820
+41338490544838
+56360997041491
+47442311996254
+42680987958156
+24480026982684
+27982912826799
+34186299496334
+33584317816861
+46544724147007
+31059635068391
+33121735234898
+36661040058453
+40494074625850
+57705859662887
+41513153609824
+42270712770622
+42340447987138
+44708942530272
+43227891688346
+44072307607334
+59519227626718
+75392448005520
+47256706207314
+50877519374504
+52462939809483
+66750739753306
+87936834218618
+97422243521511
+85063818870838
+55539662051075
+79001488045591
+74680374122184
+69782775293351
+64181370303289
+102796368258389
+88661397285973
+156941471148229
+82007228235674
+83783866380446
+83853601596962
+84611160757760
+87300199295680
+132009141825952
+153636376890313
+91329013814648
+178188816263909
+116644310112772