From f6a7e1dbfef0d911cfbf005b619da5d085b6f6c3 Mon Sep 17 00:00:00 2001 From: Jonathan Hodgson Date: Wed, 1 Dec 2021 22:03:50 +0000 Subject: [PATCH] Day 1 --- day1/Makefile | 10 + day1/example.txt | 10 + day1/realinput.txt | 2000 ++++++++++++++++++++++++++++++++++++++++ day1/solution-part1.c | 30 + day1/solution-part1.sh | 17 + day1/solution-part2.c | 54 ++ day1/solution-part2.sh | 22 + 7 files changed, 2143 insertions(+) create mode 100644 day1/Makefile create mode 100644 day1/example.txt create mode 100644 day1/realinput.txt create mode 100644 day1/solution-part1.c create mode 100755 day1/solution-part1.sh create mode 100644 day1/solution-part2.c create mode 100755 day1/solution-part2.sh diff --git a/day1/Makefile b/day1/Makefile new file mode 100644 index 0000000..da2e708 --- /dev/null +++ b/day1/Makefile @@ -0,0 +1,10 @@ +solution-part%: solution-part%.c + gcc $< -o $@ + +test-%: solution-% example.txt + $< example.txt + +run-%: solution-% realinput.txt + $< realinput.txt + +.PHONY: $(patsubst %,run-%, 1 2) $(patsubst %,test-%, 1 2) diff --git a/day1/example.txt b/day1/example.txt new file mode 100644 index 0000000..167e291 --- /dev/null +++ b/day1/example.txt @@ -0,0 +1,10 @@ +199 +200 +208 +210 +200 +207 +240 +269 +260 +263 diff --git a/day1/realinput.txt b/day1/realinput.txt new file mode 100644 index 0000000..b1e4840 --- /dev/null +++ b/day1/realinput.txt @@ -0,0 +1,2000 @@ +143 +147 +150 +166 +180 +187 +188 +185 +199 +198 +199 +197 +218 +221 +238 +239 +233 +234 +217 +212 +215 +216 +215 +217 +220 +241 +245 +232 +230 +234 +206 +251 +272 +273 +278 +279 +285 +292 +288 +286 +289 +290 +294 +301 +298 +300 +297 +305 +306 +310 +340 +343 +352 +356 +362 +376 +380 +383 +382 +385 +372 +391 +384 +385 +398 +430 +426 +422 +423 +433 +419 +421 +431 +424 +442 +450 +453 +449 +487 +491 +462 +464 +466 +467 +468 +466 +482 +484 +501 +500 +501 +497 +498 +497 +508 +516 +521 +540 +541 +536 +537 +536 +542 +549 +535 +541 +548 +549 +551 +570 +572 +606 +605 +601 +608 +609 +611 +605 +631 +618 +635 +636 +635 +636 +637 +638 +676 +673 +668 +674 +683 +684 +683 +680 +687 +688 +698 +700 +706 +696 +713 +707 +708 +719 +721 +728 +719 +723 +715 +712 +722 +721 +709 +680 +681 +680 +681 +690 +686 +687 +682 +683 +680 +693 +708 +718 +730 +743 +726 +753 +747 +748 +751 +772 +801 +805 +797 +789 +790 +797 +812 +806 +813 +815 +813 +815 +822 +835 +843 +854 +858 +859 +860 +869 +872 +870 +864 +876 +880 +882 +865 +869 +876 +882 +886 +885 +898 +900 +909 +913 +902 +910 +914 +916 +920 +898 +899 +931 +936 +937 +938 +941 +950 +939 +942 +935 +932 +941 +945 +944 +957 +959 +967 +975 +990 +1011 +1008 +1011 +1015 +1017 +1018 +1011 +1010 +1035 +1036 +1040 +1041 +1046 +1048 +1027 +1047 +1069 +1079 +1074 +1075 +1080 +1079 +1083 +1071 +1077 +1080 +1081 +1082 +1083 +1085 +1087 +1089 +1095 +1096 +1099 +1110 +1112 +1119 +1122 +1124 +1130 +1133 +1143 +1139 +1141 +1142 +1144 +1176 +1175 +1159 +1165 +1172 +1182 +1203 +1209 +1193 +1194 +1193 +1191 +1196 +1201 +1202 +1205 +1216 +1219 +1220 +1213 +1212 +1220 +1226 +1233 +1234 +1236 +1239 +1240 +1242 +1237 +1261 +1259 +1257 +1245 +1251 +1252 +1272 +1276 +1277 +1279 +1284 +1285 +1286 +1285 +1315 +1316 +1331 +1333 +1335 +1347 +1344 +1343 +1342 +1340 +1354 +1359 +1367 +1368 +1378 +1380 +1358 +1370 +1371 +1387 +1388 +1402 +1404 +1406 +1401 +1403 +1405 +1407 +1408 +1428 +1432 +1433 +1434 +1435 +1446 +1460 +1461 +1460 +1468 +1471 +1472 +1474 +1465 +1473 +1496 +1507 +1510 +1506 +1502 +1509 +1512 +1515 +1516 +1524 +1526 +1527 +1528 +1527 +1541 +1544 +1545 +1543 +1548 +1551 +1544 +1545 +1550 +1554 +1556 +1557 +1558 +1575 +1578 +1581 +1587 +1586 +1590 +1584 +1571 +1576 +1574 +1575 +1585 +1601 +1604 +1612 +1613 +1600 +1587 +1591 +1596 +1595 +1601 +1608 +1614 +1615 +1614 +1634 +1636 +1637 +1639 +1642 +1629 +1617 +1618 +1617 +1612 +1613 +1614 +1615 +1617 +1608 +1609 +1611 +1608 +1634 +1636 +1641 +1637 +1657 +1665 +1668 +1679 +1680 +1695 +1697 +1698 +1669 +1670 +1669 +1673 +1672 +1678 +1670 +1671 +1673 +1689 +1683 +1685 +1691 +1692 +1669 +1670 +1679 +1672 +1684 +1704 +1700 +1692 +1714 +1715 +1716 +1712 +1714 +1716 +1714 +1718 +1719 +1713 +1717 +1710 +1711 +1721 +1745 +1752 +1767 +1762 +1787 +1785 +1786 +1790 +1796 +1814 +1843 +1859 +1871 +1876 +1890 +1891 +1892 +1894 +1912 +1914 +1939 +1955 +1956 +1960 +1964 +1967 +1970 +1971 +1972 +1969 +1970 +1984 +1996 +2017 +2018 +2020 +2005 +2006 +2005 +2007 +2005 +2006 +2027 +2031 +2032 +2012 +2014 +2013 +2016 +2014 +2018 +2040 +2042 +2061 +2064 +2063 +2080 +2086 +2094 +2063 +2071 +2092 +2077 +2078 +2079 +2080 +2099 +2100 +2103 +2118 +2119 +2124 +2128 +2135 +2136 +2135 +2136 +2137 +2135 +2163 +2168 +2162 +2164 +2160 +2161 +2162 +2165 +2169 +2168 +2185 +2191 +2197 +2212 +2224 +2228 +2229 +2230 +2243 +2250 +2251 +2252 +2255 +2256 +2247 +2253 +2262 +2296 +2317 +2324 +2327 +2299 +2300 +2290 +2291 +2303 +2292 +2293 +2294 +2297 +2298 +2308 +2309 +2312 +2337 +2340 +2349 +2354 +2345 +2341 +2340 +2341 +2344 +2345 +2341 +2346 +2352 +2357 +2340 +2339 +2336 +2339 +2351 +2356 +2358 +2364 +2366 +2367 +2370 +2376 +2377 +2375 +2378 +2391 +2394 +2389 +2391 +2388 +2389 +2407 +2420 +2417 +2416 +2424 +2443 +2455 +2457 +2446 +2449 +2458 +2466 +2479 +2486 +2478 +2491 +2503 +2507 +2513 +2518 +2500 +2502 +2504 +2514 +2516 +2517 +2518 +2529 +2532 +2543 +2544 +2543 +2545 +2553 +2548 +2559 +2563 +2573 +2587 +2584 +2581 +2579 +2582 +2577 +2588 +2589 +2614 +2617 +2620 +2622 +2615 +2619 +2618 +2610 +2616 +2615 +2618 +2616 +2627 +2630 +2641 +2659 +2662 +2663 +2661 +2669 +2662 +2687 +2690 +2691 +2693 +2696 +2728 +2746 +2743 +2746 +2745 +2746 +2742 +2749 +2756 +2750 +2738 +2743 +2744 +2746 +2750 +2753 +2762 +2770 +2767 +2766 +2778 +2780 +2781 +2776 +2763 +2762 +2763 +2766 +2762 +2753 +2761 +2768 +2772 +2746 +2762 +2767 +2774 +2795 +2806 +2809 +2816 +2830 +2821 +2837 +2836 +2838 +2842 +2831 +2815 +2816 +2818 +2817 +2822 +2817 +2823 +2834 +2825 +2818 +2821 +2847 +2846 +2845 +2847 +2848 +2849 +2863 +2872 +2866 +2874 +2875 +2885 +2886 +2892 +2898 +2897 +2917 +2922 +2916 +2921 +2922 +2921 +2940 +2945 +2935 +2950 +2946 +2949 +2951 +2948 +2940 +2941 +2940 +2939 +2955 +2943 +2941 +2942 +2945 +2961 +2962 +2965 +2975 +2970 +2971 +2960 +2961 +2956 +2960 +2961 +2967 +2966 +2979 +2976 +2983 +3012 +3024 +3021 +3041 +3040 +3041 +3042 +3048 +3077 +3088 +3091 +3109 +3107 +3116 +3111 +3115 +3121 +3095 +3097 +3077 +3059 +3065 +3068 +3061 +3062 +3061 +3063 +3068 +3070 +3073 +3057 +3075 +3082 +3089 +3086 +3090 +3092 +3106 +3112 +3118 +3120 +3119 +3121 +3119 +3126 +3133 +3135 +3163 +3166 +3167 +3181 +3185 +3191 +3197 +3198 +3199 +3194 +3210 +3216 +3224 +3225 +3235 +3237 +3245 +3253 +3259 +3248 +3256 +3273 +3265 +3248 +3249 +3258 +3257 +3256 +3255 +3250 +3252 +3260 +3261 +3267 +3268 +3269 +3284 +3294 +3334 +3337 +3340 +3307 +3316 +3320 +3328 +3330 +3350 +3357 +3363 +3364 +3361 +3396 +3397 +3410 +3411 +3433 +3452 +3453 +3465 +3467 +3469 +3470 +3467 +3480 +3494 +3517 +3519 +3527 +3542 +3546 +3547 +3544 +3570 +3571 +3596 +3604 +3603 +3587 +3588 +3591 +3589 +3590 +3591 +3600 +3598 +3599 +3600 +3599 +3600 +3609 +3608 +3610 +3611 +3632 +3633 +3632 +3633 +3635 +3642 +3643 +3656 +3652 +3653 +3657 +3676 +3677 +3687 +3690 +3693 +3694 +3697 +3702 +3703 +3722 +3729 +3730 +3731 +3737 +3744 +3747 +3751 +3757 +3758 +3761 +3769 +3781 +3796 +3798 +3797 +3798 +3812 +3848 +3849 +3850 +3854 +3861 +3859 +3863 +3867 +3870 +3882 +3884 +3876 +3879 +3909 +3908 +3910 +3909 +3908 +3886 +3888 +3889 +3891 +3920 +3921 +3922 +3913 +3955 +3956 +3974 +3975 +3976 +3988 +3992 +3999 +4009 +4010 +4019 +4023 +4024 +4035 +4045 +4044 +4046 +4072 +4074 +4093 +4101 +4105 +4111 +4141 +4146 +4147 +4160 +4162 +4161 +4162 +4156 +4158 +4160 +4168 +4169 +4168 +4169 +4192 +4191 +4222 +4239 +4227 +4228 +4226 +4228 +4227 +4228 +4221 +4222 +4223 +4248 +4249 +4258 +4249 +4257 +4260 +4258 +4257 +4259 +4260 +4267 +4284 +4285 +4286 +4283 +4280 +4281 +4280 +4279 +4282 +4289 +4290 +4295 +4300 +4289 +4290 +4291 +4300 +4310 +4314 +4319 +4320 +4321 +4324 +4330 +4326 +4340 +4326 +4327 +4332 +4333 +4334 +4339 +4361 +4369 +4370 +4374 +4372 +4382 +4398 +4399 +4395 +4396 +4397 +4400 +4404 +4401 +4405 +4415 +4427 +4433 +4446 +4447 +4455 +4456 +4457 +4458 +4467 +4471 +4463 +4470 +4460 +4458 +4459 +4457 +4468 +4470 +4448 +4449 +4453 +4474 +4470 +4475 +4474 +4473 +4472 +4474 +4476 +4478 +4479 +4483 +4484 +4485 +4481 +4483 +4492 +4505 +4513 +4529 +4530 +4531 +4503 +4504 +4505 +4499 +4500 +4516 +4517 +4513 +4497 +4493 +4494 +4524 +4537 +4543 +4544 +4551 +4552 +4555 +4565 +4567 +4566 +4567 +4566 +4590 +4591 +4596 +4597 +4588 +4572 +4575 +4572 +4574 +4575 +4576 +4584 +4572 +4578 +4579 +4593 +4600 +4604 +4584 +4586 +4583 +4599 +4601 +4603 +4625 +4624 +4625 +4627 +4623 +4624 +4620 +4618 +4617 +4616 +4617 +4612 +4623 +4634 +4635 +4623 +4628 +4629 +4631 +4633 +4654 +4659 +4669 +4674 +4677 +4678 +4694 +4699 +4701 +4713 +4714 +4722 +4724 +4725 +4761 +4785 +4791 +4783 +4790 +4791 +4792 +4809 +4835 +4840 +4845 +4852 +4851 +4852 +4846 +4848 +4849 +4854 +4856 +4862 +4856 +4875 +4876 +4878 +4892 +4893 +4903 +4891 +4909 +4923 +4938 +4939 +4944 +4953 +4954 +4975 +4977 +4978 +4984 +4987 +4989 +4987 +4995 +4994 +4996 +5000 +5008 +5018 +5024 +5027 +5028 +5031 +5035 +5051 +5059 +5061 +5065 +5069 +5090 +5110 +5111 +5113 +5115 +5129 +5128 +5139 +5140 +5141 +5142 +5149 +5148 +5147 +5153 +5157 +5178 +5189 +5209 +5217 +5211 +5215 +5221 +5243 +5244 +5275 +5287 +5290 +5300 +5301 +5297 +5300 +5289 +5311 +5314 +5303 +5304 +5306 +5307 +5316 +5329 +5330 +5333 +5334 +5335 +5343 +5359 +5380 +5382 +5383 +5392 +5403 +5409 +5410 +5420 +5427 +5438 +5448 +5455 +5458 +5489 +5490 +5491 +5492 +5494 +5497 +5522 +5523 +5525 +5536 +5537 +5542 +5522 +5529 +5534 +5535 +5545 +5546 +5544 +5538 +5547 +5551 +5550 +5551 +5552 +5553 +5565 +5578 +5589 +5590 +5589 +5592 +5601 +5614 +5615 +5614 +5615 +5616 +5626 +5627 +5629 +5647 +5649 +5650 +5645 +5646 +5647 +5672 +5690 +5692 +5697 +5701 +5697 +5702 +5705 +5718 +5720 +5725 +5747 +5749 +5751 +5756 +5762 +5764 +5771 +5772 +5771 +5772 +5809 +5822 +5823 +5840 +5841 +5839 +5838 +5839 +5840 +5838 +5852 +5853 +5859 +5865 +5879 +5885 +5899 +5907 +5910 +5936 +5943 +5957 +5953 +5958 +5968 +5979 +5971 +5966 +5967 +5971 +5991 +5993 +5994 +5993 +5996 +5993 +6004 +6010 +6017 +6018 +6017 +6038 +6036 +6035 +6036 +6039 +6042 +6037 +6059 +6062 +6067 +6068 +6084 +6077 +6080 +6081 +6079 +6093 +6095 +6098 +6111 +6107 +6114 +6105 +6104 +6111 +6112 +6120 +6126 +6140 +6143 +6142 +6148 +6152 +6159 +6150 +6152 +6168 +6169 +6167 +6161 +6160 +6159 +6160 +6168 +6169 +6188 +6200 +6220 +6221 +6219 +6221 +6220 +6232 +6233 +6234 +6242 +6247 +6246 +6247 +6253 +6258 +6256 +6249 +6228 +6225 +6223 +6227 +6228 +6259 +6265 +6268 +6270 +6271 +6275 +6270 +6263 +6265 +6279 +6323 +6365 +6374 +6397 +6415 +6416 +6419 +6403 +6405 +6410 +6417 +6424 +6425 +6422 +6432 +6442 +6451 +6459 +6458 +6463 +6485 +6486 +6509 +6508 +6532 +6535 +6551 +6562 +6563 +6572 +6571 +6590 +6628 +6629 +6627 +6625 +6620 +6619 +6625 +6616 +6615 +6646 +6648 +6649 +6651 +6653 +6645 +6651 +6652 +6665 +6660 +6664 +6665 +6684 +6685 +6694 +6695 +6696 +6693 +6694 +6703 +6702 +6696 +6693 +6694 +6691 +6725 +6728 +6715 +6722 +6728 +6729 +6730 +6742 +6753 +6754 +6752 +6753 +6761 +6771 +6773 +6778 +6779 +6795 +6819 +6797 +6799 +6798 +6807 +6809 +6837 +6861 +6864 +6865 +6868 +6869 +6867 +6861 +6871 +6874 +6872 +6873 +6875 +6867 +6877 +6876 +6898 +6900 +6902 +6904 +6905 +6915 +6908 +6927 +6928 +6935 +6942 +6943 +6945 +6948 +6943 +6947 +6990 +6993 +7013 +7014 +6997 +6976 +6972 +6975 +6976 +6982 +6991 +7006 +7009 +7015 +7019 +7018 +7008 +7011 +7018 +7023 +7027 +7044 +7054 +7060 +7058 +7057 +7061 +7038 +7036 +7040 +7041 +7039 +7042 +7039 +7037 +7049 +7056 +7057 +7072 +7095 +7101 +7102 +7104 +7103 +7106 +7107 +7108 +7112 +7117 +7119 +7117 +7132 +7130 +7137 +7143 +7135 +7138 +7152 +7156 +7180 +7181 +7182 +7179 +7180 +7174 +7172 +7162 +7177 +7180 +7187 +7153 +7159 +7145 +7139 +7140 +7137 +7156 +7160 +7161 +7162 +7163 +7170 +7163 +7171 +7167 +7166 +7167 +7169 +7170 +7178 +7179 +7166 +7177 +7176 +7203 +7194 +7187 +7191 +7202 +7198 +7199 +7205 +7206 +7207 +7208 +7214 +7215 +7214 +7215 +7217 +7208 +7205 +7214 +7215 +7216 +7212 +7214 +7222 +7226 +7231 +7229 +7239 +7238 +7239 +7250 +7252 +7255 +7277 +7279 +7284 +7290 +7293 +7294 +7316 +7325 +7326 +7327 +7328 +7326 +7325 +7329 +7335 +7340 +7344 +7337 +7338 +7348 +7366 +7376 +7377 +7382 +7387 +7401 +7392 +7396 +7400 +7406 +7405 +7410 +7425 +7426 +7432 +7449 +7450 +7444 +7450 +7455 +7456 +7471 +7472 +7494 +7514 +7515 +7512 +7515 +7517 +7514 +7538 +7539 +7540 +7556 +7557 +7553 +7549 +7546 +7566 +7567 +7564 +7565 +7559 +7560 +7563 +7571 +7570 +7572 +7580 +7587 +7595 +7597 +7614 +7622 +7621 +7627 +7656 +7652 +7659 +7658 +7657 +7660 +7665 +7664 +7666 +7669 +7678 +7680 +7682 +7683 +7669 +7692 +7695 +7699 +7705 +7717 +7720 +7730 +7733 +7748 +7759 +7747 +7748 +7759 +7778 +7777 +7778 +7780 +7781 +7780 +7788 +7787 +7788 +7814 +7810 +7787 +7788 +7802 +7803 +7813 +7815 +7818 +7823 +7830 +7852 +7868 +7874 +7879 +7873 +7854 +7871 +7877 +7876 +7883 +7885 +7887 +7886 +7889 +7893 +7900 +7909 +7910 +7906 +7910 +7913 +7918 +7919 +7941 +7947 +7948 +7949 +7951 +7952 +7977 +7976 +7979 +8010 +8011 +8014 +8015 +8031 +8054 +8056 +8059 +8085 +8087 +8107 +8109 +8115 +8116 +8117 +8115 +8116 +8119 +8127 +8145 +8147 diff --git a/day1/solution-part1.c b/day1/solution-part1.c new file mode 100644 index 0000000..f4fa604 --- /dev/null +++ b/day1/solution-part1.c @@ -0,0 +1,30 @@ +#include + +int main( int argc, char *argv[] ){ + if( argc == 2 ) { + + unsigned int last = ~0; + unsigned int increasing = 0; + unsigned int value; + + // Read the values into an array + FILE *fp=fopen(argv[1], "r"); + while (!feof (fp)){ + fscanf(fp, "%d\n", &value); + + printf( "value %d\n", value ); + if ( value > last ) + increasing++; + last = value; + } + fclose(fp); + + + printf( "%d were increasing\n", increasing ); + + return 0; + } else { + printf("You need to provide a file\n"); + return 1; + } +} diff --git a/day1/solution-part1.sh b/day1/solution-part1.sh new file mode 100755 index 0000000..c2b945c --- /dev/null +++ b/day1/solution-part1.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +# Give file as option 1 + +file="$1" + +increasing=0 +last=99999999 + +while read line; do + if [ "$line" -gt "$last" ]; then + increasing="$(( increasing + 1 ))" + fi + last="$line" +done < <(cat "$file") + +echo "$increasing were increasing" diff --git a/day1/solution-part2.c b/day1/solution-part2.c new file mode 100644 index 0000000..b0924dd --- /dev/null +++ b/day1/solution-part2.c @@ -0,0 +1,54 @@ +#include + +int countLines(char filename[]){ + FILE *fp; + fp=fopen(filename, "r"); + long int lines =0; + + if ( fp == NULL ) { + return -1; + } + + while (EOF != (fscanf(fp, "%*[^\n]"), fscanf(fp,"%*c"))) + ++lines; + + fclose(fp); // closing file + return lines; +} + +int main( int argc, char *argv[] ){ + if( argc == 2 ) { + // 1 less because file ends with trailing new line + int lines=countLines(argv[1]); + int values[lines]; + int line=0; + FILE *fp=fopen(argv[1], "r"); + int increasing = 0; + + // Read the values into an array + while (!feof (fp)){ + fscanf(fp, "%d\n", &values[line]); + + + if ( (line >= 3) && (values[line] > values[line-3]) ){ + + increasing++; + + } + + + line++; + } + fclose(fp); // closing file + + + + printf( "Increasing: %d\n", increasing ); + + + return 0; + } else { + printf("You need to provide a file\n"); + return 1; + } +} diff --git a/day1/solution-part2.sh b/day1/solution-part2.sh new file mode 100755 index 0000000..0752345 --- /dev/null +++ b/day1/solution-part2.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +# Give file as option 1 + +file="$1" +increasing=0 + +readarray -t "arr" < "$file" + +length="${#arr[@]}" + +for i in $(seq 3 "$length"); do + curr="${arr[$i]}" + prev3="${arr[$((i - 3))]}" + + if [ -n "$curr" ] && [ "$curr" -gt "$prev3" ]; then + increasing="$((increasing + 1))" + fi +done + +echo "$increasing were increasing" +