summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--02/Cargo.toml9
-rw-r--r--02/src/main.rs75
-rw-r--r--Cargo.lock56
-rw-r--r--Cargo.nix160
-rw-r--r--Cargo.toml1
-rw-r--r--lib/src/lib.rs23
6 files changed, 324 insertions, 0 deletions
diff --git a/02/Cargo.toml b/02/Cargo.toml
new file mode 100644
index 0000000..f4176dc
--- /dev/null
+++ b/02/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "advent_02"
+version = "0.1.0"
+authors = ["Irene Knapp <ireneista@gmail.com>"]
+edition = "2018"
+
+[dependencies]
+advent_lib = { path = "../lib" }
+regex = "1"
diff --git a/02/src/main.rs b/02/src/main.rs
new file mode 100644
index 0000000..5bcf2c1
--- /dev/null
+++ b/02/src/main.rs
@@ -0,0 +1,75 @@
+use advent_lib::prelude::*;
+
+use regex::Regex;
+use std::convert::TryInto;
+
+
+fn main() -> Result<()> {
+  let mut args = std::env::args();
+  if args.len() != 2 {
+    eprintln!("Usage: advent input");
+  }
+  let _ = args.next();
+  let filename = args.next().unwrap();
+
+  let input = advent_lib::read_lines_file(&filename)?;
+
+  let regex = Regex::new(r"^(\d+)-(\d+) ([a-z]): ([a-z]*)$").unwrap();
+
+  let mut valid_passwords = 0;
+
+  for line in &input {
+    let captures = regex.captures(line).unwrap();
+    let min = captures[1].parse::<i64>()?;
+    let max = captures[2].parse::<i64>()?;
+    let required_char = captures[3].chars().nth(0);
+    let password = &captures[4];
+
+    let mut occurrences = 0;
+    for c in password.chars() {
+      if Some(c) == required_char {
+        occurrences += 1;
+      }
+    }
+
+    if occurrences >= min && occurrences <= max {
+      valid_passwords += 1;
+    }
+  }
+
+  println!("{}", valid_passwords);
+
+  let mut valid_passwords_2 = 0;
+
+  for line in &input {
+    let captures = regex.captures(line).unwrap();
+    let index_a = captures[1].parse::<i64>()?;
+    let index_b = captures[2].parse::<i64>()?;
+    let required_char = captures[3].chars().nth(0);
+    let password = &captures[4];
+
+    let a_is_match =
+            password.len() >= index_a.try_into().unwrap()
+            && required_char
+               == password.chars().nth((index_a - 1).try_into().unwrap());
+    let b_is_match =
+            password.len() >= index_b.try_into().unwrap()
+            && required_char
+               == password.chars().nth((index_b - 1).try_into().unwrap());
+    let mut occurrences = 0;
+    if a_is_match {
+      occurrences += 1;
+    }
+    if b_is_match {
+      occurrences += 1;
+    }
+
+    if occurrences == 1 {
+      valid_passwords_2 += 1;
+    }
+  }
+
+  println!("{}", valid_passwords_2);
+
+  Ok(())
+}
diff --git a/Cargo.lock b/Cargo.lock
index 494a001..28aa121 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -8,5 +8,61 @@ dependencies = [
 ]
 
 [[package]]
+name = "advent_02"
+version = "0.1.0"
+dependencies = [
+ "advent_lib",
+ "regex",
+]
+
+[[package]]
 name = "advent_lib"
 version = "0.1.0"
+
+[[package]]
+name = "aho-corasick"
+version = "0.7.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5"
+dependencies = [
+ "memchr",
+]
+
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
+[[package]]
+name = "memchr"
+version = "2.3.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525"
+
+[[package]]
+name = "regex"
+version = "1.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "38cf2c13ed4745de91a5eb834e11c00bcc3709e773173b2ce4c56c9fbde04b9c"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-syntax",
+ "thread_local",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.6.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189"
+
+[[package]]
+name = "thread_local"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
+dependencies = [
+ "lazy_static",
+]
diff --git a/Cargo.nix b/Cargo.nix
index 68c77e5..64edc9d 100644
--- a/Cargo.nix
+++ b/Cargo.nix
@@ -38,6 +38,16 @@ rec {
       # File a bug if you depend on any for non-debug work!
       debug = internal.debugCrate { inherit packageId; };
     };
+    "advent_02" = rec {
+      packageId = "advent_02";
+      build = internal.buildRustCrateWithFeatures {
+        packageId = "advent_02";
+      };
+
+      # Debug support which might change between releases.
+      # File a bug if you depend on any for non-debug work!
+      debug = internal.debugCrate { inherit packageId; };
+    };
     "advent_lib" = rec {
       packageId = "advent_lib";
       build = internal.buildRustCrateWithFeatures {
@@ -89,6 +99,29 @@ rec {
         ];
         
       };
+      "advent_02" = rec {
+        crateName = "advent_02";
+        version = "0.1.0";
+        edition = "2018";
+        crateBin = [
+          { name = "advent_02"; path = "src/main.rs"; }
+        ];
+        src = (builtins.filterSource sourceFilter ./02);
+        authors = [
+          "Irene Knapp <ireneista@gmail.com>"
+        ];
+        dependencies = [
+          {
+            name = "advent_lib";
+            packageId = "advent_lib";
+          }
+          {
+            name = "regex";
+            packageId = "regex";
+          }
+        ];
+        
+      };
       "advent_lib" = rec {
         crateName = "advent_lib";
         version = "0.1.0";
@@ -99,6 +132,133 @@ rec {
         ];
         
       };
+      "aho-corasick" = rec {
+        crateName = "aho-corasick";
+        version = "0.7.15";
+        edition = "2015";
+        sha256 = "1rb8gzhljl8r87dpf2n5pnqnkl694casgns4ma0sqzd4zazzw13l";
+        libName = "aho_corasick";
+        authors = [
+          "Andrew Gallant <jamslam@gmail.com>"
+        ];
+        dependencies = [
+          {
+            name = "memchr";
+            packageId = "memchr";
+            usesDefaultFeatures = false;
+          }
+        ];
+        features = {
+          "default" = [ "std" ];
+          "std" = [ "memchr/use_std" ];
+        };
+        resolvedDefaultFeatures = [ "default" "std" ];
+      };
+      "lazy_static" = rec {
+        crateName = "lazy_static";
+        version = "1.4.0";
+        edition = "2015";
+        sha256 = "0in6ikhw8mgl33wjv6q6xfrb5b9jr16q8ygjy803fay4zcisvaz2";
+        authors = [
+          "Marvin Löbel <loebel.marvin@gmail.com>"
+        ];
+        features = {
+          "spin_no_std" = [ "spin" ];
+        };
+      };
+      "memchr" = rec {
+        crateName = "memchr";
+        version = "2.3.4";
+        edition = "2015";
+        sha256 = "098m9clfs495illlw00hv2gg67mhm7jflld3msyclvi5m9xc9q8f";
+        authors = [
+          "Andrew Gallant <jamslam@gmail.com>"
+          "bluss"
+        ];
+        features = {
+          "default" = [ "std" ];
+          "use_std" = [ "std" ];
+        };
+        resolvedDefaultFeatures = [ "default" "std" "use_std" ];
+      };
+      "regex" = rec {
+        crateName = "regex";
+        version = "1.4.2";
+        edition = "2015";
+        sha256 = "172bw2yryv65whn3n5vkww4kgk0bq08lx0zbln8xwia7xl9jrkrq";
+        authors = [
+          "The Rust Project Developers"
+        ];
+        dependencies = [
+          {
+            name = "aho-corasick";
+            packageId = "aho-corasick";
+            optional = true;
+          }
+          {
+            name = "memchr";
+            packageId = "memchr";
+            optional = true;
+          }
+          {
+            name = "regex-syntax";
+            packageId = "regex-syntax";
+            usesDefaultFeatures = false;
+          }
+          {
+            name = "thread_local";
+            packageId = "thread_local";
+            optional = true;
+          }
+        ];
+        features = {
+          "default" = [ "std" "perf" "unicode" "regex-syntax/default" ];
+          "perf" = [ "perf-cache" "perf-dfa" "perf-inline" "perf-literal" ];
+          "perf-cache" = [ "thread_local" ];
+          "perf-literal" = [ "aho-corasick" "memchr" ];
+          "unicode" = [ "unicode-age" "unicode-bool" "unicode-case" "unicode-gencat" "unicode-perl" "unicode-script" "unicode-segment" "regex-syntax/unicode" ];
+          "unicode-age" = [ "regex-syntax/unicode-age" ];
+          "unicode-bool" = [ "regex-syntax/unicode-bool" ];
+          "unicode-case" = [ "regex-syntax/unicode-case" ];
+          "unicode-gencat" = [ "regex-syntax/unicode-gencat" ];
+          "unicode-perl" = [ "regex-syntax/unicode-perl" ];
+          "unicode-script" = [ "regex-syntax/unicode-script" ];
+          "unicode-segment" = [ "regex-syntax/unicode-segment" ];
+          "unstable" = [ "pattern" ];
+          "use_std" = [ "std" ];
+        };
+        resolvedDefaultFeatures = [ "aho-corasick" "default" "memchr" "perf" "perf-cache" "perf-dfa" "perf-inline" "perf-literal" "std" "thread_local" "unicode" "unicode-age" "unicode-bool" "unicode-case" "unicode-gencat" "unicode-perl" "unicode-script" "unicode-segment" ];
+      };
+      "regex-syntax" = rec {
+        crateName = "regex-syntax";
+        version = "0.6.21";
+        edition = "2015";
+        sha256 = "12d176jkgw9749g07zjxz0n78nyvb2nqx3j4sp5aqyphvji1n61v";
+        authors = [
+          "The Rust Project Developers"
+        ];
+        features = {
+          "default" = [ "unicode" ];
+          "unicode" = [ "unicode-age" "unicode-bool" "unicode-case" "unicode-gencat" "unicode-perl" "unicode-script" "unicode-segment" ];
+        };
+        resolvedDefaultFeatures = [ "default" "unicode" "unicode-age" "unicode-bool" "unicode-case" "unicode-gencat" "unicode-perl" "unicode-script" "unicode-segment" ];
+      };
+      "thread_local" = rec {
+        crateName = "thread_local";
+        version = "1.0.1";
+        edition = "2015";
+        sha256 = "054vlrr1vsdy1h4b7n99mr24pnj8928ig9qwzg36wnkld4dns36l";
+        authors = [
+          "Amanieu d'Antras <amanieu@gmail.com>"
+        ];
+        dependencies = [
+          {
+            name = "lazy_static";
+            packageId = "lazy_static";
+          }
+        ];
+        
+      };
     };
 
     #
diff --git a/Cargo.toml b/Cargo.toml
index 2446b10..1caad48 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -2,4 +2,5 @@
 members = [
   "lib",
   "01",
+  "02",
 ]
diff --git a/lib/src/lib.rs b/lib/src/lib.rs
index 4b4b679..4913cbb 100644
--- a/lib/src/lib.rs
+++ b/lib/src/lib.rs
@@ -14,6 +14,29 @@ pub fn greeting() -> Result<()> {
   Ok(())
 }
 
+pub fn read_lines_file(filename: &str) -> Result<Vec<String>> {
+  let file = File::open(filename)?;
+  let mut reader = BufReader::new(file);
+  let mut buffer = String::new();
+
+  let mut input: Vec<String> = Vec::new();
+  loop {
+    reader.read_line(&mut buffer)?;
+    if buffer.len() == 0 {
+      break;
+    }
+
+    let mut line_copy = String::new();
+    line_copy.push_str(buffer.trim());
+    input.push(line_copy);
+
+    buffer.clear();
+  }
+
+  Ok(input)
+}
+
+
 pub fn read_int_file(filename: &str) -> Result<Vec<i64>> {
   let file = File::open(filename)?;
   let mut reader = BufReader::new(file);