summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <tom@ScaryTerry.local>2020-12-15 13:14:58 +0100
committerGravatar Tom van der Lee <tom@ScaryTerry.local>2020-12-15 13:14:58 +0100
commit37aa8eec0498d7e8491084711132f16db9129a39 (patch)
treebfcf5bd4a5fe3d6a364db2ea2b65d60112d879a8
parent9d38cb75c2e23a173adc7e390777c50361c31436 (diff)
download2021-37aa8eec0498d7e8491084711132f16db9129a39.tar.gz
2021-37aa8eec0498d7e8491084711132f16db9129a39.tar.bz2
2021-37aa8eec0498d7e8491084711132f16db9129a39.zip
Added day 9
-rw-r--r--aoc/__main__.py17
-rw-r--r--day9/__init__.py63
-rw-r--r--day9/example.txt20
-rw-r--r--day9/input.txt1000
4 files changed, 1098 insertions, 2 deletions
diff --git a/aoc/__main__.py b/aoc/__main__.py
index 8c6a21e..efb215d 100644
--- a/aoc/__main__.py
+++ b/aoc/__main__.py
@@ -2,6 +2,8 @@ import argparse
2import os 2import os
3import sys 3import sys
4import importlib 4import importlib
5from typing import Tuple, List
6
5 7
6def day(assignment_part: str) -> str: 8def day(assignment_part: str) -> str:
7 return { 9 return {
@@ -9,6 +11,9 @@ def day(assignment_part: str) -> str:
9 '2': 'Two', 11 '2': 'Two',
10 }[assignment_part] 12 }[assignment_part]
11 13
14def kwargs(kwarg: str) -> List:
15 return kwarg.split('=')
16
12parser = argparse.ArgumentParser(description='Advent of Code') 17parser = argparse.ArgumentParser(description='Advent of Code')
13 18
14parser.add_argument('day', type=str, help='Assignment day') 19parser.add_argument('day', type=str, help='Assignment day')
@@ -17,6 +22,7 @@ parser.add_argument(
17 help='Assingment part. Defaults to one.' 22 help='Assingment part. Defaults to one.'
18) 23)
19parser.add_argument('--example', default=False, action='store_true') 24parser.add_argument('--example', default=False, action='store_true')
25parser.add_argument('kwargs', type=kwargs, nargs='*')
20 26
21 27
22if __name__ == '__main__': 28if __name__ == '__main__':
@@ -24,6 +30,13 @@ if __name__ == '__main__':
24 assignment_day = importlib.import_module(args.day) 30 assignment_day = importlib.import_module(args.day)
25 31
26 Assignment = getattr(assignment_day, f'Assignment{args.part}') 32 Assignment = getattr(assignment_day, f'Assignment{args.part}')
27 assignment = Assignment(path=os.path.dirname(assignment_day.__file__)) 33 assignment = Assignment(
34 path=os.path.dirname(assignment_day.__file__),
35 **dict(args.kwargs)
36 )
28 37
29 print(assignment.run(input=assignment.read_input(example=args.example))) 38 print(
39 assignment.run(
40 input=assignment.read_input(example=args.example),
41 )
42 )
diff --git a/day9/__init__.py b/day9/__init__.py
new file mode 100644
index 0000000..0cf3770
--- /dev/null
+++ b/day9/__init__.py
@@ -0,0 +1,63 @@
1from typing import Any, Iterator, List
2
3from aoc import BaseAssignment
4
5
6class Assignment(BaseAssignment):
7 def __init__(self, path, preamble_length=25):
8 super().__init__(path)
9 self.preamble_length = int(preamble_length)
10
11 def read_input(self, example=False) -> List:
12 return list(super(Assignment, self).read_input(example))
13
14 def parse_item(self, item: str) -> Any:
15 return int(item)
16
17
18class AssignmentOne(Assignment):
19 def find_invalid(self, input: List, preamble_length: int = 25) -> int:
20 preamble = sorted(input[:preamble_length])
21 to_check = input[preamble_length]
22
23 front_position = 0
24 end_position = preamble_length - 1
25
26 while True:
27 if front_position is end_position:
28 return to_check
29
30 sum = preamble[front_position] + preamble[end_position]
31
32 if sum > to_check:
33 end_position -= 1
34 elif sum < to_check:
35 front_position += 1
36 else:
37 break
38
39 return self.find_invalid(input[1:], preamble_length=preamble_length)
40
41 def run(self, input: List) -> int:
42 return self.find_invalid(
43 input, preamble_length=int(self.preamble_length))
44
45
46class AssignmentTwo(AssignmentOne):
47 def find_weakness(self, input: List, invalid_nr: int) -> int:
48 for index, current_num in enumerate(input):
49 if index < 2:
50 continue
51
52 items_to_check = input[:index]
53 sum_list = sum(items_to_check)
54
55 if sum_list > invalid_nr:
56 break
57 elif sum_list == invalid_nr:
58 return min(items_to_check) + max(items_to_check)
59
60 return self.find_weakness(input[1:], invalid_nr)
61
62 def run(self, input: List) -> int:
63 return self.find_weakness(input, super().run(input))
diff --git a/day9/example.txt b/day9/example.txt
new file mode 100644
index 0000000..cda4246
--- /dev/null
+++ b/day9/example.txt
@@ -0,0 +1,20 @@
135
220
315
425
547
640
762
855
965
1095
11102
12117
13150
14182
15127
16219
17299
18277
19309
20576 \ No newline at end of file
diff --git a/day9/input.txt b/day9/input.txt
new file mode 100644
index 0000000..e9932d5
--- /dev/null
+++ b/day9/input.txt
@@ -0,0 +1,1000 @@
118
219
346
414
529
645
740
847
925
1043
1136
1222
1321
144
1532
1633
1737
1838
1926
202
2142
2215
235
2413
2531
269
276
2830
297
3014
3110
328
3369
3411
3512
3625
3716
3817
3922
4019
4118
4220
4351
4421
4524
4623
4726
4815
4960
5013
5129
5227
5344
5442
5528
5638
5730
5836
5931
6032
6133
6234
6335
6437
6555
6639
6740
6870
6941
7043
7178
7258
7373
74101
7560
7656
7757
7875
7959
8067
8171
8261
8368
84110
8569
8680
8772
88112
8976
9079
91144
92129
93114
9484
9599
96113
97132
98115
99148
100116
101118
102120
103128
104126
105187
106130
107190
108152
109229
110141
111151
112155
113299
114209
115163
116239
117183
118197
119331
120212
121228
122270
123231
124234
125236
126244
127246
128355
129256
130392
131271
132342
133292
134360
135296
136306
137318
138346
139602
140375
141380
142395
143534
144456
145440
146459
147470
148502
149478
150480
151490
152517
153527
154548
155563
1561082
1571136
158896
1591044
160614
161693
162698
163988
164755
1651065
1661352
167835
168899
169915
170942
171929
1721173
173958
1741043
175970
1761007
1771075
1781090
1791529
1801750
1811663
1821307
1831312
1841533
1851369
1861391
1871705
1881590
1891654
1901734
1911764
1921805
1932923
1942649
1951936
1961899
1972633
1981928
1992681
2001977
2013956
2022165
2032981
2042619
2053926
2062676
2072840
2082703
2094292
2104547
2114383
2124628
2133244
2143388
2153539
2166659
2176602
2185143
2193827
2205316
2218558
2223905
2235322
2244142
2254817
2264784
2279069
2285379
22911206
2305516
2317536
23210701
2336242
2347293
2356632
23615190
23711260
2387366
2397732
2407681
2417969
2428959
2439521
2448611
2458047
24610139
2478689
24816605
2498926
25010300
25110163
25210895
25315262
25412148
25513925
25612874
25715025
25813974
25919229
26014313
26115047
26215098
26315335
26415413
26516370
26616016
26716658
26827899
26916736
27018852
27135245
27217615
27319089
27419821
27529984
27623769
27723043
27830309
27931705
28026799
28126848
28235105
28328287
28430382
28542183
28630748
28730433
28831351
28931429
29032386
29132674
29233394
29344635
29434351
29550281
29636704
29760293
29869456
29942864
30049891
30146812
30249842
30353647
30455086
30585047
306119298
30760961
30875099
30962099
31073297
31194916
31261784
31362780
31463815
31575250
31696654
31767745
318104928
31971055
320111242
321133358
32289676
32392706
324151460
325134938
326101898
327158753
328108733
329122831
330122745
331123060
332293691
333126595
334165971
335142995
336125599
337130525
338146305
339194115
340402424
341179788
342138800
343226064
344160731
345182382
346235256
347191574
348194604
349210631
350224643
351269464
352231478
353231564
354245576
355253585
356248659
357252194
358276830
359256124
360411266
361371362
362299531
363354846
364654618
365340519
366330374
367459212
368343113
369695365
370431041
371386178
372402205
373405235
374435274
375456121
376633683
377504783
378477140
379586095
380701736
381500853
382508318
383654377
384555655
385640050
386741024
387642644
388673487
3891288060
390802325
391716552
392729291
3931497690
3941073685
3951273733
396821452
397807440
398891395
399912414
400933261
401977993
4021117190
4031140903
4041210054
4051009171
4061143497
4071063973
4081445843
4091195705
4101282694
4112428963
4121316131
4131390039
4141518877
4152095183
4162337706
4171536731
4181628892
4191698835
4202076758
4211712847
4221803809
4231824656
4241845675
4252962865
4261987164
4272073144
4282150074
4292152668
4302204876
4312207470
4322981529
4332478399
4343128369
4352908916
4364192040
4372706170
4382926770
4393055608
4403790973
4413165623
4423235566
4433327727
4443502644
4455835686
4463516656
4473628465
4483670331
4497131109
4505941736
4515116386
4524278020
4534913640
4544412346
4555186405
4564685869
4577293617
4585834539
4595615086
4606383335
4615632940
4629001309
4635982378
4646221231
4656401189
4666493350
4676563293
4686830371
4698188513
4707145121
47110907100
47210354082
4737948351
47411998421
4758690366
4769528732
4778963889
47815987022
47910318809
4809872274
48113376235
48216136864
48311248026
48412016275
48514983687
48617469257
48712203609
48812383567
48926433146
49012894539
49113056643
49213393664
49313975492
49415093472
49526270774
49616638717
49716912240
49818492621
49917654255
50022066601
50125440210
50218836163
50320191083
50421120300
50523264301
50626870031
50733085622
50823451635
50924399842
51024587176
51125098148
51251578243
51335095792
51434176943
51537980840
51635460265
51727369156
51829068964
51937160073
52033550957
52136146876
52262568016
52347164749
52441311383
52544778259
52645631293
52739027246
52852333265
52955938995
53048549783
53150321666
53277431413
53347851477
53448987018
53565898559
53652467304
53783311625
53868646749
53974487511
54069011222
54180925135
54276187319
54362619921
544115214565
545167547830
546136864130
54791943008
548131266670
54980338629
55083805505
55186878723
55287577029
553100184742
554117561005
555139346027
556116498226
55796838495
558129912153
559101454322
560115087225
561213051449
562150674830
563246410379
564154562929
565216668887
566138807240
567195425854
568142958550
569164144134
570267732572
571167217352
572167915658
573170684228
574185259827
575171382534
576244412872
577184415524
578265598456
579198292817
580211925720
581213336721
582334233094
583216541547
584240261562
585253894465
586369675351
587281765790
588335133010
589323222764
590505860018
591328218377
592354884270
593307102684
594331361486
595337901580
596379143072
597338599886
598342066762
599456803109
600355798058
601382708341
602594634061
603410218537
604536559485
605425262441
606429878268
607547903033
608523644231
609535660255
610585255951
611753480818
612588868474
613630325448
614662900742
615788164595
616635321061
617638464170
618645004264
619669263066
620734941130
621680666648
622694397944
6231055587889
6241363661010
625738506399
626792926878
627945878792
6281277125049
6291108900182
6301357298686
6311298221803
632776203571
6331275329712
6341124528729
6351174124425
6361343567390
6371219193922
6381310992096
6391759849790
6401527868008
6411428247939
6421283468434
6431314267330
6441349929714
6451375064592
6461917455607
6472517415725
6481531433277
6491514709970
6502328094104
6512959681216
6521722082363
6532811336442
6541885103753
6551900732300
6562126133285
6571950327996
6582884378519
6592552776668
6602549189017
6612502662356
6622530186018
6632594460530
6642633398148
6652597735764
6662658533026
6675542911545
6682664197044
6692724994306
6704032125695
6713046143247
6724048849002
6733236792333
6743399813723
6753607186116
6766636606056
6773622814663
6786048128775
6796109848472
6803851060296
6814076461281
6827094992249
6835032848374
6845051851373
6855079375035
6865097122886
6875124646548
6885192196294
6895231133912
6906843978449
6915322730070
6925771137553
6937268918028
6947448662725
6956445956970
6967122604528
6976859606996
6987006999839
6997022628386
70010068771633
70114029628225
7027473874959
70310514926364
70413866606835
7057927521577
7069109309655
70710084699747
70812705008871
70912219727414
71010176497921
71110316842842
71210355780460
71312253762298
71410553863982
71519522680326
71621340481794
71712630744549
71813305563966
71913452956809
72013468585356
72113882235382
72214480874798
72314934521416
72417199126307
72519663173637
72615401396536
72716583184614
72817036831232
72923021851713
73018104019498
73132584894296
73224022449338
73337475406147
73430076544308
73520672623302
73637709454534
73720909644442
73822807626280
73923184608531
74025936308515
74126083701358
74227187799348
74328240085382
74426921542165
74528403106772
74628363110180
74729415396214
74846507126270
74948026283817
75035140850730
75140058682945
75241059280570
75343480249582
75443694475015
75538776642800
75649268309889
75746993345800
75841582267744
75943717270722
76055590906120
76149272754622
76244094252973
76371720334964
76449120917046
76592815392061
76653005243523
76754109341513
76891506533399
76955284652345
77079835923370
771104405569391
77276408742014
77391720758832
77473917493530
77578835325745
77685769988600
77780358910544
778140841675878
779124053385559
780113302602708
781120126012736
78285299538466
783123190248152
784142088146683
785125529659060
78693367007595
787236492850860
788107114585036
789102126160569
790108289895868
791130518083527
792174235354249
793165424026929
794140584190811
795170556084577
796150326235544
797161708280480
798192137928453
799180961486314
800205425551202
801166128899144
802165658449010
803178666546061
804200481592631
805233951198406
806307551711771
807187425699035
808306713089955
809201656903463
810195493168164
811209240745605
812366092245096
813238807979395
814210416056437
815366610491775
816302292471291
817290910426355
818374535389217
819311140275388
820312034516024
821331787348154
822327366729490
823402138496094
824344324995071
825344795445205
826353084148045
827387907291666
828374159714225
829491392018986
830434301147559
831382918867199
832389082602498
833503949374754
834397150071627
835715274021156
836419656802042
837449224035832
838512708527728
839612554552531
840593202897646
841776673885311
842685675664605
843623174791412
844638507004878
845736003015244
846659154077644
847886868241953
848733878047703
849763981797113
8501269787109152
851750234219672
852763242316723
8531248711542972
854780068938826
8551048236680142
8561862990006798
8571418575943704
8581620746289656
8592003665156855
8602198644882530
861868880837874
8622005790124396
8631205757450177
8641216377689058
8651297661082522
8661324182669483
8673868780131194
8681261681796290
8691372385052581
8701393032125347
8711409388297316
8721497120364426
8731484112267375
8741513476536395
8751530303158498
8761543311255549
8771632123154597
8781648949776700
8791828305618968
8802532348947517
8812241265890455
8822946610859222
8832074638288051
8842193063507357
8852517830614574
8862085258526932
8872422135139235
8882585864465773
8892514038771580
8902654713921637
8912893804950887
8922634066848871
8932745794063665
8942765417177928
8954424108109385
8962893500564691
8972981232631801
8987009972575158
8993043779694893
9003073614414047
9013175434410146
9026249048824193
9034606987235568
9044250440758203
9054827130356228
9064159896814983
9077588219867369
9084599297298512
9097672911712559
9104507393666167
91110418705776224
9125076849060872
9135099903237353
9145689473181726
9155527871799758
9165399484026799
91714682884287717
9185511211241593
9195658917742619
9207497722523432
9215874733196492
9226025012326694
9236117394108940
9247425875168349
9257233511229030
9267335331225129
9278410337573186
9289327289819075
92910127169098270
9308987027171211
93112734815251928
93219190277953884
9339906877692966
93422180606811149
9359584242727039
93612825359195148
93710176752298225
93815596350874692
93910910695268392
94011170128984212
94111058401769418
94211385944438085
94311533650939111
94411683930069313
94514435349899880
94613210064421621
94728298000920089
94813350905337970
94921660820037381
95016560801048105
95116919573952168
95220713234257160
95318314316990286
95418571269898250
95524120759690013
95619491120420005
95719760995025264
95820083629991191
95920494937995431
96020642644496457
96129372718759704
96221235154067643
96331146939463349
96431469574429276
96539831651859200
96631553339764849
96723069874507398
96826560969759591
96945904924329156
97050681729449604
97132842025757975
97230270479290138
97331665222328256
97433480375000273
97534875118038391
97655785982018269
97740255933020695
97836885586888536
97938062390318255
98039252115445269
98139844625016455
98240403639521721
98347055907755022
98467698552251479
98541877798564100
98653340353797536
98759297544385898
98849630844266989
98954539448936674
99062321989952667
99154623214272247
99283941494643558
99372686650774430
99463112505048113
995113794234497717
996123786119660013
99768355493038664
99888215471835927
99998549659831167
100071760704926927 \ No newline at end of file