From 017345beb681926d630d200275078b4005d78fb7 Mon Sep 17 00:00:00 2001 From: Dory Date: Mon, 11 Mar 2024 23:57:48 -0700 Subject: [PATCH] d9p2 --- day9/src/main.rs | 50 ++++++++++++++++++++++++++++------------------- day9/src/main1.rs | 39 ++++++++++++++++++++++++++++++++++++ day9/test2.txt | 8 ++++++++ 3 files changed, 77 insertions(+), 20 deletions(-) create mode 100644 day9/src/main1.rs create mode 100644 day9/test2.txt diff --git a/day9/src/main.rs b/day9/src/main.rs index 637786b..a18f4f7 100644 --- a/day9/src/main.rs +++ b/day9/src/main.rs @@ -6,34 +6,44 @@ use std::collections::HashSet; fn main() { let file = File::open(&env::args().nth(1).unwrap()).unwrap(); let reader = io::BufReader::new(file); - let (mut hx, mut hy, mut tx, mut ty) = (0, 0, 0, 0); + let mut rope: [(i64, i64); 10] = [(0, 0); 10]; let mut visited: HashSet<(i64, i64)> = vec![(0, 0)].into_iter().collect(); for line in reader.lines().flatten() { - let (movx, movy) = match line.chars().nth(0) { - Some('L') => (-1, 0), - Some('R') => (1, 0), - Some('U') => (0, -1), - Some('D') => (0, 1), - _ => (999, 999), - }; - let count = line[2..].parse::().unwrap(); + let ((movx, movy), count) = parse_line(line).unwrap(); for _ in 0..count { - (hx, hy) = (hx + movx, hy + movy); - (tx, ty) = move_tail(hx, hy, tx, ty).unwrap(); - visited.insert((tx, ty)); + rope[0] = (rope[0].0 + movx, rope[0].1 + movy); + for link in 1..=9 { + rope[link] = move_tail(rope[link - 1], rope[link]); + } + visited.insert(rope[9]); } } println!("{:?}", visited.len()); } -fn move_tail(hx: i64, hy: i64, tx: i64, ty: i64) -> Option<(i64, i64)> { - match (hx - tx, hy - ty) { - (-1..=1, -1..=1) => Some((tx, ty)), - (-1..=1, 2) => Some((hx, ty + 1)), - (-1..=1, -2) => Some((hx, ty - 1)), - (2, -1..=1) => Some((tx + 1, hy)), - (-2, -1..=1) => Some((tx - 1, hy)), - _ => None +fn parse_line(line: String) -> Option<((i64, i64), i64)> { + let mov = match line.chars().nth(0) { + Some('L') => Some((-1, 0)), + Some('R') => Some((1, 0)), + Some('U') => Some((0, -1)), + Some('D') => Some((0, 1)), + _ => None, + }; + let count = line[2..].parse::(); + match (mov, count) { + (Some(mov), Ok(count)) => Some((mov, count)), + _ => None, + } +} + +fn move_tail((hx, hy): (i64, i64), (tx, ty): (i64, i64)) -> (i64, i64) { + let dx = if hx > tx { 1 } else { -1 }; + let dy = if hy > ty { 1 } else { -1 }; + match (hx - tx, hy - ty) { + (-1..=1, -1..=1) => (tx, ty), + (-1..=1, _) => (hx, ty + dy), + (_, -1..=1) => (tx + dx, hy), + (_, _) => (tx + dx, ty + dy), } } diff --git a/day9/src/main1.rs b/day9/src/main1.rs new file mode 100644 index 0000000..637786b --- /dev/null +++ b/day9/src/main1.rs @@ -0,0 +1,39 @@ +use std::env; +use std::fs::File; +use std::io::{self, BufRead}; +use std::collections::HashSet; + +fn main() { + let file = File::open(&env::args().nth(1).unwrap()).unwrap(); + let reader = io::BufReader::new(file); + let (mut hx, mut hy, mut tx, mut ty) = (0, 0, 0, 0); + let mut visited: HashSet<(i64, i64)> = vec![(0, 0)].into_iter().collect(); + + for line in reader.lines().flatten() { + let (movx, movy) = match line.chars().nth(0) { + Some('L') => (-1, 0), + Some('R') => (1, 0), + Some('U') => (0, -1), + Some('D') => (0, 1), + _ => (999, 999), + }; + let count = line[2..].parse::().unwrap(); + for _ in 0..count { + (hx, hy) = (hx + movx, hy + movy); + (tx, ty) = move_tail(hx, hy, tx, ty).unwrap(); + visited.insert((tx, ty)); + } + } + println!("{:?}", visited.len()); +} + +fn move_tail(hx: i64, hy: i64, tx: i64, ty: i64) -> Option<(i64, i64)> { + match (hx - tx, hy - ty) { + (-1..=1, -1..=1) => Some((tx, ty)), + (-1..=1, 2) => Some((hx, ty + 1)), + (-1..=1, -2) => Some((hx, ty - 1)), + (2, -1..=1) => Some((tx + 1, hy)), + (-2, -1..=1) => Some((tx - 1, hy)), + _ => None + } +} diff --git a/day9/test2.txt b/day9/test2.txt new file mode 100644 index 0000000..60bd43b --- /dev/null +++ b/day9/test2.txt @@ -0,0 +1,8 @@ +R 5 +U 8 +L 8 +D 3 +R 17 +D 10 +L 25 +U 20