diff --git a/Rust/.gitignore b/Rust/.gitignore new file mode 100644 index 0000000..5028f00 --- /dev/null +++ b/Rust/.gitignore @@ -0,0 +1,7 @@ +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# These are backup files generated by rustfmt +**/*.rs.bk \ No newline at end of file diff --git a/Rust/day1/Cargo.lock b/Rust/day1/Cargo.lock new file mode 100644 index 0000000..464d0c4 --- /dev/null +++ b/Rust/day1/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "day1" +version = "0.1.0" diff --git a/Rust/day1/Cargo.toml b/Rust/day1/Cargo.toml new file mode 100644 index 0000000..014a020 --- /dev/null +++ b/Rust/day1/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "day1" +version = "0.1.0" +authors = ["GitHub "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + +[[bin]] +name = "day1" +path = "main.rs" diff --git a/Rust/day1/input b/Rust/day1/input new file mode 100644 index 0000000..a9175b8 --- /dev/null +++ b/Rust/day1/input @@ -0,0 +1,200 @@ +1822 +1917 +1642 +1617 +1941 +1740 +1529 +1896 +1880 +568 +1897 +1521 +1832 +1936 +611 +1475 +1950 +1895 +1532 +1721 +1498 +1905 +1770 +1845 +2003 +1854 +1705 +1916 +1913 +1956 +1798 +1823 +1955 +1713 +1942 +1710 +1696 +1590 +1966 +1476 +1800 +1672 +1533 +1524 +1957 +1923 +1545 +534 +1707 +1760 +1104 +1471 +1947 +1802 +1525 +1931 +1653 +1608 +1937 +1977 +1598 +1470 +1794 +1488 +1786 +1652 +1482 +1603 +1667 +1245 +1478 +667 +1948 +1885 +547 +1971 +1795 +1910 +1571 +1711 +1727 +1987 +1597 +1586 +1661 +1893 +1873 +1827 +1561 +2006 +1782 +1813 +2000 +1592 +1714 +1849 +1501 +1809 +1751 +1935 +1692 +1697 +1878 +1502 +1738 +1731 +1682 +1690 +1499 +1641 +1925 +1996 +1972 +1886 +1836 +1747 +1841 +1668 +715 +1698 +1859 +1637 +1477 +1785 +1695 +1702 +1944 +1631 +1771 +1623 +1892 +1466 +1834 +1899 +201 +1801 +1978 +1830 +1591 +1673 +1949 +1846 +1677 +1657 +1576 +1817 +1851 +1894 +1754 +1604 +1568 +1730 +1985 +1614 +1980 +1554 +1997 +1960 +1983 +1848 +1883 +1968 +1729 +1716 +628 +1472 +1676 +1943 +1821 +1681 +1619 +1644 +842 +1492 +1633 +1921 +775 +1861 +1584 +1974 +585 +1898 +1560 +1708 +1927 +1563 +1872 +1876 +1865 +1535 +1994 +1756 +1662 +1621 +1993 +1825 +1679 +1959 +1691 +1875 \ No newline at end of file diff --git a/Rust/day1/main.rs b/Rust/day1/main.rs new file mode 100644 index 0000000..f4893e9 --- /dev/null +++ b/Rust/day1/main.rs @@ -0,0 +1,64 @@ +use std::fs::File; +use std::io; +use std::io::prelude::*; +use std::path::Path; + +fn parse_input(buf: B) -> Vec { + buf.lines() + .map(|line| { + line.expect("Failed to read line.") + .parse() + .expect("Failed to parse line input.") + }) + .collect() +} + +fn puzzle_a(nums: &[u64]) -> Option { + for a in nums { + for b in nums { + if a + b == 2020 { + return Some(a * b); + } + } + } + None +} + +fn puzzle_b(nums: &[u64]) -> Option { + for a in nums { + for b in nums { + for c in nums { + if a + b + c == 2020 { + return Some(a * b * c); + } + } + } + } + + None +} + +fn open_file

(filename: P) -> io::Result> +where + P: AsRef, +{ + let file = File::open(filename)?; + Ok(io::BufReader::new(file)) +} + +fn main() -> Result<(), &'static str> { + let buffer = open_file("input").unwrap(); + let puzzle_input = parse_input(buffer); + + println!( + "Part A:\n{}", + puzzle_a(&puzzle_input).expect("No solution for A found.") + ); + println!("--------"); + println!( + "Part B:\n{}", + puzzle_b(&puzzle_input).expect("No solution for B found.") + ); + + Ok(()) +}