Skip to content

Commit f639008

Browse files
committed
Part 2 solved
1 parent 8328c33 commit f639008

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

src/day_09.rs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,23 @@ impl Eq for Position {}
4444

4545
fn move_head(head: &Position, direction: &Direction) -> Position {
4646
match direction {
47-
Direction::U => return Position { x: head.x + 1, y: head.y },
48-
Direction::D => return Position { x: head.x - 1, y: head.y },
49-
Direction::L => return Position { x: head.x, y: head.y - 1 },
50-
Direction::R => return Position { x: head.x, y: head.y + 1 }
47+
Direction::U => return Position { x: head.x, y: head.y + 1 },
48+
Direction::D => return Position { x: head.x, y: head.y - 1 },
49+
Direction::L => return Position { x: head.x - 1, y: head.y },
50+
Direction::R => return Position { x: head.x + 1, y: head.y }
5151
}
5252
}
5353

5454
fn follow_head(head: &Position, tail: &Position) -> Position {
55-
if (head.x - tail.x).abs() >= 2 {
56-
// Two steps left or right...
57-
if head.x > tail.x {
58-
return Position { x: head.x - 1, y: head.y }
55+
let dx = head.x - tail.x;
56+
let dy = head.y - tail.y;
57+
if dx.abs() + dy.abs() >= 3 || dx.abs() >= 2 || dy.abs() >= 2 {
58+
if dx.abs() == dy.abs() {
59+
return Position { x: head.x - dx.signum(), y: head.y - dy.signum() }
60+
} else if dx.abs() > dy.abs() {
61+
return Position { x: head.x - dx.signum(), y: head.y }
5962
} else {
60-
return Position { x: head.x + 1, y: head.y }
61-
}
62-
} else if (head.y - tail.y).abs() >= 2 {
63-
// Two steps up or down...
64-
if head.y > tail.y {
65-
return Position { x: head.x, y: head.y - 1 }
66-
} else {
67-
return Position { x: head.x, y: head.y + 1 }
63+
return Position { x: head.x, y: head.y - dy.signum() }
6864
}
6965
}
7066
return tail.clone()
@@ -105,14 +101,36 @@ fn part2(movements: &Vec<Movement>) {
105101
knots[k] = follow_head(&knots[k-1], &knots[k]);
106102
}
107103

108-
println!("Part 2: {}", knots.last().unwrap().to_hashable());
109104
tail_visits.insert(knots.last().unwrap().to_hashable(), 0);
105+
// print_map(&knots);
110106
}
111107
}
112108

113109
println!("Part 2: {}", tail_visits.len());
114110
}
115111

112+
// fn print_map(knots: &Vec<Position>) {
113+
// let w: i32 = 30;
114+
// let h: i32 = 30;
115+
// let mut map = vec![vec![' '; w as usize]; h as usize];
116+
117+
// for i in 0..knots.len() {
118+
// let x = (w/2 + knots[i].x) as usize;
119+
// let y = (h/2 - knots[i].y) as usize;
120+
// map[y][x] = std::char::from_digit(i as u32, 10).unwrap();
121+
// }
122+
123+
// map[h as usize /2][w as usize /2] = 'x';
124+
125+
// for row in map {
126+
// for col in row {
127+
// print!("{}", col);
128+
// }
129+
// println!();
130+
// }
131+
// println!();
132+
// }
133+
116134
pub fn run() {
117135
let input = input_reader::read_file_in_cwd("src/day_09.data");
118136

0 commit comments

Comments
 (0)