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"
dependencies = [
"aoc_libs",
"once_cell",
"regex",
]
[[package]]

View file

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

View file

@ -12,6 +12,6 @@ fn main() {
println!("Part One");
println!("Result: {}", part1::part1(&structured_input));
// println!("Part Two");
// println!("Result: {}", part2::part2(&structured_input));
println!("Part Two");
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)]
pub enum Direction {
Up,
@ -6,18 +9,25 @@ pub enum Direction {
Left,
}
static PARSE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^([URDL]) (\d+)$").unwrap());
pub fn parse(input: &str) -> Vec<Direction> {
let mut ret = Vec::new();
for line in input.lines() {
let mut chars = line.chars();
let dir = match chars.next().unwrap() {
'U' => Direction::Up,
'R' => Direction::Right,
'D' => Direction::Down,
'L' => Direction::Left,
let captures = PARSE_REGEX
.captures(line)
.unwrap_or_else(|| panic!("invalid line {}", line));
let dir = match &captures[1] {
"U" => Direction::Up,
"R" => Direction::Right,
"D" => Direction::Down,
"L" => Direction::Left,
_ => 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)
}
}

View file

@ -2,25 +2,30 @@ use std::collections::HashSet;
use aoc_libs::points::Point;
use crate::{rope::Rope, parse::Direction};
use crate::{parse::Direction, rope::Rope};
pub fn part2(input: &Vec<Direction>) -> usize {
let mut visited: HashSet<Point> = HashSet::new();
let mut rope: Rope<10> = Rope::new();
for direction in input {
visited.insert(*rope.get_tail_pos());
for direction in input {
rope.update_rope(direction);
println!("{}", rope)
visited.insert(*rope.get_tail_pos());
}
visited.len()
}
#[cfg(test)]
mod tests {
use crate::parse::parse;
use super::*;
#[test]
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::Left => Point { x: -1, y: 0 },
};
println!("direction is {:?}", direction);
for segment in 1..self.segments.len() {
self.segments[segment] += Rope::<L>::update_single_segment_pair(
&self.segments[segment - 1],
&self.segments[segment],
)
}
println!("{}", self);
}
pub fn get_tail_pos(&self) -> &Point {
@ -138,9 +136,11 @@ mod tests {
let mut visited: HashSet<Point> = HashSet::new();
let mut rope: Rope<2> = Rope::new();
visited.insert(*rope.get_tail_pos());
println!("{}", rope);
for direction in input {
rope.update_rope(&direction);
visited.insert(*rope.get_tail_pos());
println!("{}", rope);
}
// let mut graph = [
// ['.', '.', '.', '.', '.', '.'],