From dd87be81ed0480150b488306d4f181e9d404f0d9 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Tue, 1 Dec 2020 21:15:53 -0800 Subject: [PATCH] advent of code day 01 --- .gitignore | 3 + Cargo.toml | 9 +++ inputs/01-01.txt | 200 +++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 39 +++++++++ src/main.rs | 8 ++ 5 files changed, 259 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 inputs/01-01.txt create mode 100644 src/lib.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8ff0eb3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +.idea +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8652bc0 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "advent_of_code" +version = "0.1.0" +authors = ["Dan Ballard "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/inputs/01-01.txt b/inputs/01-01.txt new file mode 100644 index 0000000..a011bbf --- /dev/null +++ b/inputs/01-01.txt @@ -0,0 +1,200 @@ +1956 +1994 +457 +1654 +2003 +1902 +1741 +1494 +1597 +1129 +1146 +1589 +1989 +1093 +1881 +1288 +1848 +1371 +1508 +1035 +1813 +1335 +1634 +1102 +1262 +1637 +1048 +1807 +1270 +1528 +1670 +1803 +1202 +1294 +1570 +1640 +1484 +1872 +1140 +1207 +1485 +1781 +1778 +1772 +1334 +1267 +1045 +1194 +1873 +1441 +1557 +1414 +1123 +1980 +1527 +1591 +1665 +1916 +1662 +1139 +1973 +1258 +1041 +1134 +1609 +1554 +1455 +1124 +1478 +1938 +1759 +1281 +1410 +1511 +930 +1319 +1302 +1827 +1216 +1404 +1460 +2002 +1590 +1817 +1341 +1631 +1608 +1382 +1158 +1594 +1049 +1804 +1555 +1753 +447 +1021 +1079 +609 +1766 +1327 +1851 +1052 +1737 +1175 +1043 +1945 +1573 +1113 +1724 +1203 +1856 +1682 +1623 +1135 +1015 +1423 +1412 +1315 +1375 +1895 +1351 +1530 +1758 +1445 +1518 +1819 +1567 +1305 +1919 +1952 +1432 +1099 +1476 +1883 +1871 +1900 +1442 +1393 +1214 +1283 +1538 +1391 +1008 +1109 +1621 +1876 +1998 +1032 +1324 +1927 +481 +1732 +1370 +1683 +1199 +1465 +1882 +1293 +1671 +1456 +1197 +1506 +1381 +1469 +1830 +1957 +1850 +1184 +1564 +1170 +1943 +1131 +1867 +1208 +1788 +1337 +1722 +1760 +1651 +1069 +1574 +1959 +1770 +66 +1190 +1606 +1899 +1054 +980 +1693 +1173 +1479 +1333 +1579 +1720 +1782 +1971 +1438 +1178 +1306 \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..d158675 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,39 @@ + +use std::fs::File; +use std::io::{prelude::*, BufReader, Error}; + + + +pub fn aoc_01_01(filename: &str) -> Result<(), Error> { + + let file = File::open(filename).expect("No file"); + let br = BufReader::new(file); + let mut v: Vec = vec![]; + + for line in br.lines() { + v.push(line? + .trim() + .parse().unwrap()); + } + + for i in 0..v.len()-2 { + for j in i+1..v.len()-1 { + if v[i] + v[j] == 2020 { + println!("aoc_01_01 result: {}", v[i]*v[j]); + } + } + } + + for i in 0..v.len()-3 { + for j in i+1..v.len()-2 { + for k in j+1..v.len()-1 { + if v[i] + v[j] + v[k] == 2020 { + println!("aoc_01_02 result: {}", v[i] * v[j] * v[k]); + } + } + } + } + + return Ok(()); + +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..f37a13e --- /dev/null +++ b/src/main.rs @@ -0,0 +1,8 @@ + + + +fn main() { + //let filename = env::args().nth(1).expect("input filename required"); + + advent_of_code::aoc_01_01("./inputs/01-01.txt"); +}