From d8ab28bbaba52e2c9f30aad2cc9812bd8a44becf Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Mon, 30 Nov 2020 22:06:13 -0800 Subject: Day 1! --- 01/src/main.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to '01') diff --git a/01/src/main.rs b/01/src/main.rs index 480e6db..a46ee99 100644 --- a/01/src/main.rs +++ b/01/src/main.rs @@ -2,7 +2,55 @@ use advent_lib::prelude::*; fn main() -> Result<()> { - advent_lib::greeting()?; + let mut args = std::env::args(); + if args.len() != 2 { + eprintln!("Usage: advent input"); + } + let _ = args.next(); + let filename = args.next().unwrap(); + + let mut input = advent_lib::read_int_file(&filename)?; + input.sort(); + + for i in 0 .. input.len() { + let a = input[i]; + if a > 2020 { + break; + } + + for j in i+1 .. input.len() { + let b = input[j]; + + if a + b == 2020 { + let product = a * b; + println!("a: {:?}, b: {:?}, a*b: {:?}", a, b, product); + } + } + } + + for i in 0 .. input.len() { + let a = input[i]; + if a > 2020 { + break; + } + + for j in i+1 .. input.len() { + let b = input[j]; + + if a + b > 2020 { + break; + } + + for k in j+1 .. input.len() { + let c = input[k]; + + if a + b + c == 2020 { + let product = a * b * c; + println!("a: {:?}, b: {:?}, c: {:?}, a*b*c: {:?}", a, b, c, product); + } + } + } + } Ok(()) } -- cgit 1.4.1