day20 part 1
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
extern crate core;
|
||||
|
||||
mod tasks;
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -2,6 +2,7 @@ use super::day05::{IntCodeComputer, RAM};
|
||||
use itertools::Itertools;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn run() {
|
||||
let program = super::day05::load_ram("input/day19.txt");
|
||||
part1(&program, 50);
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fmt::format;
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
|
||||
pub fn run() {}
|
||||
pub fn run() {
|
||||
let input = std::fs::read_to_string("input/day20.txt").unwrap();
|
||||
let maze = Maze::from(&input);
|
||||
let part1 = maze.shortest_path();
|
||||
println!("Part 1: {}", part1);
|
||||
}
|
||||
|
||||
type C = i32;
|
||||
|
||||
@@ -27,23 +31,17 @@ impl Point {
|
||||
}
|
||||
|
||||
struct Field {
|
||||
point: Point,
|
||||
neighbors: Vec<Point>,
|
||||
}
|
||||
|
||||
impl Field {
|
||||
fn of(point: Point) -> Self {
|
||||
Field {
|
||||
point,
|
||||
neighbors: vec![],
|
||||
}
|
||||
fn of() -> Self {
|
||||
Field { neighbors: vec![] }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Maze {
|
||||
map: HashMap<Point, Field>,
|
||||
width: C,
|
||||
height: C,
|
||||
start: Point,
|
||||
finish: Point,
|
||||
}
|
||||
@@ -51,7 +49,6 @@ pub struct Maze {
|
||||
impl Maze {
|
||||
pub fn from(input: &String) -> Self {
|
||||
let (moc, width, height) = Self::map_of_chars(input);
|
||||
println!("moc size {}, w {width}, h {height}", moc.len());
|
||||
|
||||
let mut map = Self::create_map_of_free_spots(&moc);
|
||||
Self::add_physical_neighbors(&mut map);
|
||||
@@ -60,15 +57,14 @@ impl Maze {
|
||||
let start = labels["AA"][0];
|
||||
let finish = labels["ZZ"][0];
|
||||
|
||||
Maze { map, width, height, start, finish }
|
||||
Maze { map, start, finish }
|
||||
}
|
||||
|
||||
pub fn shortest_path(&self) -> usize {
|
||||
todo!()
|
||||
bfs(&self.map, self.start, self.finish)
|
||||
}
|
||||
|
||||
fn add_portals(map: &mut HashMap<Point, Field>, labels: &HashMap<String, Vec<Point>>) {
|
||||
println!("{:?}", labels);
|
||||
for (label, points) in labels {
|
||||
if label == "AA" || label == "ZZ" {
|
||||
continue;
|
||||
@@ -84,7 +80,6 @@ impl Maze {
|
||||
.into_iter()
|
||||
.flat_map(|x| {
|
||||
(0..height).into_iter().flat_map(move |y| {
|
||||
println!("{x} {y} h?");
|
||||
let mut triple = [
|
||||
moc[&Point::of(x, y)],
|
||||
moc[&Point::of(x + 1, y)],
|
||||
@@ -103,7 +98,6 @@ impl Maze {
|
||||
.into_iter()
|
||||
.flat_map(|x| {
|
||||
(0..height - 2).into_iter().flat_map(move |y| {
|
||||
println!("{x} {y} v?");
|
||||
let mut triple = [
|
||||
moc[&Point::of(x, y)],
|
||||
moc[&Point::of(x, y + 1)],
|
||||
@@ -129,7 +123,6 @@ impl Maze {
|
||||
}
|
||||
|
||||
fn label(chars: [char; 3]) -> Option<String> {
|
||||
println!("{}{}{}", chars[0], chars[1], chars[2]);
|
||||
if chars[0] == '.' && chars[1].is_alphabetic() && chars[2].is_alphabetic() {
|
||||
let label = if chars[1] < chars[2] {
|
||||
format!("{}{}", chars[1], chars[2])
|
||||
@@ -157,7 +150,7 @@ impl Maze {
|
||||
fn create_map_of_free_spots(moc: &HashMap<Point, char>) -> HashMap<Point, Field> {
|
||||
moc.keys()
|
||||
.filter(|p| moc[p] == '.')
|
||||
.map(|p| (*p, Field::of(*p)))
|
||||
.map(|p| (*p, Field::of()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -186,6 +179,27 @@ impl Maze {
|
||||
}
|
||||
}
|
||||
|
||||
fn bfs(map: &HashMap<Point, Field>, start: Point, finish: Point) -> usize {
|
||||
let mut open: VecDeque<(Point, usize)> = VecDeque::new();
|
||||
let mut seen: HashSet<Point> = HashSet::new();
|
||||
|
||||
open.push_back((start, 0));
|
||||
|
||||
while let Some((p, d)) = open.pop_front() {
|
||||
if p == finish {
|
||||
return d;
|
||||
}
|
||||
|
||||
for neighbor in &map[&p].neighbors {
|
||||
if !seen.contains(neighbor) {
|
||||
open.push_back((*neighbor, d + 1));
|
||||
}
|
||||
seen.insert(*neighbor);
|
||||
}
|
||||
}
|
||||
panic!("no path found")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::tasks::day20::Maze;
|
||||
@@ -196,4 +210,11 @@ mod test {
|
||||
let maze = Maze::from(&input);
|
||||
assert_eq!(maze.shortest_path(), 23);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn result() {
|
||||
let input = std::fs::read_to_string("input/day20.txt").unwrap();
|
||||
let maze = Maze::from(&input);
|
||||
assert_eq!(maze.shortest_path(), 454);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user