From 6ed18289c89eaff3557907df63cf58bd2009b3ee Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Tue, 7 Dec 2021 14:26:36 +0100 Subject: Added the first days --- 2021/01/first.b98 | 4 + 2021/01/first.go | 39 + 2021/01/input | 2000 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2021/01/second.b98 | 4 + 2021/01/second.go | 57 ++ 2021/02/first.b98 | 5 + 2021/02/first.go | 42 ++ 2021/02/input | 1000 +++++++++++++++++++++++++ 2021/02/second.b98 | 5 + 2021/02/second.go | 44 ++ 2021/03/first.go | 42 ++ 2021/03/input | 1000 +++++++++++++++++++++++++ 2021/03/second.go | 96 +++ 2021/04/first.go | 123 ++++ 2021/04/input | 601 +++++++++++++++ 2021/04/second.go | 119 +++ 2021/05/README.md | 3 + 2021/05/example | 10 + 2021/05/first.go | 48 ++ 2021/05/first_v2.go | 60 ++ 2021/05/go.mod | 3 + 2021/05/input | 500 +++++++++++++ 2021/05/line/line.go | 239 ++++++ 2021/05/second.go | 61 ++ 2021/05/second_v2.go | 58 ++ README.md | 5 + 26 files changed, 6168 insertions(+) create mode 100644 2021/01/first.b98 create mode 100644 2021/01/first.go create mode 100644 2021/01/input create mode 100644 2021/01/second.b98 create mode 100644 2021/01/second.go create mode 100644 2021/02/first.b98 create mode 100644 2021/02/first.go create mode 100644 2021/02/input create mode 100644 2021/02/second.b98 create mode 100644 2021/02/second.go create mode 100644 2021/03/first.go create mode 100644 2021/03/input create mode 100644 2021/03/second.go create mode 100644 2021/04/first.go create mode 100644 2021/04/input create mode 100644 2021/04/second.go create mode 100644 2021/05/README.md create mode 100644 2021/05/example create mode 100644 2021/05/first.go create mode 100644 2021/05/first_v2.go create mode 100644 2021/05/go.mod create mode 100644 2021/05/input create mode 100644 2021/05/line/line.go create mode 100644 2021/05/second.go create mode 100644 2021/05/second_v2.go create mode 100644 README.md diff --git a/2021/01/first.b98 b/2021/01/first.b98 new file mode 100644 index 0000000..4903889 --- /dev/null +++ b/2021/01/first.b98 @@ -0,0 +1,4 @@ +&:>#;&-:v@,a.$$; + : 0 + ^ - prev { + result++ + } + prev = i + } + fmt.Println(result) +} diff --git a/2021/01/input b/2021/01/input new file mode 100644 index 0000000..f65b86c --- /dev/null +++ b/2021/01/input @@ -0,0 +1,2000 @@ +123 +126 +130 +137 +140 +150 +155 +157 +173 +186 +201 +205 +234 +236 +237 +252 +254 +258 +265 +266 +270 +278 +285 +289 +310 +332 +323 +349 +371 +372 +373 +380 +381 +382 +383 +401 +406 +408 +411 +412 +415 +410 +413 +423 +435 +440 +445 +448 +453 +454 +455 +470 +472 +473 +476 +480 +483 +485 +486 +489 +491 +490 +495 +497 +520 +537 +547 +555 +575 +577 +588 +594 +595 +597 +598 +599 +635 +636 +637 +638 +642 +649 +652 +654 +655 +658 +699 +700 +702 +716 +718 +724 +728 +729 +730 +731 +734 +744 +753 +754 +755 +774 +775 +789 +794 +795 +793 +790 +809 +827 +828 +831 +832 +839 +840 +841 +855 +856 +860 +861 +867 +875 +894 +901 +902 +923 +931 +935 +937 +944 +947 +952 +961 +963 +965 +985 +987 +1000 +1008 +1017 +1041 +1047 +1049 +1030 +1048 +1050 +1048 +1059 +1073 +1092 +1091 +1092 +1103 +1111 +1125 +1134 +1136 +1162 +1163 +1188 +1190 +1209 +1214 +1227 +1228 +1235 +1237 +1238 +1240 +1242 +1246 +1248 +1250 +1252 +1258 +1259 +1262 +1263 +1266 +1267 +1272 +1274 +1276 +1277 +1288 +1293 +1299 +1300 +1281 +1287 +1288 +1293 +1298 +1299 +1300 +1325 +1323 +1319 +1330 +1312 +1318 +1337 +1354 +1355 +1360 +1374 +1376 +1377 +1388 +1389 +1395 +1401 +1404 +1406 +1408 +1417 +1416 +1417 +1420 +1428 +1433 +1435 +1453 +1459 +1474 +1480 +1481 +1513 +1509 +1515 +1518 +1522 +1519 +1530 +1531 +1527 +1547 +1549 +1552 +1553 +1554 +1558 +1567 +1570 +1580 +1582 +1585 +1586 +1587 +1609 +1611 +1612 +1614 +1616 +1641 +1640 +1643 +1646 +1617 +1620 +1621 +1626 +1632 +1637 +1638 +1659 +1660 +1661 +1670 +1673 +1674 +1675 +1678 +1679 +1700 +1689 +1697 +1700 +1702 +1706 +1707 +1705 +1711 +1736 +1740 +1743 +1744 +1758 +1764 +1766 +1765 +1770 +1773 +1772 +1775 +1776 +1777 +1780 +1787 +1788 +1790 +1793 +1794 +1800 +1806 +1808 +1837 +1841 +1846 +1847 +1848 +1849 +1848 +1851 +1854 +1853 +1859 +1860 +1865 +1866 +1867 +1873 +1882 +1900 +1913 +1914 +1935 +1937 +1942 +1938 +1941 +1942 +1952 +1953 +1956 +1957 +1959 +1980 +1981 +1982 +1988 +2009 +2020 +2043 +2046 +2033 +2036 +2042 +2047 +2048 +2062 +2052 +2053 +2079 +2082 +2091 +2092 +2091 +2082 +2083 +2090 +2101 +2129 +2130 +2134 +2120 +2121 +2115 +2119 +2123 +2124 +2139 +2140 +2141 +2142 +2145 +2146 +2151 +2148 +2150 +2158 +2162 +2163 +2155 +2160 +2175 +2184 +2192 +2210 +2211 +2214 +2215 +2221 +2222 +2223 +2231 +2233 +2256 +2258 +2243 +2250 +2255 +2269 +2280 +2288 +2294 +2299 +2313 +2321 +2324 +2327 +2354 +2353 +2348 +2363 +2384 +2390 +2391 +2393 +2398 +2401 +2416 +2418 +2419 +2422 +2435 +2436 +2441 +2452 +2453 +2442 +2447 +2451 +2452 +2465 +2470 +2457 +2486 +2524 +2536 +2540 +2543 +2537 +2522 +2531 +2532 +2534 +2553 +2564 +2562 +2570 +2573 +2590 +2575 +2583 +2585 +2559 +2563 +2566 +2567 +2573 +2564 +2565 +2574 +2579 +2605 +2610 +2617 +2615 +2616 +2651 +2652 +2653 +2668 +2670 +2673 +2679 +2681 +2684 +2685 +2686 +2692 +2693 +2697 +2698 +2713 +2710 +2718 +2719 +2726 +2686 +2687 +2690 +2692 +2691 +2708 +2713 +2714 +2713 +2710 +2699 +2704 +2703 +2707 +2713 +2718 +2719 +2713 +2716 +2724 +2726 +2736 +2745 +2748 +2752 +2753 +2754 +2763 +2766 +2767 +2769 +2776 +2777 +2810 +2813 +2814 +2817 +2818 +2825 +2842 +2849 +2862 +2868 +2865 +2869 +2890 +2894 +2909 +2915 +2916 +2919 +2918 +2919 +2923 +2931 +2946 +2925 +2931 +2933 +2934 +2965 +2978 +2981 +2994 +2995 +2996 +2997 +3003 +3011 +3008 +3009 +3011 +3012 +3013 +3018 +3020 +3033 +3032 +3036 +3040 +3030 +3032 +3033 +3035 +3038 +3046 +3052 +3068 +3070 +3043 +3050 +3051 +3052 +3059 +3063 +3065 +3088 +3093 +3094 +3093 +3098 +3102 +3107 +3110 +3113 +3114 +3116 +3110 +3111 +3113 +3116 +3119 +3116 +3121 +3122 +3121 +3125 +3138 +3139 +3145 +3150 +3151 +3149 +3162 +3169 +3178 +3179 +3191 +3202 +3227 +3234 +3236 +3246 +3254 +3258 +3259 +3251 +3267 +3281 +3249 +3270 +3271 +3272 +3281 +3295 +3296 +3295 +3296 +3321 +3327 +3334 +3340 +3321 +3330 +3333 +3336 +3359 +3368 +3376 +3403 +3412 +3417 +3410 +3418 +3421 +3422 +3425 +3433 +3440 +3441 +3443 +3464 +3467 +3471 +3472 +3475 +3473 +3488 +3494 +3481 +3482 +3491 +3514 +3521 +3525 +3543 +3565 +3566 +3567 +3591 +3590 +3591 +3572 +3573 +3567 +3599 +3622 +3623 +3643 +3646 +3651 +3656 +3662 +3663 +3673 +3684 +3679 +3684 +3672 +3678 +3723 +3744 +3742 +3739 +3732 +3746 +3750 +3757 +3759 +3762 +3759 +3758 +3753 +3755 +3760 +3766 +3767 +3771 +3800 +3801 +3805 +3806 +3807 +3806 +3811 +3818 +3819 +3831 +3832 +3840 +3844 +3829 +3842 +3854 +3858 +3874 +3875 +3877 +3880 +3903 +3906 +3907 +3908 +3909 +3929 +3951 +3952 +3962 +3973 +3975 +3976 +3974 +3977 +4004 +4008 +4017 +4004 +4003 +4009 +4010 +4004 +4005 +4007 +4015 +4018 +4026 +4029 +4019 +4022 +4030 +4036 +4037 +4039 +4022 +4024 +4026 +4034 +4033 +4034 +4040 +4042 +4050 +4054 +4055 +4056 +4033 +4047 +4048 +4047 +4048 +4050 +4059 +4077 +4079 +4091 +4092 +4098 +4103 +4107 +4108 +4112 +4114 +4118 +4119 +4125 +4126 +4138 +4137 +4168 +4189 +4195 +4198 +4217 +4220 +4223 +4228 +4234 +4233 +4234 +4236 +4269 +4268 +4269 +4268 +4280 +4291 +4285 +4311 +4312 +4315 +4307 +4313 +4315 +4326 +4332 +4345 +4340 +4353 +4355 +4353 +4354 +4367 +4368 +4377 +4378 +4380 +4382 +4385 +4390 +4392 +4393 +4396 +4397 +4385 +4398 +4386 +4381 +4382 +4370 +4385 +4384 +4403 +4404 +4405 +4408 +4422 +4425 +4422 +4435 +4436 +4438 +4452 +4461 +4465 +4480 +4488 +4495 +4504 +4505 +4508 +4523 +4524 +4527 +4529 +4561 +4577 +4580 +4614 +4635 +4636 +4637 +4639 +4654 +4655 +4652 +4663 +4665 +4666 +4668 +4671 +4677 +4680 +4687 +4689 +4693 +4695 +4688 +4697 +4711 +4724 +4734 +4747 +4748 +4780 +4781 +4793 +4799 +4804 +4805 +4792 +4793 +4794 +4820 +4815 +4817 +4828 +4830 +4834 +4836 +4840 +4839 +4848 +4862 +4867 +4869 +4871 +4870 +4878 +4868 +4869 +4871 +4874 +4897 +4905 +4909 +4910 +4912 +4945 +4946 +4945 +4950 +4951 +4952 +4957 +4958 +4967 +4991 +4998 +5001 +5021 +5022 +5021 +5030 +5031 +5028 +5031 +5014 +5018 +5025 +5027 +5032 +5034 +5036 +5033 +5036 +5035 +5038 +5042 +5043 +5044 +5045 +5062 +5069 +5070 +5068 +5089 +5110 +5114 +5130 +5134 +5154 +5153 +5158 +5162 +5177 +5178 +5182 +5187 +5188 +5189 +5223 +5225 +5237 +5264 +5269 +5270 +5274 +5275 +5276 +5279 +5285 +5266 +5270 +5272 +5275 +5276 +5294 +5298 +5299 +5302 +5306 +5311 +5317 +5304 +5308 +5303 +5324 +5329 +5330 +5334 +5342 +5348 +5367 +5385 +5390 +5382 +5383 +5384 +5388 +5389 +5390 +5399 +5401 +5403 +5407 +5408 +5417 +5445 +5456 +5459 +5462 +5463 +5471 +5472 +5475 +5504 +5525 +5538 +5552 +5553 +5556 +5562 +5598 +5575 +5569 +5571 +5574 +5575 +5562 +5586 +5594 +5599 +5641 +5639 +5642 +5644 +5647 +5643 +5642 +5643 +5645 +5654 +5655 +5672 +5681 +5696 +5700 +5701 +5702 +5705 +5709 +5716 +5721 +5724 +5728 +5749 +5750 +5746 +5744 +5743 +5751 +5754 +5748 +5764 +5765 +5773 +5775 +5778 +5796 +5800 +5801 +5806 +5811 +5826 +5829 +5833 +5844 +5850 +5852 +5861 +5863 +5867 +5883 +5880 +5867 +5862 +5863 +5865 +5866 +5864 +5869 +5871 +5880 +5887 +5908 +5917 +5919 +5928 +5939 +5967 +5954 +5955 +5945 +5950 +5956 +5958 +5968 +5972 +5978 +5979 +5981 +5986 +5987 +5990 +5992 +5993 +6012 +6018 +6021 +6023 +6024 +6025 +6041 +6045 +6042 +6058 +6059 +6060 +6062 +6064 +6072 +6074 +6075 +6068 +6075 +6093 +6095 +6090 +6092 +6107 +6106 +6107 +6108 +6109 +6120 +6123 +6125 +6126 +6129 +6130 +6132 +6149 +6157 +6163 +6164 +6165 +6152 +6154 +6156 +6158 +6155 +6156 +6158 +6163 +6178 +6179 +6180 +6179 +6182 +6184 +6191 +6189 +6199 +6210 +6212 +6218 +6222 +6223 +6232 +6233 +6228 +6232 +6221 +6223 +6225 +6228 +6236 +6237 +6246 +6252 +6254 +6255 +6256 +6255 +6260 +6262 +6263 +6267 +6268 +6272 +6279 +6277 +6276 +6282 +6287 +6305 +6306 +6308 +6309 +6310 +6318 +6319 +6321 +6320 +6328 +6346 +6348 +6351 +6352 +6369 +6370 +6368 +6369 +6371 +6374 +6375 +6370 +6374 +6393 +6403 +6406 +6412 +6420 +6435 +6437 +6438 +6462 +6461 +6462 +6467 +6468 +6494 +6508 +6509 +6518 +6519 +6520 +6521 +6518 +6545 +6546 +6550 +6552 +6567 +6568 +6570 +6578 +6582 +6580 +6591 +6599 +6601 +6595 +6598 +6610 +6628 +6612 +6616 +6614 +6618 +6641 +6640 +6643 +6647 +6663 +6664 +6665 +6668 +6669 +6670 +6685 +6706 +6704 +6708 +6710 +6716 +6717 +6719 +6726 +6736 +6755 +6752 +6753 +6760 +6769 +6774 +6779 +6783 +6792 +6794 +6798 +6804 +6803 +6806 +6809 +6811 +6812 +6817 +6827 +6830 +6822 +6842 +6844 +6848 +6849 +6850 +6852 +6854 +6873 +6891 +6925 +6928 +6930 +6940 +6941 +6942 +6943 +6946 +6951 +6956 +6957 +6966 +6969 +6971 +6973 +7000 +7006 +7007 +7031 +7037 +7046 +7052 +7053 +7070 +7072 +7076 +7084 +7090 +7089 +7117 +7139 +7153 +7168 +7178 +7179 +7168 +7171 +7172 +7171 +7174 +7194 +7226 +7246 +7252 +7256 +7257 +7259 +7261 +7266 +7277 +7296 +7298 +7300 +7315 +7329 +7332 +7333 +7338 +7340 +7347 +7349 +7350 +7351 +7352 +7353 +7356 +7359 +7360 +7361 +7358 +7363 +7364 +7385 +7406 +7409 +7434 +7435 +7436 +7438 +7399 +7401 +7406 +7416 +7409 +7410 +7413 +7415 +7439 +7441 +7440 +7442 +7466 +7465 +7468 +7480 +7481 +7483 +7488 +7498 +7497 +7500 +7503 +7506 +7513 +7518 +7519 +7524 +7506 +7507 +7517 +7519 +7528 +7529 +7533 +7535 +7540 +7542 +7554 +7556 +7557 +7573 +7582 +7593 +7599 +7583 +7591 +7593 +7595 +7599 +7611 +7626 +7618 +7615 +7619 +7620 +7625 +7626 +7628 +7631 +7630 +7631 +7642 +7654 +7656 +7657 +7659 +7673 +7677 +7691 +7697 +7707 +7708 +7710 +7712 +7717 +7724 +7722 +7723 +7722 +7725 +7726 +7724 +7730 +7734 +7760 +7770 +7775 +7788 +7787 +7789 +7797 +7813 +7841 +7853 +7860 +7868 +7883 +7903 +7907 +7908 +7910 +7912 +7919 +7921 +7925 +7926 +7937 +7951 +7954 +7957 +7961 +7964 +7976 +7979 +7971 +7969 +7970 +7982 +7983 +7984 +7993 +8000 +8015 +8017 +8014 +8025 +8019 +8028 +8029 +8030 +8033 +8038 +8040 +8041 +8040 +8044 +8026 +8030 +8031 +8044 +8060 +8064 +8065 +8054 +8056 +8058 +8060 +8061 +8062 +8068 +8072 +8078 +8069 +8067 +8073 +8084 +8119 +8121 +8120 +8122 +8132 +8133 +8127 +8132 +8151 +8154 +8161 +8162 +8158 +8186 +8187 +8172 +8181 +8200 +8202 +8216 +8213 +8215 +8229 +8243 +8244 +8245 +8246 +8239 +8260 +8262 +8279 +8281 +8287 +8288 +8292 +8296 +8299 +8303 +8310 +8311 +8315 +8329 +8330 +8331 +8334 +8346 +8347 +8354 +8377 +8379 +8381 +8388 +8389 +8415 +8413 +8419 +8420 +8421 +8425 +8427 +8428 +8435 +8442 +8426 +8420 +8432 +8435 +8445 +8446 +8450 +8451 +8455 +8456 +8482 +8489 +8487 +8500 +8485 +8493 +8497 +8502 +8501 +8502 +8503 +8505 +8507 +8508 +8509 +8514 +8523 +8527 +8531 +8542 +8540 +8542 +8543 +8544 +8557 +8550 +8552 +8563 +8565 +8566 +8567 +8581 +8591 +8595 +8605 +8613 +8614 +8613 +8615 +8618 +8631 +8628 +8627 +8628 +8629 +8646 +8648 +8650 +8655 +8659 +8681 +8684 +8687 +8690 +8687 +8688 +8719 +8722 +8721 +8723 +8754 +8767 +8749 +8750 +8764 +8772 +8773 +8776 +8778 +8782 +8786 +8784 +8799 +8800 +8815 +8819 +8838 +8833 +8834 +8835 +8837 +8844 +8851 +8852 +8856 +8857 +8870 +8879 +8904 +8910 +8918 +8925 +8928 +8927 +8928 +8936 +8937 +8939 +8944 +8979 +8980 +8994 +8996 +8997 +8999 +9003 +9014 +9015 +9020 +9023 +9026 +9027 +9028 +9030 +9033 +9036 +9038 +9044 +9048 +9063 +9066 +9091 +9092 +9093 +9095 +9099 +9097 +9102 +9103 +9123 +9124 +9125 +9127 +9128 +9132 +9135 +9142 +9149 +9152 +9171 +9166 +9177 +9178 +9162 +9168 +9169 +9170 +9183 +9189 +9199 +9195 +9198 +9203 +9214 +9213 +9222 +9223 +9228 +9229 +9241 +9243 +9229 +9242 +9257 +9262 +9265 +9275 +9276 +9289 +9290 +9297 +9298 +9301 +9313 +9314 +9315 +9316 +9324 +9325 +9328 +9310 +9311 +9336 +9340 +9354 +9356 +9360 +9361 +9362 +9381 +9385 +9393 +9394 +9402 +9403 +9407 +9403 +9415 +9416 +9436 +9442 +9454 +9481 +9485 +9486 +9487 +9513 +9521 +9525 +9521 +9519 +9524 +9526 +9530 +9540 +9568 +9571 +9589 +9609 +9610 +9604 +9602 +9601 +9626 +9630 +9662 +9670 +9672 +9674 +9676 +9682 +9683 +9685 +9687 +9704 +9706 +9707 +9708 +9718 +9732 +9741 +9756 +9775 +9785 +9786 +9797 +9800 +9803 +9806 +9812 +9815 +9840 +9842 +9859 +9865 +9871 +9876 +9894 +9903 +9905 +9912 +9939 +9940 +9946 +9948 +9953 +9960 +9963 +9964 +9967 +9973 +9974 +9986 +9982 +9987 +9988 +10011 +10012 +10013 +10018 +10021 +10022 +10028 +10032 +10034 +10035 +10036 +10037 +10048 +10060 +10107 +10128 +10125 +10136 +10158 +10167 +10158 +10183 +10212 +10223 +10229 +10233 +10229 +10232 +10242 +10245 +10246 +10269 +10274 +10270 +10271 +10293 +10287 +10298 +10299 +10307 +10308 +10315 +10336 +10342 +10350 +10344 +10349 +10353 +10354 +10370 +10377 +10380 +10390 +10410 +10411 diff --git a/2021/01/second.b98 b/2021/01/second.b98 new file mode 100644 index 0000000..b4f9573 --- /dev/null +++ b/2021/01/second.b98 @@ -0,0 +1,4 @@ +v > v +>&00p&10p&20p>#;&:00gw;@,a.; v + ^ >\1+\ v + ^p02p01g02p00g01< diff --git a/2021/01/second.go b/2021/01/second.go new file mode 100644 index 0000000..bea4b05 --- /dev/null +++ b/2021/01/second.go @@ -0,0 +1,57 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "strconv" +) + +func main() { + f, err := os.Open("input") + if err != nil { + log.Fatalf("%+v", err) + } + defer f.Close() + + scanner := bufio.NewScanner(f) + scanner.Split(bufio.ScanLines) + + scanner.Scan() + A, err := strconv.Atoi(scanner.Text()) + if err != nil { + log.Fatalf("%+v", err) + } + scanner.Scan() + B, err := strconv.Atoi(scanner.Text()) + if err != nil { + log.Fatalf("%+v", err) + } + A += B + scanner.Scan() + C, err := strconv.Atoi(scanner.Text()) + if err != nil { + log.Fatalf("%+v", err) + } + B += C + A += C + + result := 0 + + for scanner.Scan() { + D, err := strconv.Atoi(scanner.Text()) + if err != nil { + log.Fatalf("%+v", err) + } + C += D + B += D + if B > A { + result++ + } + A = B + B = C + C = D + } + fmt.Println(result) +} diff --git a/2021/02/first.b98 b/2021/02/first.b98 new file mode 100644 index 0000000..fd7a917 --- /dev/null +++ b/2021/02/first.b98 @@ -0,0 +1,5 @@ +v pos in 0,0, depth in 1,0 +>00p10p>#;~:'f-#v_$&00g+00p v@,a.*g01g00; + >'d-#v_$&10g+10pv + >&10g\-10p v + ^ ;,a.g01.g00; ~ < # we do not forget to strip the \n from the input diff --git a/2021/02/first.go b/2021/02/first.go new file mode 100644 index 0000000..1550bcb --- /dev/null +++ b/2021/02/first.go @@ -0,0 +1,42 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "strconv" + "strings" +) + +func main() { + f, err := os.Open("input") + if err != nil { + log.Fatalf("%+v", err) + } + defer f.Close() + + scanner := bufio.NewScanner(f) + scanner.Split(bufio.ScanLines) + + pos := 0 + depth := 0 + for scanner.Scan() { + elts := strings.Split(scanner.Text(), " ") + i, err := strconv.Atoi(elts[1]) + if err != nil { + log.Fatalf("%+v", err) + } + switch elts[0] { + case "forward": + pos += i + case "down": + depth += i + case "up": + depth -= i + default: + panic(elts[0]) + } + } + fmt.Println(pos * depth) +} diff --git a/2021/02/input b/2021/02/input new file mode 100644 index 0000000..594c25f --- /dev/null +++ b/2021/02/input @@ -0,0 +1,1000 @@ +forward 5 +down 9 +forward 2 +up 2 +forward 4 +forward 4 +up 5 +down 3 +forward 2 +forward 9 +down 7 +forward 2 +down 2 +forward 1 +up 9 +forward 1 +forward 4 +up 5 +down 9 +forward 8 +forward 3 +up 1 +down 2 +down 3 +forward 7 +forward 2 +up 3 +forward 9 +forward 9 +down 3 +up 8 +forward 5 +forward 7 +forward 2 +down 7 +forward 5 +down 4 +up 7 +forward 8 +forward 1 +down 3 +down 1 +forward 6 +up 7 +forward 6 +down 3 +forward 9 +forward 6 +up 3 +down 1 +forward 8 +up 7 +down 9 +down 2 +down 1 +down 5 +forward 4 +down 8 +forward 2 +down 5 +down 6 +down 5 +forward 5 +down 8 +up 8 +forward 3 +forward 4 +forward 8 +forward 9 +down 2 +forward 7 +forward 3 +forward 9 +down 3 +up 1 +down 5 +forward 2 +down 9 +down 2 +down 5 +down 2 +down 9 +up 3 +forward 6 +up 9 +down 1 +forward 8 +up 6 +down 1 +forward 1 +up 1 +forward 3 +down 8 +down 1 +down 9 +forward 4 +forward 1 +down 3 +forward 8 +down 9 +forward 7 +up 6 +down 8 +down 6 +down 8 +down 7 +down 1 +down 8 +down 1 +forward 5 +down 9 +forward 4 +down 2 +forward 8 +up 1 +forward 7 +down 7 +down 6 +forward 4 +forward 6 +down 2 +down 2 +up 7 +down 2 +up 9 +forward 6 +forward 3 +down 8 +forward 9 +down 9 +down 9 +down 8 +forward 2 +forward 5 +forward 8 +forward 1 +down 3 +forward 1 +forward 2 +forward 9 +up 5 +forward 4 +forward 2 +down 6 +forward 3 +forward 7 +forward 1 +forward 8 +down 7 +forward 4 +up 3 +down 9 +up 2 +forward 2 +forward 7 +down 9 +up 9 +forward 9 +up 8 +up 7 +down 8 +down 9 +forward 1 +forward 5 +up 7 +down 3 +up 9 +forward 9 +down 4 +down 7 +down 7 +down 1 +down 4 +down 5 +up 2 +forward 2 +forward 2 +forward 6 +down 7 +forward 7 +down 5 +forward 8 +down 7 +forward 6 +down 2 +up 2 +down 5 +down 1 +up 4 +down 8 +up 9 +forward 1 +down 9 +down 6 +down 8 +up 7 +up 1 +forward 7 +down 8 +forward 1 +down 4 +down 2 +forward 3 +forward 6 +forward 8 +down 2 +forward 7 +forward 8 +up 3 +down 1 +down 8 +up 3 +down 4 +down 5 +forward 6 +forward 9 +down 3 +up 2 +down 9 +up 2 +down 3 +down 9 +forward 4 +forward 6 +down 7 +down 8 +down 4 +forward 7 +up 2 +down 5 +up 3 +down 5 +up 1 +up 1 +forward 5 +forward 9 +down 9 +up 4 +up 4 +up 8 +up 5 +forward 7 +forward 6 +up 6 +down 5 +forward 4 +forward 3 +up 6 +down 6 +forward 5 +up 6 +up 7 +forward 1 +forward 2 +forward 5 +down 3 +forward 6 +down 6 +down 3 +up 9 +down 4 +down 5 +down 4 +forward 1 +down 1 +forward 3 +up 4 +forward 1 +forward 5 +up 3 +forward 6 +forward 5 +forward 9 +forward 6 +down 2 +forward 2 +down 1 +down 4 +forward 6 +forward 8 +down 8 +up 5 +forward 8 +forward 3 +forward 1 +forward 3 +forward 6 +down 1 +down 9 +up 7 +down 2 +forward 6 +down 4 +down 7 +down 5 +forward 2 +down 1 +forward 2 +forward 8 +forward 4 +up 3 +down 1 +forward 6 +forward 3 +down 3 +down 9 +forward 1 +up 5 +forward 3 +forward 3 +up 5 +down 7 +forward 8 +up 5 +forward 2 +forward 2 +down 6 +up 8 +up 5 +forward 2 +forward 1 +down 9 +forward 7 +down 5 +forward 3 +down 3 +down 5 +down 5 +up 7 +down 8 +forward 2 +forward 4 +forward 5 +forward 1 +down 6 +forward 3 +down 1 +down 7 +forward 3 +forward 7 +down 5 +down 3 +forward 6 +down 3 +down 2 +down 4 +down 9 +forward 7 +down 2 +up 2 +up 6 +up 9 +up 8 +forward 9 +down 1 +forward 4 +forward 2 +forward 7 +forward 2 +down 8 +down 3 +forward 4 +forward 6 +down 8 +forward 7 +forward 6 +up 3 +down 6 +down 1 +down 3 +down 8 +down 2 +down 7 +down 9 +forward 4 +forward 7 +forward 8 +forward 5 +forward 9 +up 5 +down 2 +forward 9 +forward 6 +up 6 +forward 7 +down 2 +down 3 +forward 4 +down 6 +down 1 +down 2 +down 8 +forward 3 +down 3 +forward 3 +down 5 +up 8 +down 5 +forward 8 +down 1 +forward 1 +forward 4 +forward 7 +down 2 +down 5 +forward 5 +down 8 +forward 2 +down 2 +forward 5 +forward 6 +forward 4 +down 7 +up 7 +down 1 +forward 7 +forward 8 +down 6 +up 7 +forward 6 +up 6 +down 8 +forward 5 +forward 8 +up 4 +up 2 +up 1 +down 8 +down 6 +up 2 +down 5 +down 1 +forward 5 +forward 7 +down 2 +up 3 +up 3 +forward 9 +down 1 +forward 6 +down 2 +forward 2 +down 1 +down 9 +forward 7 +down 5 +down 8 +up 1 +forward 1 +down 7 +forward 3 +down 4 +up 4 +down 6 +forward 1 +forward 3 +down 2 +forward 3 +forward 5 +forward 6 +up 2 +up 9 +forward 4 +down 4 +up 1 +up 3 +forward 8 +forward 1 +down 9 +down 9 +forward 2 +down 1 +up 9 +up 3 +up 1 +up 5 +forward 6 +down 9 +forward 6 +forward 9 +forward 6 +forward 4 +up 2 +down 6 +up 3 +forward 3 +forward 1 +up 4 +forward 7 +down 9 +down 3 +forward 9 +down 4 +down 8 +down 3 +up 8 +down 8 +down 8 +forward 2 +forward 8 +up 9 +forward 2 +up 6 +forward 7 +down 1 +forward 5 +forward 4 +forward 1 +forward 7 +up 9 +down 8 +forward 1 +up 5 +forward 9 +forward 2 +forward 8 +down 1 +forward 7 +down 2 +up 8 +down 6 +up 9 +up 3 +down 6 +forward 5 +down 1 +forward 1 +forward 6 +forward 6 +up 1 +forward 5 +forward 1 +up 2 +forward 9 +forward 6 +down 3 +up 1 +forward 7 +forward 2 +down 1 +forward 6 +down 3 +up 2 +down 3 +down 8 +forward 4 +down 2 +up 7 +down 6 +up 5 +down 7 +forward 4 +down 9 +down 3 +forward 2 +up 5 +up 4 +forward 9 +down 1 +up 2 +forward 4 +down 9 +down 8 +forward 5 +forward 2 +down 5 +forward 6 +down 4 +forward 7 +forward 1 +forward 6 +down 3 +down 9 +forward 9 +forward 2 +forward 6 +down 7 +down 5 +down 3 +forward 7 +down 3 +down 3 +down 4 +down 4 +down 7 +down 7 +down 7 +up 7 +up 9 +up 7 +up 3 +up 4 +down 9 +down 4 +up 3 +forward 2 +up 1 +down 9 +down 6 +up 1 +up 2 +down 7 +down 9 +up 2 +forward 7 +down 4 +forward 3 +down 1 +down 7 +forward 7 +up 7 +forward 3 +forward 1 +forward 6 +forward 2 +down 9 +forward 8 +up 8 +down 8 +down 9 +up 1 +down 4 +down 6 +down 8 +up 4 +down 1 +forward 1 +forward 1 +forward 4 +forward 7 +forward 1 +down 4 +forward 5 +up 3 +forward 4 +down 5 +down 1 +up 2 +down 7 +forward 7 +down 7 +up 9 +down 9 +down 3 +up 2 +up 8 +up 8 +up 7 +forward 7 +forward 5 +forward 3 +forward 2 +down 5 +forward 4 +forward 1 +down 6 +down 1 +forward 8 +down 6 +down 3 +down 5 +down 9 +down 3 +forward 7 +forward 6 +down 6 +forward 9 +up 7 +forward 3 +up 5 +down 5 +down 5 +forward 4 +up 6 +down 6 +forward 3 +up 2 +forward 4 +up 1 +down 5 +forward 6 +forward 9 +down 2 +up 2 +down 2 +up 7 +forward 3 +up 2 +forward 9 +forward 5 +down 5 +down 7 +down 8 +down 6 +up 9 +up 5 +forward 7 +down 8 +down 1 +forward 7 +up 2 +forward 4 +forward 2 +up 9 +down 8 +forward 1 +forward 7 +down 2 +down 3 +down 6 +down 3 +forward 1 +up 6 +forward 8 +down 9 +down 9 +forward 8 +up 8 +down 6 +forward 4 +up 1 +forward 5 +down 3 +down 7 +down 7 +down 3 +up 2 +forward 4 +down 9 +forward 2 +down 9 +forward 9 +forward 4 +forward 5 +down 4 +forward 1 +up 1 +forward 4 +up 3 +up 4 +forward 7 +down 9 +forward 6 +down 1 +down 1 +down 2 +down 4 +forward 7 +forward 8 +forward 6 +down 8 +forward 2 +down 3 +up 5 +forward 2 +up 5 +forward 8 +down 8 +down 8 +up 8 +forward 6 +up 1 +down 3 +forward 6 +down 1 +forward 9 +up 1 +forward 7 +forward 7 +down 1 +forward 5 +forward 2 +up 7 +down 1 +forward 2 +down 4 +forward 3 +down 9 +forward 6 +up 5 +forward 1 +forward 5 +down 7 +forward 6 +down 8 +forward 9 +down 1 +forward 9 +down 1 +forward 5 +up 9 +forward 1 +forward 6 +forward 5 +down 7 +down 6 +down 5 +down 9 +forward 9 +down 2 +down 8 +down 8 +forward 2 +forward 3 +forward 3 +down 3 +forward 8 +forward 8 +down 8 +forward 1 +up 1 +forward 4 +down 7 +forward 1 +up 2 +forward 9 +forward 1 +down 6 +up 9 +down 3 +down 1 +up 1 +up 6 +up 7 +forward 9 +up 2 +forward 4 +up 8 +down 6 +forward 3 +forward 7 +down 6 +down 5 +down 3 +forward 5 +down 1 +forward 2 +forward 9 +down 8 +up 6 +forward 3 +forward 2 +up 7 +down 3 +forward 5 +forward 9 +down 5 +down 1 +up 4 +down 8 +forward 1 +forward 3 +forward 3 +down 2 +forward 5 +down 1 +forward 2 +up 3 +forward 8 +down 2 +up 8 +down 6 +down 8 +forward 4 +down 4 +up 7 +up 6 +down 7 +forward 2 +up 3 +forward 3 +down 8 +forward 8 +down 5 +forward 5 +down 3 +up 7 +down 1 +down 2 +up 8 +down 6 +up 6 +down 7 +forward 5 +up 3 +forward 7 +forward 2 +down 9 +down 1 +down 4 +down 7 +forward 9 +up 7 +forward 5 +up 8 +forward 8 +up 1 +forward 2 +down 7 +down 5 +down 6 +down 4 +up 4 +forward 5 +forward 6 +up 4 +forward 8 +forward 4 +forward 3 +up 5 +down 6 +up 4 +forward 8 +down 7 +forward 3 +down 2 +down 7 +down 5 +down 4 +forward 5 +up 4 +forward 4 +down 7 +down 3 +down 9 +down 7 +forward 2 +forward 1 +down 7 +down 8 +forward 1 +forward 2 +down 5 +up 1 +down 1 +forward 5 +down 2 +forward 9 +forward 7 +down 2 +forward 6 +forward 9 +up 5 +forward 3 +up 5 +forward 7 +down 6 +down 3 +up 3 +down 4 +forward 2 +up 4 +forward 5 +up 9 +down 3 +up 1 +down 1 +up 3 +forward 4 +forward 5 +down 3 +forward 5 +down 6 +down 2 +forward 5 +forward 3 +down 7 +down 8 +forward 4 +down 5 +forward 7 +forward 2 +forward 7 +down 7 +up 1 +forward 6 +down 1 +forward 1 +down 4 +forward 1 +up 6 +forward 8 +forward 6 +forward 7 +up 6 +up 7 +up 2 +down 9 +forward 4 +up 3 +down 1 +down 1 +forward 3 +down 4 +down 6 +down 8 +forward 9 +forward 6 +down 1 +forward 5 diff --git a/2021/02/second.b98 b/2021/02/second.b98 new file mode 100644 index 0000000..2c5504c --- /dev/null +++ b/2021/02/second.b98 @@ -0,0 +1,5 @@ +v pos in 0,0, depth in 1,0, aim in 2,0 +>00p10p20p>#;~:'f-#v_$&:00g+00p v@,a.*g01g00; + >'d-#v_$&20g+20pv>20g*10g+10pv + >&20g\-20p v < + ^ ;,a.g01.g00; ~ << # we do not forget to strip the \n from the input diff --git a/2021/02/second.go b/2021/02/second.go new file mode 100644 index 0000000..e6d0e94 --- /dev/null +++ b/2021/02/second.go @@ -0,0 +1,44 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "strconv" + "strings" +) + +func main() { + f, err := os.Open("input") + if err != nil { + log.Fatalf("%+v", err) + } + defer f.Close() + + scanner := bufio.NewScanner(f) + scanner.Split(bufio.ScanLines) + + aim := 0 + pos := 0 + depth := 0 + for scanner.Scan() { + elts := strings.Split(scanner.Text(), " ") + i, err := strconv.Atoi(elts[1]) + if err != nil { + log.Fatalf("%+v", err) + } + switch elts[0] { + case "forward": + pos += i + depth += i * aim + case "down": + aim += i + case "up": + aim -= i + default: + panic(elts[0]) + } + } + fmt.Println(pos * depth) +} diff --git a/2021/03/first.go b/2021/03/first.go new file mode 100644 index 0000000..5343505 --- /dev/null +++ b/2021/03/first.go @@ -0,0 +1,42 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" +) + +func main() { + f, err := os.Open("input") + if err != nil { + log.Fatalf("%+v", err) + } + defer f.Close() + + scanner := bufio.NewScanner(f) + scanner.Split(bufio.ScanLines) + + bits := make([]int, 12) + for scanner.Scan() { + num := scanner.Text() + for n := 0; n < 12; n++ { + if num[n] == '1' { + bits[n]++ + } + } + } + + gamma := 0 + epsilon := 0 + for n := 0; n < 12; n++ { + gamma *= 2 + epsilon *= 2 + if bits[n] > 500 { + gamma++ + } else { + epsilon++ + } + } + fmt.Println(gamma * epsilon) +} diff --git a/2021/03/input b/2021/03/input new file mode 100644 index 0000000..99c0fc4 --- /dev/null +++ b/2021/03/input @@ -0,0 +1,1000 @@ +111011001010 +010011101110 +110001001010 +001101011101 +110100000011 +010110110010 +101000110110 +010100000011 +111001101111 +111001100011 +000110000100 +111011110100 +010001100000 +100011000010 +001001111010 +101001010010 +110000111010 +011100110101 +010111101111 +100011001001 +011110100001 +011101110110 +001011111001 +001110010100 +100001001000 +000100110111 +011111110010 +010101110011 +001101011100 +100001110101 +111000100000 +010111001010 +110000001010 +101011110100 +101010011111 +011001001000 +010010100100 +001100111110 +011111100101 +011010011001 +000010010100 +010001110111 +000101011001 +001110101100 +100011100101 +110001011100 +100101011110 +111000001010 +000010111010 +001001000010 +011011001100 +000111110000 +110110110010 +001011111100 +110111110101 +001000101010 +101010011001 +111110011001 +001000111110 +110110010010 +010011001000 +110100100010 +111000100011 +110111000001 +110110010001 +001100011011 +111100010111 +110110101001 +100010000101 +001001101000 +010000110101 +111010001100 +110000001110 +100111110010 +110101110011 +011100111010 +100000010001 +101000111101 +001010001100 +110100000000 +011001110100 +011010101111 +001011011000 +111100101000 +101001110100 +001111100001 +110111110111 +101100010000 +110000010101 +110111010101 +101110100100 +011010001001 +011101110111 +101011110111 +011001101111 +101101001011 +010000100011 +111011111100 +110101000100 +011111101011 +101000001110 +000111001010 +011100010010 +010011011111 +100000001101 +000110001011 +101100110101 +001001010000 +001111101100 +110100010011 +100011111001 +110000101100 +101111101101 +010011100110 +011000011101 +111110001111 +000100111011 +000001001100 +111110000010 +001001110111 +100000110010 +011000111000 +100010100110 +111110100010 +000001010110 +011010001101 +001100001010 +100111001001 +001010110101 +100111000101 +110011001110 +011100000110 +101111111110 +001110100010 +110101011010 +000011111001 +101011110110 +000111000110 +110010001100 +001001010101 +011111001011 +100010101101 +001000010000 +110111110001 +001111110011 +110111101101 +010011010001 +100110101000 +001011100100 +001010111011 +101000010001 +000101001011 +011010010011 +011011000001 +111111010101 +010000011001 +101100111101 +001001110010 +000010001011 +111000001001 +010001001100 +110110011011 +111011100100 +001000001010 +101110010110 +010010000100 +100000011011 +010000110011 +111111101101 +101010001011 +001011110100 +000111100011 +110011100111 +000011100000 +100101001110 +110100000010 +110101110101 +100001010101 +010100000110 +011010110110 +001011000101 +010010100001 +001001100110 +111011110111 +100110011111 +100111010000 +010010111010 +001000101100 +000101101110 +011001010111 +010001000110 +001110111000 +010001001010 +111000011000 +000011110000 +111100011110 +101110111001 +001101001000 +111010110110 +001110101010 +100001111011 +110010010110 +101001101011 +001100111011 +111001010100 +101111011011 +010010000010 +101101101111 +101000101000 +001100100010 +111001100101 +110110001011 +000110011001 +110010110000 +000111101111 +010010001101 +110110111101 +010100100100 +001111010110 +001101000011 +011110010011 +101111101010 +111001000110 +000110001001 +100100001000 +010100110010 +001011001011 +100010110100 +001110101111 +001001101011 +000111100100 +000001111011 +100111111101 +111011100110 +000100101101 +100110110000 +010001000000 +111111111111 +011010001110 +001100000100 +000010010000 +000100100000 +010100010101 +100100111110 +001100111100 +000001010010 +011010000001 +000000111010 +110011110111 +110011100110 +000011011011 +001001001001 +100111100111 +110000101110 +101100101111 +110001110110 +111011111001 +101000110001 +010000011110 +001011110101 +001100011010 +111001110001 +001110110001 +101011010100 +110110100111 +101011001100 +110010111100 +011111001111 +110001101011 +011100111100 +111101110010 +001111000001 +100101110101 +110001101000 +110001100110 +000011110110 +001111111111 +110101001111 +101000111001 +101100111001 +110011011011 +011100100111 +111000001100 +110010001001 +000101001000 +010101101100 +110010000100 +011000010101 +010001101111 +011000000100 +111111100101 +000011101011 +011001111010 +011010000101 +000001001011 +001111001010 +001011011010 +010011111010 +101001110001 +101001000000 +100101100101 +111100000010 +110000000100 +010110000101 +011011011101 +010110101001 +010011000111 +110000010001 +000101100001 +111000000100 +000011001101 +101101110011 +110101011101 +001111000010 +101110011100 +000100001010 +111101110001 +110110111111 +001011101011 +100001010100 +011001110010 +010001111000 +001101101011 +011011101001 +010111101010 +011110000000 +111111000010 +100010110111 +111010111111 +100110001001 +011111000000 +000111000010 +111001111100 +111010100111 +001010110010 +100011110001 +011100000100 +011110010110 +101001000011 +100011111101 +001100100111 +000110100100 +001001000111 +101000000000 +011100010101 +101110010111 +000110010001 +010101001111 +010010000111 +001010001111 +000111000101 +111101111010 +010010111111 +101111101110 +101100101000 +101101010101 +000011111011 +011111000101 +011111111001 +101000100111 +010101110111 +000111101100 +011010011010 +110001000010 +100110111100 +011110011011 +001101011010 +010001011001 +100110100001 +001010111001 +011100011011 +110001111101 +100111100100 +101111111101 +111000011001 +000000101110 +101111011100 +010100111111 +010001010100 +011001111100 +110100010001 +110001111110 +111001110000 +001110010111 +001100101101 +111001100001 +000100011101 +011101011010 +010110010000 +100000111100 +100100100110 +110111111000 +001111000111 +111111110010 +111010111011 +001000111000 +101010110101 +011001110110 +001100110001 +000111101001 +010011000001 +001101011001 +111001111010 +000110011011 +000101000100 +110000011011 +010011011000 +011100111111 +010101101101 +101100110100 +001011010101 +010001000011 +001000000111 +111111011011 +110000000011 +100101010110 +001001001101 +000101010100 +111101101100 +000111110101 +100101010010 +101110001110 +101011111110 +011011000011 +000111010101 +011010001000 +001010110000 +011110000110 +001111100110 +111001111110 +000101101111 +110111100100 +100011101001 +111000100101 +100100111101 +110001000011 +110000101111 +110001111100 +000101000111 +001011110000 +010000001100 +101101110001 +000110100111 +011101000001 +001111101101 +100000000100 +010010000000 +001111101000 +001010111010 +101010011010 +111010100011 +101111110010 +001011110110 +100010010001 +010110010110 +110111111010 +000111001011 +000001111100 +100101101111 +010100011101 +000010011110 +011100100000 +011101100000 +110000100111 +110111100000 +100001110100 +011111011011 +101110110001 +010000011000 +010011010101 +001101100101 +110110100101 +101000011110 +101011000011 +000010010101 +101001110000 +110001011111 +001101101101 +000001111111 +110010111001 +111001010101 +000010101100 +111011100101 +011111001110 +011000011010 +111111110110 +111010110111 +011010010110 +110011001011 +000010101000 +011101011100 +111010110000 +010011110010 +011110010001 +101111111001 +010010110101 +101010101001 +001101110001 +011111110110 +011110110001 +001111001100 +110000011001 +111001000101 +111110101001 +010011010100 +110011000010 +110000100100 +101000101001 +010101011010 +011001100011 +101110101101 +100100001011 +111010010001 +111010001110 +100110101100 +111010010111 +101111001010 +111101111111 +101111000110 +101111101111 +011001001101 +100110000111 +101101010100 +110100011010 +010111010001 +110011101011 +101001001100 +100111010111 +000011111110 +011110000101 +001011110111 +100000110110 +011110100111 +011100011111 +010100111101 +100100010011 +001000101001 +010111010110 +100000011100 +000000111111 +111101111001 +011011100100 +101011010110 +111010001111 +111101111101 +110100110010 +010001001110 +011001110011 +110100110101 +110010100010 +111111111101 +000110011100 +011010100110 +000000010011 +100100010110 +110110010111 +101011101001 +110000000010 +001110011001 +111111011110 +000111000001 +110010101011 +111111111001 +010001010010 +001111010100 +011011110101 +110000000111 +100011010010 +000010110111 +111100111111 +111111101010 +011000100011 +011111000100 +011100001001 +110001011011 +011011010111 +100000101111 +101001100100 +110000110101 +000001110111 +110101001000 +000001100000 +010001111110 +011110011101 +011011100111 +110000011010 +111010100110 +001101110010 +111000010000 +010110101000 +010001010111 +000010101111 +111000001110 +111011100001 +111100011001 +101111001110 +101011011000 +101100101100 +111111110011 +101010010101 +110110010101 +001100111010 +100001101000 +001100000011 +010110110000 +101110101011 +100001100101 +011001011010 +110110001111 +001000000110 +011101011111 +001010101001 +001111101001 +111100011111 +100010110010 +000001101110 +011110011010 +010110100101 +010010100011 +101010001000 +010000001001 +111100110011 +110010001111 +000101100010 +011111100000 +000100000001 +100100010000 +011110000011 +000010010001 +110010101001 +000000001010 +110010011100 +000000100100 +100010110001 +101111001100 +000001101010 +111011010110 +010101011101 +100110101011 +101001011011 +010011100100 +111000001000 +100111110001 +100100101011 +011001101001 +010101110000 +101000011001 +101111110111 +010111110011 +011111111111 +011100110011 +010010110010 +111011111101 +011110000010 +100001101010 +110100011100 +001001010011 +001110000010 +000110000101 +010110001010 +101000101010 +100000101001 +000001101100 +001001111011 +100011010000 +010000100000 +001111111000 +111011111000 +100011101000 +100110001100 +010001010011 +100110110101 +100001010011 +100100101111 +011101101011 +011110111101 +101110010100 +101010110111 +001010001011 +011111001101 +111011001101 +111011000110 +001100000001 +101100110111 +101100110010 +001000110011 +111010101010 +010101010110 +111100111001 +111000101001 +111000010101 +001000101101 +100111000010 +100000010110 +000100011000 +010100011100 +111010110011 +100011100011 +111111110101 +110101010110 +011001001011 +011101110100 +111110000110 +011010100101 +110100010010 +011111100010 +111010110001 +100111000100 +100000010101 +111000110010 +111100101001 +101010100010 +000110010010 +001111101111 +010011111011 +110010000011 +000010101101 +111111100111 +101001110110 +100111011001 +011010011111 +011011011001 +110001000001 +111011011011 +001011001111 +100101000010 +110000010010 +000011011000 +010111001001 +010001111010 +110101000110 +111011101010 +001100110101 +001111000110 +000000000010 +010100111010 +111101110110 +101101110101 +011001010101 +010110001101 +011110100011 +010101010101 +100110100011 +101111110011 +000111111101 +010011000100 +000010110101 +000001011010 +101100001000 +000101111001 +100001001100 +110110000100 +111010011000 +110000001000 +010111001101 +100111000000 +000001010000 +000110101110 +000100110011 +101011101000 +100110110010 +111011100000 +100000101011 +111100101011 +100010100011 +101001000001 +110001111010 +001001110101 +001000101110 +101011111010 +000110110110 +000100010000 +111001110010 +100010111101 +111110110010 +001111110001 +011111101101 +001001101111 +110111100001 +000010111111 +101110001111 +101011101011 +110001010010 +011010100111 +011011010110 +111100001000 +111100001111 +111101011110 +010000000000 +000000011000 +001000000100 +010111100001 +101110010011 +000101000101 +101100010110 +010111011111 +000010100101 +100001111110 +011101001000 +100010001101 +100111100001 +011010110001 +010110101100 +111010110100 +010110010101 +010100111100 +110001100101 +010010011000 +111110110011 +110001101100 +100011001110 +100001000000 +101001100000 +010111111101 +011000110001 +101001011101 +100110011101 +011101111000 +000000100001 +010000010000 +110100010110 +101100011101 +111011001111 +100101111000 +110101010100 +101111000011 +000101110000 +110101110100 +010110111000 +100010101001 +010111001111 +001011111101 +001111010011 +101001101010 +011110010100 +001100110111 +010111010111 +000001100100 +100001000010 +101001111011 +000011000100 +111000100100 +111010111001 +101101001111 +001010110011 +001000000011 +111111101001 +101001010101 +001010110110 +110101110010 +101101010001 +101010111000 +110100000101 +111100110100 +100010111000 +010010001111 +011000111101 +110011011000 +101010111001 +011000010000 +001001000001 +001100101000 +110111001001 +011001000110 +101001101000 +100010011101 +000001001101 +010001011110 +111111000001 +000111100010 +110110011111 +000001110110 +110101010001 +110100100011 +100110010100 +010101100111 +000000011110 +001000010011 +000101011010 +010101101110 +001010111100 +110100111101 +101101011011 +011011101100 +100000000001 +000101010111 +101010010011 +110001000110 +101110001000 +010110000010 +110100001011 +010110100110 +000110100101 +111110111100 +110010101101 +110111101110 +001111000000 +001101010000 +010011000011 +011110010101 +100001000100 +000000111011 +010011010110 +100110110111 +111000000000 +100101111110 +110011001100 +100001001111 +100100010010 +001101111011 +110010000010 +011101001011 +011000010111 +010000010100 +100100101010 +001100101100 +000100101001 +100100000011 +101010111011 +000001100101 +101011110101 +100101111111 +010101100001 +101000001010 +000011110010 +111001010010 +100001100011 +010111111111 +001110011000 +010001011000 +000011001110 +110010111011 +001100001111 +100001110111 +110000010000 +101110000001 +001011100110 +010101001101 +001001001111 +001110100100 +111110111010 +011101100010 +001010011011 +011011010011 +111000101011 +110110110001 +110100101111 +000000100010 +111111101111 +111000101101 +011001000010 +100000001001 +000101011100 +110100100110 +111011011111 +110110111100 +110011110000 +111110011110 +001101010010 +101000111110 +010101110110 +101100000011 +001111101110 +010101011001 +110010110010 +100100110111 +011100000001 +101110000110 +001011101000 +000011100111 +010101000000 +101011100000 +100100010001 +011011100010 +011010000100 +010010110011 +011010111101 +010010011100 +010010010011 +011000011111 +101101111111 +010100001010 +100100110100 +101011101111 +100100100010 +010001011101 +101101110000 +110010000101 +111011010111 +010011110111 +100101000011 +001100111101 +110110100000 +101001100011 +111001000011 +100001011111 +010000111101 +100011100001 +101100011010 +100110100000 diff --git a/2021/03/second.go b/2021/03/second.go new file mode 100644 index 0000000..33938a1 --- /dev/null +++ b/2021/03/second.go @@ -0,0 +1,96 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" +) + +func findBitCount(data []string) []int { + bits := make([]int, 12) + for _, line := range data { + for n := 0; n < 12; n++ { + if line[n] == '1' { + bits[n]++ + } + } + } + return bits +} + +func main() { + f, err := os.Open("input") + if err != nil { + log.Fatalf("%+v", err) + } + defer f.Close() + + scanner := bufio.NewScanner(f) + scanner.Split(bufio.ScanLines) + + data := make([]string, 0) + for scanner.Scan() { + data = append(data, scanner.Text()) + } + + datacsr := make([]string, len(data)) + copy(datacsr, data) + + var ogr int + for n := 0; n < 12; n++ { + if len(data) == 1 { + break + } + bits := findBitCount(data) + var i byte + if 2*bits[n] >= len(data) { + i = '1' + } else { + i = '0' + } + newdata := make([]string, 0) + for j := 0; j < len(data); j++ { + if data[j][n] == i { + newdata = append(newdata, data[j]) + } + } + data = newdata + } + for n := 0; n < 12; n++ { + ogr *= 2 + if data[0][n] == '1' { + ogr++ + } + } + + data = datacsr + var csr int + for n := 0; n < 12; n++ { + if len(data) == 1 { + break + } + bits := findBitCount(data) + var i byte + if 2*bits[n] < len(data) { + i = '1' + } else { + i = '0' + } + newdata := make([]string, 0) + for j := 0; j < len(data); j++ { + if data[j][n] == i { + newdata = append(newdata, data[j]) + } + } + data = newdata + } + for n := 0; n < 12; n++ { + csr *= 2 + if data[0][n] == '1' { + csr++ + } + } + + fmt.Println(ogr * csr) +} diff --git a/2021/04/first.go b/2021/04/first.go new file mode 100644 index 0000000..ed3942b --- /dev/null +++ b/2021/04/first.go @@ -0,0 +1,123 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "strconv" + "strings" +) + +type grille struct { + lines [][]int + columns [][]int +} + +var ( + best = 100000000 + score = 0 +) + +// winning func +func win(g *grille, n int, iter int) { + sum := 0 + for i := 0; i < 5; i++ { + for j := 0; j < len(g.lines[i]); j++ { + sum += g.lines[i][j] + } + } + if iter < best { + best = iter + score = sum * n + //fmt.Println(iter, n, score, g) + } +} + +func main() { + f, err := os.Open("input") + if err != nil { + log.Fatalf("%+v", err) + } + defer f.Close() + + scanner := bufio.NewScanner(f) + scanner.Split(bufio.ScanLines) + + scanner.Scan() + tirage := make([]int, 0) + tirageStr := strings.Split(scanner.Text(), ",") + // lets process the tirage + for _, v := range tirageStr { + n, err := strconv.Atoi(v) + if err != nil { + log.Fatalf("%+v", err) + } + tirage = append(tirage, n) + } + + for scanner.Scan() { + // we just scanned the new line + // lets init the grille + g := new(grille) + g.lines = make([][]int, 5) + g.columns = make([][]int, 5) + for i := 0; i < 5; i++ { + g.lines[i] = make([]int, 5) + g.columns[i] = make([]int, 5) + } + // lets populate the grille + for i := 0; i < 5; i++ { + scanner.Scan() + numbers := strings.Fields(scanner.Text()) + for j := 0; j < 5; j++ { + n, err := strconv.Atoi(numbers[j]) + if err != nil { + log.Fatalf("%+v", err) + } + g.lines[i][j] = n + g.columns[j][i] = n + } + } + // lets process the tirage + out: + for iter, n := range tirage { + if iter >= best { + //fmt.Println(iter, n, g) + break out + } + // remove the number + for i := 0; i < 5; i++ { + for j := 0; j < len(g.lines[i]); j++ { + if g.lines[i][j] == n { + if len(g.lines[i]) == 1 { + g.lines[i] = []int{} + win(g, n, iter) + break out + } + if j < len(g.lines[i])-1 { + g.lines[i][j] = g.lines[i][len(g.lines[i])-1] + } + g.lines[i] = g.lines[i][:len(g.lines[i])-1] + } + } + } + for i := 0; i < 5; i++ { + for j := 0; j < len(g.columns[i]); j++ { + if g.columns[i][j] == n { + if len(g.columns[i]) == 1 { + g.columns[i] = []int{} + win(g, n, iter) + break out + } + if j < len(g.columns[i])-1 { + g.columns[i][j] = g.columns[i][len(g.columns[i])-1] + } + g.columns[i] = g.columns[i][:len(g.columns[i])-1] + } + } + } + } + } + fmt.Println(score) +} diff --git a/2021/04/input b/2021/04/input new file mode 100644 index 0000000..a560663 --- /dev/null +++ b/2021/04/input @@ -0,0 +1,601 @@ +14,30,18,8,3,10,77,4,48,67,28,38,63,43,62,12,68,88,54,32,17,21,83,64,97,53,24,2,60,96,86,23,20,93,65,34,45,46,42,49,71,9,61,16,31,1,29,40,59,87,95,41,39,27,6,25,19,58,80,81,50,79,73,15,70,37,92,94,7,55,85,98,5,84,99,26,66,57,82,75,22,89,74,36,11,76,56,33,13,72,35,78,47,91,51,44,69,0,90,52 + +13 62 38 10 41 +93 59 60 74 75 +79 18 57 90 28 +56 76 34 96 84 +78 42 69 14 19 + +96 38 62 8 7 +78 50 53 29 81 +88 45 34 58 52 +33 76 13 54 68 +59 95 10 80 63 + +36 26 74 29 55 +43 87 46 70 21 + 9 17 38 58 63 +56 79 85 51 2 +50 57 67 86 8 + +29 78 3 24 79 +15 81 20 6 38 +97 41 28 42 82 +45 68 89 85 92 +48 33 40 62 4 + +68 38 43 2 27 +74 92 49 11 22 +33 97 73 87 30 +42 28 60 39 67 + 3 66 35 94 47 + + 6 65 2 30 88 +69 3 33 61 23 +62 40 37 7 43 +55 12 70 36 73 +15 20 85 38 44 + +99 36 10 83 69 +31 58 93 87 56 +33 25 82 59 30 +45 86 79 12 2 +39 57 51 53 13 + +48 46 11 20 69 +89 9 96 45 76 +67 78 82 6 97 +30 41 37 32 64 +47 31 93 29 75 + +49 93 50 83 64 +54 4 18 1 76 +75 30 19 78 12 +84 55 28 3 57 +16 77 87 25 2 + +15 57 86 92 60 +87 66 76 12 52 +62 74 93 88 78 +39 13 44 20 46 +59 31 40 91 79 + +82 16 73 45 42 +21 79 97 62 5 +96 52 53 74 49 +38 84 85 69 68 +35 44 92 1 47 + + 8 22 61 35 88 +84 86 33 26 11 +57 24 83 25 64 +46 47 28 6 96 +58 99 3 29 40 + +61 49 56 59 81 +68 58 98 50 23 +92 33 43 65 16 +94 47 74 21 60 +83 69 35 31 27 + + 4 32 91 53 18 +41 84 26 69 31 +25 81 0 71 57 +39 11 43 14 63 +24 73 58 17 95 + + 3 50 57 9 94 +48 43 42 96 15 +25 0 38 12 39 +45 56 77 47 53 +19 95 24 28 54 + +78 67 64 33 40 +29 48 8 17 38 +22 79 71 46 83 +11 45 47 87 80 + 3 84 26 5 28 + +77 92 70 44 61 +59 78 12 8 50 +60 58 55 98 85 +66 2 52 83 81 +34 79 20 30 38 + +67 3 44 4 91 +78 64 32 54 31 +89 41 94 18 8 +58 23 48 62 16 +24 12 80 53 28 + +76 54 87 69 88 +16 36 90 48 5 +75 62 15 96 23 +70 42 61 1 41 +71 22 45 92 8 + +34 99 93 83 80 +31 9 43 12 57 +18 59 10 53 0 +76 87 74 38 39 +61 48 60 16 3 + +98 1 32 7 97 +87 99 14 53 46 +54 26 43 57 49 +89 72 5 0 40 +11 28 76 84 85 + +58 65 14 33 32 +13 27 30 44 35 +45 56 10 1 31 +68 93 96 38 26 +17 53 72 6 86 + +85 34 72 52 11 +92 22 79 55 47 +12 3 86 95 14 +84 81 17 20 67 +31 16 69 61 2 + +40 10 23 8 61 +88 11 86 87 49 +36 3 73 58 20 +33 62 98 38 93 +63 95 6 90 18 + +49 15 55 14 57 +41 36 32 1 35 +78 70 79 54 26 +23 0 34 21 77 +96 48 51 47 82 + +29 61 54 64 35 +79 11 31 55 42 + 0 88 20 34 3 +84 59 46 68 50 +43 6 12 17 73 + +53 91 64 39 5 +23 51 33 13 55 +25 66 56 20 98 + 6 46 72 70 3 +19 38 54 16 34 + +14 94 60 28 2 +97 80 93 89 83 +71 86 74 52 5 +77 69 64 10 81 +21 22 95 39 78 + +79 33 4 61 66 +31 49 67 30 98 +43 71 84 72 52 +29 39 81 35 37 + 2 95 94 13 14 + +77 19 40 46 96 +37 81 64 75 95 +47 68 83 25 69 +70 35 11 17 91 +31 92 1 44 14 + +68 66 30 84 55 +87 76 73 29 53 +37 58 23 12 35 + 1 7 20 34 82 +27 86 16 60 54 + +11 68 61 36 13 +78 38 98 22 73 +56 89 93 8 10 + 5 82 60 40 76 +45 39 72 48 75 + +35 22 88 77 8 +75 30 90 6 41 +12 69 48 73 61 +18 1 58 32 79 +37 36 85 9 17 + +81 76 31 87 10 +27 69 36 57 15 +44 72 30 59 9 +24 91 13 73 61 +20 84 55 51 26 + +95 74 5 11 44 +16 26 91 58 53 +82 55 27 61 76 +40 4 20 78 33 +32 28 98 9 15 + +20 1 36 71 23 +56 42 0 73 45 +92 64 5 50 43 +69 37 87 53 7 +57 84 61 70 58 + +25 95 78 27 43 +77 97 74 13 68 +20 81 18 37 4 +61 9 55 92 73 +72 16 80 41 57 + +86 74 92 16 62 +97 82 26 66 41 +73 46 6 40 83 +48 50 67 13 3 +20 27 61 10 81 + +53 24 29 60 79 +57 39 31 34 15 +12 38 13 8 30 +94 98 14 54 6 +47 82 65 22 42 + +72 69 86 31 40 +19 77 21 9 52 +53 97 7 27 20 +35 46 22 23 39 +11 43 50 29 28 + +34 10 81 75 42 +27 67 59 20 87 +45 17 46 23 76 +40 4 77 25 96 +18 8 88 53 32 + +39 37 52 22 70 +51 15 69 23 64 +65 50 43 29 91 + 3 67 1 84 76 +96 72 54 28 42 + +45 81 20 22 59 +94 70 27 61 77 + 4 83 44 68 42 +13 93 1 30 34 +84 37 35 8 48 + +24 91 55 76 74 +48 64 59 58 44 +99 29 3 34 87 +20 4 42 68 65 +90 13 82 93 78 + +61 36 18 56 26 +81 41 1 96 78 +68 54 84 4 86 +71 52 28 59 39 +60 72 40 58 37 + +66 91 90 56 73 +30 28 22 8 6 +19 70 95 17 72 +46 20 10 21 36 +53 64 99 9 79 + +93 62 37 28 17 +51 42 76 58 65 +99 71 12 66 11 +19 13 3 97 22 +86 50 36 39 16 + +10 32 80 74 2 +38 59 90 63 98 +51 7 9 57 24 +19 48 75 79 30 +40 86 72 71 54 + +10 99 7 84 46 +16 79 74 55 57 +54 12 63 30 82 +49 81 33 39 14 +65 58 67 98 61 + +60 53 43 12 2 +26 25 49 61 54 +17 73 75 47 19 + 9 95 67 46 98 +86 8 35 81 77 + +45 1 88 26 31 +53 37 64 11 28 + 7 39 32 9 72 +75 51 50 70 3 +82 79 71 90 8 + +58 26 97 61 76 +24 90 98 15 80 +62 75 34 69 4 +10 0 64 73 99 +46 83 40 23 65 + +11 42 17 59 88 +71 25 27 60 96 +43 72 69 46 37 +29 1 66 12 76 +22 34 81 47 80 + +14 24 36 83 31 +26 59 8 60 21 +57 90 85 89 32 +64 4 91 99 81 +33 1 54 73 40 + +70 57 10 81 68 +16 97 21 11 88 +56 24 51 61 5 +38 80 31 23 9 +48 95 30 91 41 + +53 15 91 12 87 +83 72 98 0 58 +49 42 94 39 77 + 6 59 90 48 26 + 2 78 62 93 33 + +59 13 31 24 71 +18 30 3 33 34 +15 61 56 91 75 +38 58 29 72 26 +81 50 88 82 40 + +26 44 95 42 3 +52 12 51 20 68 +45 34 36 11 77 +99 54 85 47 75 +22 63 82 32 15 + +52 34 81 57 70 +44 51 31 39 80 +14 82 12 8 75 +28 38 5 58 45 +22 26 21 92 74 + +88 47 20 17 0 +50 52 53 43 57 +40 38 33 9 36 +78 93 63 12 14 +65 67 91 48 98 + +25 56 93 76 12 +39 51 97 86 94 +74 10 77 1 5 +66 55 79 89 48 +92 58 80 78 87 + +72 55 75 34 69 +31 73 42 86 70 +81 11 33 45 17 +96 27 65 50 35 +18 88 98 84 39 + +91 51 90 93 6 +77 35 13 50 17 +89 75 57 39 36 +92 64 56 20 78 +12 80 34 69 9 + +99 59 14 76 4 +84 19 72 30 40 +75 6 68 66 3 +51 28 60 2 63 +85 77 62 65 5 + +29 23 24 20 87 +96 0 6 40 98 +13 80 41 82 3 +62 88 25 10 99 +32 59 31 94 9 + +22 53 42 6 33 +40 29 54 68 27 +19 30 77 37 61 +17 62 57 36 44 +69 50 32 1 20 + +87 88 94 41 91 +75 85 67 78 9 +89 46 34 59 25 +74 20 77 97 56 +62 11 54 17 38 + +85 99 89 0 98 +82 90 62 8 24 +23 72 51 59 77 +78 31 60 10 29 +44 46 26 58 68 + +39 71 56 63 33 +66 83 68 58 69 +93 73 70 15 88 +55 76 4 92 38 +47 28 36 94 89 + +12 22 20 14 51 +30 11 86 57 97 +56 6 93 59 18 +28 40 90 67 94 +16 50 89 95 88 + +15 26 31 56 21 +47 77 23 22 66 +86 51 49 24 54 +53 2 0 84 63 +60 92 48 17 4 + +16 74 15 19 53 +86 13 59 8 39 +44 77 25 90 70 +92 3 33 60 37 +10 22 20 28 14 + +21 72 63 91 41 +46 6 8 73 71 + 9 54 52 78 43 +10 27 23 42 48 + 2 25 80 20 37 + +46 70 1 56 35 +20 33 52 25 63 +71 57 40 45 30 +26 51 95 3 47 +58 55 98 83 74 + +47 23 58 98 59 +28 49 32 81 18 +83 54 34 67 3 + 4 95 27 74 22 + 2 45 30 44 33 + +67 77 0 29 76 +15 25 34 63 42 +21 68 18 1 99 + 7 69 94 79 4 +50 8 17 81 53 + +29 90 98 59 39 +15 6 91 7 76 +22 66 27 23 37 +12 33 38 54 31 +86 48 85 16 11 + +62 57 12 76 59 +41 66 13 27 43 +55 83 78 95 65 +82 51 77 53 14 +34 45 94 92 19 + +13 71 1 27 41 +45 6 60 91 20 +90 2 98 76 32 +94 92 84 54 78 +51 21 40 50 47 + +58 47 98 68 23 +92 63 28 82 73 +64 93 94 37 31 +12 20 41 61 35 +44 83 36 95 99 + + 0 47 37 21 64 +79 17 46 70 97 + 5 89 76 36 32 +51 38 19 59 31 + 3 50 63 45 9 + +65 86 43 8 77 +38 18 49 14 88 +87 28 27 26 32 +19 9 45 98 76 +85 80 5 7 25 + +67 65 23 88 28 +70 71 83 35 13 +91 6 47 54 2 +15 76 59 74 62 +46 80 95 27 18 + +34 68 61 1 86 +28 97 29 56 24 +87 73 44 94 36 +58 8 49 72 47 +83 54 30 39 41 + +24 71 72 5 11 +37 30 88 86 22 +18 94 48 10 47 +64 0 20 74 12 +87 46 59 92 33 + +90 77 97 61 75 + 2 10 5 63 69 +43 32 83 24 13 +78 1 50 70 53 +87 47 68 73 0 + +58 95 26 35 23 + 8 2 83 41 56 +60 44 75 84 92 +88 78 53 31 55 +80 70 25 81 0 + +10 59 62 74 86 +42 64 58 36 14 +93 51 45 1 3 +52 69 94 5 44 + 4 50 48 8 12 + +38 40 21 97 74 +98 17 3 56 58 +57 63 44 93 70 +10 48 49 81 73 +12 55 86 41 82 + +93 29 38 58 5 + 9 95 45 67 97 +16 64 99 65 94 +43 34 14 23 66 + 0 88 87 50 78 + +72 70 87 50 31 +90 91 69 18 75 +42 21 11 86 81 +20 63 13 43 2 +56 92 29 30 73 + +63 98 32 50 17 +71 79 4 67 44 + 7 68 45 58 80 +59 12 55 83 34 +90 94 86 25 0 + +15 24 92 39 53 +63 7 30 77 28 +65 97 68 98 35 +88 23 85 96 70 +18 69 99 42 75 + +65 75 12 20 21 +29 31 57 45 13 +34 81 97 78 28 +63 38 51 19 37 +53 67 49 14 74 + +75 35 42 5 89 +56 50 3 90 62 + 7 15 45 16 14 +40 8 84 97 65 +46 51 27 9 18 + +62 42 24 0 53 +41 94 70 88 33 +32 19 43 21 23 +84 98 60 39 36 + 5 4 49 76 82 + +99 24 90 5 76 +78 66 72 53 85 +56 94 14 79 7 +83 6 27 26 69 +67 41 54 68 91 + + 5 72 83 53 49 +48 6 9 38 15 +99 84 90 82 70 +69 85 4 67 20 +87 16 61 21 39 + +18 43 78 0 77 +91 37 88 32 71 +15 54 2 62 17 +30 98 69 38 94 +83 63 89 39 14 + +26 70 3 5 89 +94 49 35 43 99 +82 36 62 78 37 +90 73 9 38 40 +60 68 8 2 53 diff --git a/2021/04/second.go b/2021/04/second.go new file mode 100644 index 0000000..7677e4a --- /dev/null +++ b/2021/04/second.go @@ -0,0 +1,119 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "strconv" + "strings" +) + +type grille struct { + lines [][]int + columns [][]int +} + +var ( + best = -1 + score = 0 +) + +// winning func +func win(g *grille, n int, iter int) { + sum := 0 + for i := 0; i < 5; i++ { + for j := 0; j < len(g.lines[i]); j++ { + sum += g.lines[i][j] + } + } + if iter > best { + best = iter + score = sum * n + //fmt.Println(iter, n, score, g) + } +} + +func main() { + f, err := os.Open("input") + if err != nil { + log.Fatalf("%+v", err) + } + defer f.Close() + + scanner := bufio.NewScanner(f) + scanner.Split(bufio.ScanLines) + + scanner.Scan() + tirage := make([]int, 0) + tirageStr := strings.Split(scanner.Text(), ",") + // lets process the tirage + for _, v := range tirageStr { + n, err := strconv.Atoi(v) + if err != nil { + log.Fatalf("%+v", err) + } + tirage = append(tirage, n) + } + + for scanner.Scan() { + // we just scanned the new line + // lets init the grille + g := new(grille) + g.lines = make([][]int, 5) + g.columns = make([][]int, 5) + for i := 0; i < 5; i++ { + g.lines[i] = make([]int, 5) + g.columns[i] = make([]int, 5) + } + // lets populate the grille + for i := 0; i < 5; i++ { + scanner.Scan() + numbers := strings.Fields(scanner.Text()) + for j := 0; j < 5; j++ { + n, err := strconv.Atoi(numbers[j]) + if err != nil { + log.Fatalf("%+v", err) + } + g.lines[i][j] = n + g.columns[j][i] = n + } + } + // lets process the tirage + out: + for iter, n := range tirage { + // remove the number + for i := 0; i < 5; i++ { + for j := 0; j < len(g.lines[i]); j++ { + if g.lines[i][j] == n { + if len(g.lines[i]) == 1 { + g.lines[i] = []int{} + win(g, n, iter) + break out + } + if j < len(g.lines[i])-1 { + g.lines[i][j] = g.lines[i][len(g.lines[i])-1] + } + g.lines[i] = g.lines[i][:len(g.lines[i])-1] + } + } + } + for i := 0; i < 5; i++ { + for j := 0; j < len(g.columns[i]); j++ { + if g.columns[i][j] == n { + if len(g.columns[i]) == 1 { + g.columns[i] = []int{} + win(g, n, iter) + break out + } + if j < len(g.columns[i])-1 { + g.columns[i][j] = g.columns[i][len(g.columns[i])-1] + } + g.columns[i] = g.columns[i][:len(g.columns[i])-1] + } + } + } + } + } + fmt.Println(score) +} diff --git a/2021/05/README.md b/2021/05/README.md new file mode 100644 index 0000000..6a4996d --- /dev/null +++ b/2021/05/README.md @@ -0,0 +1,3 @@ +I went a little overboard with a parsing tutorial. + +I wanted to manually write a parser from quite some time now and seized this opportunity. It would have been trivial with just a scanner or even a regex, but hey learning! diff --git a/2021/05/example b/2021/05/example new file mode 100644 index 0000000..b258f68 --- /dev/null +++ b/2021/05/example @@ -0,0 +1,10 @@ +0,9 -> 5,9 +8,0 -> 0,8 +9,4 -> 3,4 +2,2 -> 2,1 +7,0 -> 7,4 +6,4 -> 2,0 +0,9 -> 2,9 +3,4 -> 1,4 +0,0 -> 8,8 +5,5 -> 8,2 diff --git a/2021/05/first.go b/2021/05/first.go new file mode 100644 index 0000000..3bd007b --- /dev/null +++ b/2021/05/first.go @@ -0,0 +1,48 @@ +package main + +import ( + "bufio" + "fmt" + "os" + + "git.adyxax.org/aoc/2021/05/line" +) + +func main() { + matrix := make([][]int, 1000) + for i := 0; i < 1000; i++ { + matrix[i] = make([]int, 1000) + } + + parser := line.NewParser(bufio.NewReader(os.Stdin)) + for { + l, err := parser.Parse() + if err != nil { + break + } + if l.X1 == l.X2 { + if l.Y1 > l.Y2 { + l.Y1, l.Y2 = l.Y2, l.Y1 + } + for i := l.Y1; i <= l.Y2; i++ { + matrix[l.X1][i]++ + } + } else if l.Y1 == l.Y2 { + if l.X1 > l.X2 { + l.X1, l.X2 = l.X2, l.X1 + } + for i := l.X1; i <= l.X2; i++ { + matrix[i][l.Y1]++ + } + } + } + score := 0 + for i := 0; i < 1000; i++ { + for j := 0; j < 1000; j++ { + if matrix[i][j] >= 2 { + score++ + } + } + } + fmt.Println(score) +} diff --git a/2021/05/first_v2.go b/2021/05/first_v2.go new file mode 100644 index 0000000..d99f80b --- /dev/null +++ b/2021/05/first_v2.go @@ -0,0 +1,60 @@ +package main + +import ( + "bufio" + "fmt" + "os" + + "git.adyxax.org/aoc/2021/05/line" +) + +func abs(a int) int { + if a < 0 { + return -a + } + return a +} + +func computeDelta(a, b int) int { + if a < b { + return 1 + } else if a > b { + return -1 + } + return 0 +} + +func main() { + matrix := make([][]int, 1000) + for i := 0; i < 1000; i++ { + matrix[i] = make([]int, 1000) + } + + parser := line.NewParser(bufio.NewReader(os.Stdin)) + for { + l, err := parser.Parse() + if err != nil { + break + } + length := abs(l.X2 - l.X1) + if length == 0 { + length = abs(l.Y2 - l.Y1) + } + dx := computeDelta(l.X1, l.X2) + dy := computeDelta(l.Y1, l.Y2) + if dx == 0 || dy == 0 { + for i := 0; i <= length; i++ { + matrix[l.X1+i*dx][l.Y1+i*dy]++ + } + } + } + score := 0 + for i := 0; i < 1000; i++ { + for j := 0; j < 1000; j++ { + if matrix[i][j] >= 2 { + score++ + } + } + } + fmt.Println(score) +} diff --git a/2021/05/go.mod b/2021/05/go.mod new file mode 100644 index 0000000..81fcab6 --- /dev/null +++ b/2021/05/go.mod @@ -0,0 +1,3 @@ +module git.adyxax.org/aoc/2021/05 + +go 1.17 diff --git a/2021/05/input b/2021/05/input new file mode 100644 index 0000000..527ecf6 --- /dev/null +++ b/2021/05/input @@ -0,0 +1,500 @@ +223,805 -> 223,548 +609,164 -> 609,503 +461,552 -> 796,552 +207,361 -> 207,34 +503,879 -> 503,946 +937,52 -> 937,268 +560,652 -> 118,652 +771,103 -> 85,789 +119,156 -> 947,984 +356,634 -> 607,634 +348,812 -> 873,287 +409,490 -> 726,490 +298,790 -> 298,454 +407,543 -> 820,130 +206,89 -> 591,89 +164,709 -> 976,709 +208,921 -> 208,131 +515,209 -> 515,745 +876,639 -> 281,44 +270,453 -> 727,910 +190,417 -> 190,755 +522,726 -> 903,726 +390,651 -> 603,864 +707,549 -> 926,330 +471,869 -> 471,561 +970,735 -> 401,735 +612,624 -> 612,88 +844,879 -> 844,453 +400,38 -> 400,350 +832,225 -> 984,225 +971,642 -> 42,642 +70,862 -> 447,485 +183,79 -> 183,708 +598,700 -> 598,287 +635,195 -> 39,195 +587,362 -> 349,362 +108,88 -> 965,945 +700,299 -> 165,299 +295,824 -> 785,334 +211,284 -> 390,105 +288,326 -> 672,710 +595,231 -> 595,679 +671,576 -> 813,718 +14,845 -> 784,75 +700,129 -> 43,129 +83,913 -> 889,107 +830,596 -> 322,596 +391,450 -> 391,779 +384,32 -> 384,430 +311,948 -> 938,321 +460,288 -> 460,392 +924,602 -> 924,595 +703,458 -> 703,475 +335,953 -> 335,195 +692,314 -> 927,314 +131,433 -> 131,737 +590,771 -> 965,771 +650,13 -> 963,13 +586,904 -> 658,976 +238,824 -> 782,824 +366,45 -> 691,370 +428,758 -> 201,758 +240,545 -> 30,545 +396,154 -> 332,154 +549,307 -> 233,307 +187,240 -> 851,904 +151,135 -> 937,921 +342,850 -> 342,156 +695,200 -> 695,754 +385,880 -> 893,372 +986,966 -> 813,966 +727,661 -> 727,402 +316,937 -> 316,797 +422,235 -> 422,282 +965,684 -> 882,684 +266,222 -> 419,69 +649,843 -> 635,857 +618,84 -> 126,576 +588,822 -> 588,636 +569,142 -> 569,607 +899,479 -> 488,890 +986,52 -> 369,52 +987,478 -> 551,914 +867,951 -> 973,845 +90,401 -> 304,401 +60,836 -> 798,836 +143,675 -> 686,675 +743,974 -> 743,305 +981,899 -> 551,469 +705,430 -> 493,430 +301,366 -> 823,366 +978,712 -> 617,712 +426,805 -> 426,345 +532,855 -> 532,54 +612,143 -> 612,133 +57,52 -> 955,950 +880,50 -> 16,914 +89,908 -> 89,214 +487,867 -> 586,867 +181,285 -> 181,470 +526,666 -> 86,226 +117,704 -> 117,961 +289,101 -> 983,795 +586,429 -> 442,429 +442,869 -> 734,869 +564,479 -> 564,382 +447,486 -> 62,101 +42,218 -> 509,218 +21,890 -> 843,68 +84,978 -> 921,141 +590,960 -> 590,934 +54,949 -> 967,36 +799,39 -> 767,39 +979,232 -> 979,628 +489,482 -> 339,482 +759,473 -> 290,942 +960,958 -> 32,30 +134,180 -> 134,864 +972,981 -> 13,22 +106,385 -> 11,385 +849,454 -> 447,454 +477,385 -> 955,863 +853,180 -> 922,180 +509,274 -> 751,32 +905,295 -> 779,295 +661,629 -> 104,629 +935,117 -> 93,959 +165,372 -> 746,953 +988,141 -> 122,141 +625,621 -> 625,406 +24,710 -> 465,710 +417,468 -> 851,34 +365,285 -> 572,285 +217,164 -> 217,214 +943,439 -> 465,439 +80,102 -> 80,717 +869,19 -> 54,834 +399,480 -> 399,458 +644,826 -> 644,911 +651,189 -> 651,687 +671,946 -> 332,607 +531,417 -> 657,417 +847,350 -> 847,112 +315,733 -> 871,177 +749,118 -> 692,118 +55,616 -> 55,894 +570,307 -> 633,307 +12,964 -> 883,93 +84,299 -> 84,185 +49,187 -> 903,187 +592,40 -> 842,40 +639,381 -> 802,544 +59,61 -> 836,61 +968,51 -> 266,753 +883,373 -> 883,130 +612,45 -> 406,45 +206,698 -> 206,823 +385,685 -> 385,46 +656,338 -> 73,921 +256,794 -> 365,903 +671,247 -> 248,247 +722,509 -> 635,422 +460,783 -> 615,783 +946,980 -> 946,129 +343,780 -> 343,723 +218,371 -> 218,856 +363,809 -> 143,589 +434,739 -> 889,739 +75,71 -> 975,971 +57,253 -> 582,778 +976,237 -> 976,148 +386,866 -> 386,544 +901,797 -> 901,630 +976,706 -> 195,706 +264,420 -> 272,428 +693,72 -> 693,379 +888,832 -> 888,490 +363,900 -> 363,350 +25,312 -> 25,58 +292,307 -> 481,307 +715,393 -> 976,132 +641,450 -> 96,450 +650,38 -> 432,38 +339,97 -> 476,97 +916,24 -> 13,927 +933,934 -> 34,35 +971,367 -> 971,919 +726,310 -> 477,559 +12,984 -> 986,10 +318,531 -> 318,72 +604,979 -> 12,387 +890,39 -> 890,213 +944,954 -> 33,43 +507,830 -> 284,607 +724,111 -> 724,242 +425,912 -> 425,445 +371,903 -> 371,634 +415,314 -> 415,509 +884,849 -> 884,454 +726,647 -> 447,926 +588,463 -> 588,426 +807,453 -> 807,593 +32,449 -> 975,449 +593,757 -> 593,607 +521,850 -> 521,139 +843,478 -> 843,317 +408,834 -> 408,455 +65,241 -> 864,241 +532,138 -> 613,138 +477,239 -> 477,676 +92,400 -> 92,935 +268,104 -> 300,104 +942,20 -> 93,869 +294,134 -> 695,134 +748,477 -> 748,311 +581,879 -> 481,879 +292,57 -> 874,639 +829,787 -> 944,787 +130,780 -> 442,780 +754,435 -> 956,435 +306,659 -> 306,491 +252,612 -> 646,612 +846,949 -> 846,924 +197,888 -> 145,836 +156,790 -> 151,790 +903,305 -> 671,73 +195,79 -> 195,40 +781,67 -> 781,635 +742,743 -> 742,280 +297,42 -> 618,42 +237,151 -> 156,151 +851,930 -> 47,126 +425,368 -> 659,134 +57,890 -> 898,49 +86,62 -> 86,445 +133,499 -> 133,604 +202,567 -> 872,567 +749,578 -> 749,804 +379,379 -> 147,379 +510,474 -> 510,388 +184,115 -> 738,115 +904,613 -> 550,613 +755,649 -> 755,305 +461,306 -> 461,547 +469,124 -> 542,124 +736,218 -> 736,968 +307,662 -> 307,188 +360,970 -> 58,668 +36,267 -> 214,267 +980,330 -> 22,330 +222,972 -> 222,178 +846,774 -> 714,774 +798,837 -> 789,837 +567,258 -> 567,502 +325,582 -> 325,976 +138,386 -> 138,691 +326,878 -> 326,386 +790,276 -> 811,276 +517,522 -> 81,86 +493,567 -> 406,567 +522,370 -> 13,370 +31,697 -> 607,121 +297,524 -> 297,320 +790,681 -> 753,681 +90,961 -> 901,150 +262,46 -> 262,68 +18,26 -> 977,985 +782,381 -> 956,381 +353,740 -> 353,595 +32,448 -> 941,448 +405,254 -> 686,254 +853,57 -> 297,613 +555,209 -> 439,209 +765,679 -> 142,56 +175,903 -> 175,685 +693,653 -> 845,653 +394,108 -> 394,901 +351,108 -> 335,108 +841,83 -> 841,716 +525,608 -> 525,496 +874,32 -> 874,214 +354,760 -> 44,760 +249,330 -> 864,945 +553,377 -> 553,944 +903,374 -> 335,374 +387,34 -> 387,86 +380,331 -> 380,124 +618,520 -> 797,520 +718,169 -> 703,169 +355,184 -> 851,184 +582,570 -> 582,313 +312,952 -> 312,460 +269,70 -> 269,197 +701,907 -> 701,768 +645,417 -> 645,548 +931,532 -> 367,532 +497,361 -> 497,348 +563,642 -> 976,642 +376,504 -> 376,448 +538,945 -> 538,773 +594,886 -> 594,281 +879,558 -> 192,558 +985,68 -> 66,987 +599,420 -> 599,41 +296,318 -> 296,132 +330,619 -> 302,619 +245,137 -> 918,810 +823,798 -> 556,531 +64,201 -> 723,860 +955,365 -> 955,829 +372,976 -> 255,859 +804,962 -> 168,962 +200,442 -> 200,97 +965,964 -> 870,869 +534,158 -> 128,564 +380,739 -> 577,542 +740,391 -> 740,651 +167,177 -> 619,177 +215,449 -> 215,330 +494,612 -> 19,137 +458,634 -> 458,257 +884,817 -> 393,326 +407,291 -> 19,679 +627,173 -> 627,570 +53,93 -> 552,592 +809,363 -> 119,363 +588,418 -> 588,764 +807,131 -> 807,834 +616,61 -> 514,61 +553,642 -> 236,325 +959,553 -> 683,553 +36,754 -> 36,830 +533,293 -> 144,293 +950,780 -> 396,780 +949,878 -> 14,878 +453,180 -> 989,180 +22,46 -> 670,694 +479,206 -> 479,552 +22,53 -> 599,53 +254,964 -> 884,334 +578,813 -> 100,813 +945,247 -> 778,80 +312,978 -> 312,518 +882,225 -> 882,967 +581,683 -> 293,395 +107,540 -> 534,967 +382,946 -> 28,946 +864,743 -> 246,743 +538,558 -> 733,753 +811,436 -> 814,436 +982,33 -> 65,950 +785,829 -> 945,829 +322,471 -> 346,471 +904,528 -> 904,669 +231,471 -> 772,471 +773,490 -> 669,386 +867,482 -> 417,32 +352,856 -> 352,478 +723,355 -> 619,355 +667,922 -> 667,247 +642,386 -> 241,386 +594,35 -> 594,580 +916,723 -> 793,723 +73,774 -> 269,970 +43,273 -> 148,168 +744,637 -> 825,637 +98,30 -> 98,383 +130,277 -> 802,277 +167,122 -> 672,627 +871,866 -> 564,559 +923,475 -> 539,859 +422,714 -> 422,946 +667,950 -> 667,640 +758,181 -> 12,927 +129,927 -> 129,288 +485,661 -> 402,661 +114,573 -> 974,573 +674,779 -> 851,779 +977,184 -> 977,143 +229,937 -> 229,138 +520,887 -> 520,512 +918,329 -> 918,990 +732,41 -> 521,41 +399,245 -> 883,729 +824,618 -> 356,618 +215,218 -> 845,848 +704,34 -> 307,431 +124,166 -> 696,738 +692,749 -> 839,749 +790,637 -> 790,598 +697,396 -> 669,396 +419,140 -> 113,446 +426,738 -> 171,738 +489,494 -> 190,793 +320,301 -> 320,398 +275,809 -> 275,717 +537,703 -> 465,703 +536,450 -> 560,450 +153,927 -> 914,166 +246,692 -> 485,453 +26,179 -> 26,554 +487,678 -> 487,696 +807,719 -> 224,719 +605,920 -> 899,920 +112,262 -> 112,765 +752,898 -> 752,429 +861,103 -> 861,477 +628,505 -> 628,248 +556,293 -> 556,276 +826,682 -> 273,129 +685,324 -> 685,692 +544,410 -> 544,678 +796,633 -> 796,950 +753,843 -> 753,936 +817,40 -> 817,600 +137,941 -> 677,401 +563,457 -> 599,457 +251,644 -> 251,67 +170,792 -> 805,792 +171,486 -> 171,877 +337,481 -> 268,412 +43,158 -> 44,158 +148,610 -> 863,610 +332,765 -> 202,765 +307,637 -> 334,637 +557,380 -> 231,54 +858,76 -> 150,784 +477,329 -> 319,329 +306,608 -> 306,38 +245,42 -> 245,929 +15,786 -> 745,786 +946,321 -> 841,321 +837,281 -> 837,762 +847,391 -> 847,840 +304,52 -> 304,299 +938,122 -> 877,122 +214,347 -> 862,347 +494,540 -> 751,540 +184,29 -> 913,758 +904,12 -> 15,901 +573,23 -> 158,23 +926,603 -> 643,603 +105,506 -> 518,506 +551,917 -> 983,917 +708,33 -> 831,33 +347,173 -> 218,44 +933,175 -> 933,781 +902,556 -> 902,812 +556,485 -> 252,789 +823,807 -> 368,352 +217,744 -> 217,470 +795,455 -> 795,783 +170,944 -> 926,188 +55,655 -> 258,655 +158,57 -> 959,858 +714,823 -> 714,550 +238,18 -> 388,18 +980,985 -> 12,17 +360,596 -> 770,596 +846,684 -> 220,58 +552,107 -> 552,974 +228,552 -> 354,552 +421,41 -> 421,103 +674,475 -> 912,475 +455,626 -> 455,683 +952,841 -> 946,841 +920,792 -> 381,253 +786,918 -> 786,175 +889,859 -> 889,24 +178,604 -> 573,209 +71,621 -> 550,621 +38,36 -> 922,920 +104,690 -> 575,690 +252,883 -> 894,241 +627,107 -> 417,107 +768,913 -> 13,158 +708,337 -> 708,407 +156,941 -> 156,297 +814,653 -> 814,829 +234,920 -> 896,920 +652,170 -> 128,170 +765,825 -> 347,825 +681,901 -> 681,112 +410,301 -> 979,301 +462,681 -> 462,726 +117,957 -> 117,693 +479,948 -> 698,948 +839,965 -> 97,223 +102,189 -> 102,366 +93,798 -> 819,72 +27,336 -> 27,655 +161,635 -> 527,269 +140,272 -> 140,336 +884,915 -> 41,72 +575,563 -> 155,563 +387,601 -> 387,597 +355,186 -> 782,613 +866,435 -> 816,435 +96,161 -> 764,161 +971,29 -> 21,979 diff --git a/2021/05/line/line.go b/2021/05/line/line.go new file mode 100644 index 0000000..aedf455 --- /dev/null +++ b/2021/05/line/line.go @@ -0,0 +1,239 @@ +package line + +import ( + "bufio" + "bytes" + "fmt" + "io" + "strconv" +) + +type Line struct { + X1, Y1, X2, Y2 int +} + +// Token represents a lexical token. +type Token int + +const ( + // Special tokens + ILLEGAL Token = iota + EOF + WHITESPACE + + // Literals + INT // integers + + // Misc characters + COMMA // , + ARROW // -> +) + +func isWhitespace(ch rune) bool { + return ch == ' ' || ch == '\t' || ch == '\n' +} + +func isDigit(ch rune) bool { + return (ch >= '0' && ch <= '9') +} + +var eof = rune(0) + +// Scanner represents a lexical scanner. +type Scanner struct { + r *bufio.Reader +} + +// NewScanner returns a new instance of Scanner. +func NewScanner(r io.Reader) *Scanner { + return &Scanner{r: bufio.NewReader(r)} +} + +// read reads the next rune from the bufferred reader. +// Returns the rune(0) if an error occurs (or io.EOF is returned). +func (s *Scanner) read() rune { + ch, _, err := s.r.ReadRune() + if err != nil { + return eof + } + return ch +} + +// unread places the previously read rune back on the reader. +func (s *Scanner) unread() { _ = s.r.UnreadRune() } + +// Scan returns the next token and literal value. +func (s *Scanner) Scan() (tok Token, lit string) { + // Read the next rune. + ch := s.read() + + // If we see whitespace then consume all contiguous whitespace. + // If we see a digit then consume as an int. + if isWhitespace(ch) { + return s.scanWhitespace(ch) + } else if isDigit(ch) { + return s.scanInt(ch) + } + + // Otherwise read the individual character. + switch ch { + case eof: + return EOF, "" + case ',': + return COMMA, string(ch) + case '-': + return s.scanArrow(ch) + } + + return ILLEGAL, string(ch) +} + +// scanWhitespace consumes the current rune and all contiguous whitespace. +func (s *Scanner) scanWhitespace(read rune) (tok Token, lit string) { + // Create a buffer and read the current character into it. + var buf bytes.Buffer + buf.WriteRune(read) + + // Read every subsequent whitespace character into the buffer. + // Non-whitespace characters and EOF will cause the loop to exit. + for { + if ch := s.read(); ch == eof { + break + } else if !isWhitespace(ch) { + s.unread() + break + } else { + buf.WriteRune(ch) + } + } + + return WHITESPACE, buf.String() +} + +// scanInt consumes the current rune and all contiguous digit runes. +func (s *Scanner) scanInt(read rune) (tok Token, lit string) { + // Create a buffer and read the current character into it. + var buf bytes.Buffer + buf.WriteRune(read) + + // Read every subsequent ident character into the buffer. + // Non-ident characters and EOF will cause the loop to exit. + for { + if ch := s.read(); ch == eof { + break + } else if !isDigit(ch) { + s.unread() + break + } else { + _, _ = buf.WriteRune(ch) + } + } + + // Otherwise return as a regular identifier. + return INT, buf.String() +} + +func (s *Scanner) scanArrow(read rune) (tok Token, lit string) { + // Create a buffer and read the current character into it. + var buf bytes.Buffer + buf.WriteRune(read) + + // Read every subsequent ident character into the buffer. + // Non-ident characters and EOF will cause the loop to exit. + for { + if ch := s.read(); ch == eof { + break + } else if ch == '>' { + _, _ = buf.WriteRune(ch) + return ARROW, buf.String() + } else { + _, _ = buf.WriteRune(ch) + break + } + } + + // Otherwise return as a regular identifier. + return ILLEGAL, buf.String() +} + +// Parser represents a parser. +type Parser struct { + s *Scanner + buf struct { + tok Token // last read token + lit string // last read literal + n int // buffer size (max=1) + } +} + +// NewParser returns a new instance of Parser. +func NewParser(r io.Reader) *Parser { + return &Parser{s: NewScanner(r)} +} + +// scan returns the next token from the underlying scanner. +// If a token has been unscanned then read that instead. +func (p *Parser) scan() (tok Token, lit string) { + // If we have a token on the buffer, then return it. + if p.buf.n != 0 { + p.buf.n = 0 + return p.buf.tok, p.buf.lit + } + + // Otherwise read the next token from the scanner. + tok, lit = p.s.Scan() + + // Save it to the buffer in case we unscan later. + p.buf.tok, p.buf.lit = tok, lit + + return +} + +// unscan pushes the previously read token back onto the buffer. +func (p *Parser) unscan() { p.buf.n = 1 } + +// scanIgnoreWhitespace scans the next non-whitespace token. +func (p *Parser) scanIgnoreWhitespace() (tok Token, lit string) { + tok, lit = p.scan() + if tok == WHITESPACE { + tok, lit = p.scan() + } + return +} + +func (p *Parser) Parse() (*Line, error) { + l := &Line{} + if tok, lit := p.scanIgnoreWhitespace(); tok != INT { + return nil, fmt.Errorf("found %q, expected INT", lit) + } else { + l.X1, _ = strconv.Atoi(lit) + } + if tok, lit := p.scanIgnoreWhitespace(); tok != COMMA { + return nil, fmt.Errorf("found %q, expected COMMA", lit) + } + if tok, lit := p.scanIgnoreWhitespace(); tok != INT { + return nil, fmt.Errorf("found %q, expected INT", lit) + } else { + l.Y1, _ = strconv.Atoi(lit) + } + if tok, lit := p.scanIgnoreWhitespace(); tok != ARROW { + return nil, fmt.Errorf("found %q, expected ARROW", lit) + } + if tok, lit := p.scanIgnoreWhitespace(); tok != INT { + return nil, fmt.Errorf("found %q, expected INT", lit) + } else { + l.X2, _ = strconv.Atoi(lit) + } + if tok, lit := p.scanIgnoreWhitespace(); tok != COMMA { + return nil, fmt.Errorf("found %q, expected COMMA", lit) + } + if tok, lit := p.scanIgnoreWhitespace(); tok != INT { + return nil, fmt.Errorf("found %q, expected INT", lit) + } else { + l.Y2, _ = strconv.Atoi(lit) + } + if tok, lit := p.scan(); tok != WHITESPACE { + return nil, fmt.Errorf("found %q, expected WHITESPACE", lit) + } + return l, nil +} diff --git a/2021/05/second.go b/2021/05/second.go new file mode 100644 index 0000000..c79025a --- /dev/null +++ b/2021/05/second.go @@ -0,0 +1,61 @@ +package main + +import ( + "bufio" + "fmt" + "os" + + "git.adyxax.org/aoc/2021/05/line" +) + +func main() { + matrix := make([][]int, 1000) + for i := 0; i < 1000; i++ { + matrix[i] = make([]int, 1000) + } + + parser := line.NewParser(bufio.NewReader(os.Stdin)) + for { + l, err := parser.Parse() + if err != nil { + break + } + if l.X1 == l.X2 { + if l.Y1 > l.Y2 { + l.Y1, l.Y2 = l.Y2, l.Y1 + } + for i := l.Y1; i <= l.Y2; i++ { + matrix[l.X1][i]++ + } + } else if l.Y1 == l.Y2 { + if l.X1 > l.X2 { + l.X1, l.X2 = l.X2, l.X1 + } + for i := l.X1; i <= l.X2; i++ { + matrix[i][l.Y1]++ + } + } else { + if l.X1 > l.X2 { + l.X1, l.X2, l.Y1, l.Y2 = l.X2, l.X1, l.Y2, l.Y1 + } + if l.Y1 < l.Y2 { + for i := 0; i <= l.X2-l.X1; i++ { + matrix[l.X1+i][l.Y1+i]++ + } + } else { + for i := 0; i <= l.X2-l.X1; i++ { + matrix[l.X1+i][l.Y1-i]++ + } + } + } + } + score := 0 + for i := 0; i < 1000; i++ { + for j := 0; j < 1000; j++ { + if matrix[i][j] >= 2 { + score++ + } + } + } + fmt.Println(score) +} diff --git a/2021/05/second_v2.go b/2021/05/second_v2.go new file mode 100644 index 0000000..7d02a19 --- /dev/null +++ b/2021/05/second_v2.go @@ -0,0 +1,58 @@ +package main + +import ( + "bufio" + "fmt" + "os" + + "git.adyxax.org/aoc/2021/05/line" +) + +func abs(a int) int { + if a < 0 { + return -a + } + return a +} + +func computeDelta(a, b int) int { + if a < b { + return 1 + } else if a > b { + return -1 + } + return 0 +} + +func main() { + matrix := make([][]int, 1000) + for i := 0; i < 1000; i++ { + matrix[i] = make([]int, 1000) + } + + parser := line.NewParser(bufio.NewReader(os.Stdin)) + for { + l, err := parser.Parse() + if err != nil { + break + } + length := abs(l.X2 - l.X1) + if length == 0 { + length = abs(l.Y2 - l.Y1) + } + dx := computeDelta(l.X1, l.X2) + dy := computeDelta(l.Y1, l.Y2) + for i := 0; i <= length; i++ { + matrix[l.X1+i*dx][l.Y1+i*dy]++ + } + } + score := 0 + for i := 0; i < 1000; i++ { + for j := 0; j < 1000; j++ { + if matrix[i][j] >= 2 { + score++ + } + } + } + fmt.Println(score) +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..be724a6 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +This repository contains my solutions to the advent of code puzzles available at https://adventofcode.com/ + +I do not strive for elegance or anything, I just solve a puzzle (often brute forcing it) and if it executes fast enough I move on to the next one. + +If the puzzle suits my fancy I implement a [Funge-98](https://github.com/catseye/Funge-98/blob/master/doc/funge98.markdown) solution for fun that I run using my own interpreter [gofunge98]*(https://git.adyxax.org/adyxax/gofunge98). -- cgit v1.2.3