From 392657fda37143320dc0079b2a99ce42365feabe Mon Sep 17 00:00:00 2001 From: Anson Date: Sat, 9 Jan 2021 00:57:38 -0700 Subject: [PATCH] day1 in rust --- .gitignore | 2 ++ day1/main.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 .gitignore create mode 100644 day1/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f370ae --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +main.pdb +main.exe \ No newline at end of file diff --git a/day1/main.rs b/day1/main.rs new file mode 100644 index 0000000..e4fc809 --- /dev/null +++ b/day1/main.rs @@ -0,0 +1,33 @@ +use std::fs; + +fn main() { + let input = fs::read_to_string("day1\\input.txt").expect("Could not read file"); + + let lines: Vec<&str> = input.lines().collect(); + + println!("Part1:"); + 'outer1: for i in lines.iter() { + let l1 = i.parse::().unwrap(); + for j in lines.iter() { + let l2 = j.parse::().unwrap(); + if l1 + l2 == 2020 { + println!("{}+{} = 2020\nProduct {}", l1, l2, l1 * l2); + break 'outer1; + } + } + } + println!("\nPart2:"); + 'outer2: for i in lines.iter() { + let l1 = i.parse::().unwrap(); + for j in lines.iter() { + let l2 = j.parse::().unwrap(); + for k in lines.iter() { + let l3 = k.parse::().unwrap(); + if l1 + l2 + l3 == 2020 { + println!("{}+{}+{} = 2020\nProduct {}", l1, l2, l3, l1 * l2 * l3); + break 'outer2; + } + } + } + } +}