d21 with weird offset

This commit is contained in:
JonOfUs 2023-12-21 19:42:33 +01:00
parent 53a7a24e91
commit 8073809b28
4 changed files with 231 additions and 7 deletions

View file

@ -0,0 +1,11 @@
...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........

131
res/21/input Normal file
View file

@ -0,0 +1,131 @@
...................................................................................................................................
......#...........#................#......................................#.....#......#..##.#.......#....#.#........#...##...#....
...........#.................##...#...##......#............#...........##..#....................#.................#......#....#..#.
.....#.........#.....#.#.....###........###........#..........................##..##.......#.......#.....#...............#.......#.
....#..#......#......................#......#...............................#...........#........................#.............#...
.......#.....................#.............#......#...............#....................#............#.#.........#..#...............
.#.#.....#....#...................##......#..#..................#............................................#...............#.....
.....#....#...#....##...#......#.............#......##........#................#................#................#...............#.
.......#...................#..........#......................................#.........................##.........#..#.#..#........
....#..........#.........#....#......##...........##.................#................#...#............#...#..##....#...........#..
...#..........###............#.............#........................##..................#...##..................##......#...#......
.......#.......#.#..#.......#.......##..........#.........#..............................##.....#..#..........#.....#......#.......
..#.........#.....#..#..#..#..................#.........................#......................#.#..#...#..........#.....#....##.#.
..................................#.....#................#........#..........................#........#.....#...............#....#.
..........#..........#.#.................#.#.........................................#.......#..........#..##...#......#...........
.........#........#...........#...#.#....#.................##..#....................#..........#....#.#....##........#.......#.....
....#........#....................##..#......................#......#.........................#..#............................#....
...#.#...#...#......................#..#..........................#...#.#...............#......#.#........#....#..........#.....#..
..............#.....#.#......#........#...#..........#..#.#..#.........................#.....#..#......#.......#......#.......#....
.......................#.....#.....#................................##...#..#...#...........#..#..........#..#.#...#.........#...#.
.........#........#......#..#................................#.##.........#...#.................##......#.........#.##...#...#.....
.#.#.........##....#..........#..##.....................#..##...#..#..#...#.......#...........................#....................
.#.#......#..#..............##.#...#.#.............#.........#..............#.....#...............#..........................#...#.
.....##.....#.....##..#...#..#..#......................................#......#.............#.............#.....#......#........#..
......................#........#................#.......#..........#.............##............#..........#....#............#......
.....#..............................#.......##......#..#....#..............#.....................................#.....#.........#.
....................##...........#................#.#.....#.............#......#..##...#....................#................#.#...
.................................#...........#.....#..#..#..............#..##.........#..............#......#................#.....
............##.................................#..#...##.#........#...................................#...........#.#........#...#.
...#.......#.#........#......#...........#....#..#.......#....#........#.#.#.............##.......#.................#..............
...###............#......................##..##.........................#.........#......#..............#..#............#..........
..#.......#...##...#..#.......#.......#...#...........................................#...................#........##....#..#......
..#..................#.....#...........#.................#.#.#...........#..................#............#.......#.................
...##..#.............#.....#.....................#................#..........................#.............##...............#......
............#....#....#...#............#....#...........#.#.................#....#..................................#..............
...#.....#.............#...........................#.....##..........#...............#....#...............#........................
......#.....##.........#..........##..#...............#....#..#.#.#.....#.............#.###..............#..#......................
....................#...#...............#....................#...............#.#.#..##.............................#...............
...................#...#...........#...#.#...#...........#..............#............#..#......#............#.............#......#.
...#................#.................#.....#..........#........#.#........#...................#..#...........#.##........#......#.
....................#..........#....................#....#...#.....................#.......#.................#......#........#.....
...#.........#...................#.......##...###......#..#.......#.#..................#............#...........#....#...........#.
......#..#.#...#.#...............#.......................#.........#.......##................#...................#.###.............
...............#..............................................#.....#....#...#..#...#.....#......#.....#...........................
.......#.......#..........#.#.#..#.....#...#......#.................#........#...#..#.#.......#....................................
...........#..#.....................#.........................#...#.#.....#..........#....##.....#.................................
.......#.#.#.#..........#...#.##.##..#....................#.#.#..............#...................#....##...........................
.........................#.#.#...................#......#......#.............#.....#..............#......###..............#.#......
......#...............#...........#...................##..............#.##..#.#.#.#.#......#.....#.##.......#............#.#....##.
.....#....#.............#..##........###...#.......#.....#....#.......###....................#..#.#............................#...
.#........................#.....#......#.#.....................#......#.#...........#..#.....#.#.#..........#..............#...#...
.....##...#.........#.#.#.#...................#..#..#.#..#...#..#.....#.#.....#.........##.........................................
.................#.#...............#...............#...........#...........#.....#..........#...#.............................#....
.......................##...........#..........#...............#..#......................................#...#................#..#.
......#......................#..##.#........#.............#.....#..#....................#......#.........#.....#...#...........#.#.
.....................#.................#....##..#....#....##..#..............#..............#.....#...............#................
.#.................##.....#.#....#.....................#.............#....#......#...................#.#.##....................#...
.##..........##....#.##......#..#.................#...#..##........#....#..#.......#..............#.......#........................
......................#...........###.#.....##.............##........#..................#..............................#........#..
..#........................#...................#.#.#...#........#........#....#.....#.....#........................#...............
.#...........................#........#.....................#.....##.#..#.....#......#....#...#............##......................
............#...#..#..........#..............#...#...#..#..........#.........#...............#....#.#......#.#...#.................
..............................#..##...............#.....#....#.........................#....#...#....#.....#....#..................
..................#.#.........................#.......#........#.......#..##....#...#....#..................................#......
..........................#..#...#....................................#......#...#...#..#...#....#.............#.#.................
.................................................................S.................................................................
..............#...#...#.....##....#..................#....#........##..#.....#.#...#.#..............#.................##...........
.........##.........#............##.............................#.#...#.#.......#..........####....#...........#...........#.......
.........#...............................#...............#........................#......#..#......#.............##................
.............#..............................#...#.................#..........###.#.#.#.........#........#..........................
.#........#..#...........##...........#.#..........................#.........#....#.............#.............##...................
..#..........#..........................#...#..#..#.##...#..............#.....#...#............##........#.#.......#............#..
.................#.........#.......#.......#............#.#.................##...#.##.....................#.....#..................
.#.##..........#...#...................#...#.....##.....#...............#..#.........#..............#.................#............
.....#.......#..##....#..........#........#.#.....#.........#...............##......#.#....#.....#.................#...............
......#.......#.#...#....................#...............#.........#....#..#.#..........##.....#...#........................#......
.#.................#.................#.#....................#..............#.................#......#.......#.....#................
.#..#................................#......#..........#..............#.#.##.......................#...............................
..#.#............#.#......#...............#.#....#...#.#..#.......#...#.....#.......#........#.#...........#...#............##.....
.......#..........#....#....................#................##...........#......#..#...#....#...#......................#...#.#....
.........##........#........#......#.......#........#..#................#..#.............#..................#.#..........#.........
......#....#.................................#..#.#....#..............#........#.....#.##........#..........#.............#....#...
.............#........#.........#...##.....#.....#................##..........#.......#....#.......................................
..........................#.....#..#......#....#....#..##...#.......................#..............#.......#..........#..#......#..
...#.......................#....#.......#......#..................#................#...##........##.#............................#.
....#....................#.####..........#......#.....#......#....#.#.#................#...#..............#.............#.....#....
..........#..#...#..............##..#..#..#..........#.......#.##........##..............#.....#......#............#...#...........
..........................#..#..............#..#....#..#..............#.#...#..........#.....#..#.#................................
.......#.......#...............#.........#..#.#...#.....................#.....#......................#..............#.#..#.........
........#.....#.............#....................#.#........#.................#.....#...#..........#............#...............#..
.............#....##.........#.#................#....#.........#...#.......................#.....#.#..................#......#.....
...............#.....#.............#.....#.......#.......#......#.#..#.....#...#...#.......#................#...#................#.
...................#..#...........#..###....................#......#........#......#...........................#......#.......#....
..#.....#.##......#................#.................#.................##....................#.#...........#....#.#...#........#...
....#......#.#..#........#..............#.......#.........#.................#.............#.#...#...........#.#................#...
..#....#.#.......#...#.................#......#....#................#.......................#...#.........##..........#.........#..
..............#...............................#.##..............................#.......#..#...#...................#...#........#..
.#..#....#......#.....#...#............#..............................#..............#....................#..##..............#..##.
.......##............#..#............######............#................#..........#.#...#...#................#....................
...#..................##....................#.......#.#....##...................##..#....#..#..........#.....#.....................
...#.....#..#..#.#.......................#......................#...........#......###...#.............#........................#..
.....#.............#..........#......................#....#............#.##....#.......#.............#..........#........##........
........#...#.#..................#........#.#......#........#.....##...#...#....#...#......................................#.......
...............#............#..#.......................##.#.....#...#.#........#..................#......#.........................
.............##..#.......#.#.....#............#............#..........##.......#.....##...............................#...#........
.#..##...............#..............#...........#..#..#...##.......#..#.......................#.......##.....................#.#...
.....................###........#..#.....................#........#...#.##...................#....##.........#.........#...........
...#.....#...............#....#..###.......................................#..................#....................................
...#.#.#.....#..#.........#.....#...##.............#................#......#.....#........................#....#..............#....
...........#......#..##.......#......#..................#.....##..................#........#.......#......#...#...#..#...#.........
.........#.#.........#......#............................#.....#............#.................#..........#..............#.......#..
............#.........#.#....#.......#...................................#..............##.##..............#....#...............#..
.............#.....##.........#.........##...............#...............#.............................###........#.....#.#........
......##..........#....#.#.#..........##.#.#................#...#....##.......................#.......#...#.#........#.#..#........
....#....#............#.#......#..#.........#.............#.#..........................#.......#..............#..#..#.....#........
...................#...........#....#...#..#..#.......#..#...#.#........................#.................#.............#..........
.........#..#...#......#......#......#.................#...........#...................#........#.......#......#.#.......#.......#.
..#..#......#.....................#...........................#.....##................#.............................#..............
..#.#.......#........................#.......#..........................#................#.....#...#.........#.#......#............
...............................#....................................#.......................##....#......#.#.........#.#.........#.
.#.....................#......#..........#......#..............#...............##............#...#.##...#......##...#.#.#..........
...............#.#......#..................#......................................................#........................#.#.....
...#...#..#...............##...#...#.....#...#....................#..#..........#.....##......................................#....
.#..#...#........................#....#...##......#...........#.......................#...##......#.......#................#.......
....#...........#.............#.....#..#..#.....#.................................#........#.#..............#......................
....#....#........#..#.#..............#................................................#..............#................#.........#.
.....#..........#.......#..................#................................................#..............##...#..................
................#...............#..........#............##.........................#........#.#...............#...##...............
........#..................#..#.....#.#.........#.....#.#.#..................#.......#.#......#....#.........#..#.........#...#....
..#...........##...............#...........#..#...#...#.#..................##......#.....#.......#........#..#...#......#........#.
...................................................................................................................................

View file

@ -1,16 +1,98 @@
use std::fs; use std::{fs, collections::{HashMap, VecDeque}};
use num::Integer;
pub fn solve() { pub fn solve() {
let path = "res/21/example"; let path = "res/21/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 s = find_char_location(&grid, 'S').unwrap();
const STEPS1: usize = 64;
let map = floodfill(&grid, s, STEPS1);
let result: usize = map.iter().filter(|(_, dist)| dist.is_even()).count();
println!("Result 1: {result}"); println!("Result 1: {result}");
let result: usize = 0;
const STEPS2: usize = 26501365;
let x_len = grid.len();
let y_len = grid[0].len();
let steps_in_first = (x_len-1)/2;
let copies_per_dir = (STEPS2-steps_in_first)/x_len;
let pos_per_odd = floodfill(&grid, (0, 0), x_len+y_len).iter().filter(|(_, dist)| dist.is_odd()).count();
let pos_per_even = floodfill(&grid, (0, 0), x_len+y_len).iter().filter(|(_, dist)| dist.is_even()).count();
let cross = copies_per_dir * 2 * (pos_per_odd + pos_per_even) + pos_per_odd;
let triangles = 4 * (1..copies_per_dir).map(|i| if i.is_odd() {i*pos_per_odd} else {i*pos_per_even}).sum::<usize>();
let lefttop_odd = floodfill(&grid, (0,0), 64).iter().filter(|(_, dist)| dist.is_odd()).count();
let righttop_odd = floodfill(&grid, (0,y_len-1), 64).iter().filter(|(_, dist)| dist.is_odd()).count();
let leftbot_odd = floodfill(&grid, (x_len-1,0), 64).iter().filter(|(_, dist)| dist.is_odd()).count();
let rightbot_odd = floodfill(&grid, (x_len-1,y_len-1), 64).iter().filter(|(_, dist)| dist.is_odd()).count();
let lefttop_even = floodfill(&grid, (0,0), 64).iter().filter(|(_, dist)| dist.is_even()).count();
let righttop_even = floodfill(&grid, (0,y_len-1), 64).iter().filter(|(_, dist)| dist.is_even()).count();
let leftbot_even = floodfill(&grid, (x_len-1,0), 64).iter().filter(|(_, dist)| dist.is_even()).count();
let rightbot_even = floodfill(&grid, (x_len-1,y_len-1), 64).iter().filter(|(_, dist)| dist.is_even()).count();
let mut result = cross+triangles;
result -= (copies_per_dir+1) * (lefttop_odd + righttop_odd + leftbot_odd + rightbot_odd + 1); // last 1 is very odd offset that fixes the result
result += copies_per_dir * (lefttop_even + righttop_even + leftbot_even + rightbot_even);
println!("Result 2: {result}"); println!("Result 2: {result}");
}
fn find_char_location(matrix: &Vec<Vec<char>>, target_char: char) -> Option<(usize, usize)> {
for (row_index, row) in matrix.iter().enumerate() {
for (col_index, &ch) in row.iter().enumerate() {
if ch == target_char {
return Some((row_index, col_index));
}
}
}
None
}
fn floodfill(grid: &Vec<Vec<char>>, s: (usize,usize), steps: usize) -> HashMap<(usize,usize), usize> {
let mut map: HashMap<(usize,usize), usize> = HashMap::new();
let mut q: VecDeque<((usize, usize), usize)> = VecDeque::new();
q.push_back((s, 0));
while !q.is_empty() {
let curr = q.pop_front().unwrap();
if curr.1 > steps || map.contains_key(&curr.0) {
continue
}
map.insert(curr.0, curr.1);
let mut next: Vec<(usize,usize)> = vec!();
if curr.0.0 > 0 {
next.push((curr.0.0-1, curr.0.1))
}
if curr.0.1 > 0 {
next.push((curr.0.0, curr.0.1-1))
}
if curr.0.0 < grid.len()-1 {
next.push((curr.0.0+1, curr.0.1))
}
if curr.0.1 < grid[0].len()-1 {
next.push((curr.0.0, curr.0.1+1))
}
for pos in next {
if !map.contains_key(&pos) && grid[pos.0][pos.1] != '#' {
q.push_back((pos, curr.1+1));
}
}
}
map
} }

View file

@ -5,7 +5,7 @@ extern crate scan_fmt;
use std::time::Instant; use std::time::Instant;
fn main() { fn main() {
days::d20::solve() days::d21::solve()
//_all_days() //_all_days()
} }
@ -93,11 +93,11 @@ fn _all_days() {
days::d20::solve(); days::d20::solve();
time = _print_elapsed(time); time = _print_elapsed(time);
/*
println!("\nDay 21"); println!("\nDay 21");
days::d21::solve(); days::d21::solve();
time = _print_elapsed(time); time = _print_elapsed(time);
/*
println!("\nDay 22"); println!("\nDay 22");
days::d22::solve(); days::d22::solve();
time = _print_elapsed(time); time = _print_elapsed(time);