aboutsummaryrefslogtreecommitdiff
path: root/2020/day01/src
diff options
context:
space:
mode:
authorjacopograndi <jacopo.grandi@outlook.it>2022-03-21 13:41:51 +0100
committerjacopograndi <jacopo.grandi@outlook.it>2022-03-21 13:41:51 +0100
commit94d08269630fb976ad0a04d4e842ec74b54299f5 (patch)
tree0b9ac72d6d2fec87b6f17463814b2b3eb7b3a36c /2020/day01/src
parent2a084ddff56ce62fd5a24ad1095235c3ecc12838 (diff)
no submodule
Diffstat (limited to '2020/day01/src')
-rw-r--r--2020/day01/src/main.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/2020/day01/src/main.rs b/2020/day01/src/main.rs
new file mode 100644
index 0000000..02a3151
--- /dev/null
+++ b/2020/day01/src/main.rs
@@ -0,0 +1,22 @@
+use std::fs;
+use std::process;
+
+fn main() {
+ let raw = fs::read_to_string("input.txt").unwrap_or_else(|err| {
+ println!("Failed to open file. {}", err);
+ process::exit(1);
+ } );
+
+ let nums : Vec<i32> = raw.lines()
+ .map(|x| x.parse().unwrap())
+ .collect();
+
+ let res : Vec<i32> = nums.iter()
+ .map(|&x| nums.iter().map(move |&y| (x, y)))
+ .flatten()
+ .filter(|(x, y)| x+y == 2020)
+ .map(|(x, y)| x*y)
+ .collect();
+
+ println!("x+y = 2020, x*y = {}", res.first().unwrap());
+}