summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--01/src/main.rs50
-rw-r--r--lib/src/error.rs37
-rw-r--r--lib/src/lib.rs26
3 files changed, 99 insertions, 14 deletions
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(())
 }
diff --git a/lib/src/error.rs b/lib/src/error.rs
index cd4a760..216df78 100644
--- a/lib/src/error.rs
+++ b/lib/src/error.rs
@@ -1,30 +1,41 @@
 #[derive(Debug)]
 pub enum Error {
-    IO(std::io::Error),
+  IO(std::io::Error),
+  Parse,
 }
 
 impl std::error::Error for Error { }
 
 impl std::fmt::Display for Error {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        match self {
-            Error::IO(e) => e.fmt(f),
-        }
+  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+    match self {
+      Error::IO(e) => e.fmt(f),
+      Error::Parse => f.write_str("Parse error"),
     }
+  }
 }
 
 impl std::cmp::PartialEq for Error {
-    fn eq(&self, other: &Self) -> bool {
-        match (self, other) {
-            (Error::IO(_), Error::IO(_)) =>
-                false,
-        }
+  fn eq(&self, other: &Self) -> bool {
+    match (self, other) {
+      (Error::IO(_), Error::IO(_)) =>
+        false,
+      (Error::Parse, Error::Parse) =>
+        true,
+      _ =>
+        false,
     }
+  }
 }
 
 impl From<std::io::Error> for Error {
-    fn from(e: std::io::Error) -> Error {
-        Error::IO(e)
-    }
+  fn from(e: std::io::Error) -> Error {
+    Error::IO(e)
+  }
 }
 
+impl From<std::num::ParseIntError> for Error {
+  fn from(_: std::num::ParseIntError) -> Error {
+    Error::Parse
+  }
+}
diff --git a/lib/src/lib.rs b/lib/src/lib.rs
index b198f10..4b4b679 100644
--- a/lib/src/lib.rs
+++ b/lib/src/lib.rs
@@ -3,9 +3,35 @@ pub mod prelude;
 
 pub use crate::prelude::Result;
 
+use std::fs::File;
+use std::io::BufReader;
+use std::io::prelude::*;
+
 
 pub fn greeting() -> Result<()> {
   println!("Hello, Irenes!");
 
   Ok(())
 }
+
+pub fn read_int_file(filename: &str) -> Result<Vec<i64>> {
+  let file = File::open(filename)?;
+  let mut reader = BufReader::new(file);
+  let mut buffer = String::new();
+
+  let mut input: Vec<i64> = Vec::new();
+  loop {
+    reader.read_line(&mut buffer)?;
+    if buffer.len() == 0 {
+      break;
+    }
+
+    let item = buffer.trim().parse::<i64>()?;
+
+    buffer.clear();
+
+    input.push(item);
+  }
+
+  Ok(input)
+}