Created
December 2, 2020 00:36
-
-
Save v21/e33069fcfe5011ad1ce2835bb1b9034b to your computer and use it in GitHub Desktop.
advent of code 2020, day 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fs::File; | |
use std::io::{self, BufRead}; | |
use std::path::Path; | |
use itertools::iproduct; | |
fn main() { | |
if let Ok(lines) = read_lines("./input.txt") { | |
let nums : Vec<i32> = lines.map(|l| l.unwrap().parse::<i32>().unwrap()).collect(); | |
for (i, j, k) in iproduct!(&nums, &nums, &nums) { | |
if (i + j + k) == 2020 { | |
println!("{} * {} * {} = {}", i , j, k, (i * j * k)); | |
} | |
} | |
} | |
} | |
// The output is wrapped in a Result to allow matching on errors | |
// Returns an Iterator to the Reader of the lines of the file. | |
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>> | |
where P: AsRef<Path>, { | |
let file = File::open(filename)?; | |
Ok(io::BufReader::new(file).lines()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment