From ad032df481ffaaf681431376c30402abeb013ba1 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Fri, 3 Dec 2021 16:58:08 +0100 Subject: [PATCH] New Makefile, day 1 solution (C) --- .gitignore | 5 + 2021/.dir-locals.el | 4 + 2021/.projectile | 0 2021/Makefile | 68 +- 2021/day01/EXAMPLE.txt | 10 + 2021/day01/INPUT.txt | 2000 +++++++++++++++++++++++++++ 2021/day01/Makefile | 53 + 2021/day01/README.txt | 104 ++ 2021/day01/aoc-c.c | 116 ++ 2021/{templates => include}/bits.h | 0 2021/{templates => include}/debug.h | 0 2021/{templates => include}/list.h | 0 2021/include/pool.h | 36 + 2021/{templates => libsrc}/bits.c | 0 2021/libsrc/bits.o | Bin 0 -> 3736 bytes 2021/{templates => libsrc}/debug.c | 0 2021/libsrc/debug.o | Bin 0 -> 9664 bytes 2021/libsrc/pool.c | 180 +++ 2021/libsrc/pool.o | Bin 0 -> 10192 bytes 2021/templates/Makefile | 57 +- 2021/templates/aoc-c.c | 116 ++ 2021/templates/ex1-c.c | 58 - 2021/templates/ex1.bash | 21 - 2021/templates/ex2-c.c | 60 - 2021/templates/ex2.bash | 34 - 25 files changed, 2728 insertions(+), 194 deletions(-) create mode 100644 2021/.dir-locals.el create mode 100644 2021/.projectile create mode 100644 2021/day01/EXAMPLE.txt create mode 100644 2021/day01/INPUT.txt create mode 100644 2021/day01/Makefile create mode 100644 2021/day01/README.txt create mode 100644 2021/day01/aoc-c.c rename 2021/{templates => include}/bits.h (100%) rename 2021/{templates => include}/debug.h (100%) rename 2021/{templates => include}/list.h (100%) create mode 100644 2021/include/pool.h rename 2021/{templates => libsrc}/bits.c (100%) create mode 100644 2021/libsrc/bits.o rename 2021/{templates => libsrc}/debug.c (100%) create mode 100644 2021/libsrc/debug.o create mode 100644 2021/libsrc/pool.c create mode 100644 2021/libsrc/pool.o create mode 100644 2021/templates/aoc-c.c delete mode 100644 2021/templates/ex1-c.c delete mode 100755 2021/templates/ex1.bash delete mode 100644 2021/templates/ex2-c.c delete mode 100755 2021/templates/ex2.bash diff --git a/.gitignore b/.gitignore index e81c793..97bd5f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ ex*-c core ex*-cob +lib/ +GPATH +GRTAGS +GTAGS +2020/day23/ \ No newline at end of file diff --git a/2021/.dir-locals.el b/2021/.dir-locals.el new file mode 100644 index 0000000..0342e84 --- /dev/null +++ b/2021/.dir-locals.el @@ -0,0 +1,4 @@ +((nil . ((eval . (let ((root (expand-file-name "2021/" (projectile-project-root)))) + (setq-local + flycheck-gcc-include-path (list (concat root "include")) + compile-command (concat "make -C " root " all"))))))) diff --git a/2021/.projectile b/2021/.projectile new file mode 100644 index 0000000..e69de29 diff --git a/2021/Makefile b/2021/Makefile index 8211a5f..82e9dd1 100644 --- a/2021/Makefile +++ b/2021/Makefile @@ -1,15 +1,53 @@ +# AOC Makefile - GNU make only. +# +# Copyright (C) 2021 Bruno Raoult ("br") +# Licensed under the GNU General Public License v3.0 or later. +# Some rights reserved. See COPYING. +# * You should have received a copy of the GNU General Public License along with this +# program. If not, see . +# +# SPDX-License-Identifier: GPL-3.0-or-later +# + + SUBDIRS := $(shell echo day??) -EXCLUDE := --exclude 'cob-01/' +CC = gcc -.PHONY: clean $(SUBDIRS) +#LIBS = -lreadline -lncurses +CFLAGS += -std=gnu99 + +#CFLAGS += -O2 +CFLAGS += -g +CFLAGS += -Wall +CFLAGS += -Wextra +CFLAGS += -march=native + +INCDIR := ./include +LIBSRCDIR := ./libsrc +LIBDIR := ./lib +LIB := libaoc_$(shell uname -m) +SLIB := $(LIBDIR)/$(LIB).a +DLIB := $(LIBDIR)/$(LIB).so +LIBSRC := $(wildcard $(LIBSRCDIR)/*.c) +LIBOBJ := $(patsubst %.c,%.o,$(LIBSRC)) +LDFLAGS := -L$(LIBDIR) +LDLIB := -l$(LIB) + +.PHONY: clean cleanall all redo output lib $(SUBDIRS) + +all: lib $(SUBDIRS) clean: for dir in $(SUBDIRS) ; do \ - make -C $$dir clean ; \ + $(MAKE) -C $$dir clean ; \ done -all: $(SUBDIRS) +cleanall: clean + echo AAAAAAAAAAAAAAAAAAAAA + $(RM) -f $(SLIB) $(DLIB) + +redo: cleanall all $(SUBDIRS): @echo "=========================================" @@ -17,9 +55,27 @@ $(SUBDIRS): @echo "=========================================" @echo @echo "+++++++++++++++++ ex1" - @$(MAKE) --no-print-directory -C $@ ex1 2>&1 + +$(MAKE) --no-print-directory -C $@ ex1 2>&1 @echo "+++++++++++++++++ ex2" - @$(MAKE) --no-print-directory -C $@ ex2 2>&1 + +$(MAKE) --no-print-directory -C $@ ex2 2>&1 output: @$(MAKE) --no-print-directory all >OUTPUT 2>&1 + +lib: $(DLIB) $(SLIB) + +$(SLIB): $(LIBOBJ) + @#echo ZZZZZZZZZZZZZZZZZZZZ $(SLIB) $(DLIB) + mkdir -p $(LIBDIR) + $(AR) $(ARFLAGS) -o $@ $^ + +$(DLIB): CFLAGS += -fPIC +$(DLIB): LDFLAGS += -shared +$(DLIB): $(LIBOBJ) + @#echo YYYYYYYYYYYYYYYYYYYY $(SLIB) $(DLIB) + mkdir -p $(LIBDIR) + $(CC) $(LDFLAGS) $^ -o $@ + +.c.o: + echo TOTO + $(CC) -c $(CFLAGS) $(LDFLAGS) -I $(INCDIR) -o $@ $< diff --git a/2021/day01/EXAMPLE.txt b/2021/day01/EXAMPLE.txt new file mode 100644 index 0000000..167e291 --- /dev/null +++ b/2021/day01/EXAMPLE.txt @@ -0,0 +1,10 @@ +199 +200 +208 +210 +200 +207 +240 +269 +260 +263 diff --git a/2021/day01/INPUT.txt b/2021/day01/INPUT.txt new file mode 100644 index 0000000..a3453ba --- /dev/null +++ b/2021/day01/INPUT.txt @@ -0,0 +1,2000 @@ +184 +205 +211 +213 +227 +225 +224 +228 +232 +231 +217 +230 +231 +237 +250 +257 +256 +255 +260 +261 +250 +252 +243 +242 +240 +248 +253 +261 +262 +241 +227 +234 +235 +236 +237 +264 +267 +246 +249 +237 +258 +255 +257 +259 +253 +262 +263 +271 +252 +254 +249 +255 +258 +267 +278 +281 +282 +287 +298 +292 +282 +283 +286 +281 +274 +272 +268 +267 +272 +288 +293 +316 +348 +340 +337 +341 +344 +343 +358 +356 +358 +359 +364 +366 +365 +368 +384 +385 +393 +387 +388 +369 +376 +393 +395 +400 +401 +402 +401 +404 +382 +381 +385 +386 +411 +414 +419 +439 +438 +432 +437 +441 +434 +438 +439 +448 +452 +453 +456 +455 +458 +466 +471 +455 +462 +463 +467 +480 +483 +486 +485 +514 +522 +521 +520 +544 +550 +552 +544 +540 +542 +550 +554 +557 +556 +565 +560 +561 +569 +572 +576 +575 +578 +597 +589 +591 +587 +584 +587 +622 +623 +624 +627 +623 +624 +623 +624 +617 +622 +628 +629 +633 +639 +643 +635 +654 +655 +658 +664 +680 +699 +701 +700 +702 +706 +705 +691 +697 +695 +697 +682 +686 +692 +709 +711 +712 +714 +713 +706 +707 +728 +730 +729 +732 +737 +742 +756 +755 +757 +759 +762 +761 +769 +772 +764 +763 +776 +783 +787 +790 +793 +804 +805 +810 +812 +811 +818 +819 +823 +819 +825 +819 +822 +824 +826 +804 +814 +818 +814 +795 +801 +794 +805 +806 +803 +823 +815 +814 +821 +819 +821 +822 +820 +813 +826 +824 +819 +816 +815 +816 +813 +823 +822 +832 +829 +830 +837 +847 +810 +806 +809 +812 +813 +815 +825 +824 +825 +861 +858 +877 +857 +856 +852 +857 +856 +860 +864 +863 +869 +874 +873 +879 +884 +885 +871 +869 +871 +866 +863 +864 +861 +859 +861 +862 +868 +867 +875 +869 +872 +871 +857 +848 +849 +853 +854 +850 +849 +840 +842 +840 +847 +853 +848 +863 +867 +874 +872 +869 +860 +863 +880 +903 +915 +918 +914 +915 +912 +913 +914 +909 +913 +914 +924 +942 +941 +922 +919 +918 +919 +921 +922 +923 +921 +902 +909 +914 +915 +914 +913 +901 +889 +891 +920 +926 +927 +932 +922 +921 +920 +928 +937 +925 +924 +922 +934 +933 +917 +922 +926 +918 +917 +943 +947 +957 +955 +954 +953 +938 +906 +911 +947 +948 +949 +950 +934 +919 +920 +921 +923 +946 +939 +940 +947 +946 +961 +962 +970 +982 +983 +988 +999 +996 +1016 +1007 +998 +1008 +1007 +1000 +1008 +1012 +1032 +1023 +1041 +1046 +1047 +1037 +1039 +1044 +1052 +1055 +1054 +1058 +1055 +1053 +1041 +1043 +1051 +1043 +1045 +1044 +1035 +1051 +1049 +1054 +1053 +1055 +1064 +1063 +1088 +1092 +1087 +1090 +1087 +1103 +1099 +1096 +1097 +1096 +1101 +1087 +1094 +1096 +1070 +1071 +1075 +1072 +1073 +1074 +1079 +1067 +1058 +1056 +1052 +1053 +1057 +1059 +1069 +1070 +1074 +1090 +1091 +1088 +1065 +1066 +1061 +1062 +1091 +1097 +1100 +1099 +1110 +1139 +1137 +1138 +1116 +1110 +1112 +1118 +1111 +1131 +1132 +1130 +1131 +1142 +1140 +1150 +1146 +1172 +1174 +1183 +1187 +1197 +1192 +1198 +1212 +1211 +1212 +1214 +1213 +1211 +1226 +1218 +1215 +1185 +1205 +1207 +1199 +1216 +1205 +1211 +1199 +1200 +1201 +1206 +1244 +1228 +1236 +1246 +1249 +1248 +1243 +1240 +1237 +1235 +1242 +1244 +1234 +1228 +1244 +1236 +1237 +1236 +1237 +1236 +1239 +1240 +1239 +1236 +1245 +1241 +1246 +1267 +1272 +1280 +1282 +1286 +1288 +1286 +1314 +1315 +1312 +1326 +1316 +1315 +1321 +1324 +1289 +1282 +1267 +1271 +1267 +1264 +1287 +1292 +1295 +1299 +1316 +1319 +1305 +1302 +1312 +1303 +1317 +1324 +1323 +1346 +1345 +1364 +1344 +1369 +1372 +1374 +1375 +1376 +1373 +1387 +1388 +1393 +1392 +1393 +1367 +1379 +1381 +1375 +1379 +1365 +1369 +1372 +1374 +1377 +1365 +1364 +1371 +1370 +1369 +1363 +1354 +1335 +1334 +1341 +1345 +1351 +1355 +1359 +1348 +1349 +1344 +1362 +1361 +1367 +1370 +1360 +1366 +1384 +1385 +1384 +1380 +1378 +1381 +1378 +1377 +1368 +1363 +1378 +1376 +1371 +1361 +1370 +1375 +1386 +1387 +1376 +1377 +1375 +1376 +1375 +1376 +1377 +1382 +1383 +1385 +1404 +1423 +1422 +1419 +1425 +1428 +1427 +1431 +1432 +1431 +1451 +1450 +1459 +1461 +1467 +1466 +1462 +1455 +1438 +1443 +1442 +1447 +1443 +1450 +1469 +1483 +1479 +1477 +1464 +1445 +1447 +1452 +1459 +1458 +1457 +1463 +1462 +1465 +1464 +1468 +1472 +1474 +1475 +1479 +1477 +1511 +1512 +1515 +1513 +1534 +1537 +1534 +1535 +1530 +1532 +1521 +1520 +1524 +1520 +1522 +1535 +1500 +1499 +1494 +1495 +1500 +1514 +1510 +1509 +1511 +1499 +1494 +1517 +1515 +1508 +1509 +1508 +1503 +1498 +1493 +1492 +1494 +1524 +1523 +1522 +1523 +1517 +1515 +1524 +1542 +1544 +1557 +1554 +1556 +1554 +1557 +1525 +1520 +1518 +1522 +1532 +1531 +1522 +1532 +1534 +1538 +1533 +1553 +1558 +1543 +1548 +1554 +1555 +1556 +1547 +1554 +1576 +1580 +1581 +1565 +1567 +1566 +1607 +1611 +1610 +1612 +1618 +1615 +1613 +1615 +1620 +1616 +1624 +1617 +1625 +1648 +1653 +1660 +1659 +1658 +1689 +1690 +1684 +1726 +1721 +1715 +1718 +1695 +1690 +1685 +1713 +1717 +1713 +1701 +1710 +1708 +1707 +1706 +1709 +1718 +1716 +1721 +1713 +1707 +1708 +1711 +1716 +1718 +1712 +1714 +1713 +1702 +1738 +1745 +1738 +1764 +1756 +1759 +1755 +1739 +1736 +1747 +1783 +1777 +1763 +1762 +1766 +1767 +1745 +1749 +1748 +1750 +1738 +1766 +1759 +1760 +1761 +1762 +1759 +1752 +1742 +1744 +1745 +1740 +1738 +1739 +1742 +1754 +1737 +1724 +1725 +1738 +1769 +1775 +1780 +1776 +1777 +1779 +1781 +1776 +1775 +1773 +1772 +1774 +1775 +1778 +1758 +1754 +1751 +1736 +1741 +1743 +1742 +1736 +1763 +1765 +1774 +1776 +1777 +1773 +1774 +1791 +1785 +1786 +1787 +1789 +1790 +1795 +1807 +1816 +1824 +1820 +1823 +1825 +1824 +1823 +1822 +1830 +1842 +1854 +1853 +1852 +1857 +1853 +1848 +1841 +1831 +1818 +1823 +1842 +1844 +1845 +1849 +1854 +1866 +1861 +1869 +1873 +1879 +1887 +1890 +1881 +1896 +1897 +1903 +1904 +1908 +1905 +1913 +1923 +1920 +1922 +1921 +1912 +1904 +1907 +1908 +1911 +1920 +1916 +1917 +1926 +1927 +1924 +1926 +1934 +1937 +1941 +1923 +1902 +1900 +1898 +1923 +1922 +1944 +1961 +1967 +1974 +1977 +1975 +1977 +1972 +2008 +2006 +2005 +2006 +2016 +2015 +2000 +2001 +2011 +2013 +2014 +2013 +2016 +2023 +2042 +2053 +2033 +2042 +2041 +2035 +2041 +2045 +2038 +2033 +2038 +2040 +2042 +2017 +2022 +2025 +2026 +2028 +2027 +2029 +2011 +2008 +2013 +1990 +1996 +1997 +1994 +1991 +1999 +1989 +1988 +1974 +1996 +2007 +2005 +2006 +1975 +1967 +1964 +1958 +1948 +1950 +1952 +1951 +1956 +1988 +1987 +1986 +1987 +1985 +1968 +2009 +2004 +2005 +2006 +2017 +2016 +2046 +2045 +2047 +2052 +2053 +2069 +2053 +2050 +2079 +2093 +2097 +2084 +2083 +2090 +2064 +2057 +2055 +2068 +2074 +2062 +2069 +2070 +2071 +2077 +2071 +2083 +2113 +2119 +2122 +2123 +2111 +2103 +2098 +2102 +2109 +2103 +2091 +2098 +2099 +2098 +2101 +2102 +2108 +2120 +2117 +2121 +2133 +2135 +2139 +2136 +2128 +2125 +2126 +2119 +2137 +2153 +2159 +2156 +2155 +2154 +2153 +2149 +2162 +2158 +2188 +2190 +2189 +2183 +2189 +2195 +2192 +2189 +2192 +2201 +2188 +2193 +2202 +2226 +2228 +2227 +2229 +2204 +2203 +2196 +2208 +2218 +2192 +2194 +2197 +2193 +2191 +2184 +2185 +2184 +2178 +2169 +2170 +2163 +2171 +2174 +2173 +2187 +2188 +2204 +2207 +2206 +2207 +2208 +2209 +2210 +2207 +2220 +2240 +2242 +2254 +2256 +2249 +2251 +2260 +2306 +2284 +2276 +2277 +2281 +2300 +2294 +2287 +2299 +2300 +2295 +2297 +2304 +2305 +2306 +2310 +2304 +2287 +2290 +2307 +2332 +2333 +2328 +2326 +2319 +2330 +2327 +2331 +2329 +2309 +2310 +2312 +2316 +2315 +2304 +2295 +2292 +2295 +2293 +2292 +2293 +2300 +2302 +2319 +2316 +2320 +2321 +2326 +2330 +2329 +2339 +2342 +2340 +2339 +2355 +2372 +2379 +2382 +2381 +2372 +2363 +2365 +2380 +2379 +2383 +2377 +2363 +2362 +2360 +2370 +2368 +2373 +2356 +2362 +2368 +2375 +2381 +2410 +2451 +2453 +2457 +2456 +2448 +2449 +2454 +2444 +2455 +2451 +2446 +2452 +2462 +2459 +2463 +2468 +2470 +2471 +2474 +2464 +2460 +2486 +2484 +2483 +2486 +2492 +2493 +2497 +2498 +2497 +2494 +2488 +2498 +2505 +2506 +2510 +2521 +2499 +2501 +2511 +2507 +2516 +2518 +2525 +2526 +2519 +2524 +2536 +2542 +2538 +2535 +2547 +2558 +2553 +2552 +2558 +2559 +2565 +2571 +2565 +2572 +2591 +2584 +2586 +2605 +2622 +2623 +2625 +2637 +2636 +2637 +2655 +2659 +2658 +2657 +2670 +2671 +2672 +2663 +2666 +2639 +2638 +2636 +2650 +2649 +2651 +2654 +2650 +2643 +2645 +2637 +2638 +2642 +2641 +2647 +2640 +2619 +2635 +2633 +2635 +2637 +2639 +2640 +2626 +2629 +2637 +2638 +2637 +2639 +2637 +2636 +2645 +2619 +2591 +2574 +2577 +2578 +2585 +2594 +2590 +2589 +2608 +2594 +2576 +2578 +2585 +2595 +2596 +2605 +2613 +2614 +2622 +2608 +2610 +2607 +2593 +2595 +2603 +2600 +2594 +2596 +2592 +2574 +2582 +2586 +2574 +2588 +2587 +2589 +2590 +2582 +2587 +2601 +2596 +2583 +2588 +2615 +2616 +2614 +2615 +2612 +2611 +2607 +2638 +2632 +2631 +2629 +2636 +2617 +2614 +2617 +2618 +2616 +2622 +2645 +2646 +2645 +2656 +2652 +2653 +2654 +2655 +2644 +2645 +2637 +2636 +2639 +2643 +2645 +2648 +2650 +2655 +2656 +2654 +2651 +2637 +2643 +2640 +2639 +2643 +2645 +2663 +2645 +2646 +2626 +2606 +2611 +2598 +2605 +2606 +2605 +2607 +2613 +2616 +2636 +2632 +2631 +2632 +2631 +2632 +2631 +2627 +2639 +2641 +2637 +2636 +2632 +2631 +2630 +2657 +2659 +2658 +2646 +2663 +2678 +2674 +2675 +2678 +2674 +2675 +2676 +2684 +2673 +2668 +2642 +2643 +2651 +2633 +2618 +2617 +2616 +2591 +2570 +2574 +2580 +2592 +2580 +2579 +2581 +2579 +2602 +2603 +2602 +2603 +2605 +2617 +2616 +2618 +2623 +2620 +2624 +2635 +2633 +2659 +2661 +2666 +2668 +2664 +2665 +2666 +2644 +2649 +2651 +2642 +2635 +2627 +2626 +2627 +2607 +2623 +2616 +2611 +2619 +2623 +2637 +2644 +2635 +2634 +2632 +2628 +2641 +2644 +2634 +2645 +2646 +2647 +2650 +2648 +2640 +2642 +2629 +2632 +2635 +2637 +2642 +2635 +2634 +2632 +2664 +2658 +2678 +2673 +2697 +2682 +2699 +2723 +2722 +2726 +2727 +2728 +2727 +2728 +2730 +2727 +2719 +2738 +2736 +2734 +2707 +2706 +2705 +2672 +2673 +2658 +2656 +2655 +2670 +2671 +2675 +2664 +2666 +2686 +2688 +2687 +2673 +2668 +2669 +2662 +2661 +2662 +2659 +2660 +2674 +2683 +2684 +2692 +2697 +2688 +2690 +2692 +2677 +2681 +2678 +2679 +2689 +2716 +2704 +2718 +2726 +2725 +2731 +2749 +2753 +2755 +2750 +2749 +2755 +2777 +2795 +2798 +2809 +2810 +2809 +2811 +2798 +2800 +2798 +2797 +2790 +2814 +2812 +2817 +2803 +2804 +2798 +2809 +2810 +2807 +2818 +2819 +2805 +2806 +2815 +2813 +2803 +2787 +2788 +2789 +2802 +2803 +2804 +2814 +2822 +2814 +2817 +2807 +2805 +2820 +2835 +2838 +2843 +2851 +2849 +2856 +2854 +2853 +2855 +2862 +2865 +2864 +2869 +2872 +2876 +2877 +2882 +2872 +2874 +2885 +2901 +2906 +2912 +2918 +2919 +2920 +2921 +2922 +2931 +2929 +2958 +2961 +2963 +2948 +2947 +2957 +2953 +2943 +2959 +2955 +2961 +2970 +2977 +2978 +2958 +2951 +2952 +2938 +2940 +2950 +2939 +2946 +2953 +2951 +2944 +2949 +2979 +2988 +2989 +2982 +2981 +2978 +2964 +2967 +2974 +2972 +2978 +2986 +2981 +2983 +2984 +2997 +3001 +3024 +3018 +3029 +3039 +3040 +3041 +3042 +3033 +3038 +3018 +3017 +3032 +3040 +3044 +3046 +3039 +3037 +3038 +3042 +3047 +3066 +3080 +3087 +3088 +3106 +3110 +3111 +3110 +3109 +3119 +3124 +3128 +3126 +3113 +3121 +3122 +3119 +3128 +3126 +3127 +3130 +3131 +3132 +3131 +3130 +3134 +3125 +3128 +3129 +3133 +3136 +3146 +3123 +3128 +3127 +3140 +3141 +3148 +3147 +3152 +3159 +3145 +3146 +3124 +3147 +3148 +3169 +3184 +3177 +3178 +3164 +3156 +3137 +3136 +3137 +3138 +3137 +3143 +3142 +3144 +3142 +3144 +3142 +3141 +3142 +3146 +3138 +3128 +3122 +3135 +3127 +3122 +3121 +3124 +3130 +3131 +3129 +3131 +3135 +3126 +3128 +3120 +3129 +3131 +3135 +3136 +3138 +3140 +3123 +3124 +3123 +3127 +3096 +3097 +3105 +3101 +3100 +3101 +3087 +3088 +3089 +3091 +3084 +3087 +3127 +3130 +3119 +3121 +3126 +3141 +3115 +3111 +3104 +3103 +3128 +3125 +3126 +3124 +3125 +3141 +3155 +3154 +3159 +3165 +3200 +3184 +3182 +3177 +3202 +3204 +3203 +3199 +3196 +3189 +3182 +3186 +3188 +3186 +3192 +3199 +3201 +3214 +3215 +3230 +3227 +3235 +3239 +3238 +3228 +3240 +3237 +3218 +3222 +3205 +3204 +3183 +3174 +3181 +3192 +3189 +3187 +3196 +3198 +3206 +3211 +3231 +3232 +3235 +3232 +3236 +3237 +3227 +3235 +3240 +3241 +3250 +3246 +3247 +3254 +3268 +3269 +3278 +3279 +3278 +3288 +3273 +3275 +3277 +3270 +3277 +3275 +3290 +3292 +3296 +3301 +3302 +3287 +3286 +3287 +3294 +3296 +3265 +3270 +3272 +3277 +3271 +3266 +3273 +3277 +3273 +3271 +3274 +3275 +3273 +3272 +3279 +3281 +3288 diff --git a/2021/day01/Makefile b/2021/day01/Makefile new file mode 100644 index 0000000..98ed3d9 --- /dev/null +++ b/2021/day01/Makefile @@ -0,0 +1,53 @@ +# AOC daily Makefile - GNU make only. +# +# Copyright (C) 2021 Bruno Raoult ("br") +# Licensed under the GNU General Public License v3.0 or later. +# Some rights reserved. See COPYING. +# * You should have received a copy of the GNU General Public License along with this +# program. If not, see . +# +# SPDX-License-Identifier: GPL-3.0-or-later +# + +INPUT := INPUT.txt +SHELL := /bin/bash + +CC := gcc + +LIB := aoc_$(shell uname -m) +INCDIR := ../include +LIBDIR := ../lib +LDFLAGS := -L$(LIBDIR) +LDLIB := -l$(LIB) + +export LD_LIBRARY_PATH = $(LIBDIR) + +CFLAGS += -std=gnu99 +CFLAGS += -O2 +CFLAGS += -g +CFLAGS += -Wall +CFLAGS += -Wextra +CFLAGS += -march=native + +CFLAGS += -DDEBUG_POOL # memory pools management + +TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n" +export PATH := .:$(PATH) + +.PHONY: clean all compile deploy ex1 ex2 + +all: ex1 ex2 + +compile: aoc-c + +ex1: aoc-c + @$(TIME) aoc-c -d 5 -p 1 < $(INPUT) + +ex2: aoc-c + @$(TIME) aoc-c -d 5 -p 2 < $(INPUT) + +clean: + @rm -f aoc-c core* + +.c: + $(CC) $(CFLAGS) $(LDFLAGS) -I $(INCDIR) $< $(LDLIB) -o $@ diff --git a/2021/day01/README.txt b/2021/day01/README.txt new file mode 100644 index 0000000..d34e8f2 --- /dev/null +++ b/2021/day01/README.txt @@ -0,0 +1,104 @@ + +Advent of Code + + [About][Events][Shop][Settings][Log Out] + +Bruno Raoult 2* + {:year 2021} + + [Calendar][AoC++][Sponsors][Leaderboard][Stats] + +Our sponsors help make Advent of Code possible: +Ximedes - From Kotlin in the cloud to NodeJS in the terminal, our software powers payments and public transport ticketing all over the world. +--- Day 1: Sonar Sweep --- + +You're minding your own business on a ship at sea when the overboard alarm goes off! You rush to see if you can help. Apparently, one of the Elves tripped and accidentally sent the sleigh keys flying into the ocean! + +Before you know it, you're inside a submarine the Elves keep ready for situations like this. It's covered in Christmas lights (because of course it is), and it even has an experimental antenna that should be able to track the keys if you can boost its signal strength high enough; there's a little meter that indicates the antenna's signal strength by displaying 0-50 stars. + +Your instincts tell you that in order to save Christmas, you'll need to get all fifty stars by December 25th. + +Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! + +As the submarine drops below the surface of the ocean, it automatically performs a sonar sweep of the nearby sea floor. On a small screen, the sonar sweep report (your puzzle input) appears: each line is a measurement of the sea floor depth as the sweep looks further and further away from the submarine. + +For example, suppose you had the following report: + +199 +200 +208 +210 +200 +207 +240 +269 +260 +263 + +This report indicates that, scanning outward from the submarine, the sonar sweep found depths of 199, 200, 208, 210, and so on. + +The first order of business is to figure out how quickly the depth increases, just so you know what you're dealing with - you never know if the keys will get carried into deeper water by an ocean current or a fish or something. + +To do this, count the number of times a depth measurement increases from the previous measurement. (There is no measurement before the first measurement.) In the example above, the changes are as follows: + +199 (N/A - no previous measurement) +200 (increased) +208 (increased) +210 (increased) +200 (decreased) +207 (increased) +240 (increased) +269 (increased) +260 (decreased) +263 (increased) + +In this example, there are 7 measurements that are larger than the previous measurement. + +How many measurements are larger than the previous measurement? + +Your puzzle answer was 1195. +--- Part Two --- + +Considering every single measurement isn't as useful as you expected: there's just too much noise in the data. + +Instead, consider sums of a three-measurement sliding window. Again considering the above example: + +199 A +200 A B +208 A B C +210 B C D +200 E C D +207 E F D +240 E F G +269 F G H +260 G H +263 H + +Start by comparing the first and second three-measurement windows. The measurements in the first window are marked A (199, 200, 208); their sum is 199 + 200 + 208 = 607. The second window is marked B (200, 208, 210); its sum is 618. The sum of measurements in the second window is larger than the sum of the first, so this first comparison increased. + +Your goal now is to count the number of times the sum of measurements in this sliding window increases from the previous sum. So, compare A with B, then compare B with C, then C with D, and so on. Stop when there aren't enough measurements left to create a new three-measurement sum. + +In the above example, the sum of each three-measurement window is as follows: + +A: 607 (N/A - no previous sum) +B: 618 (increased) +C: 618 (no change) +D: 617 (decreased) +E: 647 (increased) +F: 716 (increased) +G: 769 (increased) +H: 792 (increased) + +In this example, there are 5 sums that are larger than the previous sum. + +Consider sums of a three-measurement sliding window. How many sums are larger than the previous sum? + +Your puzzle answer was 1235. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Shareon Mastodon] this puzzle. diff --git a/2021/day01/aoc-c.c b/2021/day01/aoc-c.c new file mode 100644 index 0000000..192cf88 --- /dev/null +++ b/2021/day01/aoc-c.c @@ -0,0 +1,116 @@ +/* aoc-c: Advent2021 game, day 1 parts 1 & 2 + * + * Copyright (C) 2021 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include +#include + +#include "debug.h" +#include "bits.h" +#include "pool.h" + +struct ranges { + u32 val; + struct list_head list; +}; + +LIST_HEAD(list_head); + +int ex1() +{ + u32 count = 0, res = 0, prev, cur; + + while (scanf("%d", &cur) != EOF) { + if (count && cur > prev) + res++; + count++; + prev = cur; + } + return res; +} + +int ex2() +{ + u32 count = 0, res = 0; + u32 val; + pool_t *pool; + struct ranges *input; + struct ranges *list_cur; + + if (!(pool = pool_init("pool", 10, sizeof (struct ranges)))) + return -1; + + while (scanf("%d", &val) != EOF) { + if (!(input = pool_get(pool))) + return -1; + input->val = val; + list_add_tail(&input->list, &list_head); + + if (count > 2) { + u32 loop = 0, v1 = 0, v2 = 0; + struct ranges *first = list_entry(list_head.next, struct ranges, list); + + list_for_each_entry(list_cur, &list_head, list) { + if (loop < 3) + v1 += list_cur->val; + if (loop > 0) + v2 += list_cur->val; + ++loop; + } + list_del(&first->list); + pool_add(pool, first); + if (v2 > v1) + res++; + } + count++; + } + return res; +} + +static int usage(char *prg) +{ + fprintf(stderr, "Usage: %s [-d debug_level] [-p part]\n", prg); + return 1; +} + +int main(int ac, char **av) +{ + int opt; + u32 exercise = 2, res; + + while ((opt = getopt(ac, av, "d:p:")) != -1) { + switch (opt) { + case 'd': + debug_level_set(atoi(optarg)); + break; + case 'p': /* 1 or 2 */ + exercise = atoi(optarg); + break; + default: + return usage(*av); + } + } + + if (optind < ac) + return usage(*av); + + if (exercise == 1) { + res = ex1(); + printf ("%s : res=%d\n", *av, res); + } + if (exercise == 2) { + res = ex2(); + printf ("%s : res=%d\n", *av, res); + } + + exit (0); +} diff --git a/2021/templates/bits.h b/2021/include/bits.h similarity index 100% rename from 2021/templates/bits.h rename to 2021/include/bits.h diff --git a/2021/templates/debug.h b/2021/include/debug.h similarity index 100% rename from 2021/templates/debug.h rename to 2021/include/debug.h diff --git a/2021/templates/list.h b/2021/include/list.h similarity index 100% rename from 2021/templates/list.h rename to 2021/include/list.h diff --git a/2021/include/pool.h b/2021/include/pool.h new file mode 100644 index 0000000..37fd667 --- /dev/null +++ b/2021/include/pool.h @@ -0,0 +1,36 @@ +/* pool.h - A simple memory pool manager. + * + * Copyright (C) 2021 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#ifndef POOL_H +#define POOL_H + +#include +#include +#include "list.h" +#include "bits.h" + +typedef struct { + char *name; + u32 available; + u32 allocated; + u32 growsize; + size_t eltsize; + struct list_head head; +} pool_t; + +void pool_stats(pool_t *pool); +pool_t *pool_init(const char *name, u32 grow, size_t size); +void *pool_get(pool_t *pool); +u32 pool_add(pool_t *pool, void *elt); + +#endif diff --git a/2021/templates/bits.c b/2021/libsrc/bits.c similarity index 100% rename from 2021/templates/bits.c rename to 2021/libsrc/bits.c diff --git a/2021/libsrc/bits.o b/2021/libsrc/bits.o new file mode 100644 index 0000000000000000000000000000000000000000..0375e26371b49b310a4e7dafe2c01bbd1c053677 GIT binary patch literal 3736 zcmbVOOOG5i5O&Y(LSRYALp(%~c0dGzpnG;E!;&Cvw9*1WB0>l$NN^zPhkK^A-R_R< zo|%LLKY%L-PKX0Y`~$8WxN+f_AHWX?XAV$hmuGv-1c_K0SJhWf+hzBB_1?|*dqKe5 z2-tOYA~}??`%j$khlV|57ub3BIq=&oIE7FTr>AgoBe05~_c$WKmL;bw2_7?GZyQN} zYr7B{0vRc=Iu^Xbc6P4s^v^)>bHSxE7k2u)7kAG-2lk}F#4p0nO>>$bJT#iAiswK=#-K0VTqZGF@>Oi<+h@*TG zjVwNi&>)_pv#3L0$YBx#l2`Er$g*y%urhjUG|oXts=`9Rm^fU4+@xjImR;NVnv^WM zy6jF@O0%xSWmjr2r7aTj&sWKzXpjpiwyU4ag(%iCO~9hT0#9Pt{aP;p1LI=AYMKjL z;M^|ij%Nxfy77`i$K`Yl$w&KLdESiSM7dNv#bbgTS29t%GBZ4eIH{~AEi+!#8V?fe z(lRew=$?lB(sGeF%{?w_;M1i`#>Jhq!mWh#skO@0R3))#i+rhQV`rsllXym3TQ2aV z%X_woAZu066Dk1MtdRQ?%4$Vgezk}TO+D14wa5$Fh32@m=^M#&uG85$mWaX9 z=CSEk;IR1Sm?~Z*qAtW7vn^FTr|5>Io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Y(6xF6dGzGAt>svFj= z&ET?QVOefxn6q+W&i2Aw1w)D?A%3k99Fhj!fn>w-bgfi!W<*MZMgRCluV8z`;siBElh;ZEOX+r{w~8Hk2cww1e)FF(+qB8um8s<3uu9F+H<5 z;tnP)(<<0WAI3g{%dHArr$o|q>1pXu*lcJZjd=ytlHDjEFads$8-W@KH71!i{aq!i z;FU||s-LWu#-NTR12gO5tmUD!T04>z5B8=Nlszy6MQ}g@%QgLi6g%Y-2&{aFXQP!& zIiJTej0MljfIlx_Gs>f9k7jTIGNxynj2BcK7%M}$%e&<?8MP8JTD{oGT^V*Q13(eBTB0Mz#zD=KhXW7meoIg$-2zCbP^`4I-Z0T?H?TMcS@F9&Dlbcm`^G~0rO|5 z%wT5M?+?odn_*n<~F8E>7c7L8}?7M7@~@Gcztl+kXeJ^ekWlIHEZ4-|J*&W+jfywBPl%%8 zonr=$FNazPm&a24zJj>q`8g4=KKMmIG6g+0`A&m$l+P|wGh0-=M$ZI+i#EK^6h#@F z%x7s));bG{e)y+=8M1!hIzsax--87t#vw zv+0-5fs3Oi6o_zv44q+>vK5l>)2-mA3ss{$LP5gXov{i#GI`T+l_PBc`=zj{6gZB4x=&zxIhk3$laARE8hVbxw%KLvF=<*Lq&|{N#BI0->={`;vZ7*?-Bp7f?rMf zJfh%zg#S{(pC$f;flOV|#9yi4uM)pi!DSt} zPQgDU{(1$UgA)Qxjsw>P^Jybo-cJ%=M0ggBphpChPmlKuqwN0zo8M-qf17REOzxZJllE4aKbZc%Vq2UyDARfO*V&Etd3z97Ti;3qxw3YWd{M~^!ynhj= zG9f^?-lYO0QR80>IQmr8?. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +/* +#include +#include +*/ + +#include +#include +#include +#include +#include "list.h" +#include "pool.h" +#include "debug.h" +#include "bits.h" + +void pool_stats(pool_t *pool) +{ + if (pool) { +# ifdef DEBUG_POOL + log_f(1, "[%s] pool [%p]: avail:%u alloc:%u grow:%u eltsize:%lu\n", + pool->name, (void *)pool, pool->available, pool->allocated, + pool->growsize, pool->eltsize); +# endif + } +} + +pool_t *pool_init(const char *name, u32 growsize, size_t eltsize) +{ + pool_t *pool; + +# ifdef DEBUG_POOL + log_f(1, "name=[%s] growsize=%u eltsize=%lu\n", + name, growsize, eltsize); +# endif + /* we need at least this space in struct */ + if (eltsize < sizeof (struct list_head)) + return NULL; + if ((pool = malloc(sizeof (*pool)))) { + pool->name = strdup(name); + pool->growsize = growsize; + pool->eltsize = eltsize; + pool->available = 0; + pool->allocated = 0; + INIT_LIST_HEAD(&pool->head); + } + return pool; +} + +static u32 _pool_add(pool_t *pool, struct list_head *elt) +{ +# ifdef DEBUG_POOL + log_f(10, "pool=%p &head=%p elt=%p off1=%lu off2=%lu\n", + (void *)pool, + (void *)&pool->head, + (void *)elt, + (void *)&pool->head-(void *)pool, + offsetof(pool_t, head)); +# endif + + list_add(elt, &pool->head); + return ++pool->available; +} + +u32 pool_add(pool_t *pool, void *elt) +{ + return _pool_add(pool, elt); +} + +static struct list_head *_pool_get(pool_t *pool) +{ + struct list_head *res = pool->head.next; + pool->available--; + list_del(res); + return res; +} + +void *pool_get(pool_t *pool) +{ + if (!pool) + return NULL; + if (!pool->available) { + void *alloc = malloc(pool->eltsize * pool->growsize); + void *cur; + u32 i; +# ifdef DEBUG_POOL + log_f(1, "[%s]: growing pool from %u to %u elements.\n", + pool->name, + pool->allocated, + pool->allocated + pool->growsize); +# endif + if (!alloc) + return NULL; +# ifdef DEBUG_POOL + log_f(5, " (old=%u)\n", pool->allocated); +# endif + pool->allocated += pool->growsize; +# ifdef DEBUG_POOL + log_f(5, " (new=%u)\n", pool->allocated); +# endif + for (i = 0; i < pool->growsize; ++i) { + cur = alloc + i * pool->eltsize; +# ifdef DEBUG_POOL + log_f(5, "alloc=%p cur=%p\n", alloc, cur); +# endif + _pool_add(pool, (struct list_head *)cur); + } + pool_stats(pool); + } + /* this is the effective address if the object (and also the + * pool list_head address) + */ + return _pool_get(pool); +} + +#ifdef BIN_pool +struct d { + u16 data1; + char c; + struct list_head list; +}; + +static LIST_HEAD (head); + +int main(int ac, char**av) +{ + pool_t *pool; + int total; + int action=0; + u16 icur=0; + char ccur='z'; + struct d *elt; + + debug_init(3); + log_f(1, "%s: sizeof(d)=%lu sizeof(*d)=%lu off=%lu\n", *av, sizeof(elt), + sizeof(*elt), offsetof(struct d, list)); + + if ((pool = pool_init("dummy", 3, sizeof(*elt)))) { + pool_stats(pool); + for (int cur=1; curdata1 = icur++; + elt->c = ccur--; + list_add(&elt->list, &head); + } + pool_stats(pool); + action = 1; + } else { /* remove one elt from list */ + log_f(2, "deleting %d elements\n", total); + for (int i = 0; i < total; ++i) { + if (!list_empty(&head)) { + elt = list_last_entry(&head, struct d, list); + printf("elt=[%d, %c]\n", elt->data1, elt->c); + list_del(&elt->list); + pool_add(pool, elt); + } + } + pool_stats(pool); + action = 0; + } + } + } + pool_stats(pool); +} +#endif diff --git a/2021/libsrc/pool.o b/2021/libsrc/pool.o new file mode 100644 index 0000000000000000000000000000000000000000..3baa4a443f2edecc38df75eea5d2a503f93d73ea GIT binary patch literal 10192 zcmbta4Qw38b)LP`l1K4K9z~fyl5Cw++lmC=ok-EL<%l+AnGR_wHtmRo9seBn_V##J z-R+%s_ee^y5Fw16va%7$aL^P`;UGv+yMd6x2$CQ&V8w1A2ToGkKn`5 z#txP)rDPa%a$*905a#npf}dYbE}y^njceDgo&4F#Xi{-SBygWMbWDZBBMfknQsJe@p0aS7}9(asYymk@%4 z2f@+XKwq9T-d4H8xn# zv!nk3fXt^6SK?lz&1Vr%ame>NKt#&uPW&8tnT^pifHX5VAd?X0cc7EGnH$yzK=(Cw zjfjq$*S!Pu`kih49sA%Okq+7hI*$WL%wWf_fKDjMkYtjI*&vyeVm2mk1+_uNZ0g5o zX%(;S`uK;yT?aqW{@w25(Af}b?I{HM@sijW>;6ONk0Ok2ge=S>?Fe%FJ0gtJAAlPC zgGxLyh;;1KrWM5VuNGuAwI>S1EHt0EjC z_6{sP6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mh+)%p@wt4p@xL@6)x~8hERNrVpFaM(0n>e=>dQ7B=Y%DoxjY8FX-|;Jz4b39?T|L7=&Q4F3cO^r_o_hU~fitgqYS`@(ATH=9-# zX-XK~VM*DZ@Bh_eqvbjJ0GOi0kWH;(kIPKI_W=@b_v1D4Ukw!diCSY$tJxplq72?5 LJML&W%!>a3D>*Mp literal 0 HcmV?d00001 diff --git a/2021/templates/Makefile b/2021/templates/Makefile index 855b615..98ed3d9 100644 --- a/2021/templates/Makefile +++ b/2021/templates/Makefile @@ -1,26 +1,53 @@ -INPUT := INPUT.txt -SHELL := /bin/bash -CFLAGS := -w -TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n" +# AOC daily Makefile - GNU make only. +# +# Copyright (C) 2021 Bruno Raoult ("br") +# Licensed under the GNU General Public License v3.0 or later. +# Some rights reserved. See COPYING. +# * You should have received a copy of the GNU General Public License along with this +# program. If not, see . +# +# SPDX-License-Identifier: GPL-3.0-or-later +# + +INPUT := INPUT.txt +SHELL := /bin/bash + +CC := gcc + +LIB := aoc_$(shell uname -m) +INCDIR := ../include +LIBDIR := ../lib +LDFLAGS := -L$(LIBDIR) +LDLIB := -l$(LIB) + +export LD_LIBRARY_PATH = $(LIBDIR) + +CFLAGS += -std=gnu99 +CFLAGS += -O2 +CFLAGS += -g +CFLAGS += -Wall +CFLAGS += -Wextra +CFLAGS += -march=native + +CFLAGS += -DDEBUG_POOL # memory pools management + +TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n" export PATH := .:$(PATH) .PHONY: clean all compile deploy ex1 ex2 all: ex1 ex2 -compile: ex1-c ex2-c +compile: aoc-c -ex1: ex1-c - @$(TIME) ex1.bash < $(INPUT) - @$(TIME) ex1-c < $(INPUT) +ex1: aoc-c + @$(TIME) aoc-c -d 5 -p 1 < $(INPUT) -ex2: ex2-c - @$(TIME) ex2.bash < $(INPUT) - @#$(TIME) ex2-sort.bash < $(INPUT) - @$(TIME) ex2-c < $(INPUT) +ex2: aoc-c + @$(TIME) aoc-c -d 5 -p 2 < $(INPUT) clean: - @rm -f ex1-c ex2-c core + @rm -f aoc-c core* -deploy: - @$(MAKE) -C .. deploy +.c: + $(CC) $(CFLAGS) $(LDFLAGS) -I $(INCDIR) $< $(LDLIB) -o $@ diff --git a/2021/templates/aoc-c.c b/2021/templates/aoc-c.c new file mode 100644 index 0000000..192cf88 --- /dev/null +++ b/2021/templates/aoc-c.c @@ -0,0 +1,116 @@ +/* aoc-c: Advent2021 game, day 1 parts 1 & 2 + * + * Copyright (C) 2021 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include +#include + +#include "debug.h" +#include "bits.h" +#include "pool.h" + +struct ranges { + u32 val; + struct list_head list; +}; + +LIST_HEAD(list_head); + +int ex1() +{ + u32 count = 0, res = 0, prev, cur; + + while (scanf("%d", &cur) != EOF) { + if (count && cur > prev) + res++; + count++; + prev = cur; + } + return res; +} + +int ex2() +{ + u32 count = 0, res = 0; + u32 val; + pool_t *pool; + struct ranges *input; + struct ranges *list_cur; + + if (!(pool = pool_init("pool", 10, sizeof (struct ranges)))) + return -1; + + while (scanf("%d", &val) != EOF) { + if (!(input = pool_get(pool))) + return -1; + input->val = val; + list_add_tail(&input->list, &list_head); + + if (count > 2) { + u32 loop = 0, v1 = 0, v2 = 0; + struct ranges *first = list_entry(list_head.next, struct ranges, list); + + list_for_each_entry(list_cur, &list_head, list) { + if (loop < 3) + v1 += list_cur->val; + if (loop > 0) + v2 += list_cur->val; + ++loop; + } + list_del(&first->list); + pool_add(pool, first); + if (v2 > v1) + res++; + } + count++; + } + return res; +} + +static int usage(char *prg) +{ + fprintf(stderr, "Usage: %s [-d debug_level] [-p part]\n", prg); + return 1; +} + +int main(int ac, char **av) +{ + int opt; + u32 exercise = 2, res; + + while ((opt = getopt(ac, av, "d:p:")) != -1) { + switch (opt) { + case 'd': + debug_level_set(atoi(optarg)); + break; + case 'p': /* 1 or 2 */ + exercise = atoi(optarg); + break; + default: + return usage(*av); + } + } + + if (optind < ac) + return usage(*av); + + if (exercise == 1) { + res = ex1(); + printf ("%s : res=%d\n", *av, res); + } + if (exercise == 2) { + res = ex2(); + printf ("%s : res=%d\n", *av, res); + } + + exit (0); +} diff --git a/2021/templates/ex1-c.c b/2021/templates/ex1-c.c deleted file mode 100644 index 7012fef..0000000 --- a/2021/templates/ex1-c.c +++ /dev/null @@ -1,58 +0,0 @@ -/* ex1-c: Advent2021 game, day 1/game 1 - */ - -#include -#include -#include - -#include "debug.h" - -#define XMOVE 3 - -int my_strlen(str) - char *str; -{ - int i; - for (i=0; *str; ++i, ++str) - ; - return i; -} - -static int usage(char *prg) -{ - fprintf(stderr, "Usage: %s [-ilw] [file...]\n", prg); - return 1; -} - -int main(int ac, char **av) -{ - int opt; - char str[80]; - - while ((opt = getopt(ac, av, "d:f:")) != -1) { - switch (opt) { - case 'd': - debug_level_set(atoi(optarg)); - break; - default: - return usage(*av); - } - } - printf("optind = %d ac = %d\n", optind, ac); - - if (optind < ac) - return usage(*av); - - scanf("%s", str); /* ignore 1st line */ - - while (scanf("%s", str) != EOF) { - line++; - col+=XMOVE; - linelen=my_strlen(str); - mod=col%linelen; - if (*(str+mod) == '#') - count++; - } - printf ("%s : lines:%d pos:%d found:%d\n", *av, line, col, count); - exit (0); -} diff --git a/2021/templates/ex1.bash b/2021/templates/ex1.bash deleted file mode 100755 index afa4f5a..0000000 --- a/2021/templates/ex1.bash +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash -# -# ex1.bash: Advent2020 game, day 3/game 1. - -CMD=${0##*/} - -XMOVE=3 # 3 right - -declare -i xpos=0 ypos=0 count=0 linelen=0 modpos=0 - -read -r line # ignore 1st line - -while read -r line; do - (( ypos++ )) - (( xpos += XMOVE )) # move right - (( linelen = ${#line} )) - (( modpos = (xpos % linelen) )) - [[ ${line:modpos:1} = \# ]] && ((count++)) -done -printf "%s : lines:%d pos=%d found:%d\n" "$CMD" $ypos $xpos $count -exit 0 diff --git a/2021/templates/ex2-c.c b/2021/templates/ex2-c.c deleted file mode 100644 index 590c40c..0000000 --- a/2021/templates/ex2-c.c +++ /dev/null @@ -1,60 +0,0 @@ -/* ex1-c: Advent2020 game, day 3/game 2 - */ - -#include -#include - -struct { - int dx; - int dy; - int pos; - int count; -} set[] = { - { 1, 1, 0, 0}, - { 3, 1, 0, 0}, - { 5, 1, 0, 0}, - { 7, 1, 0, 0}, - { 1, 2, 0, 0}, - {-1, -1, 0, 0} /* end marker - not necessary */ -}; - -int my_strlen(str) - char *str; -{ - int i; - for (i=0; *str; ++i, ++str) - ; - return i; -} - -int main(ac, av) - int ac; - char **av; -{ - int line=0, linelen=0, mod=0, i; - unsigned long res=1; - char str[80]; - - scanf("%s", str); /* ignore 1st line */ - - while (scanf("%s", str) != EOF) { - line++; - linelen=my_strlen(str); - for (i=0; set[i].dx >= 0; ++i) { - if (! (line % set[i].dy )) { /* line ok for this set */ - set[i].pos += set[i].dx; /* increment set column */ - mod = set[i].pos % linelen; - if ( str[mod] == '#') - set[i].count++; - } - } - } - printf("%s : ", *av); - for (i=0; set[i].dx != -1; ++i) { - printf("[%d]=[%d, %d, %d, %d] ", i, - set[i].dx, set[i].dy, set[i].pos, set[i].count); - res*=set[i].count; - } - printf ("res:%lu\n", res); - exit (0); -} diff --git a/2021/templates/ex2.bash b/2021/templates/ex2.bash deleted file mode 100755 index 51fc9cb..0000000 --- a/2021/templates/ex2.bash +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/bash -# -# ex1.bash: Advent2020 game, day 3/game 2. - -CMD=${0##*/} - -XMOVES=(1 3 5 7 1) # moves right -YMOVES=(1 1 1 1 2) # moves down -declare -i linelen=0 modpos=0 res=1 -declare -a xpos xcount - -read -r line # ignore 1st line - -while read -r line; do - (( ypos++ )) - (( linelen = ${#line} )) - - for ((i=0; i<${#XMOVES[@]}; ++i)); do - if ((ypos % YMOVES[i] == 0)); then - (( xpos[i] += XMOVES[i] )) # move right - (( modpos = (xpos[i] % linelen) )) - [[ ${line:modpos:1} = \# ]] && ((xcount[i]++)) - fi - done -done -echo "xpos=${xpos[*]} xcount=${xcount[*]}" -printf "%s : " "$CMD" -for ((i=0; i<${#XMOVES[@]}; ++i)); do - printf "[%d]=[%d, %d, %d, %d] " $i "${XMOVES[$i]}" "${YMOVES[$i]}" \ - "${xpos[$i]}" "${xcount[$i]}" - (( res *= xcount[i] )) -done -printf "res:%d\n " $res -exit 0