Complete day 1

pull/1/head
Tristan Daniël Maat 2020-12-02 20:30:24 +00:00
parent b613911f46
commit c19268deca
Signed by: tlater
GPG Key ID: 49670FD774E43268
5 changed files with 375 additions and 0 deletions

1
day-1/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

94
day-1/Cargo.lock generated Normal file
View File

@ -0,0 +1,94 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "day-1"
version = "0.1.0"
dependencies = [
"indoc",
"itertools",
]
[[package]]
name = "either"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "indoc"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47741a8bc60fb26eb8d6e0238bbb26d8575ff623fdc97b1a2c00c050b9684ed8"
dependencies = [
"indoc-impl",
"proc-macro-hack",
]
[[package]]
name = "indoc-impl"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce046d161f000fffde5f432a0d034d0341dc152643b2598ed5bfce44c4f3a8f0"
dependencies = [
"proc-macro-hack",
"proc-macro2",
"quote",
"syn",
"unindent",
]
[[package]]
name = "itertools"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b"
dependencies = [
"either",
]
[[package]]
name = "proc-macro-hack"
version = "0.5.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
[[package]]
name = "proc-macro2"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "1.0.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8833e20724c24de12bbaba5ad230ea61c3eafb05b881c7c9d3cfe8638b187e68"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
name = "unindent"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f14ee04d9415b52b3aeab06258a3f07093182b88ba0f9b8d203f211a7a7d41c7"

9
day-1/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "day-1"
version = "0.1.0"
authors = ["Tristan Daniël Maat <tm@tlater.net>"]
edition = "2018"
[dependencies]
indoc = "0.3"
itertools = "0.9"

200
day-1/input Normal file
View File

@ -0,0 +1,200 @@
1429
1368
1661
1687
1593
1495
1565
1500
1635
1845
1645
1999
1415
1054
1930
1774
1405
1993
1757
1623
1675
1665
631
1950
1702
1311
1509
1790
1643
1884
226
1455
1679
1746
1284
1342
1684
1543
1396
1806
1523
1363
1011
1577
1767
1287
1885
1517
1556
1722
1260
1624
1466
1263
1162
1688
1202
1913
1964
1385
1970
1976
1431
858
1748
1544
1438
1300
1926
1587
1376
1939
1039
1639
1539
1491
1631
1521
1564
1507
1637
1534
1713
1533
1118
1356
2003
282
1079
1837
1259
1941
1836
1903
1433
1467
1027
1441
1048
1742
1087
1872
1476
1657
1361
1182
1494
1529
1822
1444
1330
1514
1723
1432
1683
1997
1443
1474
1932
1504
1313
1765
19
1784
1619
992
1560
1680
1626
1558
1899
1293
1676
1161
1140
1341
1597
1628
1611
1302
1269
1241
1952
1591
1726
428
1703
1289
1109
1478
1002
1817
1849
1838
1319
1641
583
1920
1453
1411
1870
1763
1469
1646
1719
1213
1462
1545
1682
1711
18
2004
1252
1620
1559
1315
781
1656
1987
1436
1630
1985
1897
1551
1296
1282
1735
1320
1659
1271
1380
1274
1876
1492
1298
1399
1692
1265
1555
1337

71
day-1/src/main.rs Normal file
View File

@ -0,0 +1,71 @@
use std::fs;
use itertools::Itertools;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let input = fs::read_to_string("input")?;
let input = parse_number_list(input)?;
let numbers = find_2020_numbers(input, 3)?;
println!("{}", numbers.iter().product::<u32>());
Ok(())
}
fn parse_number_list(input: String) -> Result<Vec<u32>, std::num::ParseIntError> {
input.lines().map(|line| line.parse::<u32>()).collect()
}
fn find_2020_numbers(input: Vec<u32>, sum_number: usize) -> Result<Vec<u32>, String> {
input
.into_iter()
.combinations(sum_number)
.find(|i| i.iter().sum::<u32>() == 2020)
.ok_or("Could not find 2020.".to_string())
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
#[test]
fn test_first_example() -> Result<(), Box<dyn std::error::Error>> {
let example = indoc!(
"
1721
979
366
299
675
1456
"
);
let example = parse_number_list(example.to_string())?;
let numbers = find_2020_numbers(example, 2)?;
assert_eq!(numbers.iter().product::<u32>(), 514579);
Ok(())
}
#[test]
fn test_second_example() -> Result<(), Box<dyn std::error::Error>> {
let example = indoc!(
"
1721
979
366
299
675
1456
"
);
let example = parse_number_list(example.to_string())?;
let numbers = find_2020_numbers(example, 3)?;
assert_eq!(numbers.iter().to_owned().product::<u32>(), 241861950);
Ok(())
}
}