This commit is contained in:
JonOfUs 2023-12-16 15:46:34 +01:00
parent 595f4ba264
commit 663fd765a7
13 changed files with 220 additions and 24 deletions

View file

@ -0,0 +1,10 @@
.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|....

110
res/16/input Normal file
View file

@ -0,0 +1,110 @@
\./....\../......................|............../-.......|....................|....../|\..../.............\...
...........|.-.............|.././........../........................./...........||.....................-....\
...............\.............-...................-............/.|...|.......-...............................-.
.......................................\..|-.../\....../.........|............/...|.....-.|...........\...|...
............/.-..|...\........|...........-......................\.........-........\....../....|.........|...
................-...........-|....\.................-........\........\...|.././......./......................
......................-....../.-........../........\....../..../.............-..........|.|.................|.
...|....................|.|../.......\.......|.......-...............|.......\......\....................|\...
.........\.............|.......-.....................|....................|..................-.......\-...\...
.../............-........../.||..../........-...../-.........-......./......\........|........................
....|..../.../../.....\..............-.-................../........|...../....\....\...................|......
.......\..-..................|..../..........|....................-............/...................\|../......
..-.\................../................../....\........./...\.\/.............................\|..............
.......-...............................-.......|....................|......................../...........|/...
................|...\-....-.......|/......./.......................|../.\-...-....../...\........\......\.|...
.....................-.......-....-................|.............-........-....................\....../....-..
\..|.|....-......../......../.../...................|.....................\..--...............-.......||......
.....-.\.....................-...\............../....\.......-/../|...|......../..\..................../......
....|...\......................|...|.../...|...............\...........-........................|.......--.-..
......\....|.-...........\................................|.................\..............\..|...............
...................||..................\|...................|....\............-....../........\.../.....-...-.
....................................|....|......./.\..................../..............|../.............\.....
......|...\./......../......|....\..././.......|...............................-.........../.-..\.............
.|.../....|.................-..............|..................\....../..........-..........|...|......|./.....
.........|.........../..\...|....../...................................................-......................
../....|/.............|.........../....................\.......\............./.........../.......-.........\-.
................\....................\.....-..|............-......-|...............\......\...-...............
..........|..........|...........|.../.............................-......\.....|\.........\.\.............../
.........................../...........\.............|................../.-.......-....././\.........-........
\....\................../..../.............-../......|.\.........-./..\........./.............................
.\..../..............|........../.......\........|.-.-........//\.......|.....-.../..-.....-...........-......
.......|................\.....................\.............-........./..................-...-...........\....
....../.......-......././.-.....|..........|.........|..........\............................/................
................\......\................\\.\..\........\.................................\......-/.....-......
..-...../......-.......................................|.................|......-.-...-.....\....../...-......
........................\....|.....\....|............................\..............\.......................\.
............................/|...|........\.\....\......-|......................|.....|.\-...-......-.........
\..|...........................\.................\.-................../................./..../.\..............
|.../.............\...............................|..|..-......-.......-.......|..\../.........-...........|..
...........|................\............................../.......................|..../-|...................
...\.|.\..................|.\-/....../-/..|/............\......../\.\...........\\.......-....-\..............
.................................-......./..-............|..\../..................................|/./|..../..
.../..../........|-.........|.....|..............-.................../.....|-.......|.....|...............\...
-..-...................//............................./.........-...............-.........|.../...........\...
.............././\......|..............\..................|...............-............................-......
/.........|/.....-../|......-.........|..-.-...........-........|..........-..................................
.............\.......|/........../.....|.......-....-....../......-...................-....|........\.........
\...|.|.../..|....../....-.........................-..............|.....\.......-........\........-....|......
-.-..\..........|........................./..........|..-./...........................|...........-........-..
....\./..............-.........|......./.-..............-.-../\........|..|.........-..............\...\.../..
..|...........\...../......../.../.........\....../....-..................................|/............-...|.
.../.........|..-................|..........|....\/..........|..|\.......|...........|........................
........../.............\...\.............................-...................|................-...-..........
.....................-.................................../../.............................-../................
.-........./......|........-...../...................\..|-........-...\..........././||.......................
.........|../../.....-...........-....-..-...|.............................\..................................
...................-.....\......../............./...............-.......\.........../........|............../.
.|\.............................................-.........-....|.....\..................-..-.......\..........
/................/....\.........\|...............-.............|......./...........-.....\........-.......--..
..........-........\........\-...\....\\........../...|./.........................\.....|.................\../
............................-.|............................\...\../..........................\......./...../..
.....//.......|..\.........-..\.........-....\..-..\.....-.....//.../.........................................
................................|....|..\............/|./.\....|.............-..\|................./..........
.....|.......-.............-../...../.........|../.....................|........................./.....|......
.-...\...../................\.-........|..|...|...|.........\|......|...../...../..........\................|.
.../../............-.........|................\.....\....-...................../.../...........-...|...../|...
..-..........-.......-.............\..............-..............\.......|....................................
..|................/........................-...................../.................................|......../
...../........\......./....../......||.......|-...............-.....-............../../.......................
...\..../...................-........\.........\............................/....|.........-..........-.......
.../................\....\..|-...........................\.-..-......|.|.-/.....-............./...............
../............\...........................-../....|....................-..............|........./......|...\\
.........................\..................|..\........................./....................\...............
/.............|.....................-.....\.....-..|............/..........|./...|....-...-....|-....../......
.\.-.....\............././.........................\..........-......................|........\...............
........\../............../......\.....\..-............|............\............../..|.......................
....-../....\..................................-......../...|................................................-
.......................\............-...................../.........\............-../.........\/..|../....-/|.
..../.............|....../............-.|......../.....................|...../..|.....-.............../.......
....................|\...........-...................................../.......................-...-..|..\.|/.
......................-....-\........../..............\.......-....\.....|...............\.........-..........
.|......../.....|.........||.................../.....\...|.........\........../........\............../.......
.-............/-..........-...........\..........\.......-....................................................
.....|/....-.../......-......................../.|./...\.......\/......./......../..\...../...|...|.........\-
.............../..\.../-......|\\......../............../............|/......................./.......\.......
.....\.-./....../............/..-................../....\..................-.................|.......-/..-....
.-................-..-...........................-..|............-.................../...........-...........|
-......\......|......\............../.............../...-/.../....\.........................-.....|.....-.....
.......\.....--.-............................\..........-./........||......|..\...../..../................|...
.............-.....|..../.......|............|....................-......\.............../|...............|...
............|........../........|....................-....|....../.........\..........-........-..-...........
..............|.................-....|...........\.|.../.........../..........-......\|.............\..-......
......-............\.......\....................................................|\..\.......................-.
................||...........\...-\/..|..............\.........................../.......\.|.............|...|
....-...................-...../....|............\.../.........-..../....\........../..../.|..........\........
........-.....\........../....|.........\......./...........\.......-..|............../...........-..|........
.................../.....-...........|....-...\......\...|...|...........................\.......||......\|...
......-.........|.....\....-..........|.......\....\..................................../........|...|./......
..................\.........../.............../.................../.........-.......-..../............/\.../..
........\...\...\....../.......\\........../............../.-.................../........|..............-.....
....../........................|................-.-......-....|......\\.-...-.|..........|..../...............
/.......-.....|.....\...................|...../..............|......|.../....................\.....|..........
|.\....-...............|/....|........../....................................\.-............................/.
.......................-...............-.|...................|......\.....-\..|.......-......./\.....-......-.
................................|../.................................-........-...................|...........
.....-.........\..-................./...........|...............|....................\/../../........|.-......
............................../......./.\/........./..|..............|.......................\................
......./.-.......\...........-...............|............|.............|..........|.|..........|.........--..
............-..............-.............\./...|.....|................|..........|/.............\.............
.../....................-........-.................../.....\.............-...................\................

View file

@ -1,18 +1,112 @@
use std::fs; use std::{collections::HashMap, fs};
use rayon::prelude::*;
pub fn solve() { pub fn solve() {
let path = "res/16/example"; let path = "res/16/input";
let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?"); let contents = fs::read_to_string(path).expect("I/O error, wrong path?");
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?")); //let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
let grid: Vec<Vec<char>> = contents
.lines()
.map(|line| line.chars().collect::<Vec<_>>())
.collect();
let result: usize = 0; let result: usize = solve_starts(&vec![(0usize, 0usize, 0u8)], &grid);
println!("Result 1: {result}"); println!("Result 1: {result}");
let possible_starts: Vec<(usize, usize, u8)> = (0..grid.len())
.map(|x| (x, 0, 0))
.chain((0..grid.len()).map(|x| (x, grid[0].len() - 1, 2)))
.chain((0..grid[0].len()).map(|y| (0, y, 1)))
.chain((0..grid[0].len()).map(|y| (grid.len() - 1, y, 3)))
.collect();
let result: usize = 0; let result: usize = solve_starts(&possible_starts, &grid);
println!("Result 2: {result}"); println!("Result 2: {result}");
} }
fn solve_starts(possible_starts: &Vec<(usize, usize, u8)>, grid: &Vec<Vec<char>>) -> usize {
possible_starts
.par_iter()
.map(|start| {
// (x,y,dir) - dir=0 -> east, dir=1 -> south, ...
let mut starts: Vec<(usize, usize, u8)> = vec![*start];
let mut visited: HashMap<(usize, usize), u8> = HashMap::new();
while !starts.is_empty() {
let (mut x, mut y, mut dir) = starts.pop().unwrap();
'mov: loop {
if visited.contains_key(&(x, y)) && *visited.get(&(x, y)).unwrap() == dir {
break 'mov; // already been here
}
visited.insert((x, y), dir);
match grid[x][y] {
'/' => {
dir = match dir {
2 => 1,
1 => 2,
0 => 3,
3 => 0,
_ => panic!(),
}
}
'\\' => {
dir = match dir {
0 => 1,
1 => 0,
2 => 3,
3 => 2,
_ => panic!(),
}
}
'|' => {
if dir % 2 == 0 {
starts.push((x, y, 1));
starts.push((x, y, 3));
break 'mov;
}
}
'-' => {
if dir % 2 == 1 {
starts.push((x, y, 0));
starts.push((x, y, 2));
break 'mov;
}
}
_ => {}
}
(x, y) = match dir {
0 => (x, y + 1),
1 => (x + 1, y),
2 => {
if y == 0 {
break 'mov;
}
(x, y - 1)
}
3 => {
if x == 0 {
break 'mov;
}
(x - 1, y)
}
_ => panic!(),
};
if x >= grid.len() || y >= grid[0].len() {
break 'mov;
}
}
}
visited.len()
})
.max()
.unwrap()
}

View file

@ -6,12 +6,10 @@ pub fn solve() {
let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?"); let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?");
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?")); //let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
let result: usize = 0; let result: usize = 0;
println!("Result 1: {result}"); println!("Result 1: {result}");
let result: usize = 0; let result: usize = 0;
println!("Result 2: {result}"); println!("Result 2: {result}");

View file

@ -6,12 +6,10 @@ pub fn solve() {
let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?"); let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?");
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?")); //let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
let result: usize = 0; let result: usize = 0;
println!("Result 1: {result}"); println!("Result 1: {result}");
let result: usize = 0; let result: usize = 0;
println!("Result 2: {result}"); println!("Result 2: {result}");

View file

@ -6,12 +6,10 @@ pub fn solve() {
let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?"); let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?");
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?")); //let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
let result: usize = 0; let result: usize = 0;
println!("Result 1: {result}"); println!("Result 1: {result}");
let result: usize = 0; let result: usize = 0;
println!("Result 2: {result}"); println!("Result 2: {result}");

View file

@ -6,12 +6,10 @@ pub fn solve() {
let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?"); let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?");
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?")); //let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
let result: usize = 0; let result: usize = 0;
println!("Result 1: {result}"); println!("Result 1: {result}");
let result: usize = 0; let result: usize = 0;
println!("Result 2: {result}"); println!("Result 2: {result}");

View file

@ -6,12 +6,10 @@ pub fn solve() {
let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?"); let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?");
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?")); //let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
let result: usize = 0; let result: usize = 0;
println!("Result 1: {result}"); println!("Result 1: {result}");
let result: usize = 0; let result: usize = 0;
println!("Result 2: {result}"); println!("Result 2: {result}");

View file

@ -6,12 +6,10 @@ pub fn solve() {
let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?"); let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?");
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?")); //let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
let result: usize = 0; let result: usize = 0;
println!("Result 1: {result}"); println!("Result 1: {result}");
let result: usize = 0; let result: usize = 0;
println!("Result 2: {result}"); println!("Result 2: {result}");

View file

@ -6,12 +6,10 @@ pub fn solve() {
let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?"); let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?");
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?")); //let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
let result: usize = 0; let result: usize = 0;
println!("Result 1: {result}"); println!("Result 1: {result}");
let result: usize = 0; let result: usize = 0;
println!("Result 2: {result}"); println!("Result 2: {result}");

View file

@ -6,12 +6,10 @@ pub fn solve() {
let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?"); let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?");
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?")); //let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
let result: usize = 0; let result: usize = 0;
println!("Result 1: {result}"); println!("Result 1: {result}");
let result: usize = 0; let result: usize = 0;
println!("Result 2: {result}"); println!("Result 2: {result}");

View file

@ -6,12 +6,10 @@ pub fn solve() {
let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?"); let mut _contents = fs::read_to_string(path).expect("I/O error, wrong path?");
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?")); //let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
let result: usize = 0; let result: usize = 0;
println!("Result 1: {result}"); println!("Result 1: {result}");
let result: usize = 0; let result: usize = 0;
println!("Result 2: {result}"); println!("Result 2: {result}");

View file

@ -5,7 +5,7 @@ extern crate scan_fmt;
use std::time::Instant; use std::time::Instant;
fn main() { fn main() {
days::d15::solve() days::d16::solve()
//_all_days() //_all_days()
} }