summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--03/Cargo.toml8
-rw-r--r--03/src/main.rs67
-rw-r--r--Cargo.lock7
-rw-r--r--Cargo.toml1
4 files changed, 83 insertions, 0 deletions
diff --git a/03/Cargo.toml b/03/Cargo.toml
new file mode 100644
index 0000000..cffd384
--- /dev/null
+++ b/03/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "advent_03"
+version = "0.1.0"
+authors = ["Irene Knapp <ireneista@gmail.com>"]
+edition = "2018"
+
+[dependencies]
+advent_lib = { path = "../lib" }
diff --git a/03/src/main.rs b/03/src/main.rs
new file mode 100644
index 0000000..b8621da
--- /dev/null
+++ b/03/src/main.rs
@@ -0,0 +1,67 @@
+use advent_lib::prelude::*;
+
+
+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 mut trees: Vec<Vec<bool>> = Vec::new();
+
+  for line in &input {
+    let mut tree_line: Vec<bool> = Vec::new();
+
+    for c in line.trim().chars() {
+      if c == '#' {
+        tree_line.push(true);
+      } else {
+        tree_line.push(false);
+      }
+    }
+
+    trees.push(tree_line);
+  }
+
+  let mut product = 1;
+  product *= check_slope(&trees, 1, 1);
+  product *= check_slope(&trees, 3, 1);
+  product *= check_slope(&trees, 5, 1);
+  product *= check_slope(&trees, 7, 1);
+  product *= check_slope(&trees, 1, 2);
+
+  println!("product: {}", product);
+
+  Ok(())
+}
+
+
+fn check_slope(trees: &Vec<Vec<bool>>, dx: usize, dy: usize) -> usize {
+  let mut x = 0;
+  let mut y = 0;
+  let mut trees_hit = 0;
+  loop {
+    let x_adjusted = x % trees[y].len();
+    let is_tree = trees[y][x_adjusted];
+
+    if is_tree {
+      trees_hit += 1;
+    }
+
+    x += dx;
+    y += dy;
+
+    if y >= trees.len() {
+      break;
+    }
+  }
+
+  println!("dx: {}, dy: {}, trees: {}", dx, dy, trees_hit);
+
+  trees_hit
+}
+
diff --git a/Cargo.lock b/Cargo.lock
index 28aa121..41b113a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -16,6 +16,13 @@ dependencies = [
 ]
 
 [[package]]
+name = "advent_03"
+version = "0.1.0"
+dependencies = [
+ "advent_lib",
+]
+
+[[package]]
 name = "advent_lib"
 version = "0.1.0"
 
diff --git a/Cargo.toml b/Cargo.toml
index 1caad48..3f08bba 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -3,4 +3,5 @@ members = [
   "lib",
   "01",
   "02",
+  "03",
 ]