diff --git a/2020/01/Cargo.toml b/2020/01/Cargo.toml new file mode 100644 index 0000000..1943cd3 --- /dev/null +++ b/2020/01/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "day01" +version = "0.1.0" +authors = ["kageru "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "0.9.0" diff --git a/2020/01/d1.py b/2020/01/d1.py new file mode 100644 index 0000000..5c533fb --- /dev/null +++ b/2020/01/d1.py @@ -0,0 +1,218 @@ +i = '''1914 +1931 +1892 +1584 +1546 +1988 +1494 +1709 +1624 +1755 +1849 +1430 +1890 +1675 +1604 +1580 +1500 +1277 +1729 +1456 +2002 +1075 +1512 +895 +1843 +1921 +1904 +1989 +1407 +1552 +1714 +757 +1733 +1459 +1777 +1440 +1649 +1409 +1662 +1968 +1775 +1998 +1754 +1938 +1964 +1415 +1990 +1997 +1870 +1664 +1145 +1782 +1820 +1625 +1599 +1530 +1759 +1575 +1614 +1869 +1620 +1818 +1295 +1667 +1361 +1520 +1555 +1485 +1502 +1983 +1104 +1973 +1433 +1906 +1583 +1562 +1493 +1945 +1528 +1600 +1814 +1712 +1848 +1454 +1801 +1710 +1824 +1426 +1977 +1511 +1644 +1302 +1428 +1513 +1261 +1761 +1680 +1731 +1724 +1970 +907 +600 +1613 +1091 +1571 +1418 +1806 +1542 +1909 +1445 +1344 +1937 +1450 +1865 +1561 +1962 +1637 +1803 +1889 +365 +1810 +1791 +1591 +1532 +1863 +1658 +1808 +1816 +1837 +1764 +1443 +1805 +1616 +1403 +1656 +1661 +1734 +1930 +1120 +1920 +1227 +1618 +1640 +1586 +1982 +1534 +1278 +1269 +1572 +1654 +1472 +1974 +1748 +1425 +1553 +1708 +1394 +1417 +1746 +1745 +1834 +1787 +1298 +1786 +1966 +1768 +1932 +1523 +1356 +1547 +1634 +1951 +1922 +222 +1461 +1628 +1888 +1639 +473 +1568 +1783 +572 +1522 +1934 +1629 +1283 +1550 +1859 +2007 +1996 +1822 +996 +1911 +1689 +1537 +1793 +1762 +1677 +1266 +1715''' + +lines = list(map(int, i.split('\n'))) + +def p1(): + for l1 in lines: + for l2 in lines: + if l1 + l2 == 2020: + return l1 * l2 + +def p2(): + for l1 in lines: + for l2 in lines: + for l3 in lines: + if l1 + l2 + l3 == 2020: + return l1 * l2 * l3 + +print(p1()) +print(p2()) diff --git a/2020/01/input b/2020/01/input new file mode 100644 index 0000000..2643368 --- /dev/null +++ b/2020/01/input @@ -0,0 +1,200 @@ +1914 +1931 +1892 +1584 +1546 +1988 +1494 +1709 +1624 +1755 +1849 +1430 +1890 +1675 +1604 +1580 +1500 +1277 +1729 +1456 +2002 +1075 +1512 +895 +1843 +1921 +1904 +1989 +1407 +1552 +1714 +757 +1733 +1459 +1777 +1440 +1649 +1409 +1662 +1968 +1775 +1998 +1754 +1938 +1964 +1415 +1990 +1997 +1870 +1664 +1145 +1782 +1820 +1625 +1599 +1530 +1759 +1575 +1614 +1869 +1620 +1818 +1295 +1667 +1361 +1520 +1555 +1485 +1502 +1983 +1104 +1973 +1433 +1906 +1583 +1562 +1493 +1945 +1528 +1600 +1814 +1712 +1848 +1454 +1801 +1710 +1824 +1426 +1977 +1511 +1644 +1302 +1428 +1513 +1261 +1761 +1680 +1731 +1724 +1970 +907 +600 +1613 +1091 +1571 +1418 +1806 +1542 +1909 +1445 +1344 +1937 +1450 +1865 +1561 +1962 +1637 +1803 +1889 +365 +1810 +1791 +1591 +1532 +1863 +1658 +1808 +1816 +1837 +1764 +1443 +1805 +1616 +1403 +1656 +1661 +1734 +1930 +1120 +1920 +1227 +1618 +1640 +1586 +1982 +1534 +1278 +1269 +1572 +1654 +1472 +1974 +1748 +1425 +1553 +1708 +1394 +1417 +1746 +1745 +1834 +1787 +1298 +1786 +1966 +1768 +1932 +1523 +1356 +1547 +1634 +1951 +1922 +222 +1461 +1628 +1888 +1639 +473 +1568 +1783 +572 +1522 +1934 +1629 +1283 +1550 +1859 +2007 +1996 +1822 +996 +1911 +1689 +1537 +1793 +1762 +1677 +1266 +1715 diff --git a/2020/01/src/main.rs b/2020/01/src/main.rs new file mode 100644 index 0000000..bdc3b7f --- /dev/null +++ b/2020/01/src/main.rs @@ -0,0 +1,38 @@ +use itertools::iproduct; +use std::io::BufRead; + +fn main() { + let input: Vec = std::io::stdin() + .lock() + .lines() + .filter_map(|l| l.unwrap().parse().ok()) + .collect(); + let p1 = iproduct!(input.iter(), input.iter()) + .filter(|(&a, &b)| a + b == 2020) + .map(|(a, b)| a * b) + .next() + .unwrap(); + println!("Part 1: {}", p1); + /* + smol-brain, n³ solution: + let p2 = iproduct!(input.iter(), input.iter(), input.iter()) + .filter(|(&a, &b, &c)| a + b + c == 2020) + .map(|(a, b, c)| a * b * c) + .next() + .unwrap(); + println!("Part 2: {}", p2); + */ + // Ascended n² solution (thanks to Lypheo) + let mut p2_table = vec![None; 2020]; + for (&a, &b) in iproduct!(input.iter(), input.iter()) { + if a + b < 2020 { + p2_table[a + b] = Some((a, b)) + } + } + let (a, b) = input + .iter() + .filter_map(|x| p2_table[2020 - x]) + .next() + .unwrap(); + println!("Part 2: {}", a * b * (2020 - a - b)); +} diff --git a/2020/setup_day.sh b/2020/setup_day.sh new file mode 100755 index 0000000..05aa3fa --- /dev/null +++ b/2020/setup_day.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +today=$(date +%d) +mkdir "$today" +cd "$today" +cargo init --name "day$today" +echo 'Initialized cargo project' + +# this assumes that your puzzle input is already in your clipboard +xsel -b > input +# add trailing newline if necessary +sed -i -e '$a\' input