~necaris/advent-of-code

4487221e684fe4946aebbc5eaaab78db9b8766df — Rami Chowdhury 1 year, 8 months ago f33f374
Add Day 1 solution, in Rust
2 files changed, 261 insertions(+), 0 deletions(-)

A 2020/01/input.txt
A 2020/01/main.rs
A 2020/01/input.txt => 2020/01/input.txt +201 -0
@@ 0,0 1,201 @@
1046
1565
1179
1889
1683
1837
1973
1584
1581
192
1857
1373
1715
1473
1770
1907
1918
1909
1880
1903
1835
1887
1511
1844
1628
1688
1545
1469
1620
1751
1893
1861
511
1201
1641
1874
1946
1701
1777
1829
1609
1805
1678
1928
1398
1555
1675
1798
1485
1911
1974
1663
1919
1635
195
1441
1525
1490
1151
1406
1408
1095
1085
1097
1976
1987
1498
1753
1603
1933
1729
1106
1929
1832
1744
1914
1643
1571
1391
1953
1790
1797
1938
258
1957
1858
1506
628
1109
1113
1768
1649
1669
694
1803
1849
1395
1754
1421
1575
1632
1998
1693
1499
1550
1771
1902
1801
1549
1459
1826
1927
1507
1718
647
1922
1432
1625
1904
1691
1427
1519
1949
1514
1749
1616
1898
1696
1917
1661
1787
1440
1796
1560
1956
1823
1815
1557
1730
1951
1548
1527
1881
1727
1530
1460
1360
1583
1662
1954
1890
1855
1752
1935
1601
1767
1812
1990
1445
1908
2001
1544
1814
1634
1532
1788
1521
1638
1470
1524
1394
1674
1314
1588
1429
1745
1416
1637
1942
484
1467
1764
1743
1401
1471
1458
1335
1866
1399
1393
1708
1694
1447
1972
1478
1182
1672
1813
1546
1535


A 2020/01/main.rs => 2020/01/main.rs +60 -0
@@ 0,0 1,60 @@
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::io::{Error, ErrorKind};

fn find_product_2(numbers: &Vec<i64>) -> Result<i64, Error> {
    // find the two entries that sum to 2020 and multiply them
    for i in numbers.iter() {
        for j in numbers.iter() {
            if (i + j) == 2020 {
                return Ok(i * j);
            }
        }
    }
    Err(Error::new(
        ErrorKind::Other,
        "No entries found summing to 2020",
    ))
}

fn find_product_3(numbers: &Vec<i64>) -> Result<i64, Error> {
    // find the three entries that sum to 2020 and multiply them
    for i in numbers.iter() {
        for j in numbers.iter() {
            for k in numbers.iter() {
                if (i + j + k) == 2020 {
                    return Ok(i * j * k);
                }
            }
        }
    }
    Err(Error::new(
        ErrorKind::Other,
        "No entries found summing to 2020",
    ))
}

fn main() -> Result<(), Error> {
    // read all the numbers in
    let input = File::open("input.txt")?;
    let reader = BufReader::new(input);
    let mut numbers: Vec<i64> = Vec::new();
    for l in reader.lines() {
        let line = l?;
        if line.len() < 1 {
            continue;
        }
        let i: i64 = line
            .parse()
            .map_err(|e| Error::new(ErrorKind::Other, format!("{:?}", e)))?;
        numbers.push(i);
    }

    let result2 = find_product_2(&numbers)?;
    println!("Found the answer for 2! {}", result2);

    let result3 = find_product_3(&numbers)?;
    println!("Found the answer for 3! {}", result3);
    Ok(())
}