day10: added p2

This commit is contained in:
Ishan Jain 2023-12-10 16:52:09 +05:30
parent 0e42413cb9
commit f8e42fd4b9
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
2 changed files with 197 additions and 157 deletions

View File

@ -1,15 +1,12 @@
#![feature(test)]
use std::collections::VecDeque;
extern crate test;
const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")];
fn process(data: &str) -> usize {
let mut answer = std::usize::MAX;
let grid: Vec<Vec<char>> = data
let mut answer = 0;
let mut grid: Vec<Vec<char>> = data
.lines()
.filter(|x| !x.is_empty())
.map(|x| x.chars().collect())
@ -31,236 +28,274 @@ fn process(data: &str) -> usize {
let mut map = vec![vec!['.'; n]; m];
for mut c in ['F'] {
'outer: for mut c in ['|', '-', 'L', 'J', '7', 'F'] {
let mut sx = s_x;
let mut sy = s_y;
let (mut px, mut py) = (s_x, s_y);
let mut length = 0;
let mut start = true;
map[sx][sy] = c;
grid[sx][sy] = c;
while c != 'S' {
println!(
"({}, {}) => ({}, {}) c = {} {}",
px, py, sx, sy, c, grid[sx][sy]
);
let mut tmp_map = map.clone();
while start || !(sx == s_x && sy == s_y) {
start = false;
tmp_map[sx][sy] = c;
match c {
'|' => {
if sx > 0 && !(sx - 1 == px && sy == py) {
if sx > 0
&& !(sx - 1 == px && sy == py)
&& ['7', 'F', '|', 'S'].contains(&grid[sx - 1][sy])
{
px = sx;
py = sy;
sx = sx - 1;
length += 1;
sx -= 1;
c = grid[sx][sy];
} else if sx < m - 1 && !(sx + 1 == px && sy == py) {
} else if sx < m - 1
&& !(sx + 1 == px && sy == py)
&& ['L', 'J', '|', 'S'].contains(&grid[sx + 1][sy])
{
px = sx;
py = sy;
sx = sx + 1;
length += 1;
sx += 1;
c = grid[sx][sy];
} else {
continue 'outer;
}
}
'F' => {
if sy < n - 1 && !(sx == px && sy + 1 == py) {
if sy < n - 1
&& !(sx == px && sy + 1 == py)
&& ['-', 'J', '7', 'S'].contains(&grid[sx][sy + 1])
{
px = sx;
py = sy;
sy = sy + 1;
length += 1;
sy += 1;
c = grid[sx][sy];
} else if sx < m - 1 && !(sx + 1 == px && px == py) {
} else if sx < m - 1
&& !(sx + 1 == px && px == py)
&& ['L', 'J', '|', 'S'].contains(&grid[sx + 1][sy])
{
px = sx;
py = sy;
sx = sx + 1;
length += 1;
sx += 1;
c = grid[sx][sy];
} else {
continue 'outer;
}
}
'-' => {
if sy > 0 && !(sx == px && sy - 1 == py) {
if sy > 0
&& !(sx == px && sy - 1 == py)
&& ['L', 'F', '-', 'S'].contains(&grid[sx][sy - 1])
{
px = sx;
py = sy;
sy = sy - 1;
length += 1;
sy -= 1;
c = grid[sx][sy];
} else if sy < n - 1 && !(sx == px && sy + 1 == py) {
} else if sy < n - 1
&& !(sx == px && sy + 1 == py)
&& ['J', '7', '-', 'S'].contains(&grid[sx][sy + 1])
{
px = sx;
py = sy;
sy = sy + 1;
length += 1;
sy += 1;
c = grid[sx][sy];
} else {
continue 'outer;
}
}
'7' => {
if sy > 0 && !(sx == px && sy - 1 == py) {
if sy > 0
&& !(sx == px && sy - 1 == py)
&& ['L', 'F', '-', 'S'].contains(&grid[sx][sy - 1])
{
px = sx;
py = sy;
sy = sy - 1;
length += 1;
sy -= 1;
c = grid[sx][sy];
} else if sx < m - 1 && !(sx + 1 == px && sy == py) {
} else if sx < m - 1
&& !(sx + 1 == px && sy == py)
&& ['L', 'J', '|', 'S'].contains(&grid[sx + 1][sy])
{
px = sx;
py = sy;
sx = sx + 1;
length += 1;
sx += 1;
c = grid[sx][sy];
} else {
continue 'outer;
}
}
'J' => {
if sy > 0 && !(sx == px && sy - 1 == py) {
if sy > 0
&& !(sx == px && sy - 1 == py)
&& ['L', 'F', '-', 'S'].contains(&grid[sx][sy - 1])
{
px = sx;
py = sy;
sy = sy - 1;
length += 1;
sy -= 1;
c = grid[sx][sy];
} else if sx > 0 && !(sx - 1 == px && sy == py) {
} else if sx > 0
&& !(sx - 1 == px && sy == py)
&& ['7', 'F', '|', 'S'].contains(&grid[sx - 1][sy])
{
px = sx;
py = sy;
sx = sx - 1;
length += 1;
sx -= 1;
c = grid[sx][sy];
} else {
continue 'outer;
}
}
'L' => {
if sx > 0 && !(sx - 1 == px && sy == py) {
if sx > 0
&& !(sx - 1 == px && sy == py)
&& ['7', 'F', '|', 'S'].contains(&grid[sx - 1][sy])
{
px = sx;
py = sy;
sx = sx - 1;
length += 1;
sx -= 1;
c = grid[sx][sy];
} else if sy < n - 1 && !(sx == px && sy + 1 == py) {
} else if sy < n - 1
&& !(sx == px && sy + 1 == py)
&& ['J', '7', '-', 'S'].contains(&grid[sx][sy + 1])
{
px = sx;
py = sy;
sy = sy + 1;
length += 1;
sy += 1;
c = grid[sx][sy];
} else {
continue 'outer;
}
}
'.' => break,
'.' => continue 'outer,
_ => unreachable!(),
}
}
answer = std::cmp::min(answer, length / 2);
match (
sx as i32 - px as i32,
sy as i32 - py as i32,
grid[px][py],
grid[sx][sy],
) {
(-1, 0, 'J', 'F') => (),
(-1, 0, '|', '|') => {
if sx == 0 || sx == m - 1 {
continue;
}
}
(-1, 0, '|', '7') => (),
(-1, 0, '|', 'F') => (),
(0, -1, 'J', 'L') => (),
_ => unreachable!(),
};
if sx == s_x && sy == s_y {
map = tmp_map;
break;
}
}
for line in map {
println!("{:?}", line);
let mut nmap = vec![vec!['.'; n * 2]; m * 2];
for (i, line) in map.iter().enumerate() {
for (j, c) in line.iter().enumerate() {
nmap[i * 2][j * 2] = *c;
}
}
for line in nmap.iter_mut() {
for j in 0..2 * n - 2 {
if ['F', 'L', '-'].contains(&line[j]) && ['J', '7', '-'].contains(&line[j + 2]) {
line[j + 1] = '-';
}
}
}
for j in 0..n * 2 {
for i in 0..m * 2 - 2 {
if ['F', '7', '|'].contains(&nmap[i][j]) && ['L', 'J', '|'].contains(&nmap[i + 2][j]) {
nmap[i + 1][j] = '|';
}
}
}
for j in 0..2 * n {
if nmap[0][j] == '.' {
flood_fill(&mut nmap, 0, j, 'X');
}
}
let mut out = vec![vec!['.'; n]; m];
for (i, line) in nmap.iter().enumerate() {
if i % 2 != 0 {
continue;
}
for (j, c) in line.iter().enumerate() {
if j % 2 != 0 {
continue;
}
out[i / 2][j / 2] = *c;
}
}
for line in out.into_iter() {
for c in line.into_iter() {
if c == '.' {
answer += 1;
}
}
}
answer
}
fn dfs(
grid: &Vec<Vec<char>>,
visited: &mut Vec<Vec<bool>>,
path: &mut Vec<char>,
sx: usize,
sy: usize,
c: char,
distance: usize,
) {
if visited[sx][sy] {
// return;
} else {
visited[sx][sy] = true;
fn flood_fill(map: &mut Vec<Vec<char>>, i: usize, j: usize, fill_with: char) {
let m = map.len();
let n = map[0].len();
const DIRS: [[i32; 2]; 4] = [[0, 1], [0, -1], [-1, 0], [1, 0]];
let mut stack = vec![];
stack.push((i, j));
while let Some((sx, sy)) = stack.pop() {
if map[sx][sy] != '.' {
continue;
}
if c == 'S' {
return;
map[sx][sy] = fill_with;
for dir in DIRS.iter() {
let x = sx as i32 + dir[0];
let y = sy as i32 + dir[1];
if x < 0 || y < 0 || x >= m as i32 || y >= n as i32 {
continue;
}
path.push(c);
let x = x as usize;
let y = y as usize;
println!("({},{}) = {} {}", sx, sy, c, distance);
let m = grid.len();
let n = grid[0].len();
match c {
'|' => {
if sx > 0 {
let c = grid[sx - 1][sy];
dfs(grid, visited, path, sx - 1, sy, c, distance + 1);
}
if sx < m - 1 {
let c = grid[sx + 1][sy];
dfs(grid, visited, path, sx + 1, sy, c, distance + 1);
stack.push((x, y));
}
}
'-' => {
if sy > 0 {
let c = grid[sx][sy - 1];
dfs(grid, visited, path, sx, sy - 1, c, distance + 1);
}
if sy < n - 1 {
let c = grid[sx][sy + 1];
dfs(grid, visited, path, sx, sy + 1, c, distance + 1);
}
}
'L' => {
if sx > 0 {
let c = grid[sx - 1][sy];
dfs(grid, visited, path, sx - 1, sy, c, distance + 1);
}
if sy < n - 1 {
let c = grid[sx][sy + 1];
dfs(grid, visited, path, sx, sy + 1, c, distance + 1);
}
}
'J' => {
if sy > 0 {
let c = grid[sx][sy - 1];
dfs(grid, visited, path, sx, sy - 1, c, distance + 1);
}
if sx > 0 {
let c = grid[sx - 1][sy];
dfs(grid, visited, path, sx - 1, sy, c, distance + 1);
}
}
'7' => {
if sy > 0 {
let c = grid[sx][sy - 1];
dfs(grid, visited, path, sx, sy - 1, c, distance + 1);
}
if sx < m - 1 {
let c = grid[sx + 1][sy];
dfs(grid, visited, path, sx + 1, sy, c, distance + 1);
}
}
'F' => {
if sy < n - 1 {
let c = grid[sx][sy + 1];
dfs(grid, visited, path, sx, sy + 1, c, distance + 1);
}
if sx < m - 1 {
let c = grid[sx + 1][sy];
dfs(grid, visited, path, sx + 1, sy, c, distance + 1);
}
}
'.' => (),
_ => unreachable!(),
}
path.pop();
}
fn main() {
for input in INPUTS.iter() {
println!("total = {}", process(input));
break;
}
}

View File

@ -1,5 +1,10 @@
..F7.
.FJ|.
SJ.L7
|F--J
LJ...
.F----7F7F7F7F-7....
.|F--7||||||||FJ....
.||.FJ||||||||L7....
FJL7L7LJLJ||LJ.L-7..
L--J.L7...LJS7F-7L7.
....F-J..F7FJ|L7L7L7
....L7.F7||L7|.L7L7|
.....|FJLJ|FJ|F7|.LJ
....FJL-7.||.||||...
....L---J.LJ.LJLJ...