day20 part1 fast

This commit is contained in:
Johannes
2018-12-21 10:32:11 +01:00
parent a35881fb01
commit 241ca8ea18

View File

@@ -13,16 +13,17 @@ pub fn task1() {
add_default_neighbors_for_room(&mut map, Point(0, 0));
parse_input(&mut map, &input, Point(0, 0));
print_map(&map);
// print_map(&map);
println!("Most distant point: {:?}", longest_shortest_path(&map, Point(0,0)));
}
fn parse_input(map: &mut HashMap<Point, Tile>, input: &str, position: Point) {
/// Returns the end points of the parsed input
fn parse_input(map: &mut HashMap<Point, Tile>, input: &str, position: Point) -> HashSet<Point> {
// println!("{}, {:?}", input, position);
if input == "$" {
println!("End of the road.");
return;
return vec!(position).into_iter().collect();
}
let mut position = position;
let mut input = input;
@@ -31,15 +32,23 @@ fn parse_input(map: &mut HashMap<Point, Tile>, input: &str, position: Point) {
match c {
'(' => {
let (parts, rest) = split_parts(&input);
// println!("{:?}", parts);
// println!("{:?}", rest);
let mut middle_points: HashSet<Point> = HashSet::new();
for part in parts {
let mut flattened = String::with_capacity(part.len() + rest.len());
flattened = flattened.add(part);
flattened = flattened.add(rest);
parse_input(map, &flattened, position);
if part.len() > 0 {
for x in parse_input(map, part, position) {
middle_points.insert(x);
}
break;
}
}
let mut end_points: HashSet<Point> = HashSet::new();
for point in middle_points {
if rest.len() > 0 {
for x in parse_input(map, rest, point) {
end_points.insert(x);
}
}
}
return end_points;
}
'N' => {
position = Point(position.0, position.1 - 2);
@@ -65,13 +74,14 @@ fn parse_input(map: &mut HashMap<Point, Tile>, input: &str, position: Point) {
map.insert(Point(position.0 + 1, position.1), Door);
input = &input[1..];
}
'$' => return,
'$' => break,
c => panic!(
"Stumbled upon a '{}' when reading input where it shouldn't be",
c
),
}
}
vec!(position).into_iter().collect()
}
/// Splits the input by '|', but only by those that are not enclosed by further parenthesises.