Skip to content
Snippets Groups Projects
Verified Commit 49fae21e authored by Falk Rehse's avatar Falk Rehse
Browse files

Day 10

10-2 sadly gives incorrect results
parent 3f11be35
No related branches found
No related tags found
No related merge requests found
use core::panic;
use std::dbg;
use crate::util::*;
pub fn solve_from_str(input: &str) -> usize {
let grid = to_grid(&input);
let mut start_x = 0;
let mut start_y = 0;
for x in 0..grid.len() {
for y in 0..grid[0].len() {
if grid[x][y] == 'S' {
start_x = x;
start_y = y;
}
}
}
let mut old_0 = (start_x, start_y);
let mut pos_0;
let mut old_1 = (start_x, start_y);
let mut pos_1;
(pos_0, pos_1) = get_valid_pipes(&grid, start_x, start_y);
for steps in 1..usize::MAX {
if steps != 1 && pos_0 == pos_1 {
return steps;
}
let tmp_0 = pos_0;
let tmp_1 = pos_1;
let (new_0_0, new_0_1) = get_valid_pipes(&grid, pos_0.0, pos_0.1);
if new_0_0 == old_0 {
pos_0 = new_0_1;
} else {
pos_0 = new_0_0;
}
let (new_1_0, new_1_1) = get_valid_pipes(&grid, pos_1.0, pos_1.1);
if new_1_0 == old_1 {
pos_1 = new_1_1;
} else {
pos_1 = new_1_0;
}
old_0 = tmp_0;
old_1 = tmp_1;
}
panic!();
}
fn get_valid_pipes(grid: &Vec<Vec<char>>, x: usize, y: usize) -> ((usize, usize), (usize, usize)) {
let mut valid = Vec::new();
match grid[x][y] {
'-' => {
add_if_valid_left(&mut valid, grid, x, y);
add_if_valid_right(&mut valid, grid, x, y);
}
'|' => {
add_if_valid_top(&mut valid, grid, x, y);
add_if_valid_bottom(&mut valid, grid, x, y);
}
'F' => {
add_if_valid_right(&mut valid, grid, x, y);
add_if_valid_bottom(&mut valid, grid, x, y);
}
'7' => {
add_if_valid_left(&mut valid, grid, x, y);
add_if_valid_bottom(&mut valid, grid, x, y);
}
'J' => {
add_if_valid_top(&mut valid, grid, x, y);
add_if_valid_left(&mut valid, grid, x, y);
}
'L' => {
add_if_valid_top(&mut valid, grid, x, y);
add_if_valid_right(&mut valid, grid, x, y);
}
'S' => {
add_if_valid_left(&mut valid, grid, x, y);
add_if_valid_right(&mut valid, grid, x, y);
add_if_valid_top(&mut valid, grid, x, y);
add_if_valid_bottom(&mut valid, grid, x, y);
}
_ => panic!(),
}
assert!(valid.len() == 2);
(valid[0], valid[1])
}
fn add_if_valid_top(valid: &mut Vec<(usize, usize)>, grid: &Vec<Vec<char>>, x: usize, y: usize) {
if y >= 1
&& (grid[x][y - 1] == '|'
|| grid[x][y - 1] == 'F'
|| grid[x][y - 1] == '7'
|| grid[x][y - 1] == 'S')
{
valid.push((x, y - 1));
}
}
fn add_if_valid_bottom(valid: &mut Vec<(usize, usize)>, grid: &Vec<Vec<char>>, x: usize, y: usize) {
if y + 1 < grid[x].len()
&& (grid[x][y + 1] == '|'
|| grid[x][y + 1] == 'L'
|| grid[x][y + 1] == 'J'
|| grid[x][y + 1] == 'S')
{
valid.push((x, y + 1));
}
}
fn add_if_valid_left(valid: &mut Vec<(usize, usize)>, grid: &Vec<Vec<char>>, x: usize, y: usize) {
if x >= 1
&& (grid[x - 1][y] == '-'
|| grid[x - 1][y] == 'F'
|| grid[x - 1][y] == 'L'
|| grid[x - 1][y] == 'S')
{
valid.push((x - 1, y));
}
}
fn add_if_valid_right(valid: &mut Vec<(usize, usize)>, grid: &Vec<Vec<char>>, x: usize, y: usize) {
if x + 1 < grid.len()
&& (grid[x + 1][y] == '-'
|| grid[x + 1][y] == '7'
|| grid[x + 1][y] == 'J'
|| grid[x + 1][y] == 'S')
{
valid.push((x + 1, y));
}
}
use core::panic;
use std::{collections::HashMap, print};
use crate::util::*;
pub fn solve_from_str(input: &str) -> i32 {
let grid = to_grid(&input);
let mut start_x = 0;
let mut start_y = 0;
for x in 0..grid.len() {
for y in 0..grid[0].len() {
if grid[x][y] == 'S' {
start_x = x as i32;
start_y = y as i32;
}
}
}
let mut map = HashMap::new();
let mut pos_0 = (start_x, start_y);
let mut old_0= pos_0;
// map.insert((start_x, start_y), Tile::Pipe);
let (move_0, move_1) = get_valid_pipes(&grid, start_x as usize, start_y as usize);
mark_inside(move_1, pos_0, &mut map);
pos_0 = (move_1.0 + start_x as i32, move_1.1 + start_y as i32);
for steps in 1..usize::MAX {
if steps != 1 && pos_0 == (start_x, start_y) {
break;
}
// map.insert(pos_0, Tile::Pipe);
let tmp_0 = pos_0;
let (move_0, move_1) = get_valid_pipes(&grid, pos_0.0 as usize, pos_0.1 as usize);
let new_0_0 = (move_0.0 + pos_0.0, move_0.1 + pos_0.1);
if new_0_0 == old_0 {
mark_inside(move_1, pos_0, &mut map);
pos_0 = (move_1.0 + pos_0.0, move_1.1 + pos_0.1);
} else {
mark_inside(move_0, pos_0, &mut map);
pos_0 = new_0_0;
}
old_0 = tmp_0;
}
let mut sum = 0;
for i in 0..grid[0].len() {
for k in 0..grid.len() {
if map.contains_key(&(k as i32, i as i32)) {
if Tile::Inside.eq(map.get(&(k as i32, i as i32)).unwrap()) {
flood_fill((k as i32, i as i32), &mut map);
}
}
}
}
for i in 0..grid[0].len() {
for k in 0..grid.len() {
if map.contains_key(&(k as i32, i as i32)) {
if Tile::Pipe.eq(map.get(&(k as i32, i as i32)).unwrap()) {
print!("+");
}
if Tile::InsideFilled.eq(map.get(&(k as i32, i as i32)).unwrap()) {
print!("X");
sum += 1;
}
if Tile::Inside.eq(map.get(&(k as i32, i as i32)).unwrap()) {
print!("!");
panic!();
}
} else {
print!(".");
}
}
println!();
}
sum
}
fn get_valid_pipes(grid: &Vec<Vec<char>>, x: usize, y: usize) -> ((i32, i32), (i32, i32)) {
let mut valid = Vec::new();
match grid[x][y] {
'-' => {
add_if_valid_left(&mut valid, grid, x, y);
add_if_valid_right(&mut valid, grid, x, y);
}
'|' => {
add_if_valid_top(&mut valid, grid, x, y);
add_if_valid_bottom(&mut valid, grid, x, y);
}
'F' => {
add_if_valid_right(&mut valid, grid, x, y);
add_if_valid_bottom(&mut valid, grid, x, y);
}
'7' => {
add_if_valid_left(&mut valid, grid, x, y);
add_if_valid_bottom(&mut valid, grid, x, y);
}
'J' => {
add_if_valid_top(&mut valid, grid, x, y);
add_if_valid_left(&mut valid, grid, x, y);
}
'L' => {
add_if_valid_top(&mut valid, grid, x, y);
add_if_valid_right(&mut valid, grid, x, y);
}
'S' => {
add_if_valid_left(&mut valid, grid, x, y);
add_if_valid_right(&mut valid, grid, x, y);
add_if_valid_top(&mut valid, grid, x, y);
add_if_valid_bottom(&mut valid, grid, x, y);
}
_ => panic!(),
}
assert!(valid.len() == 2);
(valid[0], valid[1])
}
fn add_if_valid_top(valid: &mut Vec<(i32, i32)>, grid: &Vec<Vec<char>>, x: usize, y: usize) {
if y >= 1
&& (grid[x][y - 1] == '|'
|| grid[x][y - 1] == 'F'
|| grid[x][y - 1] == '7'
|| grid[x][y - 1] == 'S')
{
valid.push((0, -1));
}
}
fn add_if_valid_bottom(valid: &mut Vec<(i32, i32)>, grid: &Vec<Vec<char>>, x: usize, y: usize) {
if y + 1 < grid[x].len()
&& (grid[x][y + 1] == '|'
|| grid[x][y + 1] == 'L'
|| grid[x][y + 1] == 'J'
|| grid[x][y + 1] == 'S')
{
valid.push((0, 1));
}
}
fn add_if_valid_left(valid: &mut Vec<(i32, i32)>, grid: &Vec<Vec<char>>, x: usize, y: usize) {
if x >= 1
&& (grid[x - 1][y] == '-'
|| grid[x - 1][y] == 'F'
|| grid[x - 1][y] == 'L'
|| grid[x - 1][y] == 'S')
{
valid.push((-1, 0));
}
}
fn add_if_valid_right(valid: &mut Vec<(i32, i32)>, grid: &Vec<Vec<char>>, x: usize, y: usize) {
if x + 1 < grid.len()
&& (grid[x + 1][y] == '-'
|| grid[x + 1][y] == '7'
|| grid[x + 1][y] == 'J'
|| grid[x + 1][y] == 'S')
{
valid.push((1, 0));
}
}
fn mark_inside(dir: (i32, i32), pos: (i32, i32), map: &mut HashMap<(i32, i32), Tile>) {
match dir {
(1, 0) => {
if !map.contains_key(&(pos.0, pos.1-1)) {
map.insert((pos.0, pos.1-1), Tile::Inside);
}
// if !map.contains_key(&(pos.0+1, pos.1-1)) {
// map.insert((pos.0+1, pos.1-1), Tile::Inside);
// }
},
(-1, 0) => {
if !map.contains_key(&(pos.0, pos.1+1)) {
map.insert((pos.0, pos.1+1), Tile::Inside);
}
// if !map.contains_key(&(pos.0-1, pos.1+1)) {
// map.insert((pos.0-1, pos.1+1), Tile::Inside);
// }
},
(0,1) => {
if !map.contains_key(&(pos.0+1, pos.1)) {
map.insert((pos.0+1, pos.1), Tile::Inside);
}
// if !map.contains_key(&(pos.0+1, pos.1+1)) {
// map.insert((pos.0+1, pos.1+1), Tile::Inside);
// }
},
(0,-1) => {
if !map.contains_key(&(pos.0-1, pos.1)) {
map.insert((pos.0-1, pos.1), Tile::Inside);
}
// if !map.contains_key(&(pos.0-1, pos.1-1)) {
// map.insert((pos.0-1, pos.1-1), Tile::Inside);
// }
},
_ => panic!(),
};
}
fn flood_fill(pos: (i32, i32), map: &mut HashMap<(i32, i32), Tile>) {
if !map.contains_key(&pos) {
map.insert(pos, Tile::Inside);
flood_fill(pos, map);
} else {
if Tile::Inside.eq(map.get(&pos).unwrap()) {
map.insert(pos, Tile::InsideFilled);
flood_fill((pos.0+1, pos.1), map);
flood_fill((pos.0-1, pos.1), map);
flood_fill((pos.0, pos.1+1), map);
flood_fill((pos.0, pos.1-1), map);
}
}
}
#[derive(Debug, Eq, Hash, PartialEq)]
enum Tile {
Pipe,
Inside,
InsideFilled,
}
use crate::util::*; use crate::util::*;
pub fn solve_from_str(input: &str) -> u32 { pub fn solve_from_str(input: &str) -> u32 {
let grid = to_grid(input); let grid = to_grid_flipped_axis(input);
let mut parts = Vec::new(); let mut parts = Vec::new();
let mut number_in_progress = false; let mut number_in_progress = false;
......
use crate::util::*; use crate::util::*;
pub fn solve_from_str(input: &str) -> u32 { pub fn solve_from_str(input: &str) -> u32 {
let grid = to_grid(input); let grid = to_grid_flipped_axis(input);
let mut gear_ratios = Vec::new(); let mut gear_ratios = Vec::new();
......
mod day_10_1;
mod day_10_2;
mod day_1_1; mod day_1_1;
mod day_1_2; mod day_1_2;
mod day_2_1; mod day_2_1;
...@@ -24,7 +26,7 @@ use std::fs; ...@@ -24,7 +26,7 @@ use std::fs;
fn main() { fn main() {
let input = fs::read_to_string("input").expect("Could not read input!"); let input = fs::read_to_string("input").expect("Could not read input!");
let solution = day_9_2::solve_from_str(input.trim_end()); let solution = day_10_2::solve_from_str(input.trim_end());
println!("Solution: {}", solution); println!("Solution: {}", solution);
} }
use std::println;
use rayon::collections::binary_heap::Iter;
use regex::Regex; use regex::Regex;
pub fn regex_positive_number(input: &str) -> u32 { pub fn regex_positive_number(input: &str) -> u32 {
...@@ -30,6 +33,18 @@ pub fn regex_positive_numbers_u64(input: &str) -> Vec<u64> { ...@@ -30,6 +33,18 @@ pub fn regex_positive_numbers_u64(input: &str) -> Vec<u64> {
.collect() .collect()
} }
pub fn to_grid(input: &str) -> Vec<Vec<char>> { pub fn to_grid_flipped_axis(input: &str) -> Vec<Vec<char>> {
input.lines().map(|line| line.chars().collect()).collect() input.lines().map(|line| line.chars().collect()).collect()
} }
pub fn to_grid(input: &str) -> Vec<Vec<char>> {
let width = input.lines().next().unwrap().chars().count();
let height = input.lines().count();
println!("Read {} x {} grid!", width, height);
let mut lines: Vec<_> = input.lines().map(|line| line.chars()).collect();
(0..width)
.map(|i| (0..height).map(|k| lines[k].next().unwrap()).collect())
.collect()
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment