Skip to content
Snippets Groups Projects
Commit 09b99777 authored by Falk Rehse's avatar Falk Rehse
Browse files

Day 9-1

parent 53613b34
No related branches found
No related tags found
No related merge requests found
pub enum Direction {
Up,
Down,
Left,
Right,
}
impl Direction {
pub fn from_str(input: &str) -> Direction {
match input {
"U" => Self::Up,
"D" => Self::Down,
"L" => Self::Left,
"R" => Self::Right,
_ => panic!("Unexpected direction!"),
}
}
}
mod direction;
mod movement;
use direction::Direction;
use movement::Movement;
pub fn solve_from_str(input: &str) -> u32 {
let moves = input
.split('\n')
.map(|movement| Movement::from_str(movement))
.collect();
solve(&moves)
}
pub fn solve(moves: &Vec<Movement>) -> u32 {
let mut visited = Vec::new();
visited.push((0, 0));
let mut head = (0, 0);
let mut tail = (0, 0);
for movement in moves {
for _ in 0..movement.amount {
move_head(&mut head, &movement.direction);
move_tail(&mut tail, &head);
if !visited.contains(&tail) {
visited.push(tail);
}
// visited.push(tail);
}
}
// visited.dedup();
visited.len() as u32
}
fn move_head(head: &mut (i32, i32), direction: &Direction) {
match direction {
Direction::Up => head.1 += 1,
Direction::Down => head.1 -= 1,
Direction::Left => head.0 -= 1,
Direction::Right => head.0 += 1,
}
}
fn move_tail(tail: &mut (i32, i32), head: &(i32, i32)) {
if tail.0 == head.0 - 2 {
if tail.1 == head.1 - 1 {
tail.0 += 1;
tail.1 += 1;
} else if tail.1 == head.1 {
tail.0 += 1;
} else if tail.1 == head.1 + 1 {
tail.0 += 1;
tail.1 -= 1;
} else {
panic!("Head lost!");
}
} else if tail.0 == head.0 - 1 {
if tail.1 == head.1 - 2 {
tail.0 += 1;
tail.1 += 1;
} else if tail.1 == head.1 - 1 {
} else if tail.1 == head.1 {
} else if tail.1 == head.1 + 1 {
} else if tail.1 == head.1 + 2 {
tail.0 += 1;
tail.1 -= 1;
} else {
panic!("Head lost!");
}
} else if tail.0 == head.0 {
if tail.1 == head.1 - 2 {
tail.1 += 1;
} else if tail.1 == head.1 - 1 {
} else if tail.1 == head.1 {
} else if tail.1 == head.1 + 1 {
} else if tail.1 == head.1 + 2 {
tail.1 -= 1;
} else {
panic!("Head lost!");
}
} else if tail.0 == head.0 + 1 {
if tail.1 == head.1 - 2 {
tail.0 -= 1;
tail.1 += 1;
} else if tail.1 == head.1 - 1 {
} else if tail.1 == head.1 {
} else if tail.1 == head.1 + 1 {
} else if tail.1 == head.1 + 2 {
tail.0 -= 1;
tail.1 -= 1;
} else {
panic!("Head lost!");
}
} else if tail.0 == head.0 + 2 {
if tail.1 == head.1 - 1 {
tail.0 -= 1;
tail.1 += 1;
} else if tail.1 == head.1 {
tail.0 -= 1;
} else if tail.1 == head.1 + 1 {
tail.0 -= 1;
tail.1 -= 1;
} else {
panic!("Head lost!");
}
} else {
panic!("Head lost!");
}
}
use crate::day_9_1::direction::Direction;
pub struct Movement {
pub direction: Direction,
pub amount: u32,
}
impl Movement {
pub fn from_str(input: &str) -> Movement {
let mut data = input.split(' ');
let direction = Direction::from_str(data.next().expect("Expected direction!"));
let amount = data.next().expect("Expected amount!").parse().expect("Expected amount to be a number!");
Movement { direction, amount }
}
}
...@@ -12,13 +12,14 @@ mod day_6_1; ...@@ -12,13 +12,14 @@ mod day_6_1;
mod day_6_2; mod day_6_2;
mod day_8_1; mod day_8_1;
mod day_8_2; mod day_8_2;
mod day_9_1;
use std::fs; use std::fs;
fn main() { fn main() {
let input = fs::read_to_string("input").expect("Could not read input!"); let input = fs::read_to_string("input").expect("Could not read input!");
let solution = day_8_2::solve_from_str(input.as_str()); let solution = day_9_1::solve_from_str(input.as_str());
println!("Solution: {}", solution); println!("Solution: {}", solution);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment