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