day20 part 1

This commit is contained in:
2022-10-02 13:15:12 +02:00
parent 358e185501
commit 9a34cca688
4 changed files with 153 additions and 20 deletions

109
input/day20.txt Normal file
View File

@@ -0,0 +1,109 @@
V W A S Q A K
D V A H R Y N
#################################.#########.###.#.###.#########.#####.#################################
#...........#.#...#...#.#...#...........#.....#.....#.........#.....#...............#.......#.........#
#.###.#.###.#.###.#.###.###.###.#.#.#####.###.#####.#######.#######.#######.#########.#.#####.#.#####.#
#.#...#.#.#...#...#...#.#.......#.#...#.....#.#.....#.......#.....#.#.#...#.#...#.#.#.#.#.#.#.#...#...#
#########.#.#####.#.###.###.#.#####.###.#.#######.###.#.#####.###.#.#.#.#.#.#.###.#.###.#.#.###.#####.#
#...#...#...#.#.#.....#.....#.....#...#.#.....#.....#.#...#.#...#.#.#...#...#.#...............#.#.#.#.#
#.#.###.###.#.#.#.#####.###.#.#.#######.#######.#####.#####.#.#.#.#.#.#######.#.#######.###.#.#.#.#.###
#.#.......................#.#.#...#.....#...#.....#.......#...#.#...#.............#.#.....#.#.#.....#.#
#.###.#####.#####.#####.#####.#########.#.###.###.###.#.#######.###########.###.###.###.#.#####.###.#.#
#.#...#.....#.#...#.....#.#...#...#.......#...#...#.#.#...#.#...#...#.....#.#.#.......#.#.#.......#...#
#.###########.###########.###.#.#######.#.###.###.#.###.###.#.###.#.#.###.#.#.#####.###.#.#.###.#######
#.#.#...#.#.#...#.#.............#.#...#.#...#.#.#.....#.#.#.......#.#.#.......#.#...#...#.#.#...#.....#
###.#.###.#.#.###.#.###.###.#.#.#.#.###.#######.#.#.###.#.#.#.#.###.#.###.#.###.###.#.###.#####.#####.#
#...............#.#.#...#...#.#.....#...#...#...#.#.#.....#.#.#.#.#.#.#.#.#.......#.#.#.#.#.#...#.....#
#.#######.###.###.#########.#.#.#####.###.#####.###.#.###########.#.#.#.#.#.#.###.#####.###.###.###.###
#.#.#.#.#...#...#.#.#.......#.#.....#.......#...#.#.#.#.....#...#.#.#...#.#.#...#.#...#.....#.#.......#
###.#.#.#####.#.#.#.#####.#########.#####.#####.#.#.#.###.#.###.#.#.#.#####.#.#######.###.###.#.#.#####
#.#.....#.#.#.#.....#.#.....#.......#...#...#.....#.#...#.#.#.....#.#...#...#.#.........#.#...#.#.#.#.#
#.#.#####.#.###.#####.#######.#######.#.###.#.#.#.#.###.#.#.#.###.#.#.###########.#.#####.###.#.###.#.#
#.....#.#.#.......#...#.#...#.......#.#...#.#.#.#.#.#.#...#.#...#...#...#...#.#...#.#.#.#.#.#.#...#.#.#
###.###.#.#####.#####.#.#.#.#.#######.###.#.#.#.###.#.#####.###.#.###.###.###.#.###.#.#.#.#.#.#.###.#.#
#.....#...#.#...#...#.#.#.#.......#.....#...#.#.....#...#...#...#.#.#.#.#.#.......#...#...............#
###.#.###.#.###.#.###.#.#######.#.###.#.#####.#######.#.#.#.#.###.#.#.#.#.###.###############.#.###.###
#...#.#.............#.#.#.......#...#.#.....#.....#...#...#.#...#.#.........#...#.....#.#.#.#.#...#.#.#
###.#######.#.###.###.#.#####.#########.#####.#######.#######.#########.#######.###.###.#.#.###.#####.#
#.....#...#.#...#.#.....# E O X A X L #.#...#...#.#.....#...#.#
#.#.###.#####.###.#####.# N C E Y B O #.###.###.#.#.#####.###.#
#.#.....#.#.#.#...#.....# #.#.#.#.#.#.#...#...#...#
#.#.###.#.#.###.#.#.###.# #.#.#.#.#.#.#.#####.###.#
#.#.#.#...#.#...#...#...# #...............#.#.#.#.#
#####.#.#.#.###.#####.### #.#.#.#.#.#.###.#.#.#.#.#
#.#.....#.....#...#...#.# #.#.#.#.#.#.#...#.....#..UW
#.###.###.#.#.###.#.###.# #.###.#####.#.###.#.#.#.#
DK......#.#.#.#.....#......VG EJ....#.#.....#.#...#.#...#
#####.#.###.###.###.##### #############.#.#.#####.#
#.....#...#.#...#...#....SY #...#...#...#...#.#.#...#
#########.#####.#####.### #.###.#####.#####.#.#####
LO......#.....#.#.#...#...# #.........#.....#.#.....#
#.###.#####.#.###.#####.# #.#.###.###.###.###.###.#
#.#.....................# #.#.#.......#.#...#.#.#..XJ
#.#########.###.######### #.#####.#.###.###.#.#.###
#.#.#.....#...#.#.......# VD....#...#.#.......#.....#
#.#.###.#.#########.###.# #.#.###.#####.#####.###.#
#.#.#...#.........#...#..WV #.#.#...#.#...........#.#
###.###.#####.###.###.### #####.###.###############
XE........#...#.#.........# HX....#...#...#...#.....#.#
###########.############# #.###.#####.#.###.###.#.#
#...#.#...#.........#...# #...#.#.#.#...#.....#.#..XB
#.#.#.#.#.#.###.###.###.# #.#####.#.###.###.#.#.#.#
#.#.....#...#.#.#.......# #.#.#.....#.#...#.#.#.#.#
#.#####.#####.#########.# #.#.#####.#.#.#.###.#.#.#
#.#.....#.......#.#.....# #.............#.....#...#
#.#########.###.#.#.##### ###.#.#.###.#.#######.###
HX..#.#.#...#...#.#........OW XY..#.#.#.#...#.#.....#.#.#
###.#.###.#.############# #.#######.#.###.###.###.#
#.#.....#.#.#.#.#.#...#.# #...#.....#.#...#...#...#
#.#.#####.#.#.#.#.#.#.#.# ###.###########.#.###.#.#
#...#.......#.....#.#....QR #.#.#.#.#.......#.#...#..AG
#.#.###.#.#######.#.##### #.#.#.#.#####.###.#.###.#
WB..#.#...#...#.....#...#.# #...............#...#...#
###.#.#.#######.#.###.#.# #.###.###.#.#######.#####
#.#...#.........#.....#.# #.#...#...#.#.#...#.#...#
#.#####################.# #########.###.###.###.#.#
#.......................# #...#...#.#.#...#.....#..DF
#.###.#.#.#####.#######.# ###.#.#.###.###.#.###.#.#
EN..#...#.#...#...#.#...#.# ZH......#.............#.#.#
#######.#.#.#.###.#.#.#.# ###.###.#######.#.###.###
#...#.#.#.#.#.#.....#...# DF..#.#.....#.....#.#...#.#
###.#.###.###.###.#.##### #.#####.###############.#
#.......#...#.#...#......KN #.#...#.#...#.....#.#...#
###.#.#########.######### #.###.###.#######.#.#.#.#
#...#.#.#.#...#.#.......# #.....................#..GL
#.#.###.#.###.#####.#.### #########################
#.#...#.......#...#.#....AG WW..#.................#...#
#.#.#.#.#########.#.#.### #.#.###.###.#.#####.#.#.#
ZH..#.#.#.#.#...#.....#.#.# #.....#...#.#...#.....#..RP
#####.#.#.#.###.#.#.#.#.# ###.###.#####.#.###.###.#
#...............#.#.#...# #...#.#...#...#.#.....#.#
###.###.###.#.#.#.###.### X S U R W D G ###.#.#.#.###.###.###.###
#...#.#...#.#.#.#.#...#.# J H W P B K L #.#...#.#.#.#.#.#.#.....#
#####.#.###.#.###.#####.#######.#######.#####.###.#####.###########.#.#########.#.#.###.#.#.#.#####.#.#
#.#.....#.#.#...#...#.#.....#.#.....#...#.........#.........#.......#.........#...#...#.#.#.......#.#.#
#.###.#.#.#.#########.#.#####.#.#######.#####.###.#.#.#.#####.#####.#.###.#######.#####.#.###.###.#.#.#
#.#...#.#.......#...............#.#...#.....#.#.#.#.#.#.#...#.#...#.#.#...#...#.#.....#...#.....#.#.#.#
#.###.###.#########.#.#.#####.###.#.###.#.###.#.#####.#####.#.#.#.#######.###.#.###.#####.###.#########
#.......#.#.#.....#.#.#.#.............#.#.#.........#...#.....#.#.....#.........#...#.#...#...........#
#.###.###.#.#.#########.#####.#####.#####.###.#######.###.#.#.#.###.###.###.###.#####.#####.#.#.#####.#
#.#.....#.....#.....#...#.#.#.....#.#.......#.#.#...#...#.#.#...#...#.....#.#...........#.#.#.#.....#.#
#######.###.###.#######.#.#.#.#.#######.###.#.#.#.###.###########.#######.###########.#.#.#####.#####.#
#.......#...........#.#.#.....#.#.#...#.#...#.....#.......#.#...#.#...#.........#...#.#.....#.......#.#
#.#.#####.#.#.#.#.#.#.###.###.###.#.###.#.#.#.#######.#####.###.#.#.#.#.#########.###########.###.#####
#.#.#.#...#.#.#.#.#.#.#.#.#.#.........#.#.#.#.#.#.#.....#...#.......#.#...#.................#.#.......#
###.#.###.###.###.#.#.#.###.#.#.###########.#.#.#.###.###.###.###.#######.#.#####.#.#.###.#######.#.###
#.......#...#...#.#.......#...#.#.......#...#.....#.#.....#.#.#.....#...........#.#.#...#.....#...#.#.#
###.#####.#####.#.#.#.#####.#.#####.###.###.###.###.#.#####.#######.#.###.###.###.#######.###.###.###.#
#.....#...#.....#.#.#.#...#.#.....#.#...#.#.#.....#.....#.....#.....#.#...#...#.......#...#.....#.....#
###.#####.###.###########.#####.#######.#.#.#.#.#####.#######.#.#####.#####.#######.###.#.#####.###.###
#...#.#.....#.#.#.......#.......#...#.#.....#.#.#...#.....#.#.......#...#.#.#.#.#.#...#.#.#.#.#...#...#
#####.#########.###.###.#####.#####.#.###.###.#####.#.#####.#######.#.###.###.#.#.#.#######.#.#########
#.#.......#.#.#.....#.#.............#.....#.#.....#.#...#.....#.....#.#...#.#.#.#.#...#...#...........#
#.###.#####.#.#.#####.#####.#.#.###.#.#####.#.#.#.#.#.###.#.#####.###.#.#.#.#.#.#.#####.#####.#####.###
#...............#.#.#.......#.#...#.#.......#.#.#...#.#...#.#.#.#...#...#.................#.......#...#
#.#.#.#.#.#####.#.#.#.#.#.#.#.#####.#.#.###.#.#####.#.#.###.#.#.###.#.###.#.#.###.#.#.#.#####.#########
#.#.#.#.#.#.....#.....#.#.#.#.#.....#.#.#...#.#.....#...#...#.......#.#...#.#.#...#.#.#...............#
###############################.###.#####.#####.#.#######.###.#########.###############################
X Z V O O S W E
Y Z G C W Y W J

View File

@@ -1,3 +1,5 @@
extern crate core;
mod tasks; mod tasks;
fn main() { fn main() {

View File

@@ -2,6 +2,7 @@ use super::day05::{IntCodeComputer, RAM};
use itertools::Itertools; use itertools::Itertools;
use std::collections::HashMap; use std::collections::HashMap;
#[allow(dead_code)]
pub fn run() { pub fn run() {
let program = super::day05::load_ram("input/day19.txt"); let program = super::day05::load_ram("input/day19.txt");
part1(&program, 50); part1(&program, 50);

View File

@@ -1,7 +1,11 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet, VecDeque};
use std::fmt::format;
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; type C = i32;
@@ -27,23 +31,17 @@ impl Point {
} }
struct Field { struct Field {
point: Point,
neighbors: Vec<Point>, neighbors: Vec<Point>,
} }
impl Field { impl Field {
fn of(point: Point) -> Self { fn of() -> Self {
Field { Field { neighbors: vec![] }
point,
neighbors: vec![],
}
} }
} }
pub struct Maze { pub struct Maze {
map: HashMap<Point, Field>, map: HashMap<Point, Field>,
width: C,
height: C,
start: Point, start: Point,
finish: Point, finish: Point,
} }
@@ -51,7 +49,6 @@ pub struct Maze {
impl Maze { impl Maze {
pub fn from(input: &String) -> Self { pub fn from(input: &String) -> Self {
let (moc, width, height) = Self::map_of_chars(input); 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); let mut map = Self::create_map_of_free_spots(&moc);
Self::add_physical_neighbors(&mut map); Self::add_physical_neighbors(&mut map);
@@ -60,15 +57,14 @@ impl Maze {
let start = labels["AA"][0]; let start = labels["AA"][0];
let finish = labels["ZZ"][0]; let finish = labels["ZZ"][0];
Maze { map, width, height, start, finish } Maze { map, start, finish }
} }
pub fn shortest_path(&self) -> usize { 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>>) { fn add_portals(map: &mut HashMap<Point, Field>, labels: &HashMap<String, Vec<Point>>) {
println!("{:?}", labels);
for (label, points) in labels { for (label, points) in labels {
if label == "AA" || label == "ZZ" { if label == "AA" || label == "ZZ" {
continue; continue;
@@ -84,7 +80,6 @@ impl Maze {
.into_iter() .into_iter()
.flat_map(|x| { .flat_map(|x| {
(0..height).into_iter().flat_map(move |y| { (0..height).into_iter().flat_map(move |y| {
println!("{x} {y} h?");
let mut triple = [ let mut triple = [
moc[&Point::of(x, y)], moc[&Point::of(x, y)],
moc[&Point::of(x + 1, y)], moc[&Point::of(x + 1, y)],
@@ -103,7 +98,6 @@ impl Maze {
.into_iter() .into_iter()
.flat_map(|x| { .flat_map(|x| {
(0..height - 2).into_iter().flat_map(move |y| { (0..height - 2).into_iter().flat_map(move |y| {
println!("{x} {y} v?");
let mut triple = [ let mut triple = [
moc[&Point::of(x, y)], moc[&Point::of(x, y)],
moc[&Point::of(x, y + 1)], moc[&Point::of(x, y + 1)],
@@ -129,7 +123,6 @@ impl Maze {
} }
fn label(chars: [char; 3]) -> Option<String> { 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() { if chars[0] == '.' && chars[1].is_alphabetic() && chars[2].is_alphabetic() {
let label = if chars[1] < chars[2] { let label = if chars[1] < chars[2] {
format!("{}{}", 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> { fn create_map_of_free_spots(moc: &HashMap<Point, char>) -> HashMap<Point, Field> {
moc.keys() moc.keys()
.filter(|p| moc[p] == '.') .filter(|p| moc[p] == '.')
.map(|p| (*p, Field::of(*p))) .map(|p| (*p, Field::of()))
.collect() .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)] #[cfg(test)]
mod test { mod test {
use crate::tasks::day20::Maze; use crate::tasks::day20::Maze;
@@ -196,4 +210,11 @@ mod test {
let maze = Maze::from(&input); let maze = Maze::from(&input);
assert_eq!(maze.shortest_path(), 23); 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);
}
} }