day11: added

This commit is contained in:
Ishan Jain 2023-12-11 13:03:26 +05:30
parent 9d7bc70ff6
commit 0024d9175c
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
5 changed files with 311 additions and 4 deletions

View File

@ -117,8 +117,6 @@ fn process(data: &str) -> usize {
break; break;
} }
start = false; start = false;
local_map[sx][sy] = grid[sx][sy];
// what we need to figure out the next step // what we need to figure out the next step
// 1. The current character // 1. The current character
// 2. Direction we are headed in // 2. Direction we are headed in
@ -143,6 +141,7 @@ fn process(data: &str) -> usize {
let next_pipe = grid[p as usize][q as usize]; let next_pipe = grid[p as usize][q as usize];
if valid_pipes.contains(&next_pipe) { if valid_pipes.contains(&next_pipe) {
local_map[sx][sy] = grid[sx][sy];
sx = p as usize; sx = p as usize;
sy = q as usize; sy = q as usize;
direction = new_direction; direction = new_direction;

View File

@ -1 +1,79 @@
const INPUTS: [&'static str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; #![feature(test)]
extern crate test;
const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")];
fn process(data: &str) -> usize {
let grid: Vec<Vec<char>> = data
.lines()
.filter(|x| !x.is_empty())
.map(|x| x.chars().collect())
.collect();
let n = grid[0].len();
let mut blank_cols = Vec::new();
for j in 0..n {
let mut all_empty = true;
for row in grid.iter() {
if row[j] != '.' {
all_empty = false;
break;
}
}
if all_empty {
blank_cols.push(j);
}
}
let mut nodes = vec![];
let mut total_row_offset = 0;
for (i, row) in grid.iter().enumerate() {
if !row.iter().any(|&x| x == '#') {
total_row_offset += 1;
}
let mut total_col_offset = 0;
let mut j_offset = 0;
for j in 0..n {
if j_offset < blank_cols.len() && j >= blank_cols[j_offset] {
total_col_offset += 1;
j_offset += 1;
}
if grid[i][j] != '#' {
continue;
}
nodes.push(((i + total_row_offset), (j + total_col_offset)));
}
}
let mut answer = 0;
for (i, &(sx, sy)) in nodes.iter().enumerate() {
for &(dx, dy) in nodes.iter().skip(i + 1) {
let distance = ((dy as i32 - sy as i32).abs() + (dx as i32 - sx as i32).abs()) as usize;
answer += distance;
}
}
answer
}
fn main() {
for input in INPUTS.iter() {
println!("total = {}", process(input));
}
}
#[bench]
fn part1(b: &mut test::Bencher) {
b.iter(|| {
let v = process(INPUTS[INPUTS.len() - 1]);
test::black_box(v);
});
}

View File

@ -1 +1,81 @@
const INPUTS: [&'static str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; #![feature(test)]
extern crate test;
const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")];
const MULTIPLIER: usize = 999999;
fn process(data: &str) -> usize {
let grid: Vec<Vec<char>> = data
.lines()
.filter(|x| !x.is_empty())
.map(|x| x.chars().collect())
.collect();
let n = grid[0].len();
let mut blank_cols = Vec::new();
for j in 0..n {
let mut all_empty = true;
for row in grid.iter() {
if row[j] != '.' {
all_empty = false;
break;
}
}
if all_empty {
blank_cols.push(j);
}
}
let mut nodes = vec![];
let mut total_row_offset = 0;
for (i, row) in grid.iter().enumerate() {
if !row.iter().any(|&x| x == '#') {
total_row_offset += MULTIPLIER;
}
let mut total_col_offset = 0;
let mut j_offset = 0;
for j in 0..n {
if j_offset < blank_cols.len() && j >= blank_cols[j_offset] {
total_col_offset += MULTIPLIER;
j_offset += 1;
}
if grid[i][j] != '#' {
continue;
}
nodes.push(((i + total_row_offset), (j + total_col_offset)));
}
}
let mut answer = 0;
for (i, &(sx, sy)) in nodes.iter().enumerate() {
for &(dx, dy) in nodes.iter().skip(i + 1) {
let distance = ((dy as i32 - sy as i32).abs() + (dx as i32 - sx as i32).abs()) as usize;
answer += distance;
}
}
answer
}
fn main() {
for input in INPUTS.iter() {
println!("total = {}", process(input));
}
}
#[bench]
fn part1(b: &mut test::Bencher) {
b.iter(|| {
let v = process(INPUTS[INPUTS.len() - 1]);
test::black_box(v);
});
}

140
src/day11/input.txt Normal file
View File

@ -0,0 +1,140 @@
.#..........#.....................#........................................................#....................................#...........
...................#........................................#......................#........................................................
...........................................#......#..................#...................................................#..................
....#......................#...............................................#.........................#.....#......#................#........
.................................................................#.......................#..................................................
..............................................................................................................................#.............
.......#........#.............#...................................................#...................................#.....................
.........................#...................................#............................................................................#.
....................#.............#.........#..........................................#...............#....................................
.....................................................#...............#.............................................................#........
...........................................................................................................................#................
..........................................................#.........................................#...............#.......................
..............................................................................................#.............................................
.#........#........................#......#..............................................#..............................#...............#...
...............#......#...............................#.......#.........#......#.............................#.................#............
...............................#..............#.............................................................................................
............................................................................................................................................
.........................#.......................................#..........................................................................
..................#........................#......................................#........................................#................
..................................................#.......#..............................................#........................#.......#.
...#........#........................#....................................#...............#.................................................
............................................................................................................................................
.......#........................#....................................................................................#......................
...............#...........#........................................................................#.......................................
.............................................#................#.........#.............................................................#.....
...........#...........................#................#...........................#.......................................................
..................................................#.......................................#.............#...................................
.....#..................#..........#..............................#.............................#........................................#..
..............................#...............................................................................................#.............
...................................................................................................................#........................
..............#..........................................#.............#...........................#..............................#.........
...#...................................#.........................................#..........................................................
..................#...................................................................#................................................#....
..........................#...................................#.........................................#...................................
............................................................................................................................................
.........#...........#..........#..........................................#....................#..............#........#.....#...........#.
....................................................#.......................................................................................
.............................................#..............................................................................................
......................................#..................#..............................#...........#.......#...............................
............................#..................................#..............#.............................................................
...............#............................................................................................................................
...........................................#............................#.......................#...........................................
.....#..............#.................................................................................#...........................#.........
............................................................................................................................................
........................................#..........#.................#..........................................#...........................
.............................................#.................................#...........#................................................
...............................#.......................#...........................................#.......#....................#...........
#........#.............#...............................................................................................#...................#
.....................................#........................#..........#.........#........................................................
.................#............................................................................#.............................................
..........................#.......................................#.................................................#.......................
.....................................................#...................................#....................................#.............
..#.........................................................................................................................................
.............#.....#.......................#.............#....................................................#........................#....
......#........................................................................................#............................................
.................................#.................................#........................................................................
.................................................................................#.......................#................................#.
#.....................................................#...................................................................#.....#...........
.........#..................................................#......................................#........................................
..............................#.............#.................................................................#.......#.....................
........................#............................................................#......#................................#..............
.............#..............................................................................................................................
.....#........................................................#............#..........................#...........................#.........
...........................#......#.................................................................................#...................#...
.........#.....................................#...............................#............................................................
..................#.......................................#.......#................................#........................................
...............................#.....................#...................................#................................#.................
..............#..........................#.............................#............#.....................#.....................#.........#.
.................................................................................................................#..........................
......#.....................................................................................................................................
...........#.............#.........#........................................................................................................
...................................................................#..............#..................#......................................
..................#...........................................................................................#.............................
........#.................................#..............#.................................................................#..........#.....
...#.....................................................................#..............#.......#...........................................
............................................................................................................................................
............................................................................................................................................
.................................#................#..........................................................#...............#.............#
..............#............................................................#................................................................
......#.....................#..........................#........#.....#............#..............#.........................................
....................#..................................................................................#..................#.................
....................................#.........#........................................................................................#....
..........................................................#..............#..................................................................
................#...............................................................................................#.............#.............
............................................................................................................................................
............................#...................#......#.............#..............................................#.......................
.............#...................................................................................#........................................#.
......................#..........................................................#.......#..................................................
#...........................................................#...............................................................................
.........#.........................#.................#....................................................#.................#...............
............................................................................................................................................
...................#.............................................#.....#....................................................................
........................#.........................#.....................................................................................#...
.....#........................................................................................................................#.............
.......................................#...............................................................#.........#..........................
............................#...........................#...........#......................#..............................#.......#.........
..#................................#...........#............................................................................................
.................#..................................#...................#...................................................................
..........#.................................................#................................................#..............................
.....................#...............................................................................#......................................
..............#..................#..............................#.............#....................................#.........#........#.....
...#.............................................................................................#..........................................
............................................#..........................................#....................................................
.........#...............................................#..............................................................#...................
................#.........#.................................................................................................................
.....#..................................#............................#..............................#........#..................#..........#
...............................#............................................................................................................
....................................................#........#..............................................................................
........#..........................#.........#...............................................................................#..............
....................................................................................................................#.......................
....................#........#......................................#.........#.....#.....#.................................................
................................................................................................................................#.....#.....
...............#..........................................#...............#...............................#.................................
...#..............................#.........................................................................................................
........................................................................................#............#....................................#.
............................................................................................................................#...............
...................................................................................#............................#...........................
#........#........#........#..........#.........#...........................................................................................
.............................................................#..............................#............................#..................
................................#.......................#...................................................................................
.......................#................................................#...............#.........#..................#................#.....
............#..................................................................#........................#...................................
............................................................................................................................................
......#.....................#.......#......#..............#......#..........................................................................
.....................#......................................................................................................................
................................#................................................#.............#...........................#.............#..
..............................................................................................................#.............................
............#............#..........................................................................#.............................#.........
.....#...........#.....................#.............................#.....#........................................#.......................
................................................#...........#..........................#....................................................
............................................................................................................................................
#..........................#.....#................................................................#......................................#..
..........................................#.................................................................#.............#.................
................#.........................................................#.................................................................
.......#..............................#..............................................#..........................................#...........
........................#...................................#...............................................................................
............................................................................................................................#...............
..............................#.....................#.............................#...........#.................#...........................
...............................................#................#.....#................................................#....................
...................#...................#.................#.................#.........................#......................................

10
src/day11/sample.txt Normal file
View File

@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....