From 305908b6aaf10d45da28f62645f5cf09a2e97d1d Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Wed, 1 Dec 2021 10:06:58 +0100 Subject: [PATCH] C Day 1 --- .gitignore | 1 + .vscode/launch.json | 24 + AdventOfCode.code-workspace | 13 + c/build-all.ps1 | 1 + c/day1/input.txt | 2000 +++++++++++++++++++++++++++++++++++ c/day1/main.c | 96 ++ 6 files changed, 2135 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 AdventOfCode.code-workspace create mode 100644 c/build-all.ps1 create mode 100644 c/day1/input.txt create mode 100644 c/day1/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..adb36c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.exe \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..dadc49d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Start", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/c/day1.exe", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}/c/", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/AdventOfCode.code-workspace b/AdventOfCode.code-workspace new file mode 100644 index 0000000..107673c --- /dev/null +++ b/AdventOfCode.code-workspace @@ -0,0 +1,13 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": { + "files.associations": { + "*.lock": "yarnlock", + "stdlib.h": "c" + } + } +} \ No newline at end of file diff --git a/c/build-all.ps1 b/c/build-all.ps1 new file mode 100644 index 0000000..44f1a43 --- /dev/null +++ b/c/build-all.ps1 @@ -0,0 +1 @@ +gcc day1/*.c -o day1.exe -Og -g -Wall -Werror -std=c17 \ No newline at end of file diff --git a/c/day1/input.txt b/c/day1/input.txt new file mode 100644 index 0000000..e224934 --- /dev/null +++ b/c/day1/input.txt @@ -0,0 +1,2000 @@ +162 +164 +165 +166 +175 +185 +182 +191 +205 +204 +198 +196 +197 +213 +216 +217 +218 +219 +213 +219 +221 +222 +227 +231 +221 +211 +230 +232 +241 +243 +256 +272 +274 +273 +279 +287 +288 +287 +286 +288 +289 +291 +292 +294 +292 +296 +307 +314 +326 +332 +337 +347 +330 +333 +346 +344 +357 +362 +365 +366 +328 +323 +350 +362 +336 +346 +348 +325 +329 +330 +329 +333 +365 +363 +376 +377 +369 +343 +365 +360 +362 +377 +376 +379 +374 +376 +377 +369 +371 +374 +375 +377 +384 +401 +372 +374 +363 +372 +363 +377 +342 +340 +342 +344 +345 +351 +352 +353 +354 +330 +332 +324 +326 +335 +327 +324 +316 +324 +308 +311 +302 +314 +328 +340 +341 +338 +336 +337 +335 +323 +327 +321 +322 +302 +315 +289 +288 +287 +300 +294 +298 +297 +288 +289 +290 +288 +287 +283 +292 +306 +304 +306 +326 +336 +337 +340 +343 +352 +374 +373 +377 +357 +352 +357 +360 +373 +375 +377 +380 +384 +407 +409 +402 +405 +403 +426 +429 +428 +431 +399 +393 +399 +401 +418 +420 +421 +440 +446 +447 +450 +448 +463 +456 +458 +421 +409 +407 +394 +414 +412 +417 +418 +413 +414 +417 +418 +427 +426 +425 +426 +424 +431 +450 +448 +470 +484 +489 +466 +477 +479 +488 +489 +494 +501 +502 +505 +509 +514 +516 +522 +533 +537 +536 +546 +549 +539 +522 +523 +537 +536 +555 +556 +554 +541 +555 +557 +558 +573 +574 +571 +572 +573 +581 +580 +572 +586 +579 +581 +589 +597 +593 +598 +599 +600 +614 +622 +623 +646 +642 +652 +653 +655 +657 +663 +665 +667 +669 +655 +642 +643 +645 +660 +661 +658 +656 +661 +673 +672 +673 +680 +690 +691 +699 +698 +702 +716 +757 +758 +759 +778 +792 +789 +790 +791 +797 +806 +809 +801 +800 +802 +805 +814 +811 +836 +827 +836 +848 +855 +856 +857 +859 +855 +858 +864 +866 +875 +877 +865 +871 +879 +862 +869 +872 +879 +881 +883 +872 +861 +869 +867 +871 +888 +889 +893 +894 +897 +885 +891 +890 +910 +907 +909 +915 +940 +941 +944 +947 +938 +935 +927 +922 +932 +933 +895 +896 +897 +922 +931 +927 +936 +938 +962 +972 +973 +1010 +1003 +1004 +1003 +1004 +1007 +1000 +1015 +1017 +1006 +1008 +1021 +1019 +1018 +1003 +1010 +1011 +1016 +1017 +1025 +1008 +1001 +1007 +1015 +1001 +1004 +1010 +993 +994 +992 +994 +997 +990 +991 +1018 +1033 +1047 +1038 +1037 +1043 +1047 +1056 +1042 +1048 +1059 +1063 +1045 +1053 +1051 +1071 +1060 +1069 +1074 +1076 +1082 +1085 +1081 +1084 +1089 +1091 +1079 +1101 +1100 +1109 +1108 +1109 +1100 +1098 +1100 +1098 +1099 +1095 +1097 +1119 +1115 +1116 +1117 +1121 +1126 +1167 +1168 +1178 +1180 +1169 +1170 +1168 +1169 +1176 +1178 +1167 +1169 +1163 +1184 +1174 +1175 +1202 +1206 +1212 +1213 +1222 +1233 +1234 +1216 +1218 +1219 +1223 +1222 +1205 +1208 +1206 +1207 +1208 +1209 +1222 +1223 +1219 +1220 +1223 +1228 +1260 +1275 +1278 +1263 +1281 +1293 +1292 +1307 +1299 +1297 +1298 +1295 +1297 +1299 +1300 +1301 +1299 +1313 +1314 +1319 +1316 +1294 +1299 +1315 +1316 +1327 +1331 +1332 +1333 +1342 +1340 +1327 +1332 +1335 +1333 +1353 +1357 +1347 +1350 +1351 +1352 +1353 +1360 +1361 +1372 +1373 +1389 +1404 +1409 +1403 +1407 +1412 +1409 +1413 +1414 +1416 +1422 +1426 +1418 +1436 +1441 +1437 +1431 +1407 +1437 +1430 +1447 +1453 +1456 +1475 +1484 +1486 +1507 +1498 +1500 +1496 +1513 +1499 +1505 +1506 +1500 +1502 +1504 +1506 +1513 +1514 +1516 +1508 +1514 +1518 +1514 +1510 +1501 +1495 +1512 +1513 +1510 +1513 +1521 +1520 +1527 +1536 +1525 +1534 +1535 +1512 +1514 +1520 +1521 +1525 +1526 +1527 +1546 +1550 +1560 +1564 +1572 +1570 +1576 +1584 +1586 +1588 +1592 +1588 +1602 +1592 +1615 +1589 +1592 +1594 +1591 +1595 +1601 +1613 +1618 +1623 +1630 +1631 +1645 +1602 +1606 +1608 +1609 +1610 +1604 +1606 +1605 +1621 +1630 +1635 +1646 +1648 +1646 +1648 +1657 +1687 +1676 +1679 +1680 +1683 +1681 +1682 +1681 +1682 +1680 +1679 +1680 +1682 +1695 +1704 +1711 +1714 +1720 +1746 +1750 +1748 +1753 +1754 +1763 +1765 +1783 +1792 +1793 +1798 +1801 +1800 +1803 +1784 +1785 +1784 +1787 +1790 +1784 +1788 +1783 +1785 +1787 +1794 +1795 +1798 +1799 +1803 +1805 +1806 +1786 +1787 +1816 +1812 +1813 +1816 +1818 +1812 +1838 +1837 +1839 +1832 +1834 +1838 +1840 +1855 +1856 +1842 +1841 +1850 +1852 +1851 +1853 +1859 +1853 +1881 +1882 +1886 +1899 +1897 +1910 +1906 +1907 +1906 +1901 +1896 +1897 +1883 +1889 +1890 +1896 +1877 +1888 +1889 +1895 +1896 +1893 +1858 +1855 +1863 +1871 +1878 +1879 +1877 +1878 +1874 +1906 +1921 +1927 +1924 +1925 +1929 +1928 +1933 +1923 +1926 +1904 +1892 +1889 +1906 +1908 +1909 +1924 +1926 +1948 +1949 +1951 +1991 +1990 +1996 +1997 +2009 +2007 +2006 +2012 +2029 +2036 +2039 +2040 +2049 +2051 +2052 +2065 +2063 +2065 +2064 +2065 +2067 +2072 +2069 +2051 +2049 +2046 +2045 +2047 +2039 +2037 +2041 +2044 +2043 +2033 +2034 +2024 +2026 +2023 +2013 +2014 +2015 +2017 +2019 +2018 +2019 +2018 +2020 +2021 +2024 +2029 +2017 +2048 +2049 +2027 +2028 +2023 +2040 +2033 +2037 +2056 +2096 +2097 +2093 +2110 +2111 +2110 +2120 +2122 +2106 +2122 +2124 +2118 +2120 +2128 +2127 +2134 +2135 +2149 +2151 +2178 +2179 +2180 +2179 +2188 +2198 +2196 +2204 +2214 +2222 +2223 +2222 +2221 +2247 +2249 +2257 +2267 +2266 +2265 +2268 +2261 +2266 +2268 +2269 +2275 +2274 +2275 +2289 +2283 +2284 +2276 +2278 +2274 +2275 +2279 +2280 +2264 +2260 +2270 +2274 +2282 +2273 +2274 +2270 +2269 +2268 +2274 +2280 +2300 +2301 +2303 +2301 +2306 +2316 +2337 +2349 +2363 +2373 +2377 +2381 +2380 +2402 +2408 +2407 +2406 +2404 +2405 +2409 +2411 +2421 +2420 +2419 +2422 +2415 +2418 +2426 +2396 +2399 +2398 +2411 +2413 +2414 +2393 +2400 +2393 +2389 +2400 +2404 +2405 +2408 +2405 +2409 +2411 +2423 +2427 +2428 +2429 +2418 +2419 +2426 +2428 +2443 +2451 +2464 +2465 +2483 +2476 +2479 +2480 +2481 +2482 +2476 +2464 +2506 +2499 +2500 +2490 +2486 +2480 +2481 +2482 +2507 +2515 +2519 +2526 +2516 +2523 +2530 +2536 +2537 +2538 +2555 +2564 +2553 +2565 +2548 +2537 +2538 +2533 +2537 +2550 +2553 +2541 +2543 +2546 +2550 +2554 +2555 +2573 +2578 +2575 +2595 +2601 +2600 +2582 +2583 +2585 +2579 +2581 +2582 +2590 +2588 +2589 +2595 +2584 +2585 +2584 +2592 +2602 +2607 +2611 +2627 +2636 +2646 +2643 +2648 +2654 +2648 +2649 +2654 +2652 +2653 +2656 +2657 +2658 +2687 +2699 +2702 +2679 +2681 +2684 +2683 +2681 +2683 +2684 +2701 +2709 +2711 +2715 +2716 +2725 +2715 +2719 +2720 +2716 +2717 +2704 +2707 +2705 +2695 +2697 +2696 +2699 +2697 +2698 +2718 +2722 +2726 +2727 +2728 +2724 +2722 +2731 +2732 +2728 +2725 +2722 +2728 +2735 +2729 +2731 +2733 +2739 +2734 +2757 +2763 +2765 +2772 +2771 +2772 +2768 +2765 +2759 +2772 +2775 +2767 +2771 +2774 +2770 +2758 +2772 +2775 +2789 +2791 +2793 +2800 +2780 +2791 +2777 +2778 +2774 +2779 +2780 +2779 +2780 +2781 +2783 +2788 +2796 +2800 +2799 +2807 +2811 +2825 +2826 +2831 +2842 +2843 +2852 +2854 +2858 +2841 +2842 +2837 +2838 +2839 +2843 +2861 +2888 +2892 +2886 +2885 +2904 +2905 +2927 +2928 +2929 +2915 +2917 +2895 +2901 +2923 +2932 +2934 +2936 +2938 +2944 +2947 +2951 +2959 +2958 +2956 +2944 +2942 +2941 +2942 +2929 +2946 +2954 +2948 +2949 +2973 +2980 +2981 +2994 +2997 +3021 +3038 +3047 +3060 +3058 +3061 +3057 +3073 +3074 +3094 +3126 +3125 +3126 +3122 +3123 +3122 +3120 +3115 +3114 +3113 +3117 +3122 +3121 +3123 +3133 +3134 +3138 +3152 +3153 +3155 +3154 +3159 +3156 +3153 +3159 +3160 +3161 +3157 +3158 +3160 +3161 +3170 +3202 +3204 +3225 +3211 +3207 +3209 +3211 +3206 +3207 +3214 +3213 +3206 +3199 +3198 +3208 +3225 +3226 +3227 +3238 +3208 +3209 +3210 +3211 +3214 +3209 +3210 +3233 +3232 +3233 +3234 +3237 +3240 +3246 +3252 +3239 +3246 +3248 +3246 +3248 +3250 +3258 +3259 +3260 +3271 +3272 +3280 +3288 +3279 +3272 +3274 +3280 +3288 +3292 +3313 +3307 +3302 +3303 +3304 +3302 +3304 +3307 +3320 +3306 +3303 +3304 +3303 +3292 +3293 +3282 +3285 +3286 +3281 +3276 +3283 +3246 +3247 +3254 +3268 +3267 +3274 +3276 +3288 +3289 +3288 +3275 +3274 +3276 +3271 +3273 +3271 +3270 +3265 +3271 +3266 +3258 +3259 +3256 +3255 +3250 +3261 +3265 +3263 +3264 +3265 +3279 +3280 +3285 +3301 +3304 +3291 +3283 +3300 +3301 +3295 +3298 +3297 +3296 +3291 +3294 +3297 +3301 +3298 +3299 +3265 +3266 +3270 +3265 +3282 +3289 +3288 +3290 +3289 +3291 +3293 +3295 +3296 +3297 +3269 +3280 +3281 +3282 +3291 +3306 +3304 +3305 +3306 +3329 +3327 +3331 +3332 +3334 +3338 +3318 +3315 +3323 +3324 +3325 +3327 +3328 +3326 +3329 +3342 +3343 +3344 +3345 +3346 +3345 +3321 +3320 +3318 +3315 +3318 +3344 +3349 +3354 +3353 +3351 +3362 +3379 +3395 +3421 +3423 +3411 +3406 +3403 +3407 +3408 +3406 +3408 +3413 +3407 +3423 +3435 +3437 +3438 +3441 +3452 +3453 +3452 +3453 +3457 +3463 +3456 +3439 +3437 +3441 +3439 +3442 +3443 +3445 +3452 +3459 +3474 +3466 +3456 +3480 +3479 +3481 +3497 +3500 +3494 +3496 +3502 +3486 +3489 +3496 +3505 +3526 +3543 +3549 +3550 +3552 +3555 +3559 +3558 +3566 +3568 +3567 +3568 +3569 +3555 +3576 +3567 +3566 +3563 +3590 +3597 +3622 +3623 +3610 +3611 +3615 +3616 +3620 +3653 +3662 +3649 +3661 +3664 +3656 +3672 +3680 +3681 +3687 +3693 +3692 +3706 +3686 +3685 +3675 +3669 +3672 +3673 +3667 +3674 +3677 +3663 +3685 +3681 +3682 +3695 +3688 +3702 +3708 +3709 +3708 +3711 +3707 +3729 +3730 +3748 +3775 +3779 +3780 +3784 +3797 +3803 +3822 +3843 +3860 +3888 +3875 +3866 +3869 +3878 +3877 +3886 +3888 +3889 +3894 +3905 +3919 +3921 +3922 +3964 +3965 +3967 +3960 +3978 +3977 +3983 +3989 +3997 +3999 +4010 +3998 +3999 +4013 +4025 +4033 +4050 +4051 +4053 +4052 +4050 +4051 +4052 +4053 +4069 +4071 +4079 +4082 +4083 +4085 +4081 +4076 +4090 +4091 +4098 +4105 +4121 +4119 +4124 +4130 +4132 +4131 +4130 +4094 +4106 +4105 +4100 +4098 +4092 +4091 +4093 +4091 +4101 +4102 +4104 +4105 +4100 +4111 +4133 +4134 +4140 +4136 +4121 +4120 +4139 +4140 +4141 +4140 +4144 +4152 +4133 +4136 +4148 +4149 +4148 +4166 +4171 +4162 +4179 +4175 +4185 +4186 +4201 +4218 +4226 +4224 +4214 +4212 +4187 +4191 +4194 +4193 +4196 +4199 +4195 +4196 +4178 +4177 +4180 +4172 +4173 +4166 +4172 +4175 +4180 +4185 +4186 +4189 +4190 +4191 +4193 +4195 +4209 +4218 +4221 +4223 +4215 +4182 +4181 +4194 +4202 +4203 +4204 +4208 +4210 +4218 +4217 +4216 +4224 +4238 +4239 +4246 +4249 +4250 +4222 +4219 +4246 +4261 +4263 +4272 +4277 +4276 +4277 +4279 +4292 +4281 +4282 +4261 +4268 +4264 +4265 +4272 +4277 +4278 +4281 +4282 +4304 +4296 +4288 +4305 +4306 +4304 +4306 +4307 +4310 +4290 +4280 +4281 +4282 +4283 +4299 +4298 +4299 +4300 +4284 +4292 +4303 +4306 +4304 +4288 +4291 +4288 +4285 +4297 +4296 +4297 +4298 +4291 +4275 +4269 +4270 +4269 +4268 +4285 +4287 +4288 +4287 +4288 +4289 +4280 +4279 +4286 +4289 +4285 +4286 +4287 +4297 +4294 +4319 +4306 +4333 +4338 +4339 +4332 +4337 +4338 +4325 +4320 +4318 +4308 +4317 +4337 +4336 +4338 +4340 +4343 +4324 +4325 +4326 +4323 +4322 +4327 +4329 +4333 +4341 +4347 +4372 +4375 +4378 +4371 +4374 +4373 +4369 +4371 +4389 +4392 +4372 +4382 +4383 +4384 +4381 +4390 +4391 +4394 +4416 +4430 +4398 +4403 +4394 +4383 +4399 +4387 +4418 +4431 +4434 +4417 +4418 +4417 +4420 +4430 +4426 +4429 +4430 +4429 +4430 +4440 +4449 +4450 +4434 +4452 +4444 +4446 +4439 +4440 +4414 +4407 +4408 +4410 +4411 +4420 +4445 +4454 +4455 +4451 +4456 +4455 +4449 +4470 +4469 +4473 +4470 +4488 +4470 +4466 +4463 +4477 +4481 +4494 +4493 +4491 +4486 +4475 +4479 +4484 +4485 +4497 +4472 +4473 +4490 +4499 +4507 +4515 +4521 +4554 +4555 +4538 +4526 +4539 +4543 +4537 +4541 +4543 +4527 +4538 +4537 +4536 +4550 +4554 +4567 +4585 +4603 +4623 +4624 +4626 +4629 +4627 +4629 +4628 +4636 +4640 +4647 +4649 +4651 +4655 +4626 +4631 +4632 +4630 +4622 +4625 +4632 +4652 +4657 +4661 +4650 +4654 +4656 +4658 +4659 +4676 +4675 +4681 +4694 +4714 +4723 +4706 +4707 +4741 +4743 +4718 +4714 +4715 +4697 +4699 +4698 +4696 +4703 +4702 +4704 +4709 +4726 +4725 +4743 +4747 +4765 +4766 +4768 +4764 +4768 +4770 +4777 +4778 +4779 +4786 +4804 +4806 +4790 +4797 +4808 +4809 +4806 +4808 +4818 +4822 +4823 +4854 +4855 +4885 +4902 +4903 +4905 +4906 +4908 +4907 +4909 +4915 +4916 +4906 +4935 +4949 +4948 +4952 +4964 +4960 +4962 +4975 +4976 +4987 +4996 +4994 +4996 +4997 +5000 +5001 +4986 +4988 +4999 +4997 +4998 +4997 +5004 +5005 +5006 +5004 +5005 +5018 +5019 +5021 +5022 +5028 +5057 +5058 +5063 +5067 +5068 \ No newline at end of file diff --git a/c/day1/main.c b/c/day1/main.c new file mode 100644 index 0000000..70185bf --- /dev/null +++ b/c/day1/main.c @@ -0,0 +1,96 @@ +// https://adventofcode.com/2021/ +#include +#include +#include + +#define DAY 1 +#define INPUT_LENGTH 2000 + +static int parsedInput[INPUT_LENGTH]; + +void parseInput(char const *const input) +{ + char const *curLine = input; + unsigned int lineNum = 0; + while (curLine) + { + char const *nextLine = strchr(curLine, '\n'); + int curLineLen = nextLine ? nextLine - curLine : strlen(curLine); + char *const tempStr = malloc(curLineLen + 1); + memcpy(tempStr, curLine, curLineLen); + tempStr[curLineLen] = '\0'; + + parsedInput[lineNum] = _atoi64(tempStr); + + free(tempStr); + + curLine = nextLine ? nextLine + 1 : NULL; + lineNum++; + } +} + +char const *const part1() +{ + static const unsigned char MEASUREMENTS = 2; + int result = 0; + for (unsigned int i = MEASUREMENTS - 1; i < INPUT_LENGTH; i++) + { + if (parsedInput[i] > parsedInput[i - 1]) + { + result++; + } + } + char *const resultString = malloc(sizeof(char) * 10); + sprintf(resultString, "%d", result); + return resultString; +}; +char const *const part2() +{ + static const unsigned char MEASUREMENTS = 3; + int result = 0; + for (unsigned int i = MEASUREMENTS; i < INPUT_LENGTH; i++) + { + int measurementWindowA = parsedInput[i - 3] + parsedInput[i - 2] + parsedInput[i - 1]; + int measurementWindowB = parsedInput[i - 2] + parsedInput[i - 1] + parsedInput[i - 0]; + + if (measurementWindowB > measurementWindowA) + { + result++; + } + } + char *const resultString = malloc(sizeof(char) * 10); + sprintf(resultString, "%d", result); + return resultString; +}; + +int main(void) +{ + char *const fileName = malloc(sizeof(char) * sizeof("dayXX/input.txt")); + sprintf(fileName, "day%d/input.txt", DAY); + FILE *inputFile = fopen(fileName, "r"); + + if (inputFile == NULL) + { + printf("Failed to open input file %s.\n", fileName); + return 1; + } + + fseek(inputFile, 0, SEEK_END); + long length = ftell(inputFile); + fseek(inputFile, 0, SEEK_SET); + char *const input = malloc(length); + if (input != NULL) + { + fread(input, 1, length, inputFile); + } + + fclose(inputFile); + + parseInput(input); + + printf("Day %d\n", DAY); + printf("Part 1:\n%s\n", part1()); + printf("--------------\n"); + printf("Part 2:\n%s\n\n", part2()); + return 0; +} \ No newline at end of file