day09 in progress.
This commit is contained in:
		
							parent
							
								
									242989bb95
								
							
						
					
					
						commit
						8fcc676be0
					
				
					 8 changed files with 189 additions and 0 deletions
				
			
		
							
								
								
									
										4
									
								
								Cargo.lock
									
										
									
										generated
									
									
									
								
							
							
						
						
									
										4
									
								
								Cargo.lock
									
										
									
										generated
									
									
									
								
							| 
						 | 
					@ -56,6 +56,10 @@ dependencies = [
 | 
				
			||||||
name = "day08"
 | 
					name = "day08"
 | 
				
			||||||
version = "0.1.0"
 | 
					version = "0.1.0"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[[package]]
 | 
				
			||||||
 | 
					name = "day09"
 | 
				
			||||||
 | 
					version = "0.1.0"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[[package]]
 | 
					[[package]]
 | 
				
			||||||
name = "memchr"
 | 
					name = "memchr"
 | 
				
			||||||
version = "2.6.4"
 | 
					version = "2.6.4"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										5
									
								
								days/day09/Cargo.toml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								days/day09/Cargo.toml
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,5 @@
 | 
				
			||||||
 | 
					[package]
 | 
				
			||||||
 | 
					name = "day09"
 | 
				
			||||||
 | 
					authors.workspace = true
 | 
				
			||||||
 | 
					description.workspace = true
 | 
				
			||||||
 | 
					version.workspace = true
 | 
				
			||||||
							
								
								
									
										0
									
								
								days/day09/src/input.txt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								days/day09/src/input.txt
									
										
									
									
									
										Normal file
									
								
							
							
								
								
									
										17
									
								
								days/day09/src/main.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								days/day09/src/main.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,17 @@
 | 
				
			||||||
 | 
					//TODO:
 | 
				
			||||||
 | 
					#![allow(dead_code)]
 | 
				
			||||||
 | 
					mod part1;
 | 
				
			||||||
 | 
					mod part2;
 | 
				
			||||||
 | 
					mod parse;
 | 
				
			||||||
 | 
					mod rope;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn main() {
 | 
				
			||||||
 | 
					    let input = include_str!("./input.txt");
 | 
				
			||||||
 | 
					    let structured_input = parse::parse(input);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    println!("Part One");
 | 
				
			||||||
 | 
					    println!("Result: {}", part1::part1());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    println!("Part Two");
 | 
				
			||||||
 | 
					    println!("Result: {}", part2::part2());
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										65
									
								
								days/day09/src/parse.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								days/day09/src/parse.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,65 @@
 | 
				
			||||||
 | 
					#[derive(Debug, Clone, Copy, PartialEq, Eq)]
 | 
				
			||||||
 | 
					pub enum Direction {
 | 
				
			||||||
 | 
					    Up,
 | 
				
			||||||
 | 
					    Right,
 | 
				
			||||||
 | 
					    Down,
 | 
				
			||||||
 | 
					    Left,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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,
 | 
				
			||||||
 | 
					            _ => panic!("invalid direction char"),
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					        for _ in 0..chars.nth(1).unwrap().to_digit(10).unwrap() {
 | 
				
			||||||
 | 
					            ret.push(dir)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    ret
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[cfg(test)]
 | 
				
			||||||
 | 
					mod tests {
 | 
				
			||||||
 | 
					    use super::*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #[test]
 | 
				
			||||||
 | 
					    fn test_parse() {
 | 
				
			||||||
 | 
					        let input =
 | 
				
			||||||
 | 
					            concat!("R 4\n", "U 4\n", "L 3\n", "D 1\n", "R 4\n", "D 1\n", "L 5\n", "R 2\n",);
 | 
				
			||||||
 | 
					        assert_eq!(
 | 
				
			||||||
 | 
					            parse(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,
 | 
				
			||||||
 | 
					            ]
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										16
									
								
								days/day09/src/part1.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								days/day09/src/part1.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,16 @@
 | 
				
			||||||
 | 
					//TODO:
 | 
				
			||||||
 | 
					#![allow(unused)]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn part1() -> usize {
 | 
				
			||||||
 | 
					    unimplemented!()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[cfg(test)]
 | 
				
			||||||
 | 
					mod tests {
 | 
				
			||||||
 | 
					    use super::*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #[test]
 | 
				
			||||||
 | 
					    fn test_part1() {
 | 
				
			||||||
 | 
					        assert_eq!(0, 0);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										16
									
								
								days/day09/src/part2.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								days/day09/src/part2.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,16 @@
 | 
				
			||||||
 | 
					//TODO:
 | 
				
			||||||
 | 
					#![allow(unused)]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn part2() -> usize {
 | 
				
			||||||
 | 
					    unimplemented!()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[cfg(test)]
 | 
				
			||||||
 | 
					mod tests {
 | 
				
			||||||
 | 
					    use super::*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #[test]
 | 
				
			||||||
 | 
					    fn test_part2() {
 | 
				
			||||||
 | 
					        assert_eq!(0, 0);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										66
									
								
								days/day09/src/rope.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								days/day09/src/rope.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,66 @@
 | 
				
			||||||
 | 
					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,
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// L is the length of the rope in segments.
 | 
				
			||||||
 | 
					#[derive(Debug)]
 | 
				
			||||||
 | 
					struct Rope<const L: usize> {
 | 
				
			||||||
 | 
					    segments: [Point; L],
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl<const L: usize> Rope<L> {
 | 
				
			||||||
 | 
					    pub fn new()->Rope{
 | 
				
			||||||
 | 
					        Rope { segments: () }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    pub fn update_rope(&mut self, direction: Direction) {
 | 
				
			||||||
 | 
					        todo!()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    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;
 | 
				
			||||||
 | 
					        if delta.x.abs() > 2 || delta.y.abs() > 2 {
 | 
				
			||||||
 | 
					            panic!("invalid delta ({}, {})", delta.y, delta.x)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        match (delta.x, delta.y) {
 | 
				
			||||||
 | 
					            (0, 2) => Point { x: 0, y: 1 },
 | 
				
			||||||
 | 
					            (2, 0) => Point { x: 1, y: 0 },
 | 
				
			||||||
 | 
					            (0, -2) => Point { x: 0, y: -1 },
 | 
				
			||||||
 | 
					            (-2, 0) => Point { x: -1, y: 0 },
 | 
				
			||||||
 | 
					            (1, 2) | (2, 2) | (2, 1) => Point { x: 1, y: 1 },
 | 
				
			||||||
 | 
					            (2, -1) | (2, -2) | (1, -2) => Point { x: 1, y: -1 },
 | 
				
			||||||
 | 
					            (-1, -2) | (-2, -2) | (-2, -1) => Point { x: -1, y: -1 },
 | 
				
			||||||
 | 
					            (-2, 1) | (-2, 2) | (-1, 2) => Point { x: -1, y: 1 },
 | 
				
			||||||
 | 
					            _ => Point { x: 0, y: 0 },
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue