summaryrefslogtreecommitdiffstats
path: root/src/bin/2020/day1.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/2020/day1.rs')
-rw-r--r--src/bin/2020/day1.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/bin/2020/day1.rs b/src/bin/2020/day1.rs
new file mode 100644
index 0000000..8e582d8
--- /dev/null
+++ b/src/bin/2020/day1.rs
@@ -0,0 +1,41 @@
+use advent_of_code::prelude::*;
+
+pub fn parse(fh: File) -> Result<Vec<u64>> {
+ Ok(parse::lines(fh).collect())
+}
+
+pub fn part1(ints: Vec<u64>) -> Result<u64> {
+ for i in &ints {
+ for j in &ints {
+ if i + j == 2020 {
+ return Ok(i * j);
+ }
+ }
+ }
+ Err(anyhow!("no numbers summing to 2020 found"))
+}
+
+pub fn part2(ints: Vec<u64>) -> Result<u64> {
+ for i in &ints {
+ for j in &ints {
+ for k in &ints {
+ if i + j + k == 2020 {
+ return Ok(i * j * k);
+ }
+ }
+ }
+ }
+ Err(anyhow!("no numbers summing to 2020 found"))
+}
+
+#[test]
+fn test() {
+ assert_eq!(
+ part1(parse(parse::data(2020, 1).unwrap()).unwrap()).unwrap(),
+ 445536
+ );
+ assert_eq!(
+ part2(parse(parse::data(2020, 1).unwrap()).unwrap()).unwrap(),
+ 138688160
+ );
+}