summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2020-12-09 15:49:37 +0200
committerJustin Wernick <justin@worthe-it.co.za>2020-12-09 15:49:37 +0200
commit6efb8d0afb70e3fbd06dca5ec4e2bd8283b617a0 (patch)
treeae4431254679c28bb827e56782b19701f0811b42
parent60f0fce822527372135ead5ca2a4843ee9b8d31c (diff)
Day 9
-rw-r--r--inputs/day_9.txt1000
-rw-r--r--src/bin/day_9.rs120
2 files changed, 1120 insertions, 0 deletions
diff --git a/inputs/day_9.txt b/inputs/day_9.txt
new file mode 100644
index 0000000..31690cc
--- /dev/null
+++ b/inputs/day_9.txt
@@ -0,0 +1,1000 @@
+37
+7
+16
+34
+48
+24
+5
+40
+30
+35
+2
+45
+19
+33
+36
+49
+32
+1
+44
+21
+25
+3
+11
+38
+15
+4
+6
+7
+8
+16
+9
+10
+5
+12
+13
+17
+14
+35
+18
+19
+20
+23
+36
+53
+22
+21
+41
+28
+11
+24
+15
+25
+26
+27
+29
+64
+50
+31
+30
+34
+32
+44
+33
+47
+74
+35
+66
+37
+36
+38
+45
+39
+40
+41
+42
+57
+51
+55
+68
+70
+71
+72
+61
+88
+65
+75
+140
+77
+73
+82
+110
+81
+74
+116
+86
+80
+112
+83
+92
+128
+141
+106
+120
+146
+126
+147
+133
+134
+138
+139
+156
+209
+162
+160
+154
+155
+233
+216
+163
+313
+298
+273
+189
+198
+226
+253
+232
+265
+259
+260
+316
+267
+272
+277
+449
+428
+314
+309
+471
+318
+379
+430
+458
+387
+683
+475
+415
+421
+486
+525
+544
+575
+532
+519
+632
+539
+590
+739
+586
+623
+627
+688
+696
+697
+804
+1072
+1119
+940
+808
+896
+836
+990
+907
+1107
+1436
+1114
+1129
+1051
+1058
+1754
+1385
+1644
+1459
+1250
+2686
+1324
+2442
+1504
+1501
+1612
+1704
+1915
+1715
+1732
+1826
+1897
+2048
+2431
+2109
+3004
+2165
+2825
+2301
+3039
+2862
+3145
+2574
+2709
+2751
+3447
+4043
+3005
+4198
+3113
+3612
+4910
+3629
+3541
+4306
+4157
+4466
+4349
+4274
+5113
+5027
+5446
+6186
+7889
+8522
+5436
+5854
+5283
+5864
+6292
+6118
+6546
+6617
+6742
+6654
+7241
+7170
+7698
+8431
+11231
+8506
+8623
+9301
+9387
+10140
+10310
+12518
+11137
+10719
+11147
+11290
+11401
+11575
+12156
+12410
+12664
+16757
+13271
+13396
+19611
+14411
+14868
+17129
+16937
+29793
+17807
+17924
+18688
+27532
+23665
+21029
+21866
+23954
+22009
+22976
+30217
+34022
+25986
+27807
+29601
+30400
+26667
+27682
+28264
+40602
+29279
+31805
+34066
+41472
+45606
+41589
+47696
+48711
+55214
+43038
+57543
+69154
+45963
+77223
+66588
+68866
+55265
+115299
+58207
+54349
+95816
+68284
+62330
+115750
+61084
+100312
+90012
+116995
+100820
+84627
+139841
+94674
+89001
+97387
+98303
+190490
+178080
+109614
+112556
+113472
+115433
+116349
+225364
+116679
+122633
+194241
+123414
+145711
+158471
+150085
+173628
+268085
+179301
+198099
+182014
+183675
+186388
+391499
+207001
+207917
+222170
+223086
+229235
+323350
+228905
+530351
+233028
+239312
+262390
+304647
+308556
+389397
+295796
+344859
+415293
+352929
+380113
+430087
+405100
+365689
+370063
+393389
+545520
+431003
+530726
+445256
+456114
+462263
+533552
+524701
+472340
+846296
+535108
+674245
+763452
+604352
+640655
+758059
+697788
+718618
+733042
+1129053
+759078
+838645
+876259
+824392
+918090
+887117
+893266
+907519
+901370
+1591316
+934603
+1469711
+1359500
+1742482
+1139460
+1175763
+1245007
+1302140
+1362411
+1338443
+1571687
+1416406
+1963852
+1557434
+1583470
+1597723
+1663037
+1769525
+2032726
+1780383
+1827869
+3212193
+1808889
+2351009
+2908044
+2538174
+2384467
+3385303
+3352070
+2315223
+2420770
+2547147
+2640583
+2778817
+3886910
+2973840
+2999876
+4336251
+3140904
+3181193
+5009062
+4606686
+4229659
+5601963
+3589272
+3636758
+4124112
+4159898
+4666232
+4699690
+4735993
+4805237
+6183905
+4862370
+4955806
+4967917
+5187730
+5419400
+7370563
+5973716
+6589148
+7226030
+6322097
+10580402
+6817951
+7713384
+11509827
+7749170
+7760870
+7796656
+8824488
+8860105
+8895891
+10281770
+12554407
+15536208
+9667607
+16645061
+13548127
+9923723
+13692660
+10607130
+22222014
+12295813
+14531335
+12911245
+20092469
+19476293
+14567121
+15678056
+15462554
+15510040
+18356300
+30972594
+19141875
+34623804
+17755996
+18563498
+19591330
+25386277
+20274737
+20530853
+27240787
+22902943
+22219536
+30198460
+30667241
+25207058
+26827148
+27442580
+43742577
+32323117
+30029675
+34158451
+33218550
+33266036
+33866340
+36112296
+37347326
+36319494
+52721949
+44977607
+38154828
+39866067
+40805590
+42494273
+43433796
+68014567
+45122479
+55485572
+58425608
+52034206
+52649638
+76652724
+57472255
+128686930
+95216222
+73666820
+66484586
+67084890
+69378332
+91338595
+72431790
+137710495
+74474322
+78020895
+127917362
+84988546
+84239386
+83299863
+85928069
+111607065
+132899930
+135078332
+186342970
+104683844
+109506461
+110121893
+123956841
+124557145
+161700808
+256917030
+186081387
+146906112
+136463222
+141810122
+150452685
+158713708
+207374252
+157774185
+167539249
+168288409
+193421756
+169227932
+269381250
+197535134
+229240989
+214190305
+244584793
+214805737
+219628354
+177777905
+246585115
+286915907
+261020367
+278273344
+311038054
+335827658
+283369334
+292262807
+360961005
+396780238
+437669659
+325313434
+365074383
+448869343
+361710165
+347005837
+608295280
+392583642
+422362698
+391968210
+397406259
+424363020
+456051249
+503091339
+461147239
+893720908
+561642678
+589311398
+570536151
+675337544
+619196992
+575632141
+639268644
+707966842
+708716002
+672319271
+687023599
+821769279
+754293807
+738974047
+739589479
+1483032306
+1031683390
+789374469
+1036779380
+1233961949
+880414269
+1133466510
+964238578
+1022789917
+1809594090
+2272406775
+1164943539
+1209804795
+1214900785
+1194829133
+1247951412
+1461693740
+1359342870
+1791073187
+1411908750
+1651262177
+1478563526
+1493883286
+1821057859
+1776368859
+1753613047
+1954318008
+2156256427
+1844652847
+1903204186
+3010605047
+1987028495
+2129182117
+2187733456
+3196833290
+3284956473
+2359772672
+2404633928
+2409729918
+2442780545
+2607294282
+2771251620
+2837906396
+3920255304
+2905792036
+3747857033
+3323216373
+3247496333
+4630514001
+4802553217
+3598265894
+4510498468
+4307838114
+3831681342
+4174761951
+4116210612
+7346122927
+4316915573
+4597463374
+5652130261
+4847414473
+4764406600
+6236315270
+5050074827
+5214032165
+7618666093
+6369517514
+8918763829
+6153288369
+6229008409
+6570712706
+6921482267
+7422258284
+7714476506
+7906104008
+7915181467
+8148596915
+11663038500
+8679095815
+8290972563
+8433126185
+8914378947
+9081322173
+11168176080
+9611821073
+13362629080
+9814481427
+10264106992
+11203363196
+12382296778
+14284698981
+12522805883
+12724001075
+16834819698
+23726169079
+13492194973
+17995701120
+15136734790
+15620580514
+15821285475
+22196778205
+16439569478
+16724098748
+23306676400
+17514448358
+17347505132
+18728860374
+18693143246
+19426302500
+19875928065
+21467470188
+21017844623
+32651183148
+24905102661
+28002877292
+31949108383
+31860833538
+40450267827
+29313480448
+28628929763
+38018063680
+33616281634
+34238547106
+31441865989
+59143411073
+33163668226
+36040648378
+34071603880
+48956314347
+36243308732
+60070795752
+49020721915
+55083751822
+39302230565
+40893772688
+70744096554
+45922947284
+52907979953
+54218583109
+59951985675
+63385084328
+57942410211
+62477148674
+93101815502
+61792597989
+65680413095
+64605534215
+93919014663
+67235272106
+69204316604
+69406976958
+70112252258
+70314912612
+75545539297
+110813319904
+88322952480
+80196003253
+150800101154
+118824117324
+110098089292
+98830927237
+100141530393
+112160993320
+114170568784
+129027870095
+157729929438
+199136272384
+124269746663
+186062344652
+127473011084
+130285947310
+133809850819
+136439588710
+183374885388
+138611293562
+139519229216
+250332549120
+194366572037
+204465749916
+316348291962
+168518955733
+210481950563
+314563839208
+232640778056
+238752823955
+198972457630
+212302523713
+250610157494
+438833585871
+251742757747
+266992240300
+266084304646
+705825826171
+257758958394
+270249439529
+264095798129
+272421144381
+404848522600
+441133843782
+278130522778
+308038184949
+362885527770
+367491413363
+379000906296
+380821479446
+502352915241
+409454408193
+411274981343
+431613235686
+437725281585
+478386828359
+484723668094
+633575718009
+509501716141
+703809586231
+523843263040
+852408825125
+521854756523
+528008397923
+534345237658
+766448713370
+653242623827
+778766394706
+586168707727
+641016050548
+688859664395
+730376941133
+869844328604
+889661809702
+790275887639
+820729389536
+841067643879
+842888217029
+955456498726
+1110011970767
+1070892375821
+1573265158162
+1227184758275
+1031356472664
+1056199994181
+1045698019563
+1252231697656
+1162870807071
+1169024448471
+1264722178791
+1479135552034
+1239411331554
+1275028372122
+1427236351606
+1510860379152
+2589147522801
+2054998066430
+2180904346588
+1611005277175
+1631343531518
+1776185888262
+1683955860908
+1798344715755
+2077054492227
+2087556466845
+2116590395384
+3761010353135
+2101898013744
+2194227279735
+2208568826634
+2402282138625
+3878083902006
+2331895255542
+4408949747769
+3458949458526
+2514439703676
+2666647683160
+2702264723728
+2938096730758
+3121865656327
+3460141749170
+3242348808693
+4325159222018
+5090292990044
+3315299392426
+3482300576663
+5330434482961
+3992571995490
+4631030099060
+6603177027504
+4218488409128
+4296125293479
+6148948259823
+4402796106369
+6364214465020
+5884582715288
+4846334959218
+4998542938702
+5181087386836
+6017564116154
+5216704427404
+9033826205429
+5640361454486
+8698921399848
+6582007405497
+6557648201119
+9936627949262
+7718095498795
+9199882107714
+7307871387916
+9961464582021
+8838906954708
+10215247366106
+9477365058278
+8514613702607
+20151875315368
+9142460252697
+9401339045071
+9583883493205
+15142551968857
+19249073571535
+11234268543558
+10179630325538
+10397791814240
+15983346450568
+26198593816674
+12198009655605
+12948232842402
+15781889513211
+15724467658194
+13865519589035
+15025966886711
+17679560080816
+15822485090523
+16146778342624
+25939715024300
+30924441482068
+24924349765908
+17657073955304
+18098497195812
+18543799297768
+28913828624374
+18985222538276
+19763513818743
+20577422139778
+31707814108762
+21413898869096
+34325688810979
+27922477313799
+25146242498007
+38256982220594
+32409318886803
+31545079669851
+28891486475746
+53838178390282
+29688004679558
+30848451977234
+39957698166864
+50472467153603
+33803852297928
+68570964349415
+57805315100120
+37083719734088
+35755571151116
+36642296493580
+63239517435353
+38748736357019
+66735007697782
+44909756316750
+79219213969216
+46560141367103
+49336376182895
+57555561384810
+64647057626862
+78706434523883
+115861510462796
+62097323566361
+114974785120332
+78227862658641
+60536456656792
+66771724413646
diff --git a/src/bin/day_9.rs b/src/bin/day_9.rs
new file mode 100644
index 0000000..b5a02e6
--- /dev/null
+++ b/src/bin/day_9.rs
@@ -0,0 +1,120 @@
+use bevy::{app::AppExit, prelude::*};
+use std::fs::File;
+use std::io::{BufRead, BufReader};
+
+fn main() {
+ App::build()
+ .add_resource(WindowDescriptor {
+ title: "Advent of Code".to_string(),
+ width: 1920,
+ height: 1080,
+ ..Default::default()
+ })
+ .add_resource(ClearColor(Color::rgb(0., 0., 0.)))
+ .add_startup_system(setup_camera.system())
+ .add_startup_system(read_input_file.system())
+ .add_stage("add")
+ .add_system_to_stage("add", check_next_num.system())
+ .add_stage("remove")
+ .add_system_to_stage("remove", remove_oldest_line.system())
+ .add_stage("find_range")
+ .add_system_to_stage("find_range", find_contiguous_range.system())
+ .add_plugins(DefaultPlugins)
+ .run();
+}
+
+fn setup_camera(mut commands: Commands) {
+ commands.spawn(Camera2dComponents::default());
+}
+
+#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
+struct LineNum(usize);
+#[derive(Clone)]
+struct Num(i64);
+struct Active;
+struct Invalid;
+struct Done;
+
+fn read_input_file(mut commands: Commands) {
+ let f = File::open("./inputs/day_9.txt").unwrap();
+
+ for (line_num, line) in BufReader::new(f).lines().enumerate() {
+ let line = line.unwrap();
+ let line = line.trim();
+ let num = line.parse().unwrap();
+ commands.spawn((LineNum(line_num), Num(num)));
+ if line_num < 25 {
+ commands.with(Active);
+ }
+ }
+}
+
+fn check_next_num(
+ mut commands: Commands,
+ active: Query<With<Active, &Num>>,
+ next: Query<Without<Done, Without<Active, (Entity, &LineNum, &Num)>>>,
+) {
+ if let Some((min_entity, line, num)) = next.iter().min_by_key(|(_, line, _)| line.0) {
+ let mut is_sum = false;
+ for active_1 in active.iter() {
+ for active_2 in active.iter() {
+ is_sum = is_sum || (num.0 == active_1.0 + active_2.0)
+ }
+ }
+ commands.insert_one(min_entity, Active);
+
+ if !is_sum {
+ commands.insert_one(min_entity, Invalid);
+ println!(
+ "{} on line {} was not a sum of the previous lot!",
+ num.0, line.0
+ );
+ }
+ }
+}
+
+fn remove_oldest_line(
+ mut commands: Commands,
+ line_nums: Query<Without<Done, With<Active, (Entity, &LineNum)>>>,
+) {
+ if let Some((min_entity, _)) = line_nums.iter().min_by_key(|(_, line)| line.0) {
+ commands.remove_one::<Active>(min_entity);
+ commands.insert_one(min_entity, Done);
+ } else {
+ println!("All numbers cleared");
+ }
+}
+
+fn find_contiguous_range(
+ mut exit_events: ResMut<Events<AppExit>>,
+ line_nums: Query<(&LineNum, &Num)>,
+ invalid: Query<With<Invalid, &Num>>,
+) {
+ for invalid in invalid.iter() {
+ let mut nums: Vec<(LineNum, Num)> = line_nums
+ .iter()
+ .map(|(line, num)| (line.clone(), num.clone()))
+ .collect();
+
+ nums.sort_by_key(|(line, _num)| line.clone());
+ let nums: Vec<i64> = nums.into_iter().map(|(_line, num)| num.0).collect();
+
+ for min in 0..nums.len() {
+ for max in min + 2..nums.len() + 1 {
+ let sum: i64 = nums[min..max].iter().sum();
+ if sum == invalid.0 {
+ let min_in_range = nums[min..max].iter().min().unwrap();
+ let max_in_range = nums[min..max].iter().max().unwrap();
+ println!(
+ "Range found! {} + {} = {}",
+ min_in_range,
+ max_in_range,
+ min_in_range + max_in_range
+ );
+ }
+ }
+ }
+
+ exit_events.send(AppExit);
+ }
+}