day09: Chage stuff for part 2
Part 1 was missing the "y_diff.abs() == x_diff.abs()" condition in Pos.move_after(), which made the program show the wrong output at first.
This commit is contained in:
parent
ae6ac6979a
commit
ba121691ba
|
@ -40,7 +40,9 @@ impl Pos {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if x_diff.abs() > y_diff.abs() {
|
if x_diff.abs() == y_diff.abs() {
|
||||||
|
self.move_((x_diff.abs() - 1) * x_diff.signum(), (y_diff.abs() - 1) * y_diff.signum());
|
||||||
|
} else if x_diff.abs() > y_diff.abs() {
|
||||||
self.move_((x_diff.abs() - 1) * x_diff.signum(), y_diff);
|
self.move_((x_diff.abs() - 1) * x_diff.signum(), y_diff);
|
||||||
} else {
|
} else {
|
||||||
self.move_(x_diff, (y_diff.abs() - 1) * y_diff.signum());
|
self.move_(x_diff, (y_diff.abs() - 1) * y_diff.signum());
|
||||||
|
@ -56,8 +58,11 @@ fn main() -> Result<(), Box<dyn error::Error>> {
|
||||||
let f = File::open("input.txt")?;
|
let f = File::open("input.txt")?;
|
||||||
let reader = BufReader::new(f);
|
let reader = BufReader::new(f);
|
||||||
|
|
||||||
let mut h_pos = Pos { x: 0, y: 0 };
|
let mut knot_positions = vec![];
|
||||||
let mut t_pos = Pos { x: 0, y: 0 };
|
for _ in 0..10 {
|
||||||
|
knot_positions.push(Pos { x: 0, y: 0 });
|
||||||
|
}
|
||||||
|
|
||||||
let mut visited_locations = HashSet::new();
|
let mut visited_locations = HashSet::new();
|
||||||
for line in reader.lines() {
|
for line in reader.lines() {
|
||||||
let line = line?;
|
let line = line?;
|
||||||
|
@ -65,13 +70,16 @@ fn main() -> Result<(), Box<dyn error::Error>> {
|
||||||
let direction = s_iter.next().ok_or(format!("Could not split {line} on whitespace"))?;
|
let direction = s_iter.next().ok_or(format!("Could not split {line} on whitespace"))?;
|
||||||
let amount: i32 = s_iter.next().ok_or(format!("Could not split {line} into 2 parts."))?.parse()?;
|
let amount: i32 = s_iter.next().ok_or(format!("Could not split {line} into 2 parts."))?.parse()?;
|
||||||
for _ in 0..amount {
|
for _ in 0..amount {
|
||||||
h_pos.move_into_direction(direction)?;
|
knot_positions[0].move_into_direction(direction)?;
|
||||||
t_pos.move_after(&h_pos);
|
for i in 1..knot_positions.len() {
|
||||||
if DEBUG {
|
let (head, tail) = knot_positions.split_at_mut(i);
|
||||||
h_pos.show();
|
tail[0].move_after(head.last().ok_or("head doesn't have an element")?);
|
||||||
t_pos.show();
|
|
||||||
}
|
}
|
||||||
visited_locations.insert((t_pos.x.to_owned(), t_pos.y.to_owned()));
|
if DEBUG {
|
||||||
|
knot_positions[0].show();
|
||||||
|
knot_positions[9].show();
|
||||||
|
}
|
||||||
|
visited_locations.insert((knot_positions[9].x.to_owned(), knot_positions[9].y.to_owned()));
|
||||||
}
|
}
|
||||||
if DEBUG {
|
if DEBUG {
|
||||||
println!("");
|
println!("");
|
||||||
|
|
Loading…
Reference in New Issue