mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Clean up solution to day 1.
This commit is contained in:
@@ -6,12 +6,21 @@ use std::io::prelude::*;
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
fn dist(pos: (i32, i32)) -> i32 {
|
fn dist(pos: (i32, i32)) -> i32
|
||||||
|
{
|
||||||
let (x, y) = pos;
|
let (x, y) = pos;
|
||||||
|
|
||||||
return x.abs() + y.abs()
|
return x.abs() + y.abs()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn walk(pos: (i32, i32), dir: i32) -> (i32, i32)
|
||||||
|
{
|
||||||
|
let backwards = if dir & 2 == 2 { -1 } else { 1 };
|
||||||
|
let (x, y) = pos;
|
||||||
|
|
||||||
|
(x + backwards * (1 - (dir & 1)), y + backwards * (dir & 1))
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
let path = Path::new(&args[1]);
|
let path = Path::new(&args[1]);
|
||||||
@@ -33,13 +42,12 @@ fn main() {
|
|||||||
Ok(_) => {},
|
Ok(_) => {},
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut posx = 0;
|
let mut pos = (0, 0);
|
||||||
let mut posy = 0;
|
|
||||||
let mut dir = 0;
|
let mut dir = 0;
|
||||||
let mut found = false;
|
let mut found = false;
|
||||||
|
|
||||||
let mut positions = HashSet::new();
|
let mut positions = HashSet::new();
|
||||||
positions.insert((posx, posy));
|
positions.insert(pos);
|
||||||
|
|
||||||
for instruction in content.split(", ") {
|
for instruction in content.split(", ") {
|
||||||
let turn = &instruction[..1];
|
let turn = &instruction[..1];
|
||||||
@@ -51,36 +59,20 @@ fn main() {
|
|||||||
if turn == "R" {
|
if turn == "R" {
|
||||||
dir += 1;
|
dir += 1;
|
||||||
} else {
|
} else {
|
||||||
dir -= 1;
|
dir += 3;
|
||||||
}
|
}
|
||||||
dir = (dir + 4) % 4;
|
dir %= 4;
|
||||||
|
|
||||||
let backwards = if dir & 2 == 2 { -1 } else { 1 };
|
for _ in 0..steps {
|
||||||
if dir & 1 == 1 {
|
pos = walk(pos, dir);
|
||||||
// Move in y direction
|
if !found && positions.contains(&pos) {
|
||||||
for y in 1..(steps + 1) {
|
println!("Visited twice at dist {}", dist(pos)) ;
|
||||||
if positions.contains(&(posx, posy + y * backwards)) && !found {
|
|
||||||
found = true;
|
|
||||||
println!("Arrived at the same position, dist {}", dist((posx, posy + y * backwards)));
|
|
||||||
} else {
|
|
||||||
positions.insert((posx, posy + y * backwards));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
posy += steps * backwards;
|
|
||||||
} else {
|
|
||||||
// Move in x direction
|
|
||||||
for x in 1..(steps + 1) {
|
|
||||||
if positions.contains(&(posx + x * backwards, posy)) && !found {
|
|
||||||
println!("Arrived at the same position, dist {}", dist((posx + x * backwards, posy)));
|
|
||||||
found = true;
|
found = true;
|
||||||
} else {
|
} else {
|
||||||
positions.insert((posx + x * backwards, posy));
|
positions.insert(pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
posx += steps * backwards;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Total distance is {}", dist((posx, posy)))
|
println!("Total distance is {}", dist(pos));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user