day20 part 2
This commit is contained in:
37
input/day20_example2.txt
Normal file
37
input/day20_example2.txt
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
Z L X W C
|
||||||
|
Z P Q B K
|
||||||
|
###########.#.#.#.#######.###############
|
||||||
|
#...#.......#.#.......#.#.......#.#.#...#
|
||||||
|
###.#.#.#.#.#.#.#.###.#.#.#######.#.#.###
|
||||||
|
#.#...#.#.#...#.#.#...#...#...#.#.......#
|
||||||
|
#.###.#######.###.###.#.###.###.#.#######
|
||||||
|
#...#.......#.#...#...#.............#...#
|
||||||
|
#.#########.#######.#.#######.#######.###
|
||||||
|
#...#.# F R I Z #.#.#.#
|
||||||
|
#.###.# D E C H #.#.#.#
|
||||||
|
#.#...# #...#.#
|
||||||
|
#.###.# #.###.#
|
||||||
|
#.#....OA WB..#.#..ZH
|
||||||
|
#.###.# #.#.#.#
|
||||||
|
CJ......# #.....#
|
||||||
|
####### #######
|
||||||
|
#.#....CK #......IC
|
||||||
|
#.###.# #.###.#
|
||||||
|
#.....# #...#.#
|
||||||
|
###.### #.#.#.#
|
||||||
|
XF....#.# RF..#.#.#
|
||||||
|
#####.# #######
|
||||||
|
#......CJ NM..#...#
|
||||||
|
###.#.# #.###.#
|
||||||
|
RE....#.# #......RF
|
||||||
|
###.### X X L #.#.#.#
|
||||||
|
#.....# F Q P #.#.#.#
|
||||||
|
###.###########.###.#######.#########.###
|
||||||
|
#.....#...#.....#.......#...#.....#.#...#
|
||||||
|
#####.#.###.#######.#######.###.###.#.#.#
|
||||||
|
#.......#.......#.#.#.#.#...#...#...#.#.#
|
||||||
|
#####.###.#####.#.#.#.#.###.###.#.###.###
|
||||||
|
#.......#.....#.#...#...............#...#
|
||||||
|
#############.#.#.###.###################
|
||||||
|
A O F N
|
||||||
|
A A D M
|
||||||
@@ -5,6 +5,10 @@ pub fn run() {
|
|||||||
let maze = Maze::from(&input, PortalField::curried_factory);
|
let maze = Maze::from(&input, PortalField::curried_factory);
|
||||||
let part1 = maze.shortest_path();
|
let part1 = maze.shortest_path();
|
||||||
println!("Part 1: {}", part1);
|
println!("Part 1: {}", part1);
|
||||||
|
|
||||||
|
let maze = Maze::from(&input, DimensionField::curried_factory);
|
||||||
|
let part1 = maze.shortest_path();
|
||||||
|
println!("Part 2: {}", part1);
|
||||||
}
|
}
|
||||||
|
|
||||||
type C = i32;
|
type C = i32;
|
||||||
@@ -94,17 +98,24 @@ struct DimensionField {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DimensionField {
|
impl DimensionField {
|
||||||
fn is_inner(&self) -> bool {
|
fn is_inward(&self) -> bool {
|
||||||
!self.is_outer()
|
let p = self.point;
|
||||||
}
|
p.x > 2 && p.x < self.map_width - 3 && p.y > 2 && p.y < self.map_height - 3
|
||||||
fn is_outer(&self) -> bool {
|
|
||||||
false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Field for DimensionField {
|
impl Field for DimensionField {
|
||||||
fn neighbors(&self, at_level: usize) -> Vec<RealPoint> {
|
fn neighbors(&self, at_level: usize) -> Vec<RealPoint> {
|
||||||
todo!()
|
let mut result: Vec<RealPoint> = self.neighbors.iter().map(|n| RealPoint::from(*n, at_level)).collect();
|
||||||
|
if let Some(destination) = self.jump {
|
||||||
|
if self.is_inward() {
|
||||||
|
result.push(RealPoint::from(destination, at_level + 1));
|
||||||
|
} else if at_level > 0 {
|
||||||
|
result.push(RealPoint::from(destination, at_level - 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_label_partner(&mut self, point: MapPoint) {
|
fn set_label_partner(&mut self, point: MapPoint) {
|
||||||
@@ -266,8 +277,10 @@ fn bfs(map: &FieldMap, start: RealPoint, finish: RealPoint) -> usize {
|
|||||||
let mut seen: HashSet<RealPoint> = HashSet::new();
|
let mut seen: HashSet<RealPoint> = HashSet::new();
|
||||||
|
|
||||||
open.push_back((start, 0));
|
open.push_back((start, 0));
|
||||||
|
seen.insert(start);
|
||||||
|
|
||||||
while let Some((p, d)) = open.pop_front() {
|
while let Some((p, d)) = open.pop_front() {
|
||||||
|
println!("{:?} {}", p, d);
|
||||||
if p == finish {
|
if p == finish {
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
@@ -275,24 +288,32 @@ fn bfs(map: &FieldMap, start: RealPoint, finish: RealPoint) -> usize {
|
|||||||
for neighbor in map[&p.map_point()].neighbors(p.level) {
|
for neighbor in map[&p.map_point()].neighbors(p.level) {
|
||||||
if !seen.contains(&neighbor) {
|
if !seen.contains(&neighbor) {
|
||||||
open.push_back((neighbor, d + 1));
|
open.push_back((neighbor, d + 1));
|
||||||
|
seen.insert(neighbor);
|
||||||
}
|
}
|
||||||
seen.insert(neighbor);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
panic!("no path found")
|
panic!("no path found")
|
||||||
|
PortalField
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::tasks::day20::{Field, Maze, PortalField};
|
use crate::tasks::day20::{DimensionField, Field, Maze, PortalField};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn example1() {
|
fn example_part1() {
|
||||||
let input = std::fs::read_to_string("input/day20_example1.txt").unwrap();
|
let input = std::fs::read_to_string("input/day20_example1.txt").unwrap();
|
||||||
let maze = Maze::from(&input, PortalField::curried_factory);
|
let maze = Maze::from(&input, PortalField::curried_factory);
|
||||||
assert_eq!(maze.shortest_path(), 23);
|
assert_eq!(maze.shortest_path(), 23);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example_part2() {
|
||||||
|
let input = std::fs::read_to_string("input/day20_example2.txt").unwrap();
|
||||||
|
let maze = Maze::from(&input, DimensionField::curried_factory);
|
||||||
|
assert_eq!(maze.shortest_path(), 396);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn result() {
|
fn result() {
|
||||||
let input = std::fs::read_to_string("input/day20.txt").unwrap();
|
let input = std::fs::read_to_string("input/day20.txt").unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user