From 9a34cca688df5be23cd433ae15202bd41fff5e81 Mon Sep 17 00:00:00 2001 From: Johannes Date: Sun, 2 Oct 2022 13:15:12 +0200 Subject: [PATCH] day20 part 1 --- input/day20.txt | 109 +++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 2 + src/tasks/day19.rs | 1 + src/tasks/day20.rs | 61 ++++++++++++++++--------- 4 files changed, 153 insertions(+), 20 deletions(-) create mode 100644 input/day20.txt diff --git a/input/day20.txt b/input/day20.txt new file mode 100644 index 0000000..8528a47 --- /dev/null +++ b/input/day20.txt @@ -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 diff --git a/src/main.rs b/src/main.rs index 1f95c44..6c16eb5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +extern crate core; + mod tasks; fn main() { diff --git a/src/tasks/day19.rs b/src/tasks/day19.rs index d4bf301..347ce43 100644 --- a/src/tasks/day19.rs +++ b/src/tasks/day19.rs @@ -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); diff --git a/src/tasks/day20.rs b/src/tasks/day20.rs index ca3d163..f82bb71 100644 --- a/src/tasks/day20.rs +++ b/src/tasks/day20.rs @@ -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, } impl Field { - fn of(point: Point) -> Self { - Field { - point, - neighbors: vec![], - } + fn of() -> Self { + Field { neighbors: vec![] } } } pub struct Maze { map: HashMap, - 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, labels: &HashMap>) { - 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 { - 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) -> HashMap { 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, start: Point, finish: Point) -> usize { + let mut open: VecDeque<(Point, usize)> = VecDeque::new(); + let mut seen: HashSet = 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); } -} \ No newline at end of file + + #[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); + } +}