This commit is contained in:
Gabe Venberg 2023-11-22 17:00:27 -06:00
parent dfc41a1167
commit 3b18b90bda
6 changed files with 35 additions and 16 deletions

2
Cargo.lock generated
View file

@ -65,6 +65,8 @@ name = "day09"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"aoc_libs", "aoc_libs",
"once_cell",
"regex",
] ]
[[package]] [[package]]

View file

@ -7,3 +7,5 @@ edition.workspace = true
[dependencies] [dependencies]
aoc_libs.workspace = true aoc_libs.workspace = true
regex.workspace = true
once_cell.workspace = true

View file

@ -12,6 +12,6 @@ fn main() {
println!("Part One"); println!("Part One");
println!("Result: {}", part1::part1(&structured_input)); println!("Result: {}", part1::part1(&structured_input));
// println!("Part Two"); println!("Part Two");
// println!("Result: {}", part2::part2(&structured_input)); println!("Result: {}", part2::part2(&structured_input));
} }

View file

@ -1,3 +1,6 @@
use once_cell::sync::Lazy;
use regex::Regex;
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction { pub enum Direction {
Up, Up,
@ -6,18 +9,25 @@ pub enum Direction {
Left, Left,
} }
static PARSE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^([URDL]) (\d+)$").unwrap());
pub fn parse(input: &str) -> Vec<Direction> { pub fn parse(input: &str) -> Vec<Direction> {
let mut ret = Vec::new(); let mut ret = Vec::new();
for line in input.lines() { for line in input.lines() {
let mut chars = line.chars(); let captures = PARSE_REGEX
let dir = match chars.next().unwrap() { .captures(line)
'U' => Direction::Up, .unwrap_or_else(|| panic!("invalid line {}", line));
'R' => Direction::Right, let dir = match &captures[1] {
'D' => Direction::Down, "U" => Direction::Up,
'L' => Direction::Left, "R" => Direction::Right,
"D" => Direction::Down,
"L" => Direction::Left,
_ => panic!("invalid direction char"), _ => panic!("invalid direction char"),
}; };
for _ in 0..chars.nth(1).unwrap().to_digit(10).unwrap() { for _ in 0..captures[2]
.parse()
.unwrap_or_else(|_| panic!("invalid line {}", line))
{
ret.push(dir) ret.push(dir)
} }
} }

View file

@ -2,25 +2,30 @@ use std::collections::HashSet;
use aoc_libs::points::Point; use aoc_libs::points::Point;
use crate::{rope::Rope, parse::Direction}; use crate::{parse::Direction, rope::Rope};
pub fn part2(input: &Vec<Direction>) -> usize { pub fn part2(input: &Vec<Direction>) -> usize {
let mut visited: HashSet<Point> = HashSet::new(); let mut visited: HashSet<Point> = HashSet::new();
let mut rope: Rope<10> = Rope::new(); let mut rope: Rope<10> = Rope::new();
for direction in input {
visited.insert(*rope.get_tail_pos()); visited.insert(*rope.get_tail_pos());
for direction in input {
rope.update_rope(direction); rope.update_rope(direction);
println!("{}", rope) visited.insert(*rope.get_tail_pos());
} }
visited.len() visited.len()
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::parse::parse;
use super::*; use super::*;
#[test] #[test]
fn test_part2() { fn test_part2() {
assert_eq!(0, 0); let input = parse(concat!(
"R 5\n", "U 8\n", "L 8\n", "D 3\n", "R 17\n", "D 10\n", "L 25\n", "U 20\n",
));
assert_eq!(part2(&input), 36);
} }
} }

View file

@ -23,14 +23,12 @@ impl<const L: usize> Rope<L> {
Direction::Down => Point { x: 0, y: -1 }, Direction::Down => Point { x: 0, y: -1 },
Direction::Left => Point { x: -1, y: 0 }, Direction::Left => Point { x: -1, y: 0 },
}; };
println!("direction is {:?}", direction);
for segment in 1..self.segments.len() { for segment in 1..self.segments.len() {
self.segments[segment] += Rope::<L>::update_single_segment_pair( self.segments[segment] += Rope::<L>::update_single_segment_pair(
&self.segments[segment - 1], &self.segments[segment - 1],
&self.segments[segment], &self.segments[segment],
) )
} }
println!("{}", self);
} }
pub fn get_tail_pos(&self) -> &Point { pub fn get_tail_pos(&self) -> &Point {
@ -77,7 +75,7 @@ impl<const L: usize> Display for Rope<{ L }> {
} }
for segment in self.segments.iter().skip(1) { for segment in self.segments.iter().skip(1) {
let delta = *segment-self.segments[0]; let delta = *segment - self.segments[0];
let upoint = delta.to_upoint(&zero_point).unwrap(); let upoint = delta.to_upoint(&zero_point).unwrap();
grid[upoint.y][upoint.x] = 'T' grid[upoint.y][upoint.x] = 'T'
} }
@ -138,9 +136,11 @@ mod tests {
let mut visited: HashSet<Point> = HashSet::new(); let mut visited: HashSet<Point> = HashSet::new();
let mut rope: Rope<2> = Rope::new(); let mut rope: Rope<2> = Rope::new();
visited.insert(*rope.get_tail_pos()); visited.insert(*rope.get_tail_pos());
println!("{}", rope);
for direction in input { for direction in input {
rope.update_rope(&direction); rope.update_rope(&direction);
visited.insert(*rope.get_tail_pos()); visited.insert(*rope.get_tail_pos());
println!("{}", rope);
} }
// let mut graph = [ // let mut graph = [
// ['.', '.', '.', '.', '.', '.'], // ['.', '.', '.', '.', '.', '.'],