diff --git a/Cargo.lock b/Cargo.lock index ff825e3..7f712f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,6 +29,14 @@ dependencies = [ "criterion", ] +[[package]] +name = "aoc2020" +version = "0.1.0" +dependencies = [ + "aoc", + "criterion", +] + [[package]] name = "atty" version = "0.2.14" diff --git a/Cargo.toml b/Cargo.toml index 1cf828b..9501de0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [workspace] members = ["aoc*"] -default-members = ["aoc2019"] +default-members = ["aoc2020"] [profile.release] debug = true diff --git a/aoc2020/Cargo.toml b/aoc2020/Cargo.toml new file mode 100644 index 0000000..fe4fe68 --- /dev/null +++ b/aoc2020/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "aoc2020" +version = "0.1.0" +authors = ["Antoine Martin "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dev-dependencies] + +criterion = "0.3" + +[dependencies] + +aoc = { path = "../aoc" } + +[lib] +path = "src/lib.rs" +bench = false + +[[bin]] +name = "aoc2020" +path = "src/main.rs" +bench = false + +[[bench]] +name = "bench" +harness = false diff --git a/aoc2020/benches/bench.rs b/aoc2020/benches/bench.rs new file mode 100644 index 0000000..47801c1 --- /dev/null +++ b/aoc2020/benches/bench.rs @@ -0,0 +1,14 @@ +use criterion::{criterion_group, criterion_main, Criterion}; + +use aoc2020::day01; + +fn aoc2020_all(c: &mut Criterion) { + c.bench_function("day01", |b| b.iter(|| day01::run().unwrap())); +} + +criterion_group! { + name = all_days; + config = Criterion::default().sample_size(30); + targets = aoc2020_all +} +criterion_main!(all_days); diff --git a/aoc2020/input/day01.txt b/aoc2020/input/day01.txt new file mode 100644 index 0000000..a0657be --- /dev/null +++ b/aoc2020/input/day01.txt @@ -0,0 +1,200 @@ +1935 +1956 +1991 +1425 +1671 +1537 +1984 +1569 +1873 +1840 +1720 +1937 +1823 +1625 +1727 +1812 +1714 +1900 +1939 +1931 +1951 +1756 +1942 +1611 +1979 +1930 +1996 +2000 +1544 +1780 +1687 +1760 +1836 +1814 +1691 +1817 +1964 +1899 +1577 +1547 +866 +1560 +1988 +1601 +1970 +1738 +1507 +1667 +1851 +1933 +1515 +1856 +1969 +1860 +1801 +2007 +1866 +1800 +1749 +1843 +1711 +1495 +1905 +763 +1672 +1858 +1987 +1492 +1849 +1993 +1737 +1874 +1658 +1810 +1665 +1768 +1950 +1879 +1816 +1868 +1995 +1763 +1783 +1833 +1968 +1847 +1748 +1725 +1891 +1755 +286 +1976 +1977 +1655 +1808 +1986 +1779 +1861 +1953 +1888 +1792 +1811 +1872 +1790 +1839 +1985 +1827 +1842 +1925 +1735 +1635 +1821 +1820 +1973 +1531 +1770 +59 +1846 +1932 +1907 +1730 +933 +1395 +1753 +1751 +361 +1530 +1782 +1087 +1589 +1929 +1795 +1815 +1732 +1765 +1877 +1722 +526 +1709 +1789 +1892 +1913 +1662 +1809 +1670 +1947 +1835 +1587 +1758 +1982 +2009 +1757 +670 +1983 +1524 +1878 +1796 +1952 +566 +1922 +1882 +1870 +1799 +1731 +1724 +1805 +2003 +1596 +1566 +1853 +1911 +1857 +1739 +1744 +1627 +1729 +1745 +1845 +1582 +1884 +1883 +1941 +1764 +1685 +1791 +1837 +1697 +1742 +1781 +1948 +1876 +1989 +1643 +1871 +1906 +1726 +1958 +1502 +1927 +1946 diff --git a/aoc2020/src/day01.rs b/aoc2020/src/day01.rs new file mode 100644 index 0000000..797b478 --- /dev/null +++ b/aoc2020/src/day01.rs @@ -0,0 +1,56 @@ +use std::fmt::Write; + +use aoc::{err, Result}; + +const INPUT: &str = include_str!("../input/day01.txt"); + +pub fn run() -> Result { + let mut res = String::with_capacity(128); + + writeln!(res, "part 1: {}", part1(INPUT)?)?; + + Ok(res) +} + +fn part1(input: &str) -> Result { + let entries = input + .lines() + .map(|line| line.parse::().map_err(|e| err!("{}", e))) + .collect::>>()?; + + let (a, b) = find_2020_sum(&entries)?; + + Ok(a * b) +} + +fn find_2020_sum(entries: &[u64]) -> Result<(u64, u64)> { + for i in 0..(entries.len()) { + for j in 0..entries.len() { + if i == j { + continue; + } + if entries[i] + entries[j] == 2020 { + return Ok((entries[i], entries[j])); + } + } + } + + Err(err!("couldn't find 2 elements that sum to 2020")) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1_provided() { + let (a, b) = find_2020_sum(&vec![1721, 979, 366, 299, 675, 1456]).unwrap(); + assert_eq!(a, 1721); + assert_eq!(b, 299); + } + + #[test] + fn part1_real() { + assert_eq!(part1(INPUT).unwrap(), 1014171); + } +} diff --git a/aoc2020/src/lib.rs b/aoc2020/src/lib.rs new file mode 100644 index 0000000..12b8f18 --- /dev/null +++ b/aoc2020/src/lib.rs @@ -0,0 +1 @@ +pub mod day01; diff --git a/aoc2020/src/main.rs b/aoc2020/src/main.rs new file mode 100644 index 0000000..ac793a0 --- /dev/null +++ b/aoc2020/src/main.rs @@ -0,0 +1,10 @@ +use aoc::DayFunc; +use aoc::Result; + +use aoc2020::day01; + +fn main() -> Result<()> { + let days: &[DayFunc] = &[day01::run]; + + aoc::run(days) +}