that *should* be day 9, and the example passes,
but the real input is off!!!
This commit is contained in:
parent
8fcc676be0
commit
dfc41a1167
12 changed files with 2422 additions and 49 deletions
|
@ -3,3 +3,7 @@ name = "day09"
|
|||
authors.workspace = true
|
||||
description.workspace = true
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
|
||||
[dependencies]
|
||||
aoc_libs.workspace = true
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -10,8 +10,8 @@ fn main() {
|
|||
let structured_input = parse::parse(input);
|
||||
|
||||
println!("Part One");
|
||||
println!("Result: {}", part1::part1());
|
||||
println!("Result: {}", part1::part1(&structured_input));
|
||||
|
||||
println!("Part Two");
|
||||
println!("Result: {}", part2::part2());
|
||||
// println!("Part Two");
|
||||
// println!("Result: {}", part2::part2(&structured_input));
|
||||
}
|
||||
|
|
|
@ -1,8 +1,19 @@
|
|||
//TODO:
|
||||
#![allow(unused)]
|
||||
use std::collections::HashSet;
|
||||
|
||||
pub fn part1() -> usize {
|
||||
unimplemented!()
|
||||
use aoc_libs::points::Point;
|
||||
|
||||
use crate::parse::Direction;
|
||||
use crate::rope::Rope;
|
||||
|
||||
pub fn part1(input: &Vec<Direction>) -> usize {
|
||||
let mut visited: HashSet<Point> = HashSet::new();
|
||||
let mut rope: Rope<2> = Rope::new();
|
||||
visited.insert(*rope.get_tail_pos());
|
||||
for direction in input {
|
||||
rope.update_rope(direction);
|
||||
visited.insert(*rope.get_tail_pos());
|
||||
}
|
||||
visited.len()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -11,6 +22,32 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_part1() {
|
||||
assert_eq!(0, 0);
|
||||
let input = vec![
|
||||
Direction::Right,
|
||||
Direction::Right,
|
||||
Direction::Right,
|
||||
Direction::Right,
|
||||
Direction::Up,
|
||||
Direction::Up,
|
||||
Direction::Up,
|
||||
Direction::Up,
|
||||
Direction::Left,
|
||||
Direction::Left,
|
||||
Direction::Left,
|
||||
Direction::Down,
|
||||
Direction::Right,
|
||||
Direction::Right,
|
||||
Direction::Right,
|
||||
Direction::Right,
|
||||
Direction::Down,
|
||||
Direction::Left,
|
||||
Direction::Left,
|
||||
Direction::Left,
|
||||
Direction::Left,
|
||||
Direction::Left,
|
||||
Direction::Right,
|
||||
Direction::Right,
|
||||
];
|
||||
assert_eq!(part1(&input), 13);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,18 @@
|
|||
//TODO:
|
||||
#![allow(unused)]
|
||||
use std::collections::HashSet;
|
||||
|
||||
pub fn part2() -> usize {
|
||||
unimplemented!()
|
||||
use aoc_libs::points::Point;
|
||||
|
||||
use crate::{rope::Rope, parse::Direction};
|
||||
|
||||
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());
|
||||
rope.update_rope(direction);
|
||||
println!("{}", rope)
|
||||
}
|
||||
visited.len()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,53 +1,48 @@
|
|||
use std::fmt::Display;
|
||||
|
||||
use crate::parse::Direction;
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct Point {
|
||||
x: isize,
|
||||
y: isize,
|
||||
}
|
||||
|
||||
impl std::ops::Add for Point {
|
||||
type Output = Point;
|
||||
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
Self {
|
||||
x: self.x + rhs.x,
|
||||
y: self.y + rhs.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Sub for Point {
|
||||
type Output = Point;
|
||||
|
||||
fn sub(self, rhs: Self) -> Self::Output {
|
||||
Self {
|
||||
x: self.x - rhs.x,
|
||||
y: self.y - rhs.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
use aoc_libs::points::{Point, UPoint};
|
||||
|
||||
// L is the length of the rope in segments.
|
||||
#[derive(Debug)]
|
||||
struct Rope<const L: usize> {
|
||||
pub struct Rope<const L: usize> {
|
||||
segments: [Point; L],
|
||||
}
|
||||
|
||||
impl<const L: usize> Rope<L> {
|
||||
pub fn new()->Rope{
|
||||
Rope { segments: () }
|
||||
pub fn new() -> Rope<L> {
|
||||
Rope {
|
||||
segments: [Point::default(); L],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_rope(&mut self, direction: Direction) {
|
||||
todo!()
|
||||
pub fn update_rope(&mut self, direction: &Direction) {
|
||||
self.segments[0] += match direction {
|
||||
Direction::Up => Point { x: 0, y: 1 },
|
||||
Direction::Right => Point { x: 1, y: 0 },
|
||||
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{
|
||||
&self.segments[self.segments.len()-1]
|
||||
pub fn get_tail_pos(&self) -> &Point {
|
||||
&self.segments[self.segments.len() - 1]
|
||||
}
|
||||
|
||||
fn update_single_segment_pair(head: Point, tail: Point) -> Point {
|
||||
let delta = head - tail;
|
||||
// the rope segment will not move if the segment ahead of it is only at most one away, and will
|
||||
// move with the following rules if it is 2 away: It moves straight towards the head if the
|
||||
// head is directly up/down/left/right, and diagonally towards it if its not straight
|
||||
// up/down/left/right.
|
||||
fn update_single_segment_pair(head: &Point, tail: &Point) -> Point {
|
||||
let delta = *head - *tail;
|
||||
if delta.x.abs() > 2 || delta.y.abs() > 2 {
|
||||
panic!("invalid delta ({}, {})", delta.y, delta.x)
|
||||
}
|
||||
|
@ -64,3 +59,102 @@ impl<const L: usize> Rope<L> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<const L: usize> Display for Rope<{ L }> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let grid_size = (self.segments.len() * 2) - 1;
|
||||
let zero_point = UPoint {
|
||||
x: self.segments.len() - 1,
|
||||
y: self.segments.len() - 1,
|
||||
};
|
||||
|
||||
let mut grid: Vec<Vec<char>> = Vec::with_capacity(grid_size);
|
||||
for y in 0..grid_size {
|
||||
grid.push(Vec::with_capacity(grid_size));
|
||||
for _ in 0..grid_size {
|
||||
grid[y].push('.')
|
||||
}
|
||||
}
|
||||
|
||||
for segment in self.segments.iter().skip(1) {
|
||||
let delta = *segment-self.segments[0];
|
||||
let upoint = delta.to_upoint(&zero_point).unwrap();
|
||||
grid[upoint.y][upoint.x] = 'T'
|
||||
}
|
||||
|
||||
writeln!(
|
||||
f,
|
||||
"head is at {}, {}",
|
||||
self.segments[0].x, self.segments[0].y
|
||||
)?;
|
||||
let upoint = Point { x: 0, y: 0 }.to_upoint(&zero_point).unwrap();
|
||||
grid[upoint.y][upoint.x] = 'H';
|
||||
|
||||
for line in grid {
|
||||
let mut writeline = "".to_string();
|
||||
for char in line {
|
||||
writeline.push(char)
|
||||
}
|
||||
writeln!(f, "{}", writeline)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::HashSet;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_single_segment() {
|
||||
let input = [
|
||||
Direction::Right,
|
||||
Direction::Right,
|
||||
Direction::Right,
|
||||
Direction::Right,
|
||||
Direction::Up,
|
||||
Direction::Up,
|
||||
Direction::Up,
|
||||
Direction::Up,
|
||||
Direction::Left,
|
||||
Direction::Left,
|
||||
Direction::Left,
|
||||
Direction::Down,
|
||||
Direction::Right,
|
||||
Direction::Right,
|
||||
Direction::Right,
|
||||
Direction::Right,
|
||||
Direction::Down,
|
||||
Direction::Left,
|
||||
Direction::Left,
|
||||
Direction::Left,
|
||||
Direction::Left,
|
||||
Direction::Left,
|
||||
Direction::Right,
|
||||
Direction::Right,
|
||||
];
|
||||
let mut visited: HashSet<Point> = HashSet::new();
|
||||
let mut rope: Rope<2> = Rope::new();
|
||||
visited.insert(*rope.get_tail_pos());
|
||||
for direction in input {
|
||||
rope.update_rope(&direction);
|
||||
visited.insert(*rope.get_tail_pos());
|
||||
}
|
||||
// let mut graph = [
|
||||
// ['.', '.', '.', '.', '.', '.'],
|
||||
// ['.', '.', '.', '.', '.', '.'],
|
||||
// ['.', '.', '.', '.', '.', '.'],
|
||||
// ['.', '.', '.', '.', '.', '.'],
|
||||
// ['s', '.', '.', '.', '.', '.'],
|
||||
// ];
|
||||
// for point in &visited {
|
||||
// graph[4 - point.y as usize][point.x as usize] = '#';
|
||||
// }
|
||||
// for line in graph {
|
||||
// println!("{:?}", line)
|
||||
// }
|
||||
assert_eq!(visited.len(), 13)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue