Added Solution for 2020 day 01

This commit is contained in:
Burnus 2023-04-03 16:51:46 +02:00
parent f7e0a8cc07
commit 62674b7f08
5 changed files with 305 additions and 0 deletions

View file

@ -0,0 +1,8 @@
[package]
name = "day01_report_repair"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,46 @@
After saving Christmas [five years in a row](/events), you've decided to take a vacation at a nice resort on a tropical island. Surely, Christmas will go on without you.
The tropical island has its own currency and is entirely cash-only. The gold coins used there have a little picture of a starfish; the locals just call them *stars*. None of the currency exchanges seem to have heard of them, but somehow, you'll need to find fifty of these coins by the time you arrive so you can pay the deposit on your room.
To save your vacation, you need to get all *fifty stars* by December 25th.
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants *one star*. Good luck!
Before you leave, the Elves in accounting just need you to fix your *expense report* (your puzzle input); apparently, something isn't quite adding up.
Specifically, they need you to *find the two entries that sum to `2020`* and then multiply those two numbers together.
For example, suppose your expense report contained the following:
```
1721
979
366
299
675
1456
```
In this list, the two entries that sum to `2020` are `1721` and `299`. Multiplying them together produces `1721 * 299 = 514579`, so the correct answer is `*514579*`.
Of course, your expense report is much larger. *Find the two entries that sum to `2020`; what do you get if you multiply them together?*
Your puzzle answer was `964875`.
\--- Part Two ---
----------
The Elves in accounting are thankful for your help; one of them even offers you a starfish coin they had left over from a past vacation. They offer you a second one if you can find *three* numbers in your expense report that meet the same criteria.
Using the above example again, the three entries that sum to `2020` are `979`, `366`, and `675`. Multiplying them together produces the answer, `*241861950*`.
In your expense report, *what is the product of the three entries that sum to `2020`?*
Your puzzle answer was `158661360`.
Both parts of this puzzle are complete! They provide two gold stars: \*\*
At this point, you should [return to your Advent calendar](/2020) and try another puzzle.
If you still want to see it, you can [get your puzzle input](1/input).

View file

@ -0,0 +1,45 @@
use std::num::ParseIntError;
pub fn run(input: &str) -> Result<(usize, usize), ParseIntError> {
let mut expenses: Vec<_> = input.lines().map(|line| line.parse::<usize>()).collect::<Result<Vec<_>, _>>()?;
expenses.sort();
let first = summands_for(2020, &expenses).map(|(a, b)| a*b).unwrap();
let second = expenses.iter().find_map(|&a| {
if let Some((b, c)) = summands_for(2020-a, &expenses) {
if a != b && a != c && b != c {
Some(a*b*c)
} else {
None
}
} else {
None
}
}).unwrap();
Ok((first, second))
}
fn summands_for(target: usize, sorted_list: &[usize]) -> Option<(usize, usize)> {
sorted_list.iter().find(|&a| sorted_list.binary_search(&(target-a)).is_ok()).map(|&a| (a, (target-a)))
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::read_to_string;
fn read_file(name: &str) -> String {
read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string()
}
#[test]
fn test_sample() {
let sample_input = read_file("tests/sample_input");
assert_eq!(run(&sample_input), Ok((514579, 241861950)));
}
#[test]
fn test_challenge() {
let challenge_input = read_file("tests/challenge_input");
assert_eq!(run(&challenge_input), Ok((964875, 158661360)));
}
}

View file

@ -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

View file

@ -0,0 +1,6 @@
1721
979
366
299
675
1456