summaryrefslogtreecommitdiff
path: root/src/day9.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/day9.rs')
-rw-r--r--src/day9.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/day9.rs b/src/day9.rs
new file mode 100644
index 0000000..ac21e4d
--- /dev/null
+++ b/src/day9.rs
@@ -0,0 +1,56 @@
+use core::panic;
+use std::collections::HashSet;
+
+type Pt = (i32, i32);
+/**
+ * how should the tail move to catch up with the head?
+ */
+fn catchup(head: Pt, tail: Pt) -> Pt {
+ let (hx, hy) = head;
+ let (tx, ty) = tail;
+ let xdiff = hx - tx;
+ let ydiff = hy - ty;
+ // only move if we're not adjescent (incl diagonally)
+ if xdiff.abs() > 1 || ydiff.abs() > 1 {
+ // only move 1 square (x and or y) in the direction of the head
+ (tail.0 + xdiff.signum(), tail.1 + ydiff.signum())
+ } else {
+ (tail.0, tail.1)
+ }
+
+}
+pub fn run(input: String) {
+ // series of steps
+ // let mut head: Pt = (0,0);
+ // let mut tail: Pt = (0,0);
+ // let mut htrail: Vec<Pt> = Vec::new();
+ let mut rope: Vec<Pt> = (0..10).map(|_| { (0,0) as Pt}).collect();
+ let mut trail: Vec<Pt> = Vec::new();
+ for line in input.lines() {
+ let mut toks = line.split_whitespace();
+ let dir: &str = toks.next().expect("expected a direction!");
+ let reps: u32 = toks.next().expect("expected a reps!").parse().expect("reps wasn't a number!");
+ for _ in 0..reps {
+ let head = rope[0];
+ let newhead = match dir {
+ "U" => (head.0, head.1+1),
+ "D" => (head.0, head.1-1),
+ "L" => (head.0-1, head.1),
+ "R" => (head.0+1, head.1),
+ dir => panic!("unexpected direction {}", dir),
+ };
+ rope[0] = newhead;
+ for l in 1..rope.len() {
+ rope[l] = catchup(rope[l-1], rope[l]);
+ }
+ let tail = rope.last().unwrap();
+ // htrail.push(head);
+ // tail = catchup(head, tail);
+ trail.push(*tail);
+ }
+ }
+ //println!("htrail {:?}", htrail);
+ //println!("trail {:?}", trail);
+ let trailsz = trail.into_iter().collect::<HashSet<Pt>>().len();
+ println!("Day 9: {}", trailsz);
+} \ No newline at end of file