From decaf69ba5936e90e6bebc618991f598255ae061 Mon Sep 17 00:00:00 2001 From: trwnh Date: Wed, 2 Dec 2020 07:11:58 -0600 Subject: [PATCH] day 1 --- 01/01.exs | 9 +++ 01/01.py | 23 ++++++ 01/01.rb | 13 ++++ 01/input.txt | 200 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 245 insertions(+) create mode 100644 01/01.exs create mode 100644 01/01.py create mode 100644 01/01.rb create mode 100644 01/input.txt diff --git a/01/01.exs b/01/01.exs new file mode 100644 index 0000000..0dfbc7d --- /dev/null +++ b/01/01.exs @@ -0,0 +1,9 @@ +list = File.read!("input.txt") +|> String.split("\n") +|> Enum.map(&String.to_integer/1) +# pattern match binary tuple from list values +[{a, b} | _] = for x <- list, y <- list, x + y == 2020, do: {x, y} +IO.puts("Part 1: #{a * b}") +# same thing but with 3-ary tuple +[{a, b, c} | _] = for x <- list, y <- list, z <- list, x + y + z == 2020, do: {x, y, z} +IO.puts("Part 2: #{a * b * c}") diff --git a/01/01.py b/01/01.py new file mode 100644 index 0000000..504c075 --- /dev/null +++ b/01/01.py @@ -0,0 +1,23 @@ +def part1(): + with open('input.txt','r') as f: + expenses = [int(x) for x in f] + for a in expenses: + for b in expenses: + if a + b == 2020: + return a*b + +def part2(): + with open('input.txt','r') as f: + expenses = [int(x) for x in f] + for a in expenses: + for b in expenses: + for c in expenses: + if a + b + c == 2020: + return a*b*c + +def main(): + print(f'Part 1: {part1()}') + print(f'Part 2: {part2()}') + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/01/01.rb b/01/01.rb new file mode 100644 index 0000000..4347947 --- /dev/null +++ b/01/01.rb @@ -0,0 +1,13 @@ +def find_product_of(n) + puts File + .readlines("input.txt") # read input lines as array entries + .map(&:to_i) # convert each array entry to integer + .permutation(n) # create Enumerable with all permutations of n entries + .find { |array| array.sum == 2020} # find a permutation that sums to 2020 + .inject(:*) # get the product of that array +end + +puts "Part 1: " +find_product_of(2) +puts "Part 2: " +find_product_of(3) \ No newline at end of file diff --git a/01/input.txt b/01/input.txt new file mode 100644 index 0000000..2caf9ff --- /dev/null +++ b/01/input.txt @@ -0,0 +1,200 @@ +2004 +1823 +1628 +1867 +1073 +1951 +1909 +1761 +1093 +1992 +1986 +1106 +1537 +1905 +1233 +1961 +1760 +1562 +1781 +1329 +1272 +1660 +1367 +1248 +1697 +1515 +1470 +1980 +1884 +1784 +1966 +1778 +1426 +1255 +1089 +1748 +1253 +1870 +1651 +1131 +1623 +1595 +1128 +1014 +1863 +1855 +1203 +1395 +1521 +1365 +1202 +780 +1560 +1834 +1494 +1551 +1398 +1190 +1975 +1940 +1217 +1793 +1310 +1070 +1865 +1307 +1735 +1897 +1410 +1994 +1541 +1569 +1731 +1238 +1193 +1226 +1435 +1159 +1642 +1652 +1908 +1920 +1930 +1068 +1914 +1186 +1795 +1888 +1634 +1750 +1950 +1493 +1353 +1461 +1658 +1856 +1301 +1538 +1948 +1998 +1847 +1880 +1657 +1536 +1457 +1762 +1706 +1894 +542 +1991 +1108 +1072 +1064 +1511 +1496 +1480 +1955 +1604 +1766 +1983 +1713 +1234 +1503 +1583 +1729 +1140 +1006 +1600 +1699 +1280 +1891 +1996 +1375 +1167 +1625 +1129 +1770 +1497 +1620 +1267 +1421 +1399 +1563 +1636 +1293 +1506 +1613 +1958 +1967 +1182 +1050 +1947 +1787 +1774 +1928 +1896 +1303 +1826 +1132 +1254 +1752 +1510 +1705 +1229 +1558 +1989 +1567 +698 +1738 +1357 +1587 +1316 +1838 +1311 +1057 +1644 +1135 +1300 +1134 +1577 +1381 +1806 +1176 +1993 +1769 +1633 +1450 +1819 +1973 +1694 +969 +1987 +1095 +1717 +1933 +1593 +1045 +1355 +1459 +1619 \ No newline at end of file