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

View File

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