Migrate to using cargo-aoc.
This commit is contained in:
@@ -1,200 +0,0 @@
|
||||
1619
|
||||
1919
|
||||
1441
|
||||
1861
|
||||
1932
|
||||
1514
|
||||
1847
|
||||
1871
|
||||
1764
|
||||
1467
|
||||
1970
|
||||
1589
|
||||
2009
|
||||
1429
|
||||
1098
|
||||
1327
|
||||
1502
|
||||
1398
|
||||
1710
|
||||
1562
|
||||
1512
|
||||
1468
|
||||
1762
|
||||
1348
|
||||
1356
|
||||
1950
|
||||
1266
|
||||
1969
|
||||
1815
|
||||
1583
|
||||
1959
|
||||
1092
|
||||
1694
|
||||
1814
|
||||
1763
|
||||
1151
|
||||
1981
|
||||
1193
|
||||
1614
|
||||
1413
|
||||
1642
|
||||
1943
|
||||
1407
|
||||
895
|
||||
1430
|
||||
1706
|
||||
1962
|
||||
1522
|
||||
1486
|
||||
1986
|
||||
1623
|
||||
1489
|
||||
1411
|
||||
1851
|
||||
1817
|
||||
1416
|
||||
1654
|
||||
1438
|
||||
1419
|
||||
1649
|
||||
1362
|
||||
690
|
||||
1804
|
||||
1452
|
||||
1766
|
||||
1360
|
||||
1807
|
||||
1385
|
||||
1964
|
||||
1626
|
||||
1832
|
||||
745
|
||||
1702
|
||||
1602
|
||||
1471
|
||||
1996
|
||||
1915
|
||||
1813
|
||||
1460
|
||||
1925
|
||||
1638
|
||||
1581
|
||||
1584
|
||||
1379
|
||||
1148
|
||||
1554
|
||||
1564
|
||||
1914
|
||||
1757
|
||||
1820
|
||||
1559
|
||||
1096
|
||||
1944
|
||||
1587
|
||||
1499
|
||||
390
|
||||
1733
|
||||
1371
|
||||
1781
|
||||
2002
|
||||
324
|
||||
1655
|
||||
1639
|
||||
1482
|
||||
1198
|
||||
1264
|
||||
1953
|
||||
1320
|
||||
1704
|
||||
1321
|
||||
1449
|
||||
1455
|
||||
1509
|
||||
1765
|
||||
1797
|
||||
1703
|
||||
1758
|
||||
1610
|
||||
1756
|
||||
1901
|
||||
1707
|
||||
1968
|
||||
1601
|
||||
1328
|
||||
1336
|
||||
1592
|
||||
1678
|
||||
1699
|
||||
1793
|
||||
1957
|
||||
2000
|
||||
1306
|
||||
1094
|
||||
1545
|
||||
1331
|
||||
1751
|
||||
1739
|
||||
1335
|
||||
1753
|
||||
1983
|
||||
1966
|
||||
1934
|
||||
1831
|
||||
1426
|
||||
1711
|
||||
1840
|
||||
1857
|
||||
1347
|
||||
1789
|
||||
1409
|
||||
1310
|
||||
1752
|
||||
1897
|
||||
1497
|
||||
1485
|
||||
1125
|
||||
1803
|
||||
1577
|
||||
919
|
||||
1635
|
||||
1791
|
||||
1456
|
||||
1796
|
||||
1974
|
||||
1954
|
||||
1828
|
||||
2004
|
||||
1890
|
||||
1376
|
||||
1569
|
||||
1406
|
||||
1463
|
||||
2006
|
||||
1109
|
||||
1620
|
||||
1656
|
||||
1870
|
||||
1498
|
||||
1645
|
||||
1145
|
||||
1681
|
||||
1269
|
||||
1527
|
||||
1621
|
||||
1575
|
||||
1324
|
||||
1647
|
||||
1519
|
||||
1697
|
||||
1421
|
||||
1216
|
||||
1846
|
||||
1625
|
||||
1585
|
||||
1369
|
||||
1882
|
||||
1823
|
||||
1388
|
||||
1548
|
||||
1879
|
||||
@@ -1,6 +0,0 @@
|
||||
1721
|
||||
979
|
||||
366
|
||||
299
|
||||
675
|
||||
1456
|
||||
@@ -30,98 +30,45 @@
|
||||
//!
|
||||
//! In your expense report, what is the product of the three entries that sum to 2020?
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::Path;
|
||||
use aoc_runner_derive::{aoc, aoc_generator};
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let path = std::env::args()
|
||||
.nth(1)
|
||||
.ok_or(anyhow!("Usage: 1 <path to expense report>"))?;
|
||||
let nums = parse(path)?;
|
||||
let pair = find_pair_2020(&nums).ok_or(anyhow!("Couldn't find pairs summing to 2020"))?;
|
||||
println!("Product of {} x {} = {}", pair.0, pair.1, pair.0 * pair.1);
|
||||
let triple = find_triple_2020(&nums).ok_or(anyhow!("Couldn't find triples summing to 2020"))?;
|
||||
println!(
|
||||
"Product of {} x {} x {} = {}",
|
||||
triple.0,
|
||||
triple.1,
|
||||
triple.2,
|
||||
triple.0 * triple.1 * triple.2
|
||||
);
|
||||
Ok(())
|
||||
/// Reads text file containing one integer per line, and parses them into `Vec<u32>`. Any
|
||||
/// non-number will result in a panice.
|
||||
#[aoc_generator(day1)]
|
||||
fn parse(input: &str) -> Vec<u32> {
|
||||
input
|
||||
.split('\n')
|
||||
.map(|line| line.parse().unwrap())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Finds pairs of numbers in `nums` that sum to 2020. If no pairs are found, `None` is returned.
|
||||
fn find_pair_2020(nums: &Vec<u32>) -> Option<(u32, u32)> {
|
||||
/// Finds pairs of numbers in `nums` that sum to 2020. If no pairs are found, the function panics.
|
||||
/// TODO(wathiede): make a version that sorts or uses a hash for finding the match to compare
|
||||
/// benchmarks.
|
||||
#[aoc(day1, part1)]
|
||||
fn find_pair_2020(nums: &[u32]) -> u32 {
|
||||
for (idx, first) in nums.iter().enumerate() {
|
||||
for second in nums.iter().skip(idx + 1) {
|
||||
if first + second == 2020 {
|
||||
return Some((*first, *second));
|
||||
return first * second;
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
panic!("Couldn't find pair");
|
||||
}
|
||||
|
||||
/// Finds triple of numbers in `nums` that sum to 2020. If no triple is found, `None` is returned.
|
||||
fn find_triple_2020(nums: &Vec<u32>) -> Option<(u32, u32, u32)> {
|
||||
/// Finds triple of numbers in `nums` that sum to 2020. If no triple is found, the function
|
||||
/// panics.
|
||||
#[aoc(day1, part2)]
|
||||
fn find_triple_2020(nums: &[u32]) -> u32 {
|
||||
for (idx1, first) in nums.iter().enumerate() {
|
||||
for (idx2, second) in nums.iter().enumerate().skip(idx1 + 1) {
|
||||
for third in nums.iter().skip(idx2 + 1) {
|
||||
if first + second + third == 2020 {
|
||||
return Some((*first, *second, *third));
|
||||
return first * second * third;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Reads text file containing one integer per line, and parses them into `Vec<u32>`. Any
|
||||
/// non-number will result in an error returned.
|
||||
fn parse<P: AsRef<Path>>(path: P) -> Result<Vec<u32>> {
|
||||
let f = File::open(path)?;
|
||||
let f = BufReader::new(f);
|
||||
let mut nums = Vec::new();
|
||||
for line in f.lines() {
|
||||
let num: u32 = line?.parse()?;
|
||||
nums.push(num)
|
||||
}
|
||||
Ok(nums)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use anyhow::Context;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn get_nums() -> Vec<u32> {
|
||||
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
let path = root.join("src/bin/day1-test.txt");
|
||||
parse(&path)
|
||||
.with_context(|| format!("Input {}", path.display()))
|
||||
.expect("failed to parse")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse() {
|
||||
let nums = get_nums();
|
||||
assert_eq!(nums, vec![1721, 979, 366, 299, 675, 1456]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_pair_2020() {
|
||||
let nums = get_nums();
|
||||
assert_eq!(find_pair_2020(&nums), Some((1721, 299)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_triple_2020() {
|
||||
let nums = get_nums();
|
||||
assert_eq!(find_triple_2020(&nums), Some((979, 366, 675)));
|
||||
}
|
||||
panic!("Couldn't find triple");
|
||||
}
|
||||
5
2020/src/lib.rs
Normal file
5
2020/src/lib.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
mod day1;
|
||||
|
||||
use aoc_runner_derive::aoc_lib;
|
||||
|
||||
aoc_lib! { year = 2020 }
|
||||
Reference in New Issue
Block a user