advent-of-code

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit f57316c89ce6c31a7e9571492690c071c820c001
Author: aabacchus <bvnfuller@gmail.com>
Date:   Mon, 22 Nov 2021 16:38:28 +0000

2020 day 1 part 1

Diffstat:
A2020/01/01.c | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2020/01/input | 200+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 263 insertions(+), 0 deletions(-)

diff --git a/2020/01/01.c b/2020/01/01.c @@ -0,0 +1,63 @@ +#include <stdio.h> +#include <stdlib.h> + +void +usage(char *argv0) { + printf("usage: %s TARGET < input.file\n", argv0); +} + +int +main(int argc, char **argv) { + if (argc < 2) { + usage(*argv); + return 1; + } + + int target = atoi(argv[1]); + if (target == 0) { + fprintf(stderr, "error parsing target '%s'\n", argv[1]); + return 1; + } + + /* buffer for each line */ + char buf[100] = {'\0'}; + + size_t nums_len = 200; + int *nums = malloc(sizeof(int) * nums_len); + if (nums == NULL) { + perror("malloc"); + return 1; + } + int *n = nums; + + while (fgets(buf, 100, stdin) != NULL) { + *n++ = atoi(buf); + size_t offset = n - nums; + if (offset > nums_len) { + nums_len += 10; + int *tmp = realloc(nums, sizeof(int) * nums_len); + if (tmp == NULL) { + perror("realloc"); + goto error_free_nums; + } + + nums = tmp; + n = nums + offset; + } + } + + for (size_t i = 0; i < nums_len; i++) { + for (size_t j = i + 1; j < nums_len; j++) { + if (nums[i] + nums[j] == target) { + printf("%d * %d = %d\n", nums[i], nums[j], nums[i] * nums[j]); + goto end; + } + } + } +end: + free(nums); + return 0; +error_free_nums: + free(nums); + return 1; +} diff --git a/2020/01/input b/2020/01/input @@ -0,0 +1,200 @@ +1630 +1801 +1917 +1958 +1953 +1521 +1990 +1959 +1543 +1798 +638 +1499 +1977 +1433 +1532 +1780 +1559 +1866 +1962 +1999 +1623 +1772 +1730 +1670 +1791 +1947 +1961 +1523 +959 +1998 +1693 +1490 +1712 +910 +1635 +1837 +586 +1590 +1741 +1739 +1660 +1883 +1777 +1734 +1413 +1456 +1511 +1957 +1738 +1685 +1677 +1419 +1566 +1639 +1578 +1922 +1856 +1946 +1965 +1649 +1854 +1610 +1806 +1424 +1616 +218 +1678 +1992 +1985 +903 +1626 +1412 +1964 +671 +1692 +1571 +1690 +1587 +1933 +1367 +1585 +1575 +498 +1601 +2005 +1711 +1948 +1991 +1580 +1704 +207 +1560 +1867 +1600 +1594 +1930 +1541 +1832 +1613 +1599 +1757 +71 +1534 +1940 +1982 +1960 +1530 +1908 +1857 +1410 +1987 +1526 +1546 +2002 +1923 +1972 +1752 +1984 +1754 +1916 +1942 +1980 +1608 +1398 +1438 +1955 +1968 +1799 +1976 +1847 +1775 +1904 +1983 +1945 +1554 +1486 +1527 +1884 +1553 +1736 +1561 +1513 +1695 +1431 +1997 +1405 +1872 +1434 +1679 +1609 +105 +1582 +1795 +1826 +1886 +1472 +2007 +1617 +1978 +1669 +1764 +1865 +1773 +1993 +1666 +1583 +2009 +1969 +2001 +1659 +1833 +1713 +1893 +2000 +1520 +1652 +1437 +1556 +1633 +1386 +1819 +1973 +1426 +1975 +2010 +1863 +1593 +1996 +1796 +1986 +1995 +657 +1784 +1644 +1941 +1596 +1849 +1065 +1927 +1525