Mistivia 1 год назад
Родитель
Сommit
087a111b34
45 измененных файлов с 11881 добавлено и 0 удалено
  1. 2 0
      advent-of-code/2022/.gitignore
  2. 25 0
      advent-of-code/2022/01/1.c
  3. 41 0
      advent-of-code/2022/01/2.c
  4. 2252 0
      advent-of-code/2022/01/input
  5. 30 0
      advent-of-code/2022/02/1.c
  6. 36 0
      advent-of-code/2022/02/2.c
  7. 2501 0
      advent-of-code/2022/02/input
  8. 50 0
      advent-of-code/2022/03/1.c
  9. 47 0
      advent-of-code/2022/03/2.c
  10. 301 0
      advent-of-code/2022/03/input
  11. 14 0
      advent-of-code/2022/04/1.c
  12. 14 0
      advent-of-code/2022/04/2.c
  13. 1000 0
      advent-of-code/2022/04/input
  14. 60 0
      advent-of-code/2022/05/1.c
  15. 64 0
      advent-of-code/2022/05/2.c
  16. 513 0
      advent-of-code/2022/05/input
  17. 26 0
      advent-of-code/2022/06/1.c
  18. 26 0
      advent-of-code/2022/06/2.c
  19. 0 0
      advent-of-code/2022/06/input
  20. 101 0
      advent-of-code/2022/07/1.c
  21. 106 0
      advent-of-code/2022/07/2.c
  22. 989 0
      advent-of-code/2022/07/input
  23. 106 0
      advent-of-code/2022/08/1.c
  24. 82 0
      advent-of-code/2022/08/2.c
  25. 100 0
      advent-of-code/2022/08/input
  26. 91 0
      advent-of-code/2022/09/1.c
  27. 106 0
      advent-of-code/2022/09/2.c
  28. 12 0
      advent-of-code/2022/09/Makefile
  29. 2001 0
      advent-of-code/2022/09/input
  30. 54 0
      advent-of-code/2022/10/1.c
  31. 62 0
      advent-of-code/2022/10/2.c
  32. 12 0
      advent-of-code/2022/10/Makefile
  33. 141 0
      advent-of-code/2022/10/input
  34. 16 0
      advent-of-code/2022/LICENSE
  35. 2 0
      advent-of-code/2022/README.md
  36. 55 0
      advent-of-code/2022/lib/crc32.c
  37. 9 0
      advent-of-code/2022/lib/crc32.h
  38. 106 0
      advent-of-code/2022/lib/htable.c
  39. 29 0
      advent-of-code/2022/lib/htable.h
  40. 72 0
      advent-of-code/2022/lib/pque.c
  41. 18 0
      advent-of-code/2022/lib/pque.h
  42. 396 0
      advent-of-code/2022/lib/rbtree.c
  43. 59 0
      advent-of-code/2022/lib/rbtree.h
  44. 130 0
      advent-of-code/2022/lib/str.c
  45. 24 0
      advent-of-code/2022/lib/str.h

+ 2 - 0
advent-of-code/2022/.gitignore

@@ -0,0 +1,2 @@
+*.out
+*.swp

+ 25 - 0
advent-of-code/2022/01/1.c

@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define BUFSZ 1024
+
+char buf[BUFSZ];
+
+int main() {
+    int maxval = 0;
+    int cur = 0;
+    FILE* fp = fopen("input", "r");
+    while (fgets(buf, BUFSZ, fp)) {
+        int len = strlen(buf);
+        char *end;
+        if (len <= 1) {
+            maxval = cur > maxval ? cur : maxval;
+            cur = 0;
+        } else {
+            cur += strtol(buf, &end, 10);
+        }
+    }
+    printf("%d\n", maxval);
+    return 0;
+}

+ 41 - 0
advent-of-code/2022/01/2.c

@@ -0,0 +1,41 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define BUFSZ 1024
+
+char buf[BUFSZ];
+
+int maxheap[3] = {0, 0, 0};
+
+void push(int val) {
+    int minidx = 0;
+    for (int i = 0; i < 3; i++) {
+        if (maxheap[i] < maxheap[minidx]) {
+            minidx = i;
+        }
+    }
+    if (maxheap[minidx] < val) {
+        maxheap[minidx] = val;
+    }
+}
+
+int main() {
+    int maxval = 0;
+    int cur = 0;
+    FILE* fp = fopen("input", "r");
+    while (fgets(buf, BUFSZ, fp)) {
+        int len = strlen(buf);
+        char *end;
+        if (len <= 1) {
+            maxval = cur > maxval ? cur : maxval;
+            push(cur);
+            cur = 0;
+        } else {
+            cur += strtol(buf, &end, 10);
+        }
+    }
+    printf("%d\n", maxheap[0] + maxheap[1] + maxheap[2]);
+    return 0;
+}
+

+ 2252 - 0
advent-of-code/2022/01/input

@@ -0,0 +1,2252 @@
+15560
+3906
+7076
+11980
+11508
+
+6558
+2256
+7294
+6566
+2686
+2566
+1724
+4811
+5427
+4278
+3756
+
+3761
+5599
+13187
+12558
+7425
+9269
+
+59658
+
+34175
+9643
+
+7664
+10224
+5541
+1378
+1784
+9347
+7692
+
+3786
+4260
+4416
+6345
+6750
+6415
+5467
+4898
+2017
+4486
+1198
+1960
+
+5658
+6168
+8088
+7749
+5890
+1980
+6014
+1136
+3138
+6706
+7041
+
+9197
+6997
+12644
+12609
+7630
+12928
+
+5278
+1423
+4822
+5148
+6387
+1280
+6431
+3847
+5886
+2714
+3725
+6331
+
+7537
+6965
+10079
+8904
+8639
+10679
+8964
+
+4426
+8844
+4298
+2872
+10619
+5095
+11321
+
+7983
+1883
+8608
+5411
+8970
+7877
+5185
+3210
+8719
+
+3726
+3043
+3978
+6669
+1915
+5948
+2128
+5272
+4685
+5797
+2780
+
+4455
+10134
+10707
+2796
+5472
+8624
+
+13936
+11059
+4848
+1048
+8927
+1803
+
+11965
+3983
+8834
+12932
+2111
+10667
+
+5514
+19118
+
+1826
+2484
+4686
+3811
+1299
+3538
+1627
+4180
+3148
+2443
+6232
+4090
+3027
+4364
+
+1020
+3031
+9049
+2732
+3458
+8787
+9492
+9701
+
+5906
+7234
+7874
+5787
+1655
+5841
+2750
+5057
+9669
+
+1140
+3392
+4846
+3957
+6946
+4821
+3588
+1057
+4216
+1144
+2890
+3662
+1540
+
+3698
+5290
+2586
+3501
+1821
+2820
+2461
+2845
+6578
+4592
+1466
+6074
+2504
+
+19816
+18541
+11141
+5032
+
+5590
+3329
+4171
+2892
+12100
+5972
+1724
+
+17476
+19863
+13061
+4386
+
+33812
+18398
+
+5890
+1064
+5749
+1412
+3070
+6709
+3800
+4723
+1089
+4192
+1091
+1471
+4713
+
+5084
+7475
+7106
+1875
+2739
+1993
+6165
+6561
+7111
+1303
+
+7468
+21796
+21866
+
+3911
+5476
+1367
+1577
+1467
+1060
+4483
+5238
+4105
+3136
+1532
+4264
+3204
+5462
+3876
+
+8364
+14450
+14332
+3541
+13331
+
+9091
+6310
+9529
+4620
+5059
+6688
+8179
+8584
+2553
+
+7402
+2012
+7624
+8083
+6666
+3490
+5614
+3019
+5193
+7597
+6552
+
+4302
+2265
+7656
+1942
+4555
+1615
+3915
+5434
+6508
+6625
+5846
+
+5772
+6231
+1812
+1957
+6098
+3782
+1848
+2277
+2882
+3210
+6302
+2426
+5997
+6310
+
+14434
+13021
+8328
+13835
+12947
+
+1216
+5442
+5716
+2342
+1011
+4880
+5949
+3828
+4304
+4874
+2085
+6421
+4405
+
+9510
+6968
+5101
+7321
+3347
+3293
+10879
+
+5188
+5964
+2984
+1706
+
+20672
+37318
+
+5974
+5965
+1260
+4969
+3253
+5584
+5497
+6233
+6524
+3688
+4387
+4127
+4828
+
+2956
+3797
+3089
+4455
+5154
+6442
+4646
+3283
+3776
+4414
+2020
+1674
+3043
+
+9455
+11013
+16855
+19985
+
+15368
+2665
+
+2999
+3619
+5491
+5732
+5377
+4391
+1755
+3432
+5521
+4505
+4011
+
+21948
+16277
+7128
+
+1347
+4476
+11439
+4661
+3502
+7437
+2377
+
+6311
+3373
+4464
+4897
+3652
+4541
+3298
+5555
+5857
+3907
+7189
+6632
+
+6414
+5832
+3955
+5130
+6051
+4802
+5035
+6431
+3640
+6095
+1635
+4952
+5221
+4335
+
+3002
+14005
+4145
+3420
+
+7983
+1964
+2082
+8026
+9507
+9217
+3629
+5959
+7426
+
+10126
+2355
+4405
+1378
+7177
+3219
+2342
+7863
+
+44652
+
+7649
+6206
+1085
+10197
+7450
+8017
+4837
+6312
+
+10909
+6433
+14990
+3896
+
+5355
+4352
+2045
+8484
+4405
+9329
+8372
+1619
+5168
+
+8097
+1858
+2411
+2083
+1643
+4948
+7125
+6335
+1652
+
+45906
+
+11636
+9662
+10106
+3149
+9273
+13836
+
+6955
+4410
+5645
+1155
+1458
+2290
+8066
+2391
+5613
+
+7445
+8999
+8635
+2826
+8256
+8982
+4585
+4454
+8742
+
+9621
+1911
+7452
+5259
+9423
+7777
+3371
+3661
+3852
+
+11601
+5289
+4041
+3276
+6801
+12534
+
+10492
+10488
+5884
+
+1632
+6831
+5315
+3645
+2799
+5785
+2861
+6392
+3087
+6155
+1890
+5164
+5378
+
+1425
+3685
+9030
+3794
+7019
+1924
+7764
+2025
+8194
+
+2923
+4253
+3398
+3503
+2932
+2925
+3320
+5082
+2560
+1250
+1967
+2945
+4956
+
+1768
+6471
+8112
+14472
+8324
+
+5248
+3253
+4095
+5022
+6471
+7092
+4721
+4232
+5073
+3500
+5180
+4305
+
+1197
+6009
+2187
+2197
+2275
+1214
+5930
+6211
+5998
+6121
+3772
+2985
+3289
+6056
+
+3308
+
+33494
+15970
+
+6732
+3509
+1185
+1845
+3794
+6393
+5326
+2705
+3781
+3165
+2103
+4238
+
+2120
+2494
+3774
+5136
+5104
+3656
+2057
+1417
+4810
+5456
+1250
+1153
+2157
+6075
+4771
+
+4597
+3068
+5511
+1740
+1303
+5663
+5853
+4552
+5624
+2497
+5309
+1994
+4553
+3982
+1865
+
+8069
+13345
+1455
+9953
+4700
+3273
+
+3865
+8222
+7625
+8588
+2059
+1093
+8909
+
+7297
+8678
+12357
+2883
+4772
+7946
+
+34055
+27695
+
+20638
+20962
+
+1174
+5972
+1498
+5360
+3685
+3998
+4387
+2478
+4911
+4100
+5801
+2493
+3493
+2634
+2506
+
+6252
+2505
+2727
+2523
+4909
+4007
+8754
+
+1456
+2733
+4585
+4755
+3141
+1688
+1588
+4016
+5811
+3366
+5885
+5759
+5074
+5673
+1323
+
+14097
+19903
+23703
+
+7578
+
+3670
+6561
+2303
+1743
+4462
+4098
+3807
+3708
+1166
+3175
+4669
+5168
+
+18247
+
+2828
+6474
+20318
+
+67746
+
+3765
+5241
+4097
+7326
+2722
+3162
+6852
+3866
+3756
+5276
+1443
+4438
+
+6431
+4595
+2444
+6637
+8234
+1806
+6058
+4693
+2031
+
+3657
+6577
+2398
+3448
+2152
+5140
+2785
+6921
+4164
+4151
+2586
+6209
+1768
+
+21363
+13712
+
+3905
+5016
+4959
+2079
+2511
+2139
+5739
+3182
+2591
+4904
+3737
+1415
+2010
+5121
+5789
+
+15126
+3348
+
+5353
+6053
+2843
+7232
+5474
+3666
+7730
+2458
+1728
+3863
+3675
+
+3050
+24669
+19533
+
+5079
+6540
+16362
+9648
+7673
+
+6773
+5129
+1417
+9009
+6015
+9018
+7378
+5809
+3425
+
+3128
+1637
+9395
+10326
+7258
+9681
+1247
+3877
+
+5473
+5667
+3444
+3415
+5342
+3899
+1769
+4814
+4794
+5852
+3130
+
+6741
+1427
+3789
+2515
+
+3184
+3463
+12868
+4655
+2820
+
+7596
+10444
+2931
+2257
+10050
+3901
+8157
+2150
+
+18037
+35280
+
+9392
+8364
+5688
+6412
+9898
+2655
+3819
+
+4905
+3813
+3577
+4976
+2679
+5501
+2034
+2151
+1199
+1854
+2849
+6018
+4626
+4015
+5266
+
+1106
+10869
+8893
+12031
+1220
+9418
+1543
+
+12986
+
+10545
+9650
+3034
+5526
+2054
+1221
+7804
+3322
+
+1444
+4917
+2089
+4204
+3783
+1096
+1907
+2056
+2716
+1327
+3435
+1415
+5479
+2985
+
+6988
+11893
+11900
+6583
+10560
+2724
+4254
+
+7799
+1033
+8090
+6455
+5592
+3900
+3398
+8318
+4429
+2792
+
+4204
+10329
+2183
+10442
+12443
+7405
+
+13377
+10917
+13049
+5370
+2566
+
+5775
+1318
+5876
+3093
+4587
+2406
+4520
+5460
+2069
+2494
+2028
+2015
+2090
+6039
+
+2073
+16023
+16574
+13782
+
+1389
+4404
+7837
+5483
+1706
+4742
+4561
+5126
+2093
+7573
+1884
+
+5151
+4634
+1744
+2924
+5511
+1953
+1953
+2623
+2713
+2352
+6389
+
+3559
+3400
+25742
+
+3716
+2545
+5516
+2264
+4980
+3934
+2463
+5027
+3391
+2590
+4755
+3513
+1304
+2332
+1595
+
+9866
+1910
+3337
+5758
+2210
+
+4991
+6319
+1593
+1309
+4799
+5419
+3190
+3962
+4912
+5482
+3138
+3764
+6835
+
+18167
+18454
+
+5211
+5798
+13472
+7588
+7035
+
+13965
+16316
+12336
+8957
+4412
+
+13259
+8263
+13243
+3863
+9766
+6576
+
+1887
+5567
+5526
+2264
+2901
+5771
+6867
+4064
+
+14468
+5429
+
+7063
+5999
+6195
+5450
+5516
+8394
+5282
+7507
+2893
+5811
+
+7781
+14609
+9582
+14581
+1279
+
+8994
+
+3368
+2133
+1408
+3624
+5617
+5771
+3829
+3954
+1087
+3953
+2055
+1102
+4041
+5672
+2096
+
+4152
+5419
+4919
+6153
+1071
+4806
+4948
+2963
+3299
+1151
+1162
+1355
+4221
+1850
+
+1724
+9385
+6415
+8655
+7962
+7653
+7240
+5011
+3357
+
+3827
+10562
+1452
+8263
+9786
+
+1840
+3264
+2367
+2586
+4573
+5019
+1935
+2218
+6338
+2242
+4475
+6435
+1177
+
+8104
+1130
+6302
+5206
+10941
+
+1684
+1611
+5391
+7397
+
+5957
+1823
+4893
+5396
+5139
+4847
+4918
+5275
+6539
+2528
+5496
+6352
+6843
+
+7719
+7601
+5678
+5079
+5758
+4053
+4013
+6279
+6761
+
+6503
+5429
+6214
+6124
+3199
+6610
+2731
+2539
+1719
+2855
+1103
+6906
+3659
+
+9973
+15476
+7485
+10873
+5774
+
+5447
+15860
+19169
+4464
+
+5019
+5126
+4728
+2449
+3641
+5396
+4436
+1637
+3488
+6172
+1668
+6358
+2390
+2625
+
+8055
+4009
+9553
+6765
+
+4387
+5962
+7311
+6818
+4096
+7220
+1046
+5027
+3100
+1840
+4365
+
+4620
+4425
+2845
+7731
+7948
+3460
+7601
+7782
+3991
+2449
+3800
+
+4088
+11836
+11119
+10334
+10739
+4828
+8118
+
+1813
+3477
+5254
+4184
+2953
+7075
+5634
+4760
+2707
+5742
+3032
+4796
+
+1787
+2280
+2297
+9784
+2680
+5823
+1754
+1812
+
+18881
+
+7205
+7773
+5652
+2453
+3172
+2329
+1725
+3461
+7463
+4209
+
+5619
+7887
+7820
+6936
+5110
+4710
+3842
+
+18049
+17759
+
+5293
+11205
+4416
+8605
+9442
+2209
+5330
+
+16752
+15441
+17352
+13788
+
+2601
+5675
+1686
+5744
+5749
+1629
+4635
+2450
+3769
+6055
+4438
+1162
+3617
+2353
+2515
+
+5049
+6887
+6658
+1720
+6656
+7402
+2899
+6894
+4390
+2060
+6356
+1127
+
+4611
+6778
+7136
+2112
+6635
+5053
+4227
+4390
+1987
+2012
+3417
+5872
+
+9181
+1521
+5042
+9369
+4946
+9189
+9416
+2913
+6870
+
+3238
+5593
+3367
+1862
+4780
+7151
+7883
+
+8189
+18026
+
+8105
+1826
+9154
+10100
+6519
+9408
+1775
+10450
+
+18762
+14980
+12309
+2708
+
+3339
+3042
+2980
+1100
+5695
+4333
+2300
+3258
+5027
+1328
+2175
+2022
+1303
+6230
+
+33095
+8781
+
+68878
+
+60287
+
+4318
+7920
+6657
+2241
+5524
+5569
+5524
+6898
+5066
+3867
+1661
+
+5708
+2336
+7749
+1878
+9678
+1749
+4137
+8995
+
+6877
+17912
+25014
+
+2240
+4793
+4121
+1553
+3435
+2819
+4977
+4560
+5593
+3188
+4500
+5009
+5885
+1344
+3522
+
+2512
+1780
+1114
+5229
+3962
+6021
+1437
+5576
+1843
+3121
+4686
+4061
+3946
+
+1512
+4809
+8443
+6593
+4613
+3926
+1491
+7313
+5393
+3511
+
+3236
+11107
+21185
+
+1981
+1046
+5733
+3510
+1553
+4658
+4646
+2725
+3372
+6101
+5704
+2537
+6446
+
+3395
+6752
+2779
+1500
+2318
+1907
+4558
+3416
+3407
+6723
+5254
+6741
+5931
+
+5097
+9438
+4695
+6362
+5432
+3485
+6272
+6672
+5489
+
+15058
+13736
+13765
+9756
+
+6425
+1949
+2010
+3527
+2256
+4379
+4775
+2615
+7952
+7775
+
+3879
+1452
+2571
+6670
+6189
+7139
+5385
+4566
+5205
+2971
+1234
+7356
+
+6905
+6962
+6172
+4143
+7003
+3553
+1037
+7084
+2839
+6400
+4483
+6842
+
+9893
+3836
+4626
+3189
+10027
+2570
+10469
+8591
+
+1973
+1518
+
+5724
+2198
+5988
+4564
+2630
+4603
+1633
+5176
+4912
+5712
+4087
+3392
+3365
+4465
+
+10064
+4379
+8577
+5308
+1106
+8348
+2346
+9509
+
+2258
+1408
+1862
+7613
+3274
+6660
+4709
+5976
+
+16440
+5053
+
+22062
+13058
+
+2104
+4538
+2460
+4321
+6536
+3797
+3735
+2599
+2041
+5566
+5141
+
+1195
+3896
+8470
+6161
+3564
+5070
+7084
+3189
+7195
+4593
+
+2466
+4102
+2494
+1212
+1949
+4936
+2865
+5877
+1693
+2044
+2722
+3832
+4071
+4500
+5124
+
+5900
+2328
+5889
+1193
+1599
+1477
+3793
+6745
+6793
+5757
+5862
+
+10156
+8629
+3162
+7678
+8967
+3806
+8719
+5891
+
+1342
+7365
+3226
+1337
+3869
+4368
+10066
+4605
+
+7303
+31682
+
+4315
+5528
+2856
+14681
+
+4799
+3474
+1751
+5508
+2865
+6459
+2466
+5017
+2637
+6782
+3147
+2204
+
+5799
+8701
+3217
+6818
+4577
+9361
+2864
+3633
+
+15236
+4169
+9857
+14313
+6546
+
+5108
+1927
+2651
+6266
+3191
+4536
+3152
+5383
+5536
+6887
+5601
+2775
+1611
+
+11673
+5009
+10433
+9493
+11598
+11673
+1282
+
+16672
+11433
+9380
+19375
+
+2655
+2097
+4561
+2095
+1849
+3690
+4885
+5045
+4868
+3373
+5851
+6352
+6252
+4644
+
+4570
+6382
+5690
+3591
+3337
+3807
+3814
+3721
+3012
+4039
+2617
+2696
+2702
+2817
+
+8511
+7582
+7791
+3902
+4125
+8118
+7959
+5202
+8760
+4735
+
+35854
+23095
+
+3722
+3745
+14691
+3942
+3362
+
+5653
+7968
+9676
+7085
+7591
+1769
+1806
+9484
+3171
+
+14194
+7809
+9540
+9984
+
+5645
+2723
+9273
+7274
+2442
+9243
+7103
+2191
+8975
+
+12597
+15917
+
+1818
+2964
+5683
+14864
+4390
+
+2358
+8756
+5351
+5073
+5546
+8216
+1594
+1687
+6815
+1341
+
+29592
+3005
+
+1308
+1553
+2835
+1836
+7723
+4327
+3898
+1050
+7783
+6522
+4656
+
+10689
+14031
+13983
+
+1584
+7231
+4803
+5306
+8353
+1068
+6535
+2199
+8390
+5496
+
+5160
+1922
+1247
+1617
+7293
+7109
+3993
+7377
+1569
+6954
+7302
+6366
+
+6494
+1020
+5600
+1350
+6159
+1225
+5895
+1217
+5775
+1987
+
+1601
+4956
+5875
+5059
+1533
+5164
+2685
+3303
+1716
+1115
+3039
+2798
+2521
+3789
+
+9505
+4164
+3982
+6434
+5158
+6536
+11793
+
+13058
+12583
+22149
+
+2554
+3490
+1467
+3051
+1953
+2234
+2197
+3332
+1020
+4523
+1718
+1375
+1691
+3226
+1817
+
+4567
+5729
+7277
+5081
+5474
+6888
+2625
+
+9349
+4595
+1325
+8622
+1242
+1620
+5622
+8712
+
+6848
+5211
+1712
+3127
+3969
+6276
+4030
+4779
+6708
+3639
+6723
+
+2713
+3578
+6111
+3327
+3418
+2938
+1978
+5925
+3550
+4543
+4938
+1153
+
+10769
+6464
+7437
+9525
+8120
+
+33661
+26140
+
+6258
+6092
+3093
+6129
+2623
+2534
+4305
+4886
+3976
+4163
+3059
+6629
+6472
+
+7387
+5602
+10541
+11216
+10747
+9177
+5626
+
+2028
+4289
+7156
+3501
+3889
+8668
+4970
+3049
+1205
+
+7001
+9671
+2438
+8569
+1494
+5652
+
+2628
+4485
+4398
+4168
+4142
+3128
+1650
+5213
+5356
+6344
+5824
+1504
+3738
+3088
+
+5659
+6155
+8688
+15553
+5667
+
+9262
+3710
+6760
+4474
+
+3756
+8069
+6426
+2268
+3863
+3323
+
+10522
+11928
+9972
+8841
+10825
+
+12633
+4122
+13281
+6188
+9268
+
+5240
+5354
+2181
+2468
+6195
+2785
+2972
+4067
+4126
+6080
+7246
+2622
+
+9227
+12860
+2665
+
+7379
+12181
+12555
+10412
+1593
+3495
+
+5536
+3453
+6662
+5479
+5040
+1036
+3957
+9284
+7134
+
+3988
+5153
+3587
+5437
+4798
+5980
+2671
+4080
+4030
+1560
+2096
+4904
+1851
+2053
+2598
+
+2960
+2637
+1283
+1492
+3084
+6876
+6710
+3486
+1638
+7031
+1575
+7214
+
+32636
+4623
+
+4174
+5467
+5752
+4831
+5354
+5189
+4885
+2698
+2516
+1945
+3409
+6079
+4142
+5354
+4961
+
+10333
+6548
+12704
+13820
+10760
+2821
+
+7547
+7719
+6211
+4742
+5035
+5984
+2000
+7975
+5042
+1579
+3666
+
+7671
+3521
+1462
+5933
+
+58403
+
+4016
+7056
+1553
+6623
+2657
+6527
+1626
+3935
+3430
+5920
+3460
+

+ 30 - 0
advent-of-code/2022/02/1.c

@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define BUFSZ 1024
+
+char buf[BUFSZ];
+int valuetbl[3] = {1, 2, 3};
+int wintbl[3][3] = {
+    {3, 6, 0},
+    {0, 3, 6},
+    {6, 0, 3}
+};
+
+
+int score(char a, char b) {
+    return valuetbl[b - 'X'] + wintbl[a - 'A'][b - 'X'];
+}
+
+int main() {
+    FILE *fp = fopen("input", "r");
+    if (fp == NULL) return -1;
+    int scr = 0;
+    while (fgets(buf, BUFSZ, fp)) {
+        if (strlen(buf) <= 1) continue;
+        scr += score(buf[0], buf[2]);
+    }
+    printf("%d\n", scr);
+    return 0;
+}

+ 36 - 0
advent-of-code/2022/02/2.c

@@ -0,0 +1,36 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define BUFSZ 1024
+
+char buf[BUFSZ];
+int valuetbl[3] = {1, 2, 3};
+int wintbl[3][3] = {
+    {3, 6, 0},
+    {0, 3, 6},
+    {6, 0, 3}
+};
+char decodetbl[3][3] = {
+    {'C', 'A', 'B'},
+    {'A', 'B', 'C'},
+    {'B', 'C', 'A'}
+};
+
+
+int score(char a, char b) {
+    b = decodetbl[a - 'A'][b - 'X'];
+    return valuetbl[b - 'A'] + wintbl[a - 'A'][b - 'A'];
+}
+
+int main() {
+    FILE *fp = fopen("input", "r");
+    if (fp == NULL) return -1;
+    int scr = 0;
+    while (fgets(buf, BUFSZ, fp)) {
+        if (strlen(buf) <= 1) continue;
+        scr += score(buf[0], buf[2]);
+    }
+    printf("%d\n", scr);
+    return 0;
+}

+ 2501 - 0
advent-of-code/2022/02/input

@@ -0,0 +1,2501 @@
+B Y
+C Z
+C Y
+C Y
+A X
+C Y
+C Y
+C Y
+A X
+B X
+B Y
+B Y
+B Z
+C Z
+C Z
+C Z
+B Y
+A Z
+C Y
+C X
+B Z
+C Y
+C Z
+B Y
+B Y
+A X
+B Y
+A X
+C Y
+B Y
+B X
+A Y
+C X
+A Y
+B Y
+B Y
+A Y
+B Y
+A Z
+B Y
+B X
+C Z
+B X
+B Y
+A Z
+B X
+C Y
+C X
+C Y
+C Y
+B Y
+C Z
+C Z
+B Y
+B Y
+B Y
+C X
+B Y
+B Z
+B Y
+B X
+C X
+B X
+A Y
+C Z
+C X
+B Y
+B Y
+C Z
+B X
+C X
+C Y
+C Y
+C Y
+B X
+C Z
+C Z
+B Z
+B Y
+C Y
+C Y
+B Z
+A Y
+B Y
+A X
+B Y
+A X
+A X
+B Y
+B Z
+A X
+C Y
+C Z
+C Y
+C Z
+B X
+C Y
+A X
+B Y
+C Y
+C Z
+A Z
+A Z
+C Z
+A Y
+C Y
+B Y
+B Z
+C X
+A Z
+A Y
+C Z
+B X
+C Y
+C Y
+B Z
+C X
+B Y
+C Y
+A X
+C X
+B Y
+C Y
+B Y
+C X
+B Z
+C Y
+C Z
+B Y
+B Y
+B Y
+B Y
+B Z
+C Y
+B Z
+A Y
+C Z
+C Z
+C Z
+B Y
+B X
+C Z
+C Z
+C Z
+C Z
+C Z
+B Y
+C Y
+A Z
+B Y
+C Y
+A Y
+B Y
+C X
+A X
+A Y
+C Z
+C Y
+B X
+C Z
+B Y
+C Y
+C Y
+C X
+B Z
+B Z
+C Z
+A X
+B Z
+C Y
+C X
+C Z
+A X
+A X
+C X
+B Y
+C Y
+B Y
+B Y
+B X
+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C X
+A Z
+B Y
+B Y
+A Y
+C Z
+A X
+C Y
+A X
+C X
+B Y
+B Y
+B Y
+A Z
+B Z
+C Y
+A X
+B Y
+B Z
+B Y
+B Y
+C Z
+A Y
+B Y
+C Z
+A X
+C Y
+C X
+C Y
+B X
+C Z
+C Z
+B Y
+A Z
+A Y
+C Z
+B X
+B Y
+B Y
+A Y
+C X
+C Y
+B Y
+A Y
+A X
+B Y
+C X
+B Y
+B X
+C Z
+C Z
+C Y
+C Z
+C Z
+C X
+A X
+A Y
+C Y
+B Y
+B Y
+C Y
+A Y
+C Z
+A Y
+A X
+B X
+C X
+C Z
+C Z
+B X
+C Y
+A Z
+C Z
+C Z
+C Y
+A X
+C Z
+B Y
+A Y
+A X
+C Y
+C Y
+C Y
+B Y
+A Z
+C Y
+C Y
+C Y
+B Y
+B X
+C Y
+A Z
+B Y
+B Y
+C Y
+C Z
+C Y
+A X
+A X
+C Y
+C Y
+B Z
+A Y
+B Y
+A X
+A X
+C Y
+B Y
+B Z
+C Z
+C Y
+C Y
+C X
+B Z
+A Z
+B X
+B Y
+B Y
+A Y
+A X
+C Z
+C X
+C Z
+A Z
+B Y
+A Z
+A X
+A X
+B Z
+B Y
+A Z
+A X
+A Z
+B Y
+B Y
+C Z
+C Y
+A Y
+A Y
+B Y
+A Y
+C Z
+C Y
+C Z
+C Y
+C Z
+C Z
+C X
+C X
+C X
+B Y
+C X
+C Z
+B Y
+B Y
+C Y
+B Y
+C Y
+B X
+B Y
+B Z
+C Z
+B Y
+C Y
+B Y
+C Y
+A X
+B Y
+B Y
+C Z
+B Y
+

+ 50 - 0
advent-of-code/2022/03/1.c

@@ -0,0 +1,50 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#define BUFSZ 1024
+
+char buf[BUFSZ + 1] = {0};
+
+int table[53] = {0};
+
+int decode(char c) {
+    if (c >= 'A' && c <= 'Z') {
+        return 26 + c - 'A' + 1;
+    }
+    if (c >='a' && c <= 'z') {
+        return c - 'a' + 1;
+    }
+    return -1;
+}
+
+int findcommon(char *buf, int len) {
+    memset(table, 0, 53 * sizeof(int));
+    len = len / 2;
+    for (int i = 0; i < len; i++) {
+        int code = decode(buf[i]);
+        if (table[code] == 0) {
+            table[code] = 1;
+        }
+    }
+    for (int i = 0; i < len; i++) {
+        int code = decode(buf[i + len]);
+        if (table[code] == 1) return code;
+    }
+    return 0;
+}
+
+int main() {
+    FILE *fp = fopen("input", "r");
+    int sum = 0;
+    if (fp == NULL) return -1;
+    while (fgets(buf, BUFSZ, fp)) {
+        if (strlen(buf) <= 1) continue;
+        int len = strlen(buf) - 1;
+        assert((len % 2 == 0) && "len error");
+        int common = findcommon(buf, len);
+        sum += common;
+    }
+    printf("%d\n", sum);
+}

+ 47 - 0
advent-of-code/2022/03/2.c

@@ -0,0 +1,47 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#define BUFSZ 1024
+
+char buf[BUFSZ + 1] = {0};
+
+int table[53] = {0};
+
+int decode(char c) {
+    if (c >= 'A' && c <= 'Z') {
+        return 26 + c - 'A' + 1;
+    }
+    if (c >='a' && c <= 'z') {
+        return c - 'a' + 1;
+    }
+    return -1;
+}
+
+int process(char *buf, int len, int n) {
+    if (n == 1) memset(table, 0, 53 * sizeof(int));
+    for (int i = 0; i < len; i++) {
+        int code = decode(buf[i]);
+        if (table[code] == n - 1) {
+            if (n == 3) return code; 
+            table[code] = n;
+        }
+    }
+    return 0;
+}
+
+
+int main() {
+    FILE *fp = fopen("input", "r");
+    int sum = 0;
+    if (fp == NULL) return -1;
+    int line = 0;
+    while (fgets(buf, BUFSZ, fp)) {
+        if (strlen(buf) <= 1) continue;
+        int len = strlen(buf) - 1;
+        sum += process(buf, len, line % 3 + 1);
+        line++;
+    }
+    printf("%d\n", sum);
+}

+ 301 - 0
advent-of-code/2022/03/input

@@ -0,0 +1,301 @@
+gvNbShZZgQfWdQhdPQmggLTFLwmwjFqjVVgM
+CsJnHll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lQ
+rggqfMqPWnDqnVbZvTtPZpssVw
+GJHqPPcJnhthNHnL
+rzzlVDVTQDzSSdsSlzpBspssLhmZNjhZWjjffWBLZhgBmnjt
+TSTQTsnTbDsMvCGwcFvbJR
+PPjtzdnCnSjSthPjWJgJWjgMNBsNrT
+bZHfLLhvhpbfvLhHfQbqpNJTWWNsqNTrTNrgWN
+ZFbZZFvlRvZwhQwLDSdGnmCGmnlmdznD
+RFRRhzzsRsszRlhbrhgBcbTfHgBT
+pJtNmSgwZNtSJLgSqHcbBDDTHPfrSqTc
+ZNWdpNCJtLZpQJLLpmLtZNFGjVgFnsngRGllVsjjCnvs
+sJVRJmmmwwlmzcszMzjRjJVzspCZBsZTTFpTTZvSZTFBFbtZ
+GhNDgNWnGGqDvFSCZvbtFpvg
+fWrCNNffGnhWdrPhWjwjzRlJmcjRcPzlmV
+FdncmqmgcZztLWFvFFvjWLlT
+RsMMJVpfhJVsDVMfJpvlTWTvhjQSQvCbLCSC
+jDBjRrsVfNJgzdZrZwtmZt
+THDDQLZTGQQLQDSSTBCZSZHfFrvvlVRRlPVFfVrvqnvnnH
+VbWWcgbNpjgPrRvrNfqnzv
+swggwpJWwsWscJsDSLTJTmSVZJTBDZ
+wGsznPGGBHdzHhtHDD
+WMVSWqVmrSBMpvWVMFtdClhDtqDDHhdtQt
+RgWVRcmgnPPnjJBg
+BmfPBHBFswLLjQQnLCnzzW
+SRdvvRSGrdlRSvTgRrMVCjVWCZzMnVZhWzTj
+vlSRJrRJvbGGrSSJgWvlScptFmwbmfHHBfsNFmBbfsFm
+

+ 14 - 0
advent-of-code/2022/04/1.c

@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+int main() {
+    FILE *fp = fopen("input", "r");
+    int x1, x2, y1, y2;
+    int count = 0;
+    while (fscanf(fp, "%d-%d,%d-%d", &x1, &x2, &y1, &y2) != EOF) {
+        if (x1 <= y1 && x2 >= y2 || x1 >= y1 && x2 <= y2) {
+            count++;
+        }
+    }
+    printf("%d\n", count);
+    return 0;
+}

+ 14 - 0
advent-of-code/2022/04/2.c

@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+int main() {
+    FILE *fp = fopen("input", "r");
+    int x1, x2, y1, y2;
+    int count = 0;
+    while (fscanf(fp, "%d-%d,%d-%d", &x1, &x2, &y1, &y2) != EOF) {
+        if (x1 >= y1 && x1 <= y2 || x2 >= y1 && x2 <= y2 || x1 <= y1 && x2 >= y2) {
+            count++;
+        }
+    }
+    printf("%d\n", count);
+    return 0;
+}

+ 1000 - 0
advent-of-code/2022/04/input

@@ -0,0 +1,1000 @@
+99-99,18-99
+2-86,1-86
+9-21,10-22
+1-24,7-23
+33-37,35-38
+15-57,14-56
+1-88,3-89
+26-56,27-57
+11-94,93-98
+40-92,3-91
+6-90,5-93
+1-5,9-54
+81-81,20-80
+19-19,20-99
+52-96,52-97
+8-86,85-86
+15-93,92-93
+32-41,33-41
+90-90,23-90
+6-57,56-58
+62-84,63-83
+10-85,11-86
+1-55,54-55
+18-25,24-24
+60-98,59-98
+6-74,7-72
+51-53,52-87
+73-99,14-97
+59-87,7-60
+43-89,42-88
+88-88,79-87
+39-39,21-39
+2-79,1-1
+56-56,56-96
+46-90,47-91
+14-48,48-48
+2-2,2-11
+57-57,4-57
+87-87,1-88
+24-85,85-93
+2-2,3-96
+47-49,47-48
+90-90,29-89
+96-99,15-97
+11-28,12-33
+12-81,6-80
+18-85,85-86
+42-71,41-52
+21-84,22-98
+5-73,7-87
+5-55,55-68
+9-82,10-83
+16-16,16-71
+6-64,6-65
+56-90,90-90
+49-66,36-99
+48-85,52-88
+21-21,21-23
+49-55,4-48
+17-90,50-91
+53-95,52-96
+28-28,33-53
+1-97,1-64
+28-70,27-27
+87-87,29-88
+26-91,72-90
+21-57,22-86
+76-91,75-90
+33-92,32-93
+57-91,58-92
+18-78,17-17
+38-38,38-67
+15-98,61-96
+56-70,56-56
+57-76,57-75
+75-75,67-76
+23-67,22-67
+19-97,98-98
+21-65,20-65
+36-54,33-35
+2-88,1-88
+41-80,6-80
+4-98,4-27
+93-98,4-92
+14-69,13-14
+59-86,86-87
+29-33,17-30
+3-68,2-67
+22-98,6-20
+38-87,38-86
+3-97,97-97
+99-99,24-89
+62-63,47-63
+84-86,1-85
+79-92,91-93
+55-70,54-55
+33-38,28-32
+17-61,5-71
+25-70,69-87
+28-80,11-79
+17-51,29-52
+88-89,18-89
+46-98,21-57
+3-77,77-77
+36-71,23-70
+91-92,50-91
+36-61,37-37
+15-51,14-51
+20-31,30-94
+1-2,1-99
+76-87,76-86
+32-75,32-33
+21-27,20-21
+38-77,11-77
+13-15,14-42
+20-21,21-53
+76-95,77-96
+41-41,48-53
+72-82,71-81
+14-68,91-99
+41-82,83-83
+13-13,12-51
+26-96,27-93
+2-92,93-93
+1-40,4-39
+10-66,11-84
+9-10,9-94
+37-67,70-84
+11-69,8-68
+15-81,16-81
+75-89,82-95
+81-81,1-80
+7-99,99-99
+16-62,49-62
+25-90,25-89
+19-78,18-86
+56-62,59-63
+50-52,4-22
+1-6,7-85
+30-31,31-51
+40-50,50-50
+73-98,72-72
+84-87,72-98
+81-81,4-80
+14-61,11-61
+12-13,13-72
+28-30,18-40
+37-67,37-96
+2-98,3-98
+6-34,33-65
+18-37,18-85
+6-78,6-39
+21-43,44-71
+24-24,23-74
+46-85,46-86
+18-54,1-54
+39-96,28-38
+13-89,89-89
+74-99,75-97
+47-92,38-91
+13-13,14-71
+76-78,77-96
+11-77,76-76
+5-76,4-75
+5-25,24-99
+27-96,20-28
+32-76,32-75
+28-57,2-29
+1-96,99-99
+18-30,31-48
+84-84,17-84
+21-41,4-22
+12-31,12-78
+27-51,41-52
+13-74,73-94
+2-6,6-44
+54-90,90-90
+16-19,18-56
+8-96,11-97
+74-75,70-75
+1-80,2-80
+5-22,5-32
+31-50,30-34
+20-95,17-17
+11-19,20-84
+62-62,39-62
+18-34,33-98
+23-35,37-59
+92-92,10-92
+43-67,24-78
+56-56,28-56
+36-95,36-94
+9-63,17-64
+62-93,84-94
+6-63,5-62
+9-10,2-9
+55-64,64-96
+31-90,31-37
+11-92,10-91
+27-95,10-95
+52-65,52-66
+87-87,80-87
+4-46,45-45
+45-45,32-45
+33-96,96-97
+40-60,41-65
+24-24,21-25
+19-85,4-32
+22-93,92-94
+97-99,2-97
+47-84,6-83
+95-97,5-95
+23-49,21-24
+57-59,58-75
+44-44,45-97
+3-9,7-14
+1-70,2-71
+11-85,12-12
+14-59,3-13
+71-91,35-90
+2-89,1-88
+3-93,3-96
+28-30,27-29
+14-37,1-15
+14-89,13-15
+4-63,19-63
+2-18,18-48
+37-98,31-98
+5-5,6-95
+52-52,53-73
+75-78,78-78
+1-99,99-99
+44-69,68-69
+83-83,49-83
+29-73,6-29
+87-87,88-93
+6-93,1-7
+12-93,13-94
+2-27,2-81
+34-83,5-82
+95-99,5-96
+60-74,45-56
+19-81,18-80
+10-68,8-67
+15-98,46-54
+49-93,48-92
+11-65,64-64
+97-97,1-97
+41-86,42-94
+32-47,3-47
+2-64,1-85
+68-75,72-86
+1-89,6-44
+3-88,2-87
+19-50,18-20
+8-39,4-7
+15-86,15-86
+74-74,36-74
+25-48,26-49
+36-98,35-98
+73-75,38-74
+11-52,11-39
+17-75,16-88
+3-96,2-95
+23-48,6-47
+29-37,21-73
+4-91,91-91
+31-95,31-32
+30-90,30-89
+11-64,65-65
+12-72,13-72
+84-84,16-83
+9-77,5-9
+72-98,38-41
+37-80,36-36
+48-48,47-98
+61-73,62-74
+46-53,45-47
+8-93,2-93
+35-61,35-40
+7-16,15-16
+67-79,21-68
+93-99,61-94
+9-23,3-9
+66-87,26-87
+97-99,97-97
+2-21,21-22
+1-84,2-84
+36-87,86-87
+92-94,7-93
+50-97,44-50
+19-69,18-88
+44-57,31-66
+87-87,79-87
+56-85,55-55
+42-93,80-94
+6-66,6-67
+9-82,9-10
+53-60,52-59
+23-86,17-24
+6-44,43-72
+52-66,1-88
+3-88,3-34
+35-75,75-96
+39-54,37-37
+2-41,23-42
+56-56,6-55
+31-32,32-57
+61-61,8-61
+55-96,95-96
+32-76,14-75
+9-44,13-70
+9-30,31-31
+6-96,2-6
+26-53,23-23
+23-80,79-81
+6-65,17-65
+23-32,35-97
+57-66,57-68
+13-60,13-14
+30-52,18-29
+33-46,33-45
+45-69,46-68
+71-96,97-99
+58-60,18-59
+29-30,29-95
+34-37,37-70
+5-90,6-90
+52-96,24-37
+10-79,65-82
+3-80,81-81
+66-93,94-95
+6-25,6-72
+8-9,9-98
+6-98,4-7
+26-80,19-23
+75-75,7-75
+8-92,11-93
+13-96,12-98
+64-66,28-65
+23-93,47-96
+62-75,53-60
+42-42,34-43
+1-66,2-65
+22-46,21-46
+2-4,4-83
+8-9,8-41
+68-91,90-92
+61-94,95-95
+44-66,35-67
+25-94,93-95
+71-88,69-86
+5-48,5-47
+17-81,52-81
+4-84,1-3
+73-73,72-72
+87-87,26-86
+83-83,14-83
+12-88,11-12
+9-9,10-84
+14-88,15-89
+66-77,78-85
+64-85,94-99
+81-82,48-81
+1-93,1-94
+11-98,11-12
+52-89,37-89
+35-69,69-91
+25-59,24-59
+9-42,1-1
+94-97,90-97
+49-93,48-61
+24-24,25-76
+5-14,14-64
+12-83,29-84
+16-68,16-83
+43-48,38-49
+46-79,4-45
+74-94,34-65
+33-45,24-61
+3-82,15-83
+26-54,82-88
+63-63,38-62
+16-97,15-97
+7-88,1-98
+91-96,3-87
+28-45,44-44
+36-77,14-77
+25-81,82-82
+28-28,27-77
+6-33,32-49
+14-34,3-15
+13-98,12-94
+5-11,9-12
+54-56,13-55
+26-90,25-93
+67-88,87-89
+76-98,75-99
+1-85,1-65
+54-70,55-71
+53-87,52-86
+6-92,2-7
+38-90,91-98
+52-67,51-67
+74-93,20-74
+3-87,2-86
+7-27,26-26
+91-97,88-96
+17-25,16-24
+4-91,4-92
+43-56,44-44
+4-79,3-80
+37-95,36-96
+5-19,18-34
+9-58,73-73
+23-27,23-27
+68-70,69-96
+3-78,2-51
+6-94,7-73
+93-96,76-93
+88-99,20-88
+11-89,90-96
+5-98,99-99
+23-34,33-93
+10-23,5-22
+44-44,45-93
+2-98,2-97
+64-96,59-64
+13-13,14-98
+21-94,44-99
+71-95,94-94
+56-98,55-98
+18-66,19-67
+3-92,3-93
+74-93,69-69
+68-68,14-67
+45-97,74-98
+21-89,46-91
+10-91,11-74
+14-41,5-45
+3-39,39-80
+20-75,20-76
+21-92,15-20
+27-28,26-36
+57-75,57-80
+18-87,17-86
+23-57,22-56
+63-82,59-80
+99-99,1-97
+94-94,14-93
+22-82,21-98
+68-75,41-67
+39-77,38-76
+7-68,68-68
+15-18,18-79
+79-89,78-80
+26-68,26-51
+31-77,3-32
+29-97,11-11
+4-91,4-63
+8-90,8-98
+36-58,23-58
+14-46,13-34
+37-97,37-98
+4-4,5-21
+20-89,19-89
+42-43,43-87
+49-50,48-48
+20-86,98-98
+12-19,12-16
+58-59,40-58
+48-79,47-48
+97-99,32-98
+22-98,7-23
+16-88,17-83
+68-96,97-98
+11-50,30-64
+1-96,2-97
+44-96,44-97
+66-66,7-66
+9-42,10-42
+89-93,48-90
+6-7,6-64
+90-92,8-91
+8-12,13-74
+25-64,25-65
+62-70,63-72
+3-99,8-73
+2-79,1-78
+98-99,3-97
+35-76,6-75
+74-78,73-73
+87-97,64-87
+17-91,16-91
+2-71,8-72
+79-95,32-93
+34-97,24-35
+85-87,46-86
+36-72,72-73
+4-7,7-79
+10-11,10-80
+88-89,7-89
+15-52,52-52
+33-65,64-86
+46-48,48-63
+25-40,24-24
+53-81,11-53
+8-9,9-94
+97-97,19-97
+4-84,84-95
+22-41,40-59
+20-55,56-99
+9-46,47-47
+2-4,3-88
+31-73,30-30
+26-87,12-22
+3-45,1-44
+7-24,7-83
+11-81,8-80
+13-16,10-15
+18-44,43-44
+3-4,4-78
+87-91,2-87
+2-89,22-89
+11-93,10-10
+6-99,1-99
+12-95,12-94
+26-58,39-97
+52-98,52-97
+49-71,50-85
+41-49,41-45
+37-89,38-90
+2-90,4-80
+42-92,95-98
+58-82,57-82
+25-70,6-71
+42-96,44-96
+21-21,20-95
+9-97,8-99
+20-21,12-21
+44-93,97-98
+44-98,45-64
+19-19,18-93
+65-65,66-72
+8-42,3-7
+12-83,11-82
+19-75,63-76
+32-60,32-32
+12-90,90-99
+59-60,17-59
+8-75,7-31
+59-79,58-79
+14-83,15-84
+28-29,28-30
+60-98,1-98
+33-45,33-64
+20-20,20-20
+36-94,35-93
+13-31,31-31
+28-30,2-29
+81-81,82-86
+76-78,79-79
+54-80,54-70
+61-64,65-99
+5-11,11-89
+16-54,17-26
+2-56,2-3
+42-76,77-77
+7-82,7-81
+37-54,19-38
+77-96,71-76
+16-85,84-86
+8-93,1-9
+37-87,36-77
+54-71,19-71
+20-29,20-97
+82-96,83-96
+48-81,47-81
+4-36,5-37
+8-59,7-9
+91-99,62-98
+12-80,86-90
+25-95,24-25
+32-94,94-94
+22-97,98-98
+16-92,16-17
+84-84,74-84
+4-86,2-85
+23-56,23-57
+31-31,32-99
+14-96,13-15
+50-81,34-41
+11-44,11-11
+7-38,6-38
+10-77,10-96
+50-86,87-87
+5-87,5-86
+91-91,42-90
+19-97,97-97
+22-77,21-27
+7-7,6-81
+79-93,18-92
+39-93,59-98
+1-84,84-84
+30-35,30-48
+1-2,3-47
+4-88,3-5
+4-90,90-91
+3-95,4-98
+28-31,5-32
+39-93,4-98
+99-99,1-99
+4-96,3-95
+58-63,58-63
+44-91,33-44
+27-27,28-89
+16-84,78-85
+2-22,17-23
+30-78,77-84
+17-57,18-57
+2-99,1-3
+2-31,35-43
+74-74,17-75
+46-92,87-93
+6-97,5-96
+4-18,19-90
+31-33,32-57
+26-57,27-58
+3-78,1-1
+68-69,34-69
+66-66,26-66
+11-12,14-61
+5-6,5-76
+14-96,96-97
+4-70,4-69
+69-95,70-70
+8-93,5-8
+35-87,1-35
+51-62,51-61
+52-69,51-51
+14-69,24-70
+20-91,80-90
+74-88,75-88
+3-72,1-72
+7-67,6-65
+31-47,32-48
+99-99,11-98
+7-17,7-14
+55-88,56-99
+29-31,28-30
+65-65,66-66
+9-76,33-73
+51-72,19-52
+2-15,2-98
+78-78,24-77
+41-95,96-96
+23-94,22-23
+23-24,24-97
+34-34,35-94
+7-81,7-39
+9-17,16-91
+11-23,10-10
+17-66,67-70
+28-68,27-27
+65-91,90-98
+43-79,11-44
+15-83,48-82
+13-84,13-83
+5-5,7-71
+48-63,49-64
+75-76,28-76
+23-95,23-23
+18-95,19-96
+45-86,87-87
+2-29,30-82
+15-66,14-66
+23-94,94-97
+15-88,19-74
+1-44,13-45
+24-25,24-45
+43-49,54-67
+6-63,5-63
+70-99,70-92
+45-95,76-96
+78-96,98-98
+3-3,5-75
+3-75,2-81
+15-19,11-39
+47-70,54-70
+6-92,6-91
+39-88,40-89
+1-76,66-93
+89-89,4-88
+18-94,8-18
+6-81,7-66
+87-94,88-90
+10-29,30-30
+6-73,3-77
+80-84,79-81
+47-48,47-53
+44-61,44-57
+53-60,52-61
+56-65,56-66
+25-52,52-74
+5-6,7-93
+22-99,21-22
+3-14,2-12
+4-42,1-4
+22-85,23-86
+25-56,33-56
+16-84,84-85
+51-57,9-50
+8-91,90-90
+30-85,19-31
+15-66,46-67
+17-31,16-31
+24-94,23-24
+54-54,55-84
+41-98,42-96
+89-89,42-88
+25-72,26-45
+25-96,6-97
+6-85,3-6
+71-78,43-72
+3-74,3-73
+5-49,8-50
+5-5,6-86
+4-39,39-73
+85-87,84-86
+11-62,10-78
+61-86,62-85
+7-32,32-60
+79-79,10-79
+17-72,38-73
+13-86,12-85
+6-6,2-4
+47-49,1-48
+74-98,12-99
+1-71,1-27
+29-80,29-81
+54-94,53-94
+18-74,73-82
+95-95,7-95
+62-62,62-62
+6-93,7-94
+43-87,43-98
+17-89,9-93
+4-72,1-3
+22-45,45-95
+2-2,3-92
+75-77,29-74
+11-98,97-98
+70-70,69-69
+36-41,35-36
+6-12,10-13
+65-98,65-99
+1-97,2-98
+33-89,12-89
+24-69,18-18
+3-65,2-66
+14-14,15-80
+18-70,2-5
+8-59,9-60
+52-53,8-53
+72-89,39-72
+18-81,82-82
+1-95,1-94
+57-73,92-99
+10-20,9-19
+44-68,56-68
+18-22,19-27
+28-65,28-38
+63-87,47-86
+8-87,9-88
+26-55,25-95
+28-96,13-27
+38-38,6-37
+33-37,40-73
+72-95,95-95
+61-93,28-53
+73-85,86-86
+2-93,94-94
+42-97,43-43
+12-99,11-13
+11-94,12-86
+63-87,86-87
+47-48,46-47
+91-91,7-91
+8-21,22-45
+49-57,50-58
+10-11,11-91
+41-47,36-47
+30-64,30-65
+1-28,24-24
+3-12,1-3
+60-76,57-59
+15-65,16-16
+64-64,5-64
+7-99,6-6
+48-98,69-99
+97-99,1-97
+66-66,10-67
+5-99,4-99
+32-86,85-88
+2-99,2-2
+3-35,35-44
+62-62,37-61
+5-53,4-54
+1-98,98-99
+31-98,48-99
+3-5,4-88
+47-54,36-53
+44-91,69-92
+52-65,66-98
+10-72,62-71
+6-98,6-97
+53-53,54-80
+2-96,2-52
+10-17,11-94
+25-90,25-30
+55-71,55-70
+44-66,66-67
+83-87,82-87
+34-76,1-34
+35-38,41-54
+64-98,1-98
+58-58,58-71
+56-89,89-99
+55-73,37-79
+15-97,18-22
+4-97,4-98
+8-62,34-95
+1-4,5-51
+37-65,51-95
+22-87,51-87
+29-40,28-28
+21-83,21-83
+24-26,25-46
+23-68,24-69
+69-77,78-78
+42-92,41-72
+37-78,28-77
+2-90,2-89
+5-9,8-50
+4-82,3-89
+47-49,48-82
+10-99,11-94
+19-42,10-18
+15-48,7-16
+21-86,21-22
+69-69,38-69
+24-27,32-92
+30-94,30-78
+35-36,35-56
+85-87,86-86
+70-70,71-97
+7-7,8-84
+7-83,83-83
+9-87,10-88
+1-14,13-92
+2-93,92-94
+37-45,45-84
+6-98,5-94
+5-74,2-4
+5-23,23-57
+14-77,3-76
+5-84,3-83
+22-52,12-52
+18-71,19-72
+13-91,13-90
+95-95,19-95
+4-39,3-38
+5-71,2-3
+52-52,3-53
+4-81,2-3
+77-98,49-98
+48-48,37-47
+13-86,14-86
+9-99,10-99
+89-98,22-89
+66-73,66-80
+12-12,13-98
+4-5,6-23
+79-99,79-80
+40-72,4-41
+70-74,69-76
+15-16,16-37
+22-78,1-96
+66-79,66-80
+2-92,92-93
+80-95,81-90
+34-37,32-37
+17-34,30-49
+16-98,26-99
+11-12,11-23
+2-72,72-72
+48-50,31-54
+75-90,75-82
+4-20,21-26
+3-3,2-85
+1-58,2-59
+3-94,3-95
+55-87,54-86
+22-81,33-80
+14-80,15-15
+3-71,71-72
+60-60,60-70
+25-25,25-95
+20-62,20-61
+64-84,84-84
+5-98,5-98
+3-10,12-71
+55-77,55-74
+12-79,78-79
+23-36,23-35
+16-42,42-42
+27-85,26-26
+3-71,71-71
+87-94,29-86
+2-78,77-79
+22-86,22-85
+7-84,8-85
+63-67,64-70
+28-45,28-29
+23-74,74-75
+6-50,50-74
+49-68,51-59
+50-50,49-63
+7-7,7-83
+11-53,54-54
+5-89,5-89
+7-88,6-87
+8-58,9-58
+5-6,5-71
+16-79,7-17
+98-98,24-96
+49-49,48-86
+20-64,40-96
+50-89,90-90
+13-72,14-72
+69-70,69-93
+13-96,12-99
+21-47,36-47
+34-34,19-34
+3-82,5-83
+26-46,27-47
+39-91,6-88
+8-97,8-92
+7-8,7-82
+80-81,37-80
+33-44,99-99
+17-36,7-18
+7-99,98-98
+36-78,77-79
+24-67,24-66
+1-68,9-67
+60-60,61-90
+90-91,42-90
+3-94,3-95
+16-96,16-82
+2-4,5-92
+6-93,7-87
+2-4,4-44
+62-77,57-77
+3-99,64-99
+28-89,27-27
+53-53,26-52
+14-76,61-77
+33-94,32-33
+37-42,38-42
+94-95,55-68
+34-58,3-35
+13-48,35-45
+4-54,3-99
+11-12,11-97
+27-88,95-99
+32-92,33-74
+21-72,6-21
+2-98,2-99
+5-96,4-96
+1-67,7-49
+53-89,11-12
+30-36,29-34
+73-96,72-96
+31-68,16-31
+31-51,50-59
+90-98,76-93
+3-61,3-61
+42-47,8-28
+10-85,9-10
+50-51,51-82
+83-90,76-89
+2-70,3-21
+46-74,46-76
+4-67,1-3
+1-94,94-94
+2-4,3-51
+12-51,5-12
+18-32,18-33
+29-84,15-83

+ 60 - 0
advent-of-code/2022/05/1.c

@@ -0,0 +1,60 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+struct listnode {
+    struct listnode *next;
+    char val;
+};
+typedef struct {
+    struct listnode *head;
+} Stack;
+
+void stack_push(Stack *s, char val) {
+    struct listnode *n = malloc(sizeof(struct listnode));
+    n->next = s->head;
+    n->val = val;
+    s->head = n;
+}
+
+char* stack_top(Stack *s) {
+    if (s->head == NULL) return NULL;
+    return &(s->head->val);
+}
+
+void stack_pop(Stack *s) {
+    if (s->head == NULL) return;
+    struct listnode *next = s->head->next;
+    free(s->head);
+    s->head = next;
+}
+
+char buf[4096];
+
+int main() {
+    Stack s[9];
+    FILE *fp = fopen("input", "r");
+    fread(buf, 1, 36 * 8, fp);
+    for (int i = 0; i < 9; i++) {
+        for (int j = 0; j < 8; j++) {
+            char c = buf[(7 - j) * 36 + i * 4 + 1];
+            if (c != ' ') {
+                stack_push(&s[i], c);
+            }
+        }
+    }
+    fgets(buf, 4096, fp);
+    fgets(buf, 4096, fp);
+    int amount, from, to;
+    while (fscanf(fp, "move %d from %d to %d\n", &amount, &from, &to) > 0) {
+        for (int i = 0; i < amount; i++) {
+            char c = *stack_top(&s[from - 1]);
+            stack_pop(&s[from - 1]);
+            stack_push(&s[to - 1], c);
+        }
+    }
+    for (int i = 0; i < 9; i++) {
+        printf("%c", *stack_top(&s[i]));
+    }
+    printf("\n");
+    return 0;
+}

+ 64 - 0
advent-of-code/2022/05/2.c

@@ -0,0 +1,64 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+struct listnode {
+    struct listnode *next;
+    char val;
+};
+typedef struct {
+    struct listnode *head;
+} Stack;
+
+void stack_push(Stack *s, char val) {
+    struct listnode *n = malloc(sizeof(struct listnode));
+    n->next = s->head;
+    n->val = val;
+    s->head = n;
+}
+
+char* stack_top(Stack *s) {
+    if (s->head == NULL) return NULL;
+    return &(s->head->val);
+}
+
+void stack_pop(Stack *s) {
+    if (s->head == NULL) return;
+    struct listnode *next = s->head->next;
+    free(s->head);
+    s->head = next;
+}
+
+char buf[4096];
+
+int main() {
+    Stack s[9];
+    FILE *fp = fopen("input", "r");
+    fread(buf, 1, 36 * 8, fp);
+    for (int i = 0; i < 9; i++) {
+        for (int j = 0; j < 8; j++) {
+            char c = buf[(7 - j) * 36 + i * 4 + 1];
+            if (c != ' ') {
+                stack_push(&s[i], c);
+            }
+        }
+    }
+    fgets(buf, 4096, fp);
+    fgets(buf, 4096, fp);
+    int amount, from, to;
+    while (fscanf(fp, "move %d from %d to %d\n", &amount, &from, &to) > 0) {
+        for (int i = 0; i < amount; i++) {
+            char c = *stack_top(&s[from - 1]);
+            stack_pop(&s[from - 1]);
+            buf[i] = c;
+        }
+        for (int i = 0; i < amount; i++) {
+            int n = amount - i - 1;
+            stack_push(&s[to-1], buf[n]);
+        }
+    }
+    for (int i = 0; i < 9; i++) {
+        printf("%c", *stack_top(&s[i]));
+    }
+    printf("\n");
+    return 0;
+}

+ 513 - 0
advent-of-code/2022/05/input

@@ -0,0 +1,513 @@
+                [B] [L]     [J]    
+            [B] [Q] [R]     [D] [T]
+            [G] [H] [H] [M] [N] [F]
+        [J] [N] [D] [F] [J] [H] [B]
+    [Q] [F] [W] [S] [V] [N] [F] [N]
+[W] [N] [H] [M] [L] [B] [R] [T] [Q]
+[L] [T] [C] [R] [R] [J] [W] [Z] [L]
+[S] [J] [S] [T] [T] [M] [D] [B] [H]
+ 1   2   3   4   5   6   7   8   9 
+
+move 5 from 4 to 5
+move 2 from 5 to 8
+move 2 from 9 to 1
+move 2 from 9 to 1
+move 1 from 5 to 3
+move 10 from 5 to 8
+move 1 from 4 to 7
+move 1 from 1 to 2
+move 5 from 3 to 7
+move 1 from 2 to 8
+move 21 from 8 to 5
+move 13 from 5 to 7
+move 2 from 9 to 4
+move 1 from 7 to 4
+move 5 from 1 to 4
+move 1 from 5 to 7
+move 2 from 2 to 7
+move 1 from 3 to 2
+move 1 from 1 to 6
+move 7 from 5 to 9
+move 16 from 7 to 4
+move 7 from 9 to 3
+move 1 from 7 to 5
+move 1 from 3 to 8
+move 3 from 2 to 7
+move 1 from 8 to 9
+move 3 from 3 to 6
+move 21 from 4 to 9
+move 1 from 5 to 7
+move 4 from 4 to 9
+move 8 from 6 to 3
+move 6 from 7 to 1
+move 12 from 9 to 8
+move 6 from 7 to 2
+move 3 from 6 to 5
+move 1 from 6 to 9
+move 4 from 8 to 6
+move 3 from 8 to 5
+move 4 from 1 to 8
+move 4 from 6 to 1
+move 2 from 1 to 3
+move 1 from 5 to 8
+move 2 from 2 to 8
+move 5 from 8 to 3
+move 4 from 2 to 7
+move 5 from 8 to 1
+move 2 from 1 to 7
+move 1 from 8 to 2
+move 2 from 1 to 7
+move 11 from 9 to 2
+move 1 from 8 to 5
+move 2 from 9 to 4
+move 3 from 9 to 5
+move 2 from 5 to 1
+move 6 from 5 to 8
+move 2 from 4 to 2
+move 1 from 5 to 6
+move 7 from 1 to 8
+move 2 from 2 to 7
+move 13 from 8 to 1
+move 16 from 3 to 1
+move 3 from 2 to 1
+move 12 from 7 to 6
+move 15 from 1 to 8
+move 2 from 3 to 8
+move 16 from 1 to 2
+move 24 from 2 to 8
+move 1 from 1 to 5
+move 1 from 5 to 8
+move 3 from 6 to 7
+move 26 from 8 to 3
+move 20 from 3 to 9
+move 1 from 2 to 9
+move 16 from 9 to 3
+move 14 from 3 to 1
+move 13 from 1 to 6
+move 3 from 3 to 4
+move 3 from 9 to 4
+move 1 from 7 to 8
+move 5 from 8 to 2
+move 8 from 8 to 5
+move 18 from 6 to 1
+move 4 from 8 to 5
+move 6 from 4 to 1
+move 2 from 2 to 5
+move 5 from 3 to 8
+move 5 from 8 to 7
+move 2 from 5 to 8
+move 5 from 5 to 4
+move 3 from 2 to 8
+move 22 from 1 to 2
+move 1 from 1 to 2
+move 5 from 8 to 2
+move 2 from 5 to 2
+move 1 from 1 to 6
+move 5 from 5 to 2
+move 1 from 9 to 8
+move 5 from 4 to 1
+move 6 from 6 to 9
+move 3 from 1 to 9
+move 1 from 1 to 7
+move 8 from 9 to 6
+move 6 from 7 to 1
+move 5 from 6 to 5
+move 27 from 2 to 1
+move 4 from 5 to 7
+move 9 from 1 to 5
+move 1 from 9 to 1
+move 3 from 6 to 2
+move 9 from 2 to 1
+move 2 from 7 to 2
+move 1 from 8 to 7
+move 10 from 5 to 9
+move 1 from 9 to 7
+move 25 from 1 to 8
+move 6 from 7 to 4
+move 11 from 1 to 7
+move 3 from 8 to 1
+move 3 from 2 to 6
+move 3 from 8 to 9
+move 11 from 8 to 6
+move 1 from 2 to 6
+move 12 from 6 to 4
+move 13 from 4 to 5
+move 1 from 6 to 1
+move 3 from 7 to 5
+move 5 from 8 to 7
+move 1 from 7 to 1
+move 5 from 1 to 6
+move 3 from 6 to 4
+move 3 from 8 to 6
+move 2 from 5 to 2
+move 12 from 5 to 9
+move 5 from 6 to 2
+move 2 from 5 to 9
+move 6 from 4 to 9
+move 11 from 7 to 3
+move 1 from 2 to 5
+move 1 from 7 to 8
+move 1 from 5 to 7
+move 1 from 7 to 1
+move 1 from 8 to 1
+move 2 from 4 to 7
+move 2 from 6 to 8
+move 5 from 3 to 6
+move 2 from 7 to 2
+move 2 from 2 to 9
+move 1 from 2 to 9
+move 1 from 1 to 6
+move 35 from 9 to 7
+move 2 from 8 to 7
+move 3 from 3 to 8
+move 5 from 2 to 4
+move 3 from 3 to 7
+move 2 from 4 to 7
+move 4 from 6 to 5
+move 4 from 5 to 9
+move 3 from 4 to 5
+move 1 from 8 to 3
+move 4 from 9 to 8
+move 1 from 9 to 6
+move 38 from 7 to 2
+move 1 from 3 to 5
+move 1 from 1 to 7
+move 4 from 7 to 3
+move 3 from 6 to 1
+move 22 from 2 to 7
+move 1 from 5 to 8
+move 7 from 8 to 4
+move 8 from 2 to 8
+move 3 from 5 to 1
+move 4 from 3 to 9
+move 1 from 8 to 3
+move 1 from 3 to 7
+move 2 from 2 to 3
+move 5 from 8 to 9
+move 3 from 9 to 1
+move 2 from 1 to 7
+move 6 from 2 to 3
+move 6 from 3 to 1
+move 2 from 3 to 6
+move 1 from 6 to 1
+move 14 from 7 to 2
+move 4 from 1 to 6
+move 8 from 1 to 3
+move 4 from 3 to 6
+move 3 from 9 to 5
+move 1 from 8 to 6
+move 1 from 8 to 4
+move 9 from 7 to 1
+move 8 from 2 to 4
+move 4 from 2 to 9
+move 2 from 2 to 1
+move 3 from 5 to 8
+move 1 from 8 to 6
+move 1 from 7 to 8
+move 1 from 6 to 5
+move 3 from 9 to 5
+move 2 from 9 to 5
+move 4 from 3 to 9
+move 3 from 6 to 3
+move 3 from 6 to 9
+move 9 from 4 to 1
+move 1 from 9 to 8
+move 3 from 3 to 6
+move 2 from 7 to 4
+move 4 from 8 to 5
+move 7 from 5 to 6
+move 19 from 1 to 9
+move 5 from 9 to 3
+move 2 from 1 to 6
+move 1 from 4 to 6
+move 4 from 3 to 2
+move 21 from 9 to 7
+move 1 from 1 to 2
+move 1 from 9 to 1
+move 1 from 1 to 8
+move 16 from 7 to 6
+move 24 from 6 to 5
+move 7 from 4 to 5
+move 1 from 8 to 3
+move 2 from 2 to 8
+move 31 from 5 to 8
+move 1 from 4 to 6
+move 2 from 6 to 9
+move 1 from 7 to 4
+move 3 from 7 to 9
+move 1 from 4 to 8
+move 2 from 3 to 5
+move 1 from 2 to 3
+move 1 from 3 to 7
+move 1 from 7 to 9
+move 24 from 8 to 6
+move 1 from 8 to 1
+move 30 from 6 to 1
+move 2 from 5 to 2
+move 1 from 6 to 9
+move 6 from 9 to 7
+move 1 from 6 to 4
+move 1 from 4 to 6
+move 23 from 1 to 3
+move 21 from 3 to 4
+move 4 from 2 to 6
+move 3 from 6 to 1
+move 1 from 5 to 1
+move 4 from 1 to 9
+move 3 from 9 to 6
+move 8 from 1 to 6
+move 4 from 8 to 5
+move 2 from 7 to 5
+move 7 from 4 to 3
+move 3 from 4 to 9
+move 9 from 3 to 9
+move 1 from 7 to 6
+move 6 from 5 to 8
+move 14 from 6 to 2
+move 4 from 8 to 4
+move 7 from 4 to 5
+move 1 from 7 to 9
+move 6 from 4 to 3
+move 13 from 2 to 6
+move 5 from 3 to 7
+move 1 from 3 to 8
+move 1 from 8 to 2
+move 4 from 8 to 3
+move 6 from 6 to 4
+move 2 from 2 to 8
+move 5 from 4 to 7
+move 3 from 7 to 5
+move 1 from 7 to 9
+move 2 from 3 to 9
+move 3 from 7 to 3
+move 1 from 7 to 9
+move 1 from 7 to 9
+move 3 from 4 to 1
+move 6 from 6 to 1
+move 2 from 7 to 5
+move 1 from 3 to 5
+move 11 from 9 to 4
+move 9 from 4 to 5
+move 3 from 3 to 4
+move 1 from 3 to 9
+move 2 from 8 to 1
+move 9 from 1 to 8
+move 22 from 5 to 8
+move 2 from 1 to 3
+move 3 from 4 to 6
+move 14 from 8 to 9
+move 1 from 3 to 9
+move 19 from 9 to 3
+move 3 from 9 to 4
+move 2 from 7 to 2
+move 1 from 4 to 6
+move 1 from 3 to 8
+move 8 from 3 to 1
+move 2 from 9 to 6
+move 1 from 2 to 5
+move 3 from 4 to 9
+move 1 from 2 to 3
+move 20 from 8 to 3
+move 4 from 9 to 5
+move 1 from 4 to 2
+move 26 from 3 to 5
+move 1 from 8 to 3
+move 8 from 1 to 4
+move 1 from 3 to 7
+move 1 from 2 to 1
+move 1 from 1 to 6
+move 1 from 6 to 7
+move 4 from 5 to 3
+move 3 from 4 to 2
+move 5 from 5 to 3
+move 2 from 2 to 6
+move 3 from 3 to 5
+move 2 from 4 to 8
+move 5 from 3 to 9
+move 5 from 9 to 8
+move 19 from 5 to 9
+move 1 from 5 to 2
+move 2 from 7 to 1
+move 1 from 1 to 7
+move 1 from 7 to 4
+move 13 from 9 to 3
+move 8 from 6 to 2
+move 10 from 3 to 5
+move 14 from 5 to 4
+move 7 from 8 to 4
+move 1 from 6 to 2
+move 6 from 3 to 8
+move 4 from 9 to 7
+move 2 from 9 to 8
+move 1 from 7 to 1
+move 3 from 2 to 7
+move 1 from 5 to 3
+move 7 from 8 to 6
+move 5 from 6 to 2
+move 8 from 4 to 5
+move 3 from 5 to 8
+move 3 from 8 to 6
+move 5 from 7 to 9
+move 5 from 3 to 6
+move 1 from 9 to 4
+move 17 from 4 to 7
+move 1 from 8 to 1
+move 12 from 7 to 8
+move 3 from 1 to 4
+move 2 from 4 to 6
+move 8 from 6 to 1
+move 4 from 6 to 3
+move 1 from 7 to 8
+move 5 from 5 to 8
+move 4 from 7 to 1
+move 3 from 2 to 6
+move 2 from 5 to 1
+move 6 from 1 to 6
+move 4 from 3 to 5
+move 4 from 5 to 3
+move 1 from 4 to 8
+move 3 from 3 to 2
+move 17 from 8 to 4
+move 6 from 6 to 3
+move 14 from 4 to 9
+move 1 from 3 to 8
+move 1 from 7 to 4
+move 3 from 8 to 3
+move 5 from 2 to 5
+move 6 from 1 to 7
+move 2 from 6 to 4
+move 4 from 5 to 7
+move 1 from 1 to 5
+move 1 from 6 to 3
+move 10 from 7 to 4
+move 1 from 5 to 4
+move 1 from 2 to 3
+move 15 from 4 to 5
+move 3 from 3 to 1
+move 6 from 2 to 6
+move 1 from 2 to 3
+move 2 from 4 to 7
+move 2 from 7 to 8
+move 1 from 4 to 2
+move 2 from 1 to 7
+move 1 from 7 to 2
+move 12 from 9 to 1
+move 4 from 9 to 5
+move 4 from 6 to 2
+move 1 from 7 to 3
+move 6 from 2 to 4
+move 1 from 8 to 5
+move 2 from 4 to 2
+move 11 from 1 to 7
+move 3 from 1 to 4
+move 17 from 5 to 6
+move 15 from 6 to 4
+move 1 from 8 to 9
+move 10 from 4 to 1
+move 1 from 3 to 9
+move 2 from 6 to 5
+move 1 from 2 to 6
+move 4 from 5 to 6
+move 4 from 1 to 2
+move 6 from 6 to 7
+move 2 from 2 to 6
+move 9 from 4 to 9
+move 6 from 1 to 2
+move 3 from 4 to 1
+move 10 from 9 to 8
+move 4 from 2 to 1
+move 1 from 1 to 2
+move 5 from 8 to 6
+move 1 from 2 to 7
+move 1 from 9 to 4
+move 2 from 6 to 9
+move 13 from 7 to 2
+move 5 from 7 to 5
+move 2 from 5 to 2
+move 1 from 4 to 5
+move 4 from 8 to 4
+move 17 from 2 to 6
+move 3 from 4 to 6
+move 2 from 9 to 1
+move 7 from 6 to 8
+move 1 from 5 to 2
+move 1 from 4 to 1
+move 2 from 9 to 4
+move 1 from 3 to 9
+move 4 from 3 to 7
+move 2 from 8 to 5
+move 3 from 7 to 5
+move 10 from 5 to 8
+move 2 from 2 to 4
+move 6 from 1 to 2
+move 4 from 6 to 3
+move 8 from 2 to 6
+move 1 from 7 to 4
+move 5 from 4 to 5
+move 7 from 6 to 7
+move 5 from 3 to 5
+move 5 from 5 to 2
+move 4 from 8 to 1
+move 6 from 1 to 6
+move 3 from 3 to 2
+move 22 from 6 to 2
+move 1 from 9 to 7
+move 8 from 8 to 6
+move 1 from 7 to 6
+move 2 from 5 to 7
+move 4 from 8 to 5
+move 7 from 6 to 7
+move 2 from 6 to 4
+move 14 from 2 to 1
+move 7 from 1 to 3
+move 12 from 7 to 3
+move 1 from 4 to 3
+move 2 from 5 to 8
+move 2 from 8 to 1
+move 1 from 4 to 3
+move 6 from 2 to 9
+move 6 from 9 to 2
+move 2 from 2 to 7
+move 6 from 7 to 5
+move 13 from 3 to 5
+move 5 from 2 to 6
+move 5 from 6 to 1
+move 2 from 3 to 6
+move 1 from 6 to 5
+move 1 from 6 to 1
+move 3 from 1 to 9
+move 6 from 2 to 7
+move 1 from 2 to 3
+move 24 from 5 to 2
+move 7 from 3 to 7
+move 13 from 7 to 9
+move 4 from 1 to 9
+move 4 from 1 to 6
+move 1 from 5 to 6
+move 16 from 9 to 5
+move 1 from 6 to 4
+move 1 from 5 to 2
+move 5 from 1 to 3
+move 11 from 2 to 1
+move 4 from 9 to 6
+move 1 from 4 to 7
+move 2 from 3 to 4
+move 6 from 6 to 9
+move 1 from 1 to 3
+move 2 from 9 to 4
+move 1 from 7 to 9
+move 4 from 2 to 9
+move 8 from 9 to 2
+move 3 from 3 to 2
+move 1 from 9 to 4
+move 5 from 1 to 7
+move 1 from 4 to 8
+move 2 from 1 to 9
+move 1 from 8 to 7
+move 6 from 5 to 3
+move 1 from 5 to 1
+move 5 from 2 to 3
+move 4 from 1 to 5
+move 4 from 7 to 1
+move 8 from 5 to 8
+

+ 26 - 0
advent-of-code/2022/06/1.c

@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include <stdbool.h>
+
+bool succ(char *buf) {
+    bool table[26] = {0};
+    for (int i = 0; i < 4; i++) {
+        if(!table[buf[i] - 'a']) table[buf[i] - 'a'] = true;
+        else return false;
+    }
+    return true;
+}
+
+int main() {
+    FILE *fp = fopen("input", "r");
+    char buf[4] = {0};
+    int i = 0;
+    int c;
+    while ((c = fgetc(fp)) != EOF) {
+        if (c < 'a' || c > 'z') continue;
+        buf[i % 4] = c;
+        if (i >= 4 && succ(buf)) break;
+        i++;
+    }
+    printf("%d\n", i+1);
+    return 0;
+}

+ 26 - 0
advent-of-code/2022/06/2.c

@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include <stdbool.h>
+
+bool succ(char *buf) {
+    bool table[26] = {0};
+    for (int i = 0; i < 14; i++) {
+        if(!table[buf[i] - 'a']) table[buf[i] - 'a'] = true;
+        else return false;
+    }
+    return true;
+}
+
+int main() {
+    FILE *fp = fopen("input", "r");
+    char buf[14] = {0};
+    int i = 0;
+    int c;
+    while ((c = fgetc(fp)) != EOF) {
+        if (c < 'a' || c > 'z') continue;
+        buf[i % 14] = c;
+        if (i >= 14 && succ(buf)) break;
+        i++;
+    }
+    printf("%d\n", i+1);
+    return 0;
+}

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
advent-of-code/2022/06/input


+ 101 - 0
advent-of-code/2022/07/1.c

@@ -0,0 +1,101 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct dir Dir;
+
+#define LOG(s) fprintf(stderr, s"\n");
+
+struct dir {
+    char name[32];
+    Dir *parent;
+    Dir *next_sibling;
+    Dir *child;
+    int size;
+};
+
+char buf[4096];
+
+Dir root = {0};
+
+int sum = 0;
+
+int totalsize(Dir *dir) {
+    if (dir == NULL) return 0;
+    int total = dir->size;
+    Dir *child = dir->child;
+    while (child != NULL) {
+        total += totalsize(child);
+        child = child->next_sibling;
+    }
+    if (total <= 100000) sum += total;
+    return total;
+}
+
+void process_ls(FILE* fp, Dir *cur) {
+    while (1) {
+        int c = fgetc(fp);
+        if (c == EOF) return;
+        ungetc(c, fp);
+        if (c == '$') {
+            return;
+        }
+        buf[0] = '\0';
+        fgets(buf, 4096, fp);
+        if (buf[0] == 'd') {
+            strtok(buf, " ");
+            char *childname = strtok(NULL, "\n");
+            Dir *newdir = malloc(sizeof(Dir));
+            strcpy(newdir->name, childname);
+            newdir->parent = cur;
+            newdir->next_sibling = cur->child;
+            newdir->child = NULL;
+            newdir->size = 0;
+            cur->child = newdir;
+        } else {
+            char *pos;
+            long fsz = strtol(buf, &pos, 10);
+            cur->size += fsz;
+        }
+    }
+}
+
+void process_cmd(FILE *fp, Dir **pcur) {
+    Dir *cur = *pcur;
+    char *cmd = strtok(buf + 2, " \n"); 
+    if (strcmp(cmd, "cd") == 0) {
+        char *param = strtok(NULL, " \n"); 
+        if (strcmp(param, "/") == 0) {
+            *pcur = &root;
+        } else if (strcmp(param, "..") == 0) {
+            *pcur = cur->parent;
+        } else {
+            Dir *t = cur->child;
+            while (t != NULL) {
+                if (strcmp(t->name, param) == 0) {
+                    *pcur = t;
+                    break;
+                } else {
+                    t = t->next_sibling;
+                }
+            }
+        }
+    } else if (strcmp(cmd, "ls") == 0) {
+        process_ls(fp, cur);
+    }
+}
+
+int main() {
+    FILE *fp = fopen("input", "r");
+    Dir *cur = &root;
+    while (fgets(buf, 4096, fp)) {
+        int len = strlen(buf);
+        if (len <= 1) continue;
+        char c = buf[0];
+        if (c == '$') {
+            process_cmd(fp, &cur);
+        }
+    }
+    totalsize(&root);
+    printf("%d\n", sum);
+}

+ 106 - 0
advent-of-code/2022/07/2.c

@@ -0,0 +1,106 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+
+typedef struct dir Dir;
+
+#define LOG(s) fprintf(stderr, s"\n");
+
+struct dir {
+    char name[32];
+    Dir *parent;
+    Dir *next_sibling;
+    Dir *child;
+    int size;
+};
+
+char buf[4096];
+
+Dir root = {0};
+
+int need = INT_MAX;
+int to_del = INT_MAX;
+
+int totalsize(Dir *dir) {
+    if (dir == NULL) return 0;
+    int total = dir->size;
+    Dir *child = dir->child;
+    while (child != NULL) {
+        total += totalsize(child);
+        child = child->next_sibling;
+    }
+    if (total >= need && total < to_del) to_del = total;
+    return total;
+}
+
+void process_ls(FILE* fp, Dir *cur) {
+    while (1) {
+        int c = fgetc(fp);
+        if (c == EOF) return;
+        ungetc(c, fp);
+        if (c == '$') {
+            return;
+        }
+        buf[0] = '\0';
+        fgets(buf, 4096, fp);
+        if (buf[0] == 'd') {
+            strtok(buf, " ");
+            char *childname = strtok(NULL, "\n");
+            Dir *newdir = malloc(sizeof(Dir));
+            strcpy(newdir->name, childname);
+            newdir->parent = cur;
+            newdir->next_sibling = cur->child;
+            newdir->child = NULL;
+            newdir->size = 0;
+            cur->child = newdir;
+        } else {
+            char *pos;
+            long fsz = strtol(buf, &pos, 10);
+            cur->size += fsz;
+        }
+    }
+}
+
+void process_cmd(FILE *fp, Dir **pcur) {
+    Dir *cur = *pcur;
+    char *cmd = strtok(buf + 2, " \n"); 
+    if (strcmp(cmd, "cd") == 0) {
+        char *param = strtok(NULL, " \n"); 
+        if (strcmp(param, "/") == 0) {
+            *pcur = &root;
+        } else if (strcmp(param, "..") == 0) {
+            *pcur = cur->parent;
+        } else {
+            Dir *t = cur->child;
+            while (t != NULL) {
+                if (strcmp(t->name, param) == 0) {
+                    *pcur = t;
+                    break;
+                } else {
+                    t = t->next_sibling;
+                }
+            }
+        }
+    } else if (strcmp(cmd, "ls") == 0) {
+        process_ls(fp, cur);
+    }
+}
+
+int main() {
+    FILE *fp = fopen("input", "r");
+    Dir *cur = &root;
+    while (fgets(buf, 4096, fp)) {
+        int len = strlen(buf);
+        if (len <= 1) continue;
+        char c = buf[0];
+        if (c == '$') {
+            process_cmd(fp, &cur);
+        }
+    }
+    int total = totalsize(&root);
+    int unused = 70000000 - total;
+    need = 30000000 - unused;
+    totalsize(&root);
+    printf("%d\n", to_del);
+}

+ 989 - 0
advent-of-code/2022/07/input

@@ -0,0 +1,989 @@
+$ cd /
+$ ls
+dir bqc
+dir mwmlf
+dir ngn
+143562 nrwjb
+78449 qqvdcclf
+dir qrnm
+dir smfzmmhc
+116085 tvrms
+dir vrdrsj
+$ cd bqc
+$ ls
+5693 qqvdcclf
+$ cd ..
+$ cd mwmlf
+$ ls
+dir cmfphpc
+dir lqqshq
+dir mwmlf
+dir rlf
+dir smfzmmhc
+$ cd cmfphpc
+$ ls
+235620 tprth.gjn
+82743 vrdrsj.fbl
+$ cd ..
+$ cd lqqshq
+$ ls
+94188 crswqlvd.nsj
+dir dttthls
+60078 lbsfsspm
+dir lqp
+74624 nrwjb
+247709 tjhcqw.wrq
+267693 tvrms
+dir zshrcgfn
+$ cd dttthls
+$ ls
+109072 nrwjb
+31512 qqvdcclf
+$ cd ..
+$ cd lqp
+$ ls
+237917 nrwjb
+45489 vrdrsj.ntw
+$ cd ..
+$ cd zshrcgfn
+$ ls
+185533 smfzmmhc.zzd
+$ cd ..
+$ cd ..
+$ cd mwmlf
+$ ls
+dir bbvhpmsj
+dir mwmlf
+83589 qqvdcclf
+$ cd bbvhpmsj
+$ ls
+232123 hrvlgmjb.wrv
+137190 rlf
+21785 tghvbj.mfr
+25344 vrdrsj.swp
+$ cd ..
+$ cd mwmlf
+$ ls
+dir wnn
+$ cd wnn
+$ ls
+dir thd
+$ cd thd
+$ ls
+36125 lgt
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd rlf
+$ ls
+dir fps
+128272 lggpfj.gnr
+146745 lrdq.zgm
+dir mwmlf
+266383 mwmlf.wnp
+dir ncrlvggp
+dir qpgmrj
+$ cd fps
+$ ls
+dir rqjt
+dir wpqvmn
+$ cd rqjt
+$ ls
+175784 pcdtdblc.znj
+185424 wmstvdt.lhn
+$ cd ..
+$ cd wpqvmn
+$ ls
+141743 ljslzhjl
+$ cd ..
+$ cd ..
+$ cd mwmlf
+$ ls
+169015 rlf
+$ cd ..
+$ cd ncrlvggp
+$ ls
+81450 fqtrpm.mqr
+146399 jtbr
+dir nnqgqqj
+288302 qqvdcclf
+$ cd nnqgqqj
+$ ls
+dir fvbhh
+$ cd fvbhh
+$ ls
+121112 zmn.fjz
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd qpgmrj
+$ ls
+dir hrvlgmjb
+dir pbc
+$ cd hrvlgmjb
+$ ls
+7858 czvpvwz
+$ cd ..
+$ cd pbc
+$ ls
+150816 tprth.gjn
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd smfzmmhc
+$ ls
+dir jwvw
+184385 spbjrlv.spt
+dir trpdgl
+201578 zzhz
+$ cd jwvw
+$ ls
+47747 nrwjb
+$ cd ..
+$ cd trpdgl
+$ ls
+136682 mwmlf
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ngn
+$ ls
+dir fqbph
+282363 lscdth.lbj
+dir mwmlf
+273281 qbpmcqs
+7547 qdffbp
+223960 tprth.gjn
+$ cd fqbph
+$ ls
+dir bgtbmzj
+dir lzjf
+12361 nrwjb
+dir pdsdttz
+150218 pfvtwr.pbf
+dir rlf
+158227 sfhsszcq.tzv
+183244 smfzmmhc.drh
+$ cd bgtbmzj
+$ ls
+dir gvqhtfg
+dir jmvzv
+194008 ldpfs.qhr
+dir mtztr
+11099 nfqmvqcm.ftd
+dir szp
+153379 zrtsd.jwg
+$ cd gvqhtfg
+$ ls
+dir ntq
+dir rlf
+62076 rlf.nlc
+$ cd ntq
+$ ls
+dir wdtzhz
+$ cd wdtzhz
+$ ls
+dir tlqcbp
+$ cd tlqcbp
+$ ls
+102930 smfzmmhc
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd rlf
+$ ls
+185079 tjhcqw.wrq
+$ cd ..
+$ cd ..
+$ cd jmvzv
+$ ls
+dir hfmtzbhf
+61556 jzbvmc
+dir scjpjp
+237487 sgh
+dir smfzmmhc
+$ cd hfmtzbhf
+$ ls
+dir ldt
+136126 tvrms
+$ cd ldt
+$ ls
+dir smfzmmhc
+dir wrhpzrfm
+dir zbcfv
+$ cd smfzmmhc
+$ ls
+86537 tjhcqw.wrq
+$ cd ..
+$ cd wrhpzrfm
+$ ls
+263122 smfzmmhc.shv
+73772 tvrms
+$ cd ..
+$ cd zbcfv
+$ ls
+dir vrdrsj
+$ cd vrdrsj
+$ ls
+220800 zwdbh
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd scjpjp
+$ ls
+38282 rlf
+93306 rlf.blz
+$ cd ..
+$ cd smfzmmhc
+$ ls
+dir gqms
+dir mwmlf
+21587 nrwjb
+184621 zvq.lvr
+$ cd gqms
+$ ls
+dir frbs
+dir rlf
+$ cd frbs
+$ ls
+dir mpdll
+82901 zdmnm.wlc
+$ cd mpdll
+$ ls
+dir mgqd
+dir mrljj
+$ cd mgqd
+$ ls
+75558 vrdrsj.zwc
+$ cd ..
+$ cd mrljj
+$ ls
+dir qmsgj
+128171 qqvdcclf
+245942 rlf.hgt
+dir zzvbt
+$ cd qmsgj
+$ ls
+7382 mwmlf
+$ cd ..
+$ cd zzvbt
+$ ls
+285714 zltjmn
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd rlf
+$ ls
+43731 gttgwfgf.cpd
+dir hvtn
+112018 jdhb.ssj
+195239 rlf.gvd
+200476 spsstb
+$ cd hvtn
+$ ls
+dir zzsfgzc
+$ cd zzsfgzc
+$ ls
+116544 vrdrsj
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd mwmlf
+$ ls
+dir hmpvn
+dir shlnfcpz
+$ cd hmpvn
+$ ls
+55892 qqvdcclf
+$ cd ..
+$ cd shlnfcpz
+$ ls
+144766 fnrhmbbm.rnp
+2622 tjhcqw.wrq
+219884 tprth.gjn
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd mtztr
+$ ls
+dir plngmzl
+dir qbwzhr
+$ cd plngmzl
+$ ls
+153434 dhjv.pfn
+$ cd ..
+$ cd qbwzhr
+$ ls
+26553 mcb.djg
+$ cd ..
+$ cd ..
+$ cd szp
+$ ls
+37741 qqvdcclf
+dir vrdrsj
+$ cd vrdrsj
+$ ls
+dir hznmsclb
+13218 mwmlf.vhs
+102602 rggt
+228549 rlnf.qrt
+$ cd hznmsclb
+$ ls
+17957 mwmlf.lsv
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd lzjf
+$ ls
+40821 lfgh.jwg
+dir rplfgb
+$ cd rplfgb
+$ ls
+301639 zmrpm.pqn
+$ cd ..
+$ cd ..
+$ cd pdsdttz
+$ ls
+294779 tprth.gjn
+$ cd ..
+$ cd rlf
+$ ls
+37853 qst.zgc
+$ cd ..
+$ cd ..
+$ cd mwmlf
+$ ls
+dir dqvhgnj
+dir fvjtnl
+dir jfsgp
+230725 tjhcqw.wrq
+2872 tvrms
+dir zszcsbl
+$ cd dqvhgnj
+$ ls
+214328 bph
+138045 tjhcqw.wrq
+$ cd ..
+$ cd fvjtnl
+$ ls
+62568 sllvfsf
+286839 tjhcqw.wrq
+241004 zvqrg.bsg
+$ cd ..
+$ cd jfsgp
+$ ls
+116677 tprth.gjn
+151347 vtbt
+$ cd ..
+$ cd zszcsbl
+$ ls
+97786 hrvlgmjb.wzz
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd qrnm
+$ ls
+70534 nrwjb
+292701 qqvdcclf
+153174 sjqvgh.nvt
+$ cd ..
+$ cd smfzmmhc
+$ ls
+dir gjrqmcjl
+63451 hrvlgmjb
+dir mwmlf
+dir ncgjsjj
+246754 nrwjb
+dir pvnbj
+dir sjqwr
+dir vrdrsj
+dir vwfnglr
+$ cd gjrqmcjl
+$ ls
+283020 gwcrpgc
+36180 ltmzzz
+dir tpsr
+$ cd tpsr
+$ ls
+51208 nscwcztf.wlh
+121548 smfzmmhc
+$ cd ..
+$ cd ..
+$ cd mwmlf
+$ ls
+263186 mwmlf.fqd
+$ cd ..
+$ cd ncgjsjj
+$ ls
+204801 nrwjb
+116293 rdrmctwg.nqc
+36548 tjhcqw.wrq
+$ cd ..
+$ cd pvnbj
+$ ls
+dir cfmn
+dir cnhmcjp
+231615 ctllrm
+dir czmw
+dir drfzgwtz
+135330 jsm
+dir jtjzwjh
+dir nfg
+279734 qqvdcclf
+220762 tvrms
+35964 vrdrsj.jqj
+$ cd cfmn
+$ ls
+300636 frn.ccd
+dir qmtc
+dir sfvd
+$ cd qmtc
+$ ls
+267669 jwvjslwp.vsp
+180586 tvrms
+$ cd ..
+$ cd sfvd
+$ ls
+169064 cgzmwg.rgr
+dir hsz
+270729 qgmmc.sqq
+dir qpzbffd
+92811 shcpdlm.nwz
+dir sstdzdf
+dir thrzw
+dir wscsr
+$ cd hsz
+$ ls
+301584 qqvdcclf
+$ cd ..
+$ cd qpzbffd
+$ ls
+264911 mwmlf.rvg
+dir rlf
+32109 svhwhw.fdp
+155713 tjhcqw.wrq
+dir vrdrsj
+$ cd rlf
+$ ls
+32357 tvrms
+$ cd ..
+$ cd vrdrsj
+$ ls
+93501 nrwjb
+$ cd ..
+$ cd ..
+$ cd sstdzdf
+$ ls
+241812 mgrvcbjc.rqb
+dir mwmlf
+273819 nvrssljz
+156873 tvrms
+$ cd mwmlf
+$ ls
+133649 wdfvctwp
+$ cd ..
+$ cd ..
+$ cd thrzw
+$ ls
+67552 smfzmmhc
+$ cd ..
+$ cd wscsr
+$ ls
+132663 dnbvw.zwd
+dir ffthd
+293463 hrvlgmjb.fwh
+dir lhjvfdh
+194357 nrwjb
+dir smj
+56668 tprth.gjn
+303099 tvrms
+dir vrdrsj
+$ cd ffthd
+$ ls
+84952 nrwjb
+192566 rlf
+dir slbg
+78478 srzz
+250044 tprth.gjn
+97100 tvrms
+$ cd slbg
+$ ls
+203375 nrwjb
+39137 qqvdcclf
+$ cd ..
+$ cd ..
+$ cd lhjvfdh
+$ ls
+1747 rtphln.pgc
+163505 tjhcqw.wrq
+76891 tprth.gjn
+$ cd ..
+$ cd smj
+$ ls
+6853 hhtdhzgn
+158303 pmsflvrn.hnh
+$ cd ..
+$ cd vrdrsj
+$ ls
+dir jdlc
+263568 vrdrsj
+dir vvtl
+$ cd jdlc
+$ ls
+12435 hqggp.lrq
+292141 ntbcht.zbw
+$ cd ..
+$ cd vvtl
+$ ls
+118913 qqvdcclf
+291087 tjhcqw.wrq
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd cnhmcjp
+$ ls
+263233 tjhcqw.wrq
+$ cd ..
+$ cd czmw
+$ ls
+dir gdsgw
+dir gfd
+3390 mwmlf.zss
+$ cd gdsgw
+$ ls
+298839 dbwsmwnb.svt
+170672 tprth.gjn
+$ cd ..
+$ cd gfd
+$ ls
+193212 tld
+$ cd ..
+$ cd ..
+$ cd drfzgwtz
+$ ls
+257122 tprth.gjn
+$ cd ..
+$ cd jtjzwjh
+$ ls
+dir jhpp
+274740 lnrwrpjj
+35217 ptzmfsmr.pwc
+67549 qpdj.pfc
+$ cd jhpp
+$ ls
+40279 jgstzhw.cbq
+$ cd ..
+$ cd ..
+$ cd nfg
+$ ls
+126237 mqfl.flt
+$ cd ..
+$ cd ..
+$ cd sjqwr
+$ ls
+53423 bngqncqn
+dir lblv
+178935 nrwjb
+188826 tprth.gjn
+dir vrdrsj
+286449 vrdrsj.svn
+$ cd lblv
+$ ls
+dir bptgd
+215281 jjvcgb.nwc
+dir rlf
+300004 tvrms
+$ cd bptgd
+$ ls
+19667 mwmlf
+$ cd ..
+$ cd rlf
+$ ls
+dir mwmlf
+263985 rbhz.plr
+$ cd mwmlf
+$ ls
+254206 smfzmmhc
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd vrdrsj
+$ ls
+dir vrdrsj
+$ cd vrdrsj
+$ ls
+250310 hrvlgmjb.bbf
+130543 mwmlf.lbq
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd vrdrsj
+$ ls
+dir mwwr
+dir rlf
+$ cd mwwr
+$ ls
+dir lhz
+$ cd lhz
+$ ls
+dir ssz
+dir vrdrsj
+$ cd ssz
+$ ls
+107472 hcnsgjhj
+$ cd ..
+$ cd vrdrsj
+$ ls
+303694 rlf
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd rlf
+$ ls
+299875 nrwjb
+$ cd ..
+$ cd ..
+$ cd vwfnglr
+$ ls
+dir mtvgvvcr
+114118 mzhgbj.zvv
+273343 tprth.gjn
+28259 wgrp
+$ cd mtvgvvcr
+$ ls
+218927 bvlv.npm
+64283 hrf.czg
+dir mnmqqqgh
+103691 qtb.fnb
+dir smfzmmhc
+252712 zjlgc.sfg
+$ cd mnmqqqgh
+$ ls
+231583 hrvlgmjb.mtq
+$ cd ..
+$ cd smfzmmhc
+$ ls
+dir dzjfd
+dir jjslqwn
+264128 nrwjb
+257359 tvrms
+$ cd dzjfd
+$ ls
+dir smfzmmhc
+237116 zzgr
+$ cd smfzmmhc
+$ ls
+230613 qqvdcclf
+91615 smfzmmhc
+$ cd ..
+$ cd ..
+$ cd jjslqwn
+$ ls
+98708 mwmlf.qtq
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd vrdrsj
+$ ls
+dir bfgbvlcd
+131293 fgh.djg
+125876 lcwjtdf.sbl
+dir pnds
+263022 tvrms
+$ cd bfgbvlcd
+$ ls
+276134 vrdrsj
+$ cd ..
+$ cd pnds
+$ ls
+dir bffz
+dir jwcjpt
+dir pvnbz
+$ cd bffz
+$ ls
+194315 dndbj.gww
+115061 lspnmhml.fdb
+239758 tjhcqw.wrq
+124732 wjcbcvfd
+$ cd ..
+$ cd jwcjpt
+$ ls
+dir dcmcgc
+dir llt
+dir rfh
+dir scljqqq
+127542 tvrms
+$ cd dcmcgc
+$ ls
+dir fhdvgvtz
+dir hrvlgmjb
+dir rlf
+dir smfzmmhc
+$ cd fhdvgvtz
+$ ls
+33684 lcl
+dir lrsjg
+203352 npvgs.qdw
+6985 tvrms
+102991 wmnrjw.chn
+162852 wrcjmll.rlb
+$ cd lrsjg
+$ ls
+dir jgfpvdc
+84338 mwmlf.vdw
+147349 nrwjb
+dir sblp
+dir vrdrsj
+$ cd jgfpvdc
+$ ls
+265563 chlgpdsp.hrv
+dir dsc
+dir gvrg
+dir mwmlf
+dir qrqvl
+100762 rlf
+300872 sjvgwmdg.qhg
+164004 tzqmh
+300736 zhgmdcl.bfq
+$ cd dsc
+$ ls
+284520 rtt.qps
+263890 smfzmmhc
+$ cd ..
+$ cd gvrg
+$ ls
+dir hzclphlb
+dir vcnlmdp
+$ cd hzclphlb
+$ ls
+295429 mcdjsms.zss
+$ cd ..
+$ cd vcnlmdp
+$ ls
+dir wrlhqh
+$ cd wrlhqh
+$ ls
+41526 tmf.ctw
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd mwmlf
+$ ls
+dir cwsrt
+dir grqng
+$ cd cwsrt
+$ ls
+dir ztmtjwj
+$ cd ztmtjwj
+$ ls
+17063 svdm
+$ cd ..
+$ cd ..
+$ cd grqng
+$ ls
+216391 tvrms
+$ cd ..
+$ cd ..
+$ cd qrqvl
+$ ls
+1835 hrvlgmjb
+43155 mwmlf.sng
+238089 ngjg.zmc
+$ cd ..
+$ cd ..
+$ cd sblp
+$ ls
+178768 bgfnqf
+$ cd ..
+$ cd vrdrsj
+$ ls
+255854 qrrc.bbc
+178991 smfzmmhc
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd hrvlgmjb
+$ ls
+dir fjnp
+70585 hrvlgmjb
+dir qsrgzrdf
+43606 rmjzzgrs
+dir vds
+dir vrdrsj
+35098 wthtcg.wgd
+$ cd fjnp
+$ ls
+19740 qqvdcclf
+dir smfzmmhc
+$ cd smfzmmhc
+$ ls
+dir nnbc
+$ cd nnbc
+$ ls
+100360 mwmlf
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd qsrgzrdf
+$ ls
+279143 fgthp.qmg
+$ cd ..
+$ cd vds
+$ ls
+247595 gnclhrw.mwt
+303125 pcdnbq.zbs
+38092 tprth.gjn
+dir vrdrsj
+$ cd vrdrsj
+$ ls
+dir tnjg
+$ cd tnjg
+$ ls
+37395 brcbr
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd vrdrsj
+$ ls
+94639 tjhcqw.wrq
+$ cd ..
+$ cd ..
+$ cd rlf
+$ ls
+dir btqjc
+108141 dnplntn.mwr
+31100 lmfsgd
+263849 lpbdq.vdp
+109813 smfzmmhc.wsr
+dir vrdrsj
+$ cd btqjc
+$ ls
+31933 nwszgpm.qqr
+106249 rjqvhccg
+16766 vgzqz
+$ cd ..
+$ cd vrdrsj
+$ ls
+dir bfv
+dir jtnqr
+19593 mwmlf
+220657 rggqjp.pnt
+$ cd bfv
+$ ls
+48210 tdgd.ppw
+232603 tjhcqw.wrq
+$ cd ..
+$ cd jtnqr
+$ ls
+dir bct
+202702 grstm.ltj
+dir mwmlf
+258253 tvrms
+$ cd bct
+$ ls
+131860 fwfzsc.pls
+42452 thp
+$ cd ..
+$ cd mwmlf
+$ ls
+284648 vdjh.dct
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd smfzmmhc
+$ ls
+dir fvtlvtlq
+108335 jdbdb
+dir vrdrsj
+$ cd fvtlvtlq
+$ ls
+237419 ctwllbmc
+36570 qqvdcclf
+dir rlf
+92173 smfzmmhc
+$ cd rlf
+$ ls
+89469 dtg.bvl
+88361 mqrr
+$ cd ..
+$ cd ..
+$ cd vrdrsj
+$ ls
+47472 fftpd
+53118 qqvdcclf
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd llt
+$ ls
+98255 hnvqtbcn
+$ cd ..
+$ cd rfh
+$ ls
+29828 hgwh.tnl
+dir rchll
+dir smfzmmhc
+$ cd rchll
+$ ls
+240042 rdj.wrv
+$ cd ..
+$ cd smfzmmhc
+$ ls
+45192 clnbddrh.lbq
+105827 ndzjnttr
+140925 nmnr.lct
+dir rlf
+238998 tprth.gjn
+dir vrdrsj
+$ cd rlf
+$ ls
+115973 mnnr.smj
+75630 zld.hzv
+$ cd ..
+$ cd vrdrsj
+$ ls
+302512 phmvhpvb.fwh
+dir tjrpwhc
+$ cd tjrpwhc
+$ ls
+13802 hrvlgmjb
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd ..
+$ cd scljqqq
+$ ls
+228515 hrvlgmjb.nhn
+246019 mwmlf
+9535 mwmlf.nvm
+$ cd ..
+$ cd ..
+$ cd pvnbz
+$ ls
+dir hrvlgmjb
+279877 mwmlf
+dir zdqprndl
+278221 zwwtflb
+$ cd hrvlgmjb
+$ ls
+dir vggpr
+$ cd vggpr
+$ ls
+270197 cpzj
+$ cd ..
+$ cd ..
+$ cd zdqprndl
+$ ls
+197398 tprth.gjn
+

+ 106 - 0
advent-of-code/2022/08/1.c

@@ -0,0 +1,106 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct {
+    uint8_t *data;
+    int rows;
+    int columns;
+    int cap;
+} Mat;
+
+int get(Mat *m, int row, int column) {
+    return m->data[row * m->columns + column];
+}
+
+void set(Mat *m, int row, int column, uint8_t value) {
+    m->data[row * m->columns + column] = value;
+}
+
+char buf[4096];
+
+int main() {
+    Mat m;
+    m.data = malloc(4096);
+    m.cap = 4096;
+    m.rows = 0;
+    m.columns = 0;
+    
+    
+    FILE *fp = fopen("input", "r");
+    while(fgets(buf, 4096, fp)) {
+        int len = strlen(buf);
+        if (len <= 1) break;
+        if (m.columns == 0) m.columns = len - 1;
+        while (m.cap < (m.rows + 1) * m.columns) {
+            m.data = realloc(m.data, m.cap * 2);
+            m.cap *= 2;
+        }
+        memcpy(m.data + m.rows * m.columns, buf, m.columns);
+        m.rows++;
+    }
+    
+    Mat vis = m;
+    vis.data = malloc(m.rows * m.columns);
+    vis.cap = m.rows * m.columns;
+    memset(vis.data, 0, vis.cap);
+    
+    // top-down scan
+    for (int i = 0; i < m.columns; i++) {
+        int curmax = -1;
+        for (int j = 0; j < m.rows; j++) {
+            if (get(&m, j, i) > curmax) {
+                set(&vis, j, i, 1);
+                curmax = get(&m, j, i);
+            }
+        }
+    }
+    // down-top scan
+    for (int i = 0; i < m.columns; i++) {
+        int curmax = -1;
+        for (int j = m.rows - 1; j >= 0; j--) {
+            if (get(&m, j, i) > curmax) {
+                set(&vis, j, i, 1);
+                curmax = get(&m, j, i);
+            }
+        }
+    }
+    
+    // left-right scan
+    for (int i = 0; i < m.rows; i++) {
+        int curmax = -1;
+        for (int j = 0; j < m.columns; j++) {
+            if (get(&m, i, j) > curmax) {
+                set(&vis, i, j, 1);
+                curmax = get(&m, i, j);
+            }
+        }
+    }
+    
+    // right-left scan
+    for (int i = 0; i < m.rows; i++) {
+        int curmax = -1;
+        for (int j = m.columns - 1; j >= 0; j--) {
+            if (get(&m, i, j) > curmax) {
+                set(&vis, i, j, 1);
+                curmax = get(&m, i, j);
+            }
+        }
+    }
+    
+    // count
+    int count = 0;
+    for (int i = 0; i < m.columns; i++) {
+        int curmax = -1;
+        for (int j = 0; j < m.rows; j++) {
+            if (get(&vis, j, i) > 0) {
+                count++;
+            }
+        }
+    }
+    
+    printf("%d", count);
+    return 0;
+}
+

+ 82 - 0
advent-of-code/2022/08/2.c

@@ -0,0 +1,82 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct {
+    uint8_t *data;
+    int rows;
+    int columns;
+    int cap;
+} Mat;
+
+
+
+char buf[4096];
+
+int get(Mat *m, int row, int column) {
+    return m->data[row * m->columns + column];
+}
+
+int score(Mat *m, int i, int j) {
+    int height = get(m, i, j);
+    int s = 1;
+    int count = 0;
+    for (int p = i - 1; p >= 0; p--) {
+        count++;
+        if (get(m, p, j) >= height) break;
+    }
+    s *= count;
+    count = 0;
+    for (int p = i + 1; p < m->rows; p++) {
+        count++;
+        if (get(m, p, j) >= height) break;
+    }
+    s *= count;
+    count = 0;
+    for (int p = j - 1; p >= 0; p--) {
+        count++;
+        if (get(m, i, p) >= height) break;
+    }
+    s *= count;
+    count = 0;
+    for (int p = j + 1; p < m->columns; p++) {
+        count++;
+        if (get(m, i, p) >= height) break;
+    }
+    s *= count;
+    return s;
+}
+
+int main() {
+    Mat m;
+    m.data = malloc(4096);
+    m.cap = 4096;
+    m.rows = 0;
+    m.columns = 0;
+
+    FILE *fp = fopen("input", "r");
+    while(fgets(buf, 4096, fp)) {
+        int len = strlen(buf);
+        if (len <= 1) break;
+        if (m.columns == 0) m.columns = len - 1;
+        while (m.cap < (m.rows + 1) * m.columns) {
+            m.data = realloc(m.data, m.cap * 2);
+            m.cap *= 2;
+        }
+        memcpy(m.data + m.rows * m.columns, buf, m.columns);
+        m.rows++;
+    }
+
+    int maxscore = -1;
+    for (int i = 0; i < m.rows; i++) {
+        for (int j = 0; j < m.columns; j++) {
+            int s = score(&m, i, j);
+            if (s > maxscore) maxscore = s;
+        }
+    }
+
+    printf("%d", maxscore);
+    return 0;
+}
+

+ 100 - 0
advent-of-code/2022/08/input

@@ -0,0 +1,100 @@
+023322201321333021030055115233032255002015013044155106103011355435025324002515233340032023013101000
+333001114104040220402023231114145321226614100664520643140442646451222053211251540001413110202310311
+001122230342102202311114141015535535515054203105023201320052153606250133240404425023031241331132300
+200333324214423413533144232012440022651463612352410034332230305463511214140020132330411142212122132
+102231001140012234010224535134246522662353351650411444516502333264512265503131345351241014030131031
+123304301244220202523215244250456660036136615601260455124040610534545066145403001311004201223131020
+002003410244152204332045034034653220253344012342462512115252223350162403054433114541414402004230003
+200103132341510422452452250254525020361416010364344643552146232455465641450614242024420434042322023
+334030404131124213210110065630054201305051151553713765154353152015404165650352432220042240121431441
+310413343322502004445560402155236460562124231544313721765226711114232542101026341112103205000204213
+232113123250204533056410314645353415455472754633613676614123217267412616420454632514221110454141242
+212320215533235344164103161651553275174711711164356766211341364224361343166521543245501043403313020
+132311140310204051126312005021325536142561672714571554376563517456554662362641056124005412144432424
+231444210534331154552403515511564757547561574455777217737432721253356756144650425243250053400131102
+402305010405005545336224323172551713366727316646463352447124162453153245317302364400260244035250203
+141021340213551650603550523267762213732351446246376356435541361325455465416164254305301451555014314
+020311351332141136563303441244763611434365535243774772754265656372342514675761031061364550351222431
+413245211550112425253426472765645176882866845564838257856366634524532753142526650514203343254534533
+435452451101402442345414221334613563324588667748572632844384676268337331257112272444312406000210430
+010335302204230321512415571174254452865672753726557623855355483625486653112141562116114316324324330
+221535255034163464174647563575653473563783762385456872577376838278724787533322241360046164145143444
+023200114430135341415733511326526724522547637566455625677487874884677748355767326531554460531505423
+041325411513220355333664623324656723324354775368994776576453743226382234255146721766626165345032040
+251500552152654263475146756832747868664284795636459947469897787877228355585665321152065265452205010
+140403120623541575137615727682856477356763944336369569588959794665537746822241226172105532241014054
+453231451142201467644523625687875786636878558948395383658853983478274336626223261322670214456000012
+421401202553552263622464483244566634338573647446753646346899559455467362654676475666633635516251041
+323131612232542573237614644344532373875676439947597665797366646687698528458322143111266523602124520
+350402001311666477622377487567429458933773439373736474937847339863733784866824546353257615331613522
+250022536362331172722668268236867398553738597646787654948564865538536978368448574575767731313436224
+402502304364172157656277664376787847997386975947666779469869555838787877535542353742444675210555330
+534146105561234616268536526749363399896897558457877796995459475968646953877355285715651641221535521
+303646554131712176733825365433864398786578577698887656988984667665877535682343345654563777135012425
+324510121102677116583635857975693374387486566897798478994684955676694383688884846856322762430431244
+514510651516612266887483855356674543785959656848847555648447748596549489474486425724612453763154612
+231062261547177317255662764857346469768747497764945945965757496698454569588782672772143151160442034
+320163342042441177863252689394446487894788488755875979945694655996536354949538842337254673262442021
+332245516535336165424866833774775969444884989558587897695567778655663877687674655723514215474233645
+305304310765327184528544539776454864657499589567575587578857495776446886984958232865323375223161226
+041321134716326427345342674747478687768869565659869667675778958466596675773462357464554753265120531
+214135547421514268628575739385987456987579588577977997755957594894869954387663668252657636412005462
+303014312474744334438854664364557544877859878676955887957766794676788758349967423548311115421012031
+165600101766416548688563985883658885658968698975789866869555596449468493399749732764355275156263540
+023235337273751276356326956636445746559765659596677778588597776965655557544476853383777537142222045
+263022507373222354242473545837465487886578985999866897786966667659976798784598852254766575426052230
+051533463326323676244499536339757747598658665666877776665556856698587598784344845678845431463000630
+350253347271711387537784387454469565865799697798999978977698659775896784977364666572732246234315203
+341005566337216558547638778897554744866597657879686786676958688799857477968856776862486125354403122
+621400523354423736428594359887676575878885656668779879866568999864874549669688988364688346235212421
+505135676316352285588875489864578745965769688688697676766658757779794959756587955783527662755755550
+412201242457267256768457574464866864796989986797678879786798988756654489745697566438648525314315120
+465513676443563625564338436498947956778678989979778889888765868665989946774539564524422761417235426
+232566141256427682382546587547954498979759697699669866686878965577999966363744744433546335352521216
+426641574412112675686545366888568858676989567798688877989897665567789554385448648853822213741324260
+602662554527175344426766863695666656977788897678788977877689686966648487456799824486782375417461456
+651655223645567747258565638365989956656677767968776687998958899596855768455367926645753536315666440
+212101665433515738883643775536779945579976596567786779666997868695869664755484454857473235121532445
+050653146543541364346323653736454948456785699959977797599687588797759765975388673684657445217361543
+064052466421561857423457964965989558587599968966579676777587688855745463996659346764634531763304515
+561551524332754584232789895869594554468566797988665678658758669559768854455376555847876562742331036
+325314542655721556272469893855789755477667687765899758695565678476456955583865232637557334756250066
+142312051576173167535685734664695895767875888777758989579876988865676775699635377648226576522411336
+264405030674612265825464755969447459794894978557578676887898777658666398378833868636241721763463230
+226106504172532322756823879646746684578495976958986858585948768946563458695562852538355727621152553
+010455552047535263283644464733478545978574965849565776774485474589895848955474423534356723663335362
+344641162242761347244832643748385795576889884668584468667945796945977446594377836734377545264505202
+034234561142745745528645734559984454564945447687598468749845599859594766854257575231153232601521102
+540240251242253675342846365745937738658974994554845749686786757743643844958548825225537647141563520
+320206262105457111733842244254676634937587947685687474475998874887466954622334232535552551031406142
+514054400340122471674738287345975345554989578889687665977944569694376357735848686622213124245443111
+052523034313452374556723742753478844856887759998965975878847759763497772425467874322521153360256440
+222351100013277574717265537284546936359665666859668677959565397475778974448826377463727135443542405
+523350424041153637642545773628235636497539933545997447966795573653867437626874225421751645016443433
+312215360245443761525542452352242557749988469436989969593867853433893324724227766423165014506020545
+043301115145336365333177277445276796874975464449775989576653853847933547267577416233622000500250204
+010430464664035143116714487846468273747588694587534759975435534953667857468842565117553510343230004
+111523155243403247241622253657835525667595756477383976646535657887238383548526267564106666155410400
+331143444424136613727453361363448436384873986369887364654778533252327534346555721777153155043314351
+504400535003012413114454471286326822622755886597475895367832256758387882813774733523434423050344153
+031223513454351150575175721474553686462624367655585284263625746377334236266723565566434350565141505
+025403550435001645163142764332743577336357527577475885842585236426673634564751755732464325035342155
+330421145016464561337162167576453887346346662377775778365787447636782124712553177545200210154304402
+145134011342136360041775757661255522836536453757768526277233484847723214161226742551423535541545141
+120424442054514402401215773774217135788666446728752473458828676541113522171226164646163420350045352
+320300042111566320001225347233316661324787538457643633368888833762234461732376203555404440133510432
+040041531054536240250125277267321574523425854345828833847341557475227253772735231005626405434053110
+132032504525425163004536034651157757674257322776353873743315142663344157512332514161361250434024042
+412131202502245064331035365341616236636215157647137215154547671162456657640014205623004340003440042
+023402254450222445315156123064462256711636662722324113154653243672151665651410655544242445235033214
+030244442213354225245214420026615315266333321464545152275426214253367063445442123421222354522130443
+000404231340354415013353552601201031525655623675321422372514761615551410213215314045514055253413013
+424431424142012102050025660124656001446655324426514434456334347361251512153650510232214515000212124
+210444432021415253200025444324242403030613314565321154125546321420536312644040231522244324413232134
+304213344021421403321314610146511434231323466075612476301612210064435402534231034321214151220134113
+002213301410344533212501252101020156640662021153562032241365344241433044503534355443400214003434001
+022314333231043300404453132012064266633361244266541036223442060142103261113425041124401240222232000
+311003213421304332112132501310051205416305264342531341103003055416252025531115042105141422320032103
+133213004320332415205534404040532521244601552314401414553622300634114003055144015221303103000233300
+121313142330141112135522531401154344545521012501566652210433310564254034403545252414443110303020313
+

+ 91 - 0
advent-of-code/2022/09/1.c

@@ -0,0 +1,91 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <htable.h>
+#include <crc32.h>
+
+typedef struct {
+    int x;
+    int y;
+} Point;
+
+uint32_t point_hash(void *point) {
+    return crc32(0, point, sizeof(Point));
+}
+
+bool point_eq(void *x, void *y) {
+    Point *a = x, *b = y;
+    return (a->x == b->x) && (a->y == b->y);
+}
+
+typedef struct {
+    Point head;
+    Point tail;
+} State;
+
+bool is_adjacent(Point p1, Point p2) {
+    if (abs(p1.x - p2.x) <= 1 && abs(p2.y - p1.y) <= 1) {
+        return true;
+    }
+    return false;
+}
+
+void run_impl(State *s, HTable *record, int dx, int dy) {
+    Point new = {s->head.x + dx, s->head.y + dy};
+    if (!is_adjacent(s->tail, new)) {
+        s->tail = s->head;
+        htable_insert(record , &(s->tail));
+    }
+    s->head = new;
+}
+
+void run(State *s, HTable *record, char direction, int step) {
+    int dx, dy;
+    switch (direction) {
+        case 'U':
+            dx = 0; dy = 1;
+            break;
+        case 'D':
+            dx = 0; dy = -1;
+            break;
+        case 'L':
+            dx = -1; dy = 0;
+            break;
+        case 'R':
+            dx = 1; dy = 0;
+            break;
+        default:
+            return;
+    }
+    for (int i = 0; i < step; i++) {
+        run_impl(s, record, dx, dy);
+    }
+}
+
+int count_htable(HTable *ht) {
+    int count = 0;
+    for (void *iter = htable_begin(ht);
+            iter != NULL;
+            iter = htable_next(ht, iter)) {
+        count++;
+    }
+    return count;
+}
+
+int main() {
+    FILE *fp = fopen("input", "r");
+    State state;
+    state.head = (Point){0, 0};
+    state.tail = (Point){0, 0};
+    char direction;
+    int step;
+    HTable record;
+    htable_init(&record, sizeof(Point), -1, point_hash, point_eq);
+    while (fscanf(fp, "%c %d", &direction, &step) != EOF) {
+        run(&state, &record, direction, step);
+    }
+    int count = count_htable(&record);
+    printf("%d\n", count);
+    return 0;
+}
+

+ 106 - 0
advent-of-code/2022/09/2.c

@@ -0,0 +1,106 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <htable.h>
+#include <crc32.h>
+
+typedef struct {
+    int x;
+    int y;
+} Point;
+
+uint32_t point_hash(void *point) {
+    return crc32(0, point, sizeof(Point));
+}
+
+bool point_eq(void *x, void *y) {
+    Point *a = x, *b = y;
+    return (a->x == b->x) && (a->y == b->y);
+}
+
+typedef struct {
+    Point knots[10];
+} State;
+
+bool is_adjacent(Point p1, Point p2) {
+    if (abs(p1.x - p2.x) <= 1 && abs(p2.y - p1.y) <= 1) {
+        return true;
+    }
+    return false;
+}
+
+Point catchup(Point prev, Point new) {
+    int dx = new.x - prev.x;
+    int dy = new.y - prev.y;
+    int cx = 0, cy = 0;
+    if (dx > 0) cx = 1;
+    if (dx < 0) cx = -1;
+    if (dy > 0) cy = 1;
+    if (dy < 0) cy = -1;
+    return (Point){prev.x + cx, prev.y + cy};
+}
+
+void run_impl(State *s, HTable *record, int dx, int dy) {
+    s->knots[0].x += dx;
+    s->knots[0].y += dy;
+    for (int i = 1; i < 10; i++) {
+        if (is_adjacent(s->knots[i], s->knots[i-1])) {
+            continue;
+        }
+        s->knots[i] = catchup(s->knots[i], s->knots[i-1]);
+        if (i == 9) {
+            htable_insert(record , &(s->knots[i]));
+        }
+    }
+}
+
+void run(State *s, HTable *record, char direction, int step) {
+    int dx, dy;
+    switch (direction) {
+        case 'U':
+            dx = 0; dy = 1;
+            break;
+        case 'D':
+            dx = 0; dy = -1;
+            break;
+        case 'L':
+            dx = -1; dy = 0;
+            break;
+        case 'R':
+            dx = 1; dy = 0;
+            break;
+        default:
+            return;
+    }
+    for (int i = 0; i < step; i++) {
+        run_impl(s, record, dx, dy);
+    }
+}
+
+int count_htable(HTable *ht) {
+    int count = 0;
+    for (void *iter = htable_begin(ht);
+            iter != NULL;
+            iter = htable_next(ht, iter)) {
+        Point *p = iter;
+        count++;
+    }
+    return count;
+}
+
+int main() {
+    FILE *fp = fopen("input", "r");
+    State state = {0};
+    char direction;
+    int step;
+    HTable record;
+    htable_init(&record, sizeof(Point), -1, point_hash, point_eq);
+    htable_insert(&record, &state.knots[9]);
+    while (fscanf(fp, "%c %d", &direction, &step) != EOF) {
+        run(&state, &record, direction, step);
+    }
+    int count = count_htable(&record);
+    printf("%d\n", count);
+    return 0;
+}
+

+ 12 - 0
advent-of-code/2022/09/Makefile

@@ -0,0 +1,12 @@
+all: 1.out 2.out
+LIB=../lib/crc32.c ../lib/htable.c
+
+1.out: 1.c
+	cc -g -I../lib/ $< $(LIB) -o $@
+
+2.out: 2.c
+	cc -g -I../lib/ $< $(LIB) -o $@
+
+.PHONY: clean
+clean:
+	-rm *.out

+ 2001 - 0
advent-of-code/2022/09/input

@@ -0,0 +1,2001 @@
+D 1
+L 2
+D 2
+L 1
+R 1
+D 1
+R 2
+U 1
+R 1
+D 2
+R 1
+L 2
+U 1
+D 2
+R 1
+L 2
+R 1
+U 2
+D 2
+L 1
+U 2
+R 2
+D 1
+R 2
+D 1
+U 2
+D 2
+L 1
+U 1
+R 1
+D 1
+U 1
+D 1
+U 2
+L 2
+R 1
+U 1
+R 2
+L 2
+U 2
+R 1
+L 1
+U 1
+L 2
+R 1
+D 1
+L 1
+D 2
+U 1
+D 1
+R 1
+L 2
+D 1
+R 2
+L 2
+U 1
+D 2
+R 2
+D 1
+U 1
+R 2
+D 1
+L 1
+U 2
+R 2
+D 1
+U 2
+R 1
+D 2
+R 1
+D 1
+U 2
+R 2
+U 1
+R 1
+U 1
+L 1
+U 2
+D 1
+R 2
+D 2
+R 2
+D 1
+L 2
+D 2
+L 1
+R 1
+U 1
+D 2
+L 2
+D 2
+L 1
+D 2
+R 1
+D 1
+R 2
+L 2
+R 2
+D 2
+R 1
+D 2
+R 2
+U 2
+L 2
+R 1
+U 1
+R 1
+U 1
+D 1
+U 1
+R 2
+L 1
+R 1
+D 2
+U 3
+R 1
+L 2
+D 1
+R 3
+U 1
+D 2
+R 1
+D 1
+R 2
+L 1
+R 2
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 14
+D 5
+L 4
+U 7
+R 15
+D 6
+L 15
+U 14
+D 10
+L 3
+D 9
+L 15
+R 9
+U 1
+L 4
+D 15
+U 12
+D 9
+R 8
+D 14
+R 15
+D 7
+U 18
+L 5
+U 6
+L 6
+D 10
+L 18
+R 3
+L 11
+D 10
+L 13
+U 13
+L 13
+R 3
+L 1
+U 3
+D 7
+L 9
+R 6
+U 18
+R 18
+U 15
+R 9
+U 4
+R 12
+L 12
+D 6
+U 10
+D 13
+L 6
+R 13
+L 13
+D 12
+U 4
+D 15
+L 12
+D 10
+U 12
+R 8
+L 13
+D 11
+L 13
+U 12
+D 5
+U 12
+R 10
+L 9
+U 13
+R 7
+D 2
+L 6
+U 13
+D 15
+R 3
+U 4
+D 5
+L 2
+D 10
+U 7
+L 2
+D 5
+L 5
+D 11
+R 5
+U 18
+L 9
+D 8
+U 12
+R 3
+U 8
+R 14
+D 1
+R 4
+L 5
+D 10
+R 18
+D 2
+R 9
+L 14
+U 4
+R 15
+D 4
+R 2
+L 4
+U 13
+R 6
+U 18
+D 4
+U 7
+L 15
+D 1
+L 6
+D 3
+U 15
+L 17
+U 15
+R 14
+L 9
+U 5
+L 4
+U 13
+R 5
+D 17
+U 17
+L 15
+U 14
+R 5
+L 1
+R 9
+U 8
+R 15
+U 18
+L 12
+R 13
+D 15
+R 5
+L 11
+R 5
+U 12
+R 8
+L 6
+R 1
+L 10
+U 17
+L 16
+U 12
+L 14
+D 16
+U 13
+D 18
+L 5
+U 16
+L 11
+D 15
+U 2
+L 9
+D 5
+L 17
+D 17
+L 4
+D 4
+L 19
+R 6
+D 6
+U 18
+R 16
+U 12
+D 2
+L 4
+U 19
+R 6
+U 1
+D 15
+R 13
+U 18
+D 12
+L 9
+U 1
+R 13
+D 18
+L 4
+D 17
+L 9
+D 8
+U 3
+D 5
+R 7
+L 16
+U 3
+L 1
+D 13
+R 17
+D 8
+R 9
+U 17
+L 2
+U 17
+D 16
+L 8
+R 9
+D 7
+L 16
+R 10
+L 16
+D 10
+L 15
+U 17
+L 2
+U 10
+D 16
+R 1
+L 2
+D 4
+R 13
+L 12
+D 8
+U 10
+L 6
+D 14
+L 6
+D 11
+R 1
+D 10
+L 18
+D 12
+R 14
+L 3
+D 7
+L 12
+R 19
+U 5
+R 19
+D 3
+L 15
+U 13
+

+ 54 - 0
advent-of-code/2022/10/1.c

@@ -0,0 +1,54 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+#include <str.h>
+
+int strength = 1;
+int sum = 0;
+int time = 1;
+
+int process() {
+    if (time == 20 || time == 60 || time == 100
+            || time == 140 || time == 180 || time == 220) {
+        sum += strength * time;
+    }
+}
+
+void tick(int value) {
+    process();
+    time++;
+    strength += value;
+}
+
+int main() {
+    FILE *fp = fopen("input", "r");
+    while (true) {
+        char *rawline = fgetline(fp);
+        if (rawline == NULL) break;
+        char *line = str_strip(rawline);
+        free(rawline);
+        char** words = str_split(line, ' ');
+        if (words == NULL) {
+            free(line);
+            continue;
+        }
+        if (words[0] == NULL) {
+            free(line); free(words);
+            continue;
+        }
+        if (strcmp(words[0], "noop") == 0) {
+            tick(0);
+        } else if (strcmp(words[0], "addx") == 0) {
+            tick(0);
+            char *ptr;
+            int value = strtol(words[1], &ptr, 10);
+            tick(value);
+        }
+        str_list_free(words);
+        free(line);
+    }
+    printf("%d\n", sum);
+    return 0;
+}

+ 62 - 0
advent-of-code/2022/10/2.c

@@ -0,0 +1,62 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+#include <str.h>
+
+char screen[6][40];
+
+int regx = 0;
+int time = 0;
+
+void process() {
+    if (time >= 240) return;
+    int delta = time % 40 - regx;
+    if (delta <= 2 && delta >= 0) {
+        screen[time / 40][time % 40] = '#';
+    }
+}
+
+void tick(int value) {
+    process();
+    time++;
+    regx += value;
+}
+
+int main() {
+    memset(screen, '.', 240);
+    FILE *fp = fopen("input", "r");
+    while (true) {
+        char *rawline = fgetline(fp);
+        if (rawline == NULL) break;
+        char *line = str_strip(rawline);
+        free(rawline);
+        char** words = str_split(line, ' ');
+        if (words == NULL) {
+            free(line);
+            continue;
+        }
+        if (words[0] == NULL) {
+            free(line); free(words);
+            continue;
+        }
+        if (strcmp(words[0], "noop") == 0) {
+            tick(0);
+        } else if (strcmp(words[0], "addx") == 0) {
+            tick(0);
+            char *ptr;
+            int value = strtol(words[1], &ptr, 10);
+            tick(value);
+        }
+        str_list_free(words);
+        free(line);
+    }
+    for (int i = 0; i < 6; i++) {
+        for (int j = 0; j < 40; j++) {
+            printf("%c", screen[i][j]);
+        }
+        printf("\n");
+    }
+    return 0;
+}

+ 12 - 0
advent-of-code/2022/10/Makefile

@@ -0,0 +1,12 @@
+all: 1.out 2.out
+LIB=../lib/str.c
+
+1.out: 1.c
+	cc -g -I../lib/ $< $(LIB) -o $@
+
+2.out: 2.c
+	cc -g -I../lib/ $< $(LIB) -o $@
+
+.PHONY: clean
+clean:
+	-rm *.out

+ 141 - 0
advent-of-code/2022/10/input

@@ -0,0 +1,141 @@
+noop
+addx 25
+addx -5
+addx -14
+addx 4
+noop
+addx 2
+addx 3
+noop
+noop
+noop
+noop
+addx 3
+addx 5
+addx 2
+noop
+noop
+addx 5
+noop
+noop
+noop
+addx 1
+addx 2
+addx 5
+addx -40
+addx 5
+noop
+addx 26
+addx -20
+addx -3
+addx 2
+noop
+addx -4
+addx 9
+addx 5
+addx 2
+addx 11
+addx -10
+addx 2
+addx 5
+addx 2
+addx 5
+noop
+noop
+noop
+addx -31
+addx 32
+addx -37
+addx 1
+addx 8
+addx 13
+addx -15
+addx 4
+noop
+addx 5
+noop
+addx 3
+addx -2
+addx 4
+addx 1
+addx 4
+addx -14
+addx 15
+addx 4
+noop
+noop
+noop
+addx 3
+addx 5
+addx -40
+noop
+addx 5
+addx 8
+addx -3
+noop
+addx 2
+addx 9
+addx -4
+noop
+noop
+noop
+noop
+addx 5
+addx -9
+addx 10
+addx 4
+noop
+noop
+addx 5
+addx -19
+addx 24
+addx -2
+addx 5
+addx -40
+addx 22
+addx -19
+addx 2
+addx 5
+addx 2
+addx 5
+noop
+noop
+addx -2
+addx 2
+addx 5
+addx 3
+noop
+addx 2
+addx 2
+addx 3
+addx -2
+addx 10
+addx -3
+addx 3
+noop
+addx -40
+addx 2
+addx 11
+addx -5
+addx -1
+noop
+addx 3
+addx 7
+noop
+addx -2
+addx 5
+addx 2
+addx 3
+noop
+addx 2
+addx 6
+addx -5
+addx 2
+addx -18
+addx 26
+addx -1
+noop
+noop
+noop
+noop
+

+ 16 - 0
advent-of-code/2022/LICENSE

@@ -0,0 +1,16 @@
+Copyright (c) 2022, Dzshy <dzshy@outlook.com>
+
+Permission to use, copy, modify, and/or distribute this software for
+any purpose with or without fee is hereby granted, provided that the
+above copyright notice and this permission notice appear in all
+copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
+AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
+PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+PERFORMANCE OF THIS SOFTWARE.
+

+ 2 - 0
advent-of-code/2022/README.md

@@ -0,0 +1,2 @@
+
+My solutions for [Advent of Code 2022](https://adventofcode.com/2022).

+ 55 - 0
advent-of-code/2022/lib/crc32.c

@@ -0,0 +1,55 @@
+#include "crc32.h"
+
+const uint32_t crc32_tab[] = {
+    0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
+    0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
+    0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
+    0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
+    0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
+    0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
+    0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
+    0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
+    0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
+    0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
+    0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
+    0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
+    0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
+    0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
+    0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
+    0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
+    0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
+    0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
+    0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
+    0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
+    0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
+    0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
+    0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
+    0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
+    0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
+    0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
+    0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
+    0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
+    0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
+    0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
+    0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
+    0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
+    0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
+    0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
+    0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
+    0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
+    0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
+    0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
+    0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
+    0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
+    0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
+    0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
+    0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
+};
+
+uint32_t crc32(uint32_t crc, void *buf, int size){
+    uint8_t *p = buf;
+    crc = ~crc;
+    while (size--)
+        crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
+    return ~crc;
+}

+ 9 - 0
advent-of-code/2022/lib/crc32.h

@@ -0,0 +1,9 @@
+#ifndef CRC32_H_
+#define CRC32_H_
+
+#include <stdint.h>
+
+uint32_t crc32(uint32_t r, void* buf, int size);
+
+#endif
+

+ 106 - 0
advent-of-code/2022/lib/htable.c

@@ -0,0 +1,106 @@
+#include "htable.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+#define HTFL_NUL 0
+#define HTFL_VAL 1
+#define HTFL_DEL 2
+
+static void* htable_end(HTable *ht) {
+    return ht->buf + ht->cap * (ht->elemsz + 1);
+}
+
+static void rebuild(HTable *ht) {
+    HTable newht;
+    htable_init(&newht, ht->elemsz, ht->size * 6, ht->hash, ht->eq);
+    void *iter = htable_begin(ht);
+    while (iter != NULL) {
+        htable_insert(&newht, iter);
+        iter = htable_next(ht, iter);
+    }
+    free(ht->buf);
+    *ht = newht;
+}
+
+static uint8_t getflag(void *iter) {
+    return *(uint8_t*)(iter - 1);
+}
+
+static void setflag(void *iter, uint8_t flag) {
+    *(uint8_t *)(iter - 1) = flag;
+}
+
+void htable_init(HTable *ht, int elemsz, int cap, uint32_t (*hash)(void*),
+                 bool (*eq)(void*, void*)) {
+    if (cap < 16) cap = 16;
+    ht->buf = malloc(cap * (elemsz + 1));
+    memset(ht->buf, 0, cap * (elemsz + 1));
+    ht->size = 0;
+    ht->cap = cap;
+    ht->taken = 0;
+    ht->begin = NULL;
+    ht->elemsz = elemsz;
+    ht->hash = hash;
+    ht->eq = eq;
+}
+
+bool htable_insert(HTable *ht, void* elem) {
+    if (ht->taken + 1 > ht->cap / 2) {
+        rebuild(ht);
+    }
+    ht->taken++;
+    ht->size++;
+    int hashcode = ht->hash(elem) % ht->cap;
+    void *pos = ht->buf + hashcode * (ht->elemsz + 1) + 1;
+    while (getflag(pos) != HTFL_NUL) {
+        if (getflag(pos) == HTFL_VAL && ht->eq(pos, elem)) return false;
+        pos += ht->elemsz + 1;
+        if (pos >= htable_end(ht)) { // arrived end, restart from beginning
+            pos = ht->buf + 1;
+        }
+    }
+    memcpy(pos, elem, ht->elemsz);
+    setflag(pos, HTFL_VAL);
+    if (pos < ht->begin || ht->begin == NULL) {
+        ht->begin = pos;
+    }
+    return true;
+}
+
+void htable_del(HTable *ht, void* iter) {
+    ht->size--;
+    setflag(iter, HTFL_DEL);
+    if (iter == ht->begin) {
+        ht->begin = htable_next(ht, iter);
+    }
+}
+
+void* htable_find(HTable *ht, void* elem) {
+    int hashcode = ht->hash(elem) % ht->cap;
+    void *pos = ht->buf + hashcode * (ht->elemsz + 1) + 1;
+    while (getflag(pos) != HTFL_NUL) {
+        if (getflag(pos) == HTFL_VAL && ht->eq(pos, elem)) return pos;
+        pos += ht->elemsz + 1;
+        if (pos >= htable_end(ht)) { // arrived end, restart from beginning
+            pos = ht->buf + 1;
+        }
+    }
+    return NULL;
+}
+
+void* htable_begin(HTable *ht) {
+    return ht->begin;
+}
+
+void* htable_next(HTable *ht, void *iter) {
+    void *pos = iter;
+    do {
+        pos += ht->elemsz + 1;
+        if (pos >= htable_end(ht)) { 
+            return NULL;
+        }
+    } while (getflag(pos) != HTFL_VAL);
+    return pos;
+}
+

+ 29 - 0
advent-of-code/2022/lib/htable.h

@@ -0,0 +1,29 @@
+#ifndef HTABLE_H_
+#define HTABLE_H_
+
+#include <stdbool.h>
+#include <stdint.h>
+
+typedef struct {
+    void *buf;
+    int size;
+    int cap;
+    int taken;
+    void* begin;
+    int elemsz;
+    uint32_t (*hash)(void*);
+    bool (*eq)(void*, void*);
+} HTable;
+
+void htable_init(HTable *ht, int elemsz, int cap, uint32_t (*hash)(void*),
+                 bool (*eq)(void*, void*));
+bool htable_insert(HTable *ht, void* elem);
+void htable_del(HTable *ht, void* iter);
+
+// return a iterator
+void* htable_find(HTable *ht, void* elem);
+void* htable_begin(HTable *ht);
+void* htable_next(HTable *ht, void *iter);
+
+#endif
+

+ 72 - 0
advent-of-code/2022/lib/pque.c

@@ -0,0 +1,72 @@
+#include "pque.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+void pq_init(PQue *pq, int cap, int elem_sz, int (*cmp)(void*, void*)) {
+    pq->cap = cap;
+    pq->size = 0;
+    pq->elemsz = elem_sz;
+    pq->cmp = cmp;
+    pq->buf = malloc(cap * elem_sz);
+}
+
+static void swap(PQue *pq, int a, int b) {
+    char buf[pq->elemsz];
+    void *tmp = buf;
+    int elemsz = pq->elemsz;
+    memcpy(tmp, pq->buf + a*elemsz, elemsz);
+    memcpy(pq->buf + a*elemsz, pq->buf + b*elemsz, elemsz);
+    memcpy(pq->buf + b*elemsz, tmp, elemsz);
+}
+
+static int cmp(PQue *pq, int a, int b) {
+    return pq->cmp(pq->buf + a*pq->elemsz, pq->buf + b*pq->elemsz);
+}
+
+void pq_push(PQue *pq, void *elem) {
+    if (pq->size + 1 > pq->cap) {
+        pq->buf = realloc(pq->buf, 2 * pq->cap * pq->elemsz);
+        pq->cap *= 2;
+    }
+    memcpy(pq->buf + pq->size*pq->elemsz, elem, pq->elemsz);
+    pq->size++;
+    if (pq->size == 0) {
+        return;
+    }
+    int i = pq->size - 1;
+    while (i > 0 && cmp(pq, i, i/2) > 0) {
+        swap(pq, i, i/2);
+        i /= 2;
+    }
+}
+
+static void heapify(PQue *pq, int idx) {
+    int left, right, largest;
+    left = 2 * idx +1;
+    right = 2 * idx + 2;
+    if (left < pq->size && cmp(pq, left, idx) > 0) {
+        largest = left;
+    } else {
+        largest = idx;
+    }
+    if (right < pq->size && cmp(pq, right, largest) > 0) {
+        largest = right;
+    }
+    if (largest != idx) {
+        swap(pq, largest, idx);
+        heapify(pq, largest);
+    }
+}
+
+void pq_pop(PQue *pq) {
+    if (pq->size == 0) return;
+    memcpy(pq->buf, pq->buf+(pq->size - 1)*pq->elemsz, pq->elemsz);
+    pq->size -= 1;
+    heapify(pq, 0);
+}
+
+void* pq_top(PQue *pq) {
+    if (pq->size == 0) return NULL;
+    return pq->buf;
+}

+ 18 - 0
advent-of-code/2022/lib/pque.h

@@ -0,0 +1,18 @@
+#ifndef PQUEUE_H_
+#define PQUEUE_H_
+
+typedef struct {
+    void *buf;
+    int elemsz;
+    int cap;
+    int size;
+    int (*cmp)(void*, void*);
+} PQue;
+
+void pq_init(PQue *pq, int cap, int elemsz, int (*cmp)(void*, void*));
+void pq_push(PQue *pq, void *elem);
+void pq_pop(PQue *pq);
+void* pq_top();
+
+#endif
+

+ 396 - 0
advent-of-code/2022/lib/rbtree.c

@@ -0,0 +1,396 @@
+/*	$OpenBSD: tree.h,v 1.30 2020/10/10 18:03:41 otto Exp $	*/
+/*
+ * Copyright 2002 Niels Provos <provos@citi.umich.edu>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "rbtree.h"
+
+#define RED   1
+#define BLACK 0
+
+static struct rbnode *rbtree_minmax(struct rbtree *, int);
+void* rbtree_min(struct rbtree *head) {
+    return rbtree_minmax(head, -1);
+}
+void* rbtree_max(struct rbtree *head) {
+    return rbtree_minmax(head, 1);
+}
+
+void* rbtree_left(void *node) {
+    struct rbnode *elm = node;
+    if (node == NULL) return NULL;
+    return elm->entry.rbe_left;
+}
+void* rbtree_right(void *node) {
+    struct rbnode *elm = node;
+    if (node == NULL) return NULL;
+    return elm->entry.rbe_right;
+}
+void* rbtree_parent(void *node) {
+    struct rbnode *elm = node;
+    if (node == NULL) return NULL;
+    return elm->entry.rbe_parent;
+}
+
+static void augment(struct rbtree * head, struct rbnode *elm) {
+    if (head->augment != NULL) head->augment(elm);
+}
+
+static void rbtree_insert_color(struct rbtree *head, struct rbnode *elm);
+static void rbtree_remove_color(struct rbtree *head, struct rbnode *parent,
+                                struct rbnode *elm);
+
+static void rotate_left(struct rbtree *head, struct rbnode *elm) {
+    struct rbnode *tmp = elm->entry.rbe_right;
+    if ((elm->entry.rbe_right = tmp->entry.rbe_left)) {
+        tmp->entry.rbe_left->entry.rbe_parent = elm;
+    }
+    augment(head, elm);
+    if ((tmp->entry.rbe_parent = elm->entry.rbe_parent)) {
+        if (elm == elm->entry.rbe_parent->entry.rbe_left)
+          elm->entry.rbe_parent->entry.rbe_left = tmp;
+        else
+          elm->entry.rbe_parent->entry.rbe_right = tmp;
+    } else {
+        head->rbh_root = tmp;
+    }
+    tmp->entry.rbe_left = elm;
+    elm->entry.rbe_parent = tmp;
+    augment(head, tmp);
+    if (tmp->entry.rbe_parent) {
+        augment(head, tmp->entry.rbe_parent);
+    }
+}
+
+static void rotate_right(struct rbtree *head, struct rbnode *elm) {
+    struct rbnode *tmp = elm->entry.rbe_left;
+    if ((elm->entry.rbe_left = tmp->entry.rbe_right)) {
+        tmp->entry.rbe_right->entry.rbe_parent = elm;
+    }
+    augment(head, elm);
+    if ((tmp->entry.rbe_parent = elm->entry.rbe_parent)) {
+        if (elm == elm->entry.rbe_parent->entry.rbe_left)
+          elm->entry.rbe_parent->entry.rbe_left = tmp;
+        else
+          elm->entry.rbe_parent->entry.rbe_right = tmp;
+    } else {
+        head->rbh_root = tmp;
+    }
+    tmp->entry.rbe_right = elm;
+    elm->entry.rbe_parent = tmp;
+    augment(head, tmp);
+    if (tmp->entry.rbe_parent) {
+        augment(head, tmp->entry.rbe_parent);
+    }
+}
+
+static void rbtree_insert_color(struct rbtree *head, struct rbnode *elm) {
+  struct rbnode *parent, *gparent, *tmp;
+  while ((parent = elm->entry.rbe_parent) && parent->entry.rbe_color == 1) {
+    gparent = parent->entry.rbe_parent;
+    if (parent == gparent->entry.rbe_left) {
+      tmp = gparent->entry.rbe_right;
+      if (tmp && tmp->entry.rbe_color == 1) {
+        tmp->entry.rbe_color = BLACK;
+        parent->entry.rbe_color = BLACK;
+        gparent->entry.rbe_color = RED;
+        elm = gparent;
+        continue;
+      }
+      if (parent->entry.rbe_right == elm) {
+        rotate_left(head, parent);
+        tmp = parent;
+        parent = elm;
+        elm = tmp;
+      }
+      parent->entry.rbe_color = BLACK;
+      gparent->entry.rbe_color = RED;
+      rotate_right(head, gparent);
+    } else {
+      tmp = gparent->entry.rbe_left;
+      if (tmp && tmp->entry.rbe_color == 1) {
+        tmp->entry.rbe_color = BLACK;
+        parent->entry.rbe_color = BLACK;
+        gparent->entry.rbe_color = RED;;
+        elm = gparent;
+        continue;
+      }
+      if (parent->entry.rbe_left == elm) {
+        rotate_right(head, parent);
+        tmp = parent;
+        parent = elm;
+        elm = tmp;
+      }
+      parent->entry.rbe_color = BLACK;
+      gparent->entry.rbe_color = RED;
+      rotate_left(head, gparent);
+    }
+  }
+  head->rbh_root->entry.rbe_color = BLACK;
+}
+
+static void rbtree_remove_color(struct rbtree *head, struct rbnode *parent,
+                            struct rbnode *elm) {
+  struct rbnode *tmp;
+  while ((elm == NULL || elm->entry.rbe_color == 0) &&
+         elm != head->rbh_root) {
+    if (parent->entry.rbe_left == elm) {
+      tmp = parent->entry.rbe_right;
+      if (tmp->entry.rbe_color == 1) {
+        tmp->entry.rbe_color = BLACK;
+        parent->entry.rbe_color = RED;
+        rotate_left(head, parent);
+        tmp = parent->entry.rbe_right;
+      }
+      if ((tmp->entry.rbe_left == NULL ||
+           tmp->entry.rbe_left->entry.rbe_color == 0) &&
+          (tmp->entry.rbe_right == NULL ||
+           tmp->entry.rbe_right->entry.rbe_color == 0)) {
+        tmp->entry.rbe_color = RED;
+        elm = parent;
+        parent = elm->entry.rbe_parent;
+      } else {
+        if (tmp->entry.rbe_right == NULL ||
+            tmp->entry.rbe_right->entry.rbe_color == 0) {
+          struct rbnode *oleft;
+          if ((oleft = tmp->entry.rbe_left))
+            oleft->entry.rbe_color = BLACK;
+          tmp->entry.rbe_color = RED;
+          rotate_right(head, tmp);
+          tmp = parent->entry.rbe_right;
+        }
+        tmp->entry.rbe_color = parent->entry.rbe_color;
+        parent->entry.rbe_color = BLACK;
+        if (tmp->entry.rbe_right)
+          tmp->entry.rbe_right->entry.rbe_color = BLACK;
+        rotate_left(head, parent);
+        elm = head->rbh_root;
+        break;
+      }
+    } else {
+      tmp = parent->entry.rbe_left;
+      if (tmp->entry.rbe_color == 1) {
+        tmp->entry.rbe_color = BLACK;
+        parent->entry.rbe_color = RED;
+        rotate_right(head, parent);
+        tmp = parent->entry.rbe_left;
+      }
+      if ((tmp->entry.rbe_left == NULL ||
+           tmp->entry.rbe_left->entry.rbe_color == 0) &&
+          (tmp->entry.rbe_right == NULL ||
+           tmp->entry.rbe_right->entry.rbe_color == 0)) {
+        tmp->entry.rbe_color = RED;
+        elm = parent;
+        parent = elm->entry.rbe_parent;
+      } else {
+        if (tmp->entry.rbe_left == NULL ||
+            tmp->entry.rbe_left->entry.rbe_color == 0) {
+          struct rbnode *oright;
+          if ((oright = tmp->entry.rbe_right))
+            oright->entry.rbe_color = BLACK;
+          tmp->entry.rbe_color = RED;
+          rotate_left(head, tmp);
+          tmp = parent->entry.rbe_left;
+        }
+        tmp->entry.rbe_color = parent->entry.rbe_color;
+        parent->entry.rbe_color = BLACK;
+        if (tmp->entry.rbe_left)
+          tmp->entry.rbe_left->entry.rbe_color = BLACK;
+        rotate_right(head, parent);
+        elm = head->rbh_root;
+        break;
+      }
+    }
+  }
+  if (elm)
+    elm->entry.rbe_color = BLACK;
+}
+
+void rbtree_remove(struct rbtree *head, void* elmv) {
+  struct rbnode *elm = elmv;
+  struct rbnode *child, *parent;
+  int color;
+  if (elm->entry.rbe_left == NULL)
+    child = elm->entry.rbe_right;
+  else if (elm->entry.rbe_right == NULL)
+    child = elm->entry.rbe_left;
+  else {
+    struct rbnode *old = elm, *left;
+    elm = elm->entry.rbe_right;
+    while ((left = elm->entry.rbe_left))
+      elm = left;
+    child = elm->entry.rbe_right;
+    parent = elm->entry.rbe_parent;
+    color = elm->entry.rbe_color;
+    if (child)
+      child->entry.rbe_parent = parent;
+    if (parent) {
+      if (parent->entry.rbe_left == elm)
+        parent->entry.rbe_left = child;
+      else
+        parent->entry.rbe_right = child;
+      augment(head, parent);
+    } else
+      head->rbh_root = child;
+    if (elm->entry.rbe_parent == old)
+      parent = elm;
+    elm->entry = old->entry;
+    if (old->entry.rbe_parent) {
+      if ((old->entry.rbe_parent)->entry.rbe_left == old)
+        (old->entry.rbe_parent)->entry.rbe_left = elm;
+      else
+        (old->entry.rbe_parent)->entry.rbe_right = elm;
+      augment(head, old->entry.rbe_parent);
+    } else
+      head->rbh_root = elm;
+    old->entry.rbe_left->entry.rbe_parent = elm;
+    if (old->entry.rbe_right)
+      old->entry.rbe_right->entry.rbe_parent = elm;
+    if (parent) {
+      left = parent;
+      if (head->augment != NULL) {
+          do {
+              augment(head, left);
+          } while ((left = left->entry.rbe_parent));
+      }
+    }
+    goto color;
+  }
+  parent = elm->entry.rbe_parent;
+  color = elm->entry.rbe_color;
+  if (child)
+    child->entry.rbe_parent = parent;
+  if (parent) {
+    if (parent->entry.rbe_left == elm)
+      parent->entry.rbe_left = child;
+    else
+      parent->entry.rbe_right = child;
+    struct rbnode* goback = parent;
+    if (head->augment != NULL) {
+        do {
+            augment(head, goback);
+        } while ((goback = goback->entry.rbe_parent));
+    }
+  } else
+    head->rbh_root = child;
+color:
+  if (color == 0)
+    rbtree_remove_color(head, parent, child);
+}
+
+void* rbtree_insert(struct rbtree *head, void *elmv) {
+  struct rbnode *elm = elmv;
+  struct rbnode *tmp;
+  struct rbnode *parent = NULL;
+  int comp = 0;
+  tmp = head->rbh_root;
+  while (tmp) {
+    parent = tmp;
+    comp = head->cmp((void*)elm->content, (void*)parent->content);
+    if (comp < 0)
+      tmp = tmp->entry.rbe_left;
+    else if (comp > 0)
+      tmp = tmp->entry.rbe_right;
+    else
+      return tmp;
+  }
+  elm->entry.rbe_parent = parent;
+  elm->entry.rbe_left = elm->entry.rbe_right = NULL;
+  elm->entry.rbe_color = RED;
+  if (parent != NULL) {
+    if (comp < 0)
+      parent->entry.rbe_left = elm;
+    else
+      parent->entry.rbe_right = elm;
+    struct rbnode* goback = parent;
+    if (head->augment != NULL) {
+        do {
+            augment(head, goback);
+        } while ((goback = goback->entry.rbe_parent));
+    }
+  } else
+    head->rbh_root = elm;
+  rbtree_insert_color(head, elm);
+  return (NULL);
+}
+
+void* rbtree_find(struct rbtree *head, void *key) {
+  struct rbnode *tmp = head->rbh_root;
+  int comp;
+  while (tmp) {
+    comp = head->cmp(key, (void*)tmp->content);
+    if (comp < 0)
+      tmp = tmp->entry.rbe_left;
+    else if (comp > 0)
+      tmp = tmp->entry.rbe_right;
+    else
+      return tmp;
+  }
+  return (NULL);
+}
+
+void* rbtree_next(struct rbtree *head, void *elmv) {
+  struct rbnode *elm = elmv;
+  if (elm->entry.rbe_right) {
+    elm = elm->entry.rbe_right;
+    while (elm->entry.rbe_left)
+      elm = elm->entry.rbe_left;
+  } else {
+    if (elm->entry.rbe_parent &&
+        (elm == (elm->entry.rbe_parent)->entry.rbe_left))
+      elm = elm->entry.rbe_parent;
+    else {
+      while (elm->entry.rbe_parent &&
+             (elm == (elm->entry.rbe_parent)->entry.rbe_right))
+        elm = elm->entry.rbe_parent;
+      elm = elm->entry.rbe_parent;
+    }
+  }
+  return elm;
+}
+
+static struct rbnode *rbtree_minmax(struct rbtree *head, int val) {
+  struct rbnode *tmp = head->rbh_root;
+  struct rbnode *parent = NULL;
+  while (tmp) {
+    parent = tmp;
+    if (val < 0)
+      tmp = tmp->entry.rbe_left;
+    else
+      tmp = tmp->entry.rbe_right;
+  }
+  return parent;
+};
+
+static void rbtree_free_impl(struct rbnode *node, void (*free_cb)(void*)) {
+  if (node == NULL) return;
+  if (free_cb != NULL) free_cb(node->content);
+  rbtree_free_impl(node->entry.rbe_left, free_cb);
+  rbtree_free_impl(node->entry.rbe_right, free_cb);
+  free(node);
+}
+
+void rbtree_free(struct rbtree *head, void (*free_cb)(void*)) {
+  rbtree_free_impl(head->rbh_root, free_cb);  
+}

+ 59 - 0
advent-of-code/2022/lib/rbtree.h

@@ -0,0 +1,59 @@
+/*
+ * Copyright 2002 Niels Provos <provos@citi.umich.edu>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdlib.h>
+
+struct rbnode {
+  struct {
+    struct rbnode *rbe_left;
+    struct rbnode *rbe_right;
+    struct rbnode *rbe_parent;
+    int rbe_color;
+  } entry;
+  char content[0];
+};
+
+struct rbtree {
+  struct rbnode *rbh_root;
+  int (*cmp)(void *k1, void *k2);
+  void (*augment)(void *elm);
+};
+
+typedef struct rbnode RbNode;
+typedef struct rbtree RbTree;
+
+void rbtree_remove(struct rbtree *, void* iter);
+
+// return a iterator
+void* rbtree_insert(struct rbtree *, void *treenode);
+void* rbtree_find(struct rbtree *, void *val);
+void* rbtree_next(struct rbtree *, void* iter);
+void* rbtree_min(struct rbtree *);
+void* rbtree_max(struct rbtree *);
+void* rbtree_left(void *node);
+void* rbtree_right(void *node);
+void* rbtree_parent(void *node);
+
+void rbtree_free(struct rbtree *, void (*free_cb)(void*));

+ 130 - 0
advent-of-code/2022/lib/str.c

@@ -0,0 +1,130 @@
+#include "str.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stddef.h>
+#include <stdbool.h>
+#include <stdarg.h>
+#include <ctype.h>
+
+char** str_split(char *str, char delim) {
+    char **ret;
+
+    if (str == NULL) return NULL;
+    if (*str == '\n') {
+        ret = malloc(sizeof(char*));
+        *ret = NULL;
+        return ret;
+    }
+    int count = 0;
+    char *begin = str;
+    for (char *p = str; *p != '\0'; p++) {
+        if (*p != delim && !(delim == '\0' && isspace(*p))) {
+            continue;
+        }
+        int size = p - begin;
+        if (size > 0) count++;
+    }
+    count++;
+    ret = malloc((count + 1) * sizeof(char*));
+    memset(ret, 0, (count + 1) * sizeof(char*));
+
+    begin = str;
+    int i = 0;
+    bool finished = false;
+    for (char *p = str; !finished; p++) {
+        if (*p == '\0') finished = true;
+        if (*p != delim && *p != '\0' && !(delim == '\0' && isspace(*p))) {
+            continue;
+        }
+        int size = p - begin;
+        if (size == 0) {
+            begin = p + 1;
+            continue;
+        }
+        char *buf = malloc(sizeof(char) * (size + 1));
+        buf[size] = '\0';
+        memcpy(buf, begin, size * sizeof(char));
+        begin = p + 1;
+        ret[i] = buf;
+        i++;
+    }
+    return ret;
+}
+
+void str_list_free(char **list) {
+    char **p = list;
+    while (*p != NULL) {
+        free(*p);
+        p++;
+    }
+    free(list);
+}
+
+char* str_strip(char *str) {
+    int len = strlen(str);
+    char *begin = str;
+    char *end = str + len - 1;
+    while (isspace(*begin) && begin < end) {
+        begin++;
+    }
+    while (isspace(*end) && end >= begin) {
+        end--;
+    }
+    len = end - begin + 1;
+    char *buf = malloc(sizeof(char) * (len) + 1);
+    buf[len] = '\0';
+    memcpy(buf, begin, len);
+    return buf;
+}
+
+void sb_init(StrBuilder *sb) {
+    *sb = (StrBuilder){.size = 0, .cap = 16};
+    sb->buf = malloc(sizeof(char) * 17);
+}
+
+static void sb_reserve(StrBuilder *sb, int extra) {
+    if (sb->size + extra <= sb->cap) {
+        return;
+    }
+    int new_cap = (sb->size + extra) * 2;
+    sb->buf = realloc(sb->buf, new_cap + 1);
+    memset(sb->buf + sb->cap, 0, new_cap - sb->cap + 1);
+    sb->cap = new_cap;
+}
+
+void sb_append(StrBuilder *sb, char *format, ...) {
+    va_list va1;
+    va_list va2;
+    va_start(va1, format);
+    va_copy(va2, va1);
+    int size = vsnprintf(NULL, 0, format, va1);
+    sb_reserve(sb, size);
+    vsnprintf(sb->buf + sb->size, sb->cap - sb->size + 1, format, va2);
+}
+
+void sb_appendc(StrBuilder *sb, char c) {
+    sb_reserve(sb, 1);
+    sb->buf[sb->size] = c;
+    sb->size++;
+}
+
+char* fgetline(FILE *fp) {
+    StrBuilder sb;
+    sb_init(&sb);
+    while (true) {
+        int c = fgetc(fp);
+        if (c == EOF && sb.size == 0) return NULL;
+        if (c != EOF) sb_appendc(&sb, c);
+        if (c == EOF || c == '\n') return sb.buf;
+    }
+    return NULL;
+}
+
+int fpeek(FILE *fp) {
+    int c = fgetc(fp);
+    if (c == EOF) return c;
+    ungetc(c, fp);
+    return c;
+}

+ 24 - 0
advent-of-code/2022/lib/str.h

@@ -0,0 +1,24 @@
+#ifndef NEBUTIL_STR_H_
+#define NEBUTIL_STR_H_
+
+#include <stdio.h>
+
+char* str_strip(char *str);
+char** str_split(char *str, char delim);
+void str_list_free(char **list);
+
+typedef struct {
+    char *buf;
+    int size;
+    int cap;
+} StrBuilder;
+
+void sb_init(StrBuilder *sb);
+void sb_append(StrBuilder *sb, char *format, ...);
+void sb_appendc(StrBuilder *sb, char c);
+
+char* fgetline(FILE* fp);
+int   fpeek(FILE *fp);
+
+#endif
+

Некоторые файлы не были показаны из-за большого количества измененных файлов