feat: implement day 11 and fix day 10
This commit is contained in:
		
							parent
							
								
									575e692eb7
								
							
						
					
					
						commit
						f1c0c453d3
					
				
							
								
								
									
										23
									
								
								src/day10.rs
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								src/day10.rs
									
									
									
									
									
								
							@ -87,7 +87,7 @@ pub fn execute_task02(content: &str) {
 | 
			
		||||
    let duration = start.elapsed();
 | 
			
		||||
 | 
			
		||||
    //assert_eq!(957, command_steps);
 | 
			
		||||
    println!("Day09 - Task02 - Duration: {duration:?} - Sum of B: {}", command_steps)
 | 
			
		||||
    println!("Day10 - Task02 - Duration: {duration:?} - Caged Tiles: {}", command_steps)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -111,7 +111,7 @@ fn solve_02(content: &str) -> anyhow::Result<i32> {
 | 
			
		||||
 | 
			
		||||
    map[starting.1.0 as usize][starting.1.1 as usize] = starting.0;
 | 
			
		||||
 | 
			
		||||
    let from = (starting.1.0 + directions[0].0, starting.1.1 + directions[1].0);
 | 
			
		||||
    let from = (starting.1.0 + directions[0].0, starting.1.1 + directions[0].1);
 | 
			
		||||
    let mut pipe_network = vec![from ,starting.1];
 | 
			
		||||
 | 
			
		||||
    solve_pipeline(&map, &mut pipe_network);
 | 
			
		||||
@ -125,22 +125,30 @@ fn solve_02(content: &str) -> anyhow::Result<i32> {
 | 
			
		||||
        .for_each(|(y, column)| column.iter()
 | 
			
		||||
            .enumerate()
 | 
			
		||||
            .for_each(|(x, pipe)| {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
                if pipe_network.contains(&(y as i32, x as i32)) {
 | 
			
		||||
                    if pipe == &Pipe::Horizontal {
 | 
			
		||||
                        return
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                if pipe_network.contains(&(y as i32, x as i32)) {
 | 
			
		||||
                    match pipe {
 | 
			
		||||
                        Pipe::Vertical => is_in_pipe = !is_in_pipe,
 | 
			
		||||
                        Pipe::L => is_in_pipe = !is_in_pipe,
 | 
			
		||||
                        Pipe::F => is_in_pipe = !is_in_pipe,
 | 
			
		||||
                        Pipe::L => {
 | 
			
		||||
                            last_change = pipe.clone();
 | 
			
		||||
                            is_in_pipe = !is_in_pipe
 | 
			
		||||
                        },
 | 
			
		||||
                        Pipe::F => {
 | 
			
		||||
                            last_change = pipe.clone();
 | 
			
		||||
                            is_in_pipe = !is_in_pipe
 | 
			
		||||
                        },
 | 
			
		||||
                        Pipe::Pipe7 => {
 | 
			
		||||
                            if last_change != Pipe::F {
 | 
			
		||||
                            if last_change == Pipe::F {
 | 
			
		||||
                                is_in_pipe = !is_in_pipe
 | 
			
		||||
                            }
 | 
			
		||||
                        },
 | 
			
		||||
                        Pipe::J => {
 | 
			
		||||
                            if last_change != Pipe::L {
 | 
			
		||||
                            if last_change == Pipe::L {
 | 
			
		||||
                                is_in_pipe = !is_in_pipe
 | 
			
		||||
                            }
 | 
			
		||||
                        },
 | 
			
		||||
@ -148,7 +156,6 @@ fn solve_02(content: &str) -> anyhow::Result<i32> {
 | 
			
		||||
                    };
 | 
			
		||||
                } else {
 | 
			
		||||
                    if is_in_pipe {
 | 
			
		||||
                        println!("Find Tile: ({y},{x})");
 | 
			
		||||
                        size_of_caged_tiles += 1;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										140
									
								
								src/day11.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										140
									
								
								src/day11.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,140 @@
 | 
			
		||||
use std::cmp;
 | 
			
		||||
use std::time::Instant;
 | 
			
		||||
use itertools::Itertools;
 | 
			
		||||
use num::abs;
 | 
			
		||||
 | 
			
		||||
pub fn execute_task01(content: &str) {
 | 
			
		||||
    let start = Instant::now();
 | 
			
		||||
    let sum_of_b = solve_01(content).unwrap();
 | 
			
		||||
    let duration = start.elapsed();
 | 
			
		||||
 | 
			
		||||
    assert_eq!(10165598, sum_of_b);
 | 
			
		||||
    println!("Day11 - Task01 - Duration: {duration:?} - Sum of closest Points: {}", sum_of_b)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn execute_task02(content: &str) {
 | 
			
		||||
    let start = Instant::now();
 | 
			
		||||
    let command_steps = solve_02(content).unwrap();
 | 
			
		||||
    let duration = start.elapsed();
 | 
			
		||||
 | 
			
		||||
    //assert_eq!(957, command_steps);
 | 
			
		||||
    println!("Day11 - Task02 - Duration: {duration:?} - SUm of closest million Points: {}", command_steps)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
fn solve_01(content: &str) -> anyhow::Result<usize> {
 | 
			
		||||
    solve(content, 2)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn solve_02(content: &str) -> anyhow::Result<usize> {
 | 
			
		||||
    solve(content, 1_000_000)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn solve(content: &str, expansion: usize) -> anyhow::Result<usize> {
 | 
			
		||||
    let height = content.lines().count();
 | 
			
		||||
    let width = content.lines().next().unwrap().chars().count();
 | 
			
		||||
 | 
			
		||||
    let mut galaxies = content
 | 
			
		||||
        .lines()
 | 
			
		||||
        .enumerate()
 | 
			
		||||
        .flat_map(|(y, line)|
 | 
			
		||||
            line
 | 
			
		||||
                .chars()
 | 
			
		||||
                .enumerate()
 | 
			
		||||
                .filter_map(|(x, c)| {
 | 
			
		||||
                    if c == '#' {
 | 
			
		||||
                        return Some((y, x));
 | 
			
		||||
                    }
 | 
			
		||||
                    None
 | 
			
		||||
                })
 | 
			
		||||
                .collect::<Vec<(usize, usize)>>()
 | 
			
		||||
        )
 | 
			
		||||
        .collect::<Vec<(usize, usize)>>();
 | 
			
		||||
 | 
			
		||||
    let mut empty_y: Vec<usize> = (0..height).collect();
 | 
			
		||||
    let mut empty_x: Vec<usize> = (0..width).collect();
 | 
			
		||||
 | 
			
		||||
    galaxies
 | 
			
		||||
        .iter()
 | 
			
		||||
        .for_each(|(y, x)| {
 | 
			
		||||
            empty_y.retain(|value| *value != *y);
 | 
			
		||||
            empty_x.retain(|value| *value != *x);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
    galaxies = galaxies
 | 
			
		||||
        .iter()
 | 
			
		||||
        .map(|(y, x)| {
 | 
			
		||||
            (y + (empty_y.iter().filter(|filler| filler < &y).count() * (expansion - 1)), x + (empty_x.iter().filter(|filler| filler < &x).count() * (expansion - 1)))
 | 
			
		||||
        })
 | 
			
		||||
        .collect();
 | 
			
		||||
 | 
			
		||||
    Ok(galaxies.clone().iter()
 | 
			
		||||
        .combinations(2)
 | 
			
		||||
        .map(|data| (data[0], data[1]))
 | 
			
		||||
        .map(|(from, to)| calculate_diff(from.clone(), to.clone()))
 | 
			
		||||
        .sum())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn calculate_diff(from: (usize, usize), to: (usize, usize)) -> usize {
 | 
			
		||||
    let min_y = cmp::min(from.0, to.0) as i32;
 | 
			
		||||
    let min_x = cmp::min(from.1, to.1) as i32;
 | 
			
		||||
    let max_y = cmp::max(from.0, to.0) as i32;
 | 
			
		||||
    let max_x = cmp::max(from.1, to.1) as i32;
 | 
			
		||||
 | 
			
		||||
    (abs(max_y - min_y) + abs(max_x - min_x)) as usize
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn test_solve_01() {
 | 
			
		||||
    let example = r#"...#......
 | 
			
		||||
.......#..
 | 
			
		||||
#.........
 | 
			
		||||
..........
 | 
			
		||||
......#...
 | 
			
		||||
.#........
 | 
			
		||||
.........#
 | 
			
		||||
..........
 | 
			
		||||
.......#..
 | 
			
		||||
#...#....."#;
 | 
			
		||||
 | 
			
		||||
    let result = solve_01(example).unwrap();
 | 
			
		||||
 | 
			
		||||
    assert_eq!(374, result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn test_solve_02() {
 | 
			
		||||
    let example = r#"...#......
 | 
			
		||||
.......#..
 | 
			
		||||
#.........
 | 
			
		||||
..........
 | 
			
		||||
......#...
 | 
			
		||||
.#........
 | 
			
		||||
.........#
 | 
			
		||||
..........
 | 
			
		||||
.......#..
 | 
			
		||||
#...#....."#;
 | 
			
		||||
 | 
			
		||||
    let result = solve(example, 10).unwrap();
 | 
			
		||||
 | 
			
		||||
    assert_eq!(1030, result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn test_solve_02_2() {
 | 
			
		||||
    let example = r#"...#......
 | 
			
		||||
.......#..
 | 
			
		||||
#.........
 | 
			
		||||
..........
 | 
			
		||||
......#...
 | 
			
		||||
.#........
 | 
			
		||||
.........#
 | 
			
		||||
..........
 | 
			
		||||
.......#..
 | 
			
		||||
#...#....."#;
 | 
			
		||||
 | 
			
		||||
    let result = solve(example, 100).unwrap();
 | 
			
		||||
 | 
			
		||||
    assert_eq!(8410, result);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										140
									
								
								src/input/day11/input.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										140
									
								
								src/input/day11/input.txt
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,140 @@
 | 
			
		||||
.#..........#.....................#........................................................#....................................#...........
 | 
			
		||||
...................#........................................#......................#........................................................
 | 
			
		||||
...........................................#......#..................#...................................................#..................
 | 
			
		||||
....#......................#...............................................#.........................#.....#......#................#........
 | 
			
		||||
.................................................................#.......................#..................................................
 | 
			
		||||
..............................................................................................................................#.............
 | 
			
		||||
.......#........#.............#...................................................#...................................#.....................
 | 
			
		||||
.........................#...................................#............................................................................#.
 | 
			
		||||
....................#.............#.........#..........................................#...............#....................................
 | 
			
		||||
.....................................................#...............#.............................................................#........
 | 
			
		||||
...........................................................................................................................#................
 | 
			
		||||
..........................................................#.........................................#...............#.......................
 | 
			
		||||
..............................................................................................#.............................................
 | 
			
		||||
.#........#........................#......#..............................................#..............................#...............#...
 | 
			
		||||
...............#......#...............................#.......#.........#......#.............................#.................#............
 | 
			
		||||
...............................#..............#.............................................................................................
 | 
			
		||||
............................................................................................................................................
 | 
			
		||||
.........................#.......................................#..........................................................................
 | 
			
		||||
..................#........................#......................................#........................................#................
 | 
			
		||||
..................................................#.......#..............................................#........................#.......#.
 | 
			
		||||
...#........#........................#....................................#...............#.................................................
 | 
			
		||||
............................................................................................................................................
 | 
			
		||||
.......#........................#....................................................................................#......................
 | 
			
		||||
...............#...........#........................................................................#.......................................
 | 
			
		||||
.............................................#................#.........#.............................................................#.....
 | 
			
		||||
...........#...........................#................#...........................#.......................................................
 | 
			
		||||
..................................................#.......................................#.............#...................................
 | 
			
		||||
.....#..................#..........#..............................#.............................#........................................#..
 | 
			
		||||
..............................#...............................................................................................#.............
 | 
			
		||||
...................................................................................................................#........................
 | 
			
		||||
..............#..........................................#.............#...........................#..............................#.........
 | 
			
		||||
...#...................................#.........................................#..........................................................
 | 
			
		||||
..................#...................................................................#................................................#....
 | 
			
		||||
..........................#...................................#.........................................#...................................
 | 
			
		||||
............................................................................................................................................
 | 
			
		||||
.........#...........#..........#..........................................#....................#..............#........#.....#...........#.
 | 
			
		||||
....................................................#.......................................................................................
 | 
			
		||||
.............................................#..............................................................................................
 | 
			
		||||
......................................#..................#..............................#...........#.......#...............................
 | 
			
		||||
............................#..................................#..............#.............................................................
 | 
			
		||||
...............#............................................................................................................................
 | 
			
		||||
...........................................#............................#.......................#...........................................
 | 
			
		||||
.....#..............#.................................................................................#...........................#.........
 | 
			
		||||
............................................................................................................................................
 | 
			
		||||
........................................#..........#.................#..........................................#...........................
 | 
			
		||||
.............................................#.................................#...........#................................................
 | 
			
		||||
...............................#.......................#...........................................#.......#....................#...........
 | 
			
		||||
#........#.............#...............................................................................................#...................#
 | 
			
		||||
.....................................#........................#..........#.........#........................................................
 | 
			
		||||
.................#............................................................................#.............................................
 | 
			
		||||
..........................#.......................................#.................................................#.......................
 | 
			
		||||
.....................................................#...................................#....................................#.............
 | 
			
		||||
..#.........................................................................................................................................
 | 
			
		||||
.............#.....#.......................#.............#....................................................#........................#....
 | 
			
		||||
......#........................................................................................#............................................
 | 
			
		||||
.................................#.................................#........................................................................
 | 
			
		||||
.................................................................................#.......................#................................#.
 | 
			
		||||
#.....................................................#...................................................................#.....#...........
 | 
			
		||||
.........#..................................................#......................................#........................................
 | 
			
		||||
..............................#.............#.................................................................#.......#.....................
 | 
			
		||||
........................#............................................................#......#................................#..............
 | 
			
		||||
.............#..............................................................................................................................
 | 
			
		||||
.....#........................................................#............#..........................#...........................#.........
 | 
			
		||||
...........................#......#.................................................................................#...................#...
 | 
			
		||||
.........#.....................................#...............................#............................................................
 | 
			
		||||
..................#.......................................#.......#................................#........................................
 | 
			
		||||
...............................#.....................#...................................#................................#.................
 | 
			
		||||
..............#..........................#.............................#............#.....................#.....................#.........#.
 | 
			
		||||
.................................................................................................................#..........................
 | 
			
		||||
......#.....................................................................................................................................
 | 
			
		||||
...........#.............#.........#........................................................................................................
 | 
			
		||||
...................................................................#..............#..................#......................................
 | 
			
		||||
..................#...........................................................................................#.............................
 | 
			
		||||
........#.................................#..............#.................................................................#..........#.....
 | 
			
		||||
...#.....................................................................#..............#.......#...........................................
 | 
			
		||||
............................................................................................................................................
 | 
			
		||||
............................................................................................................................................
 | 
			
		||||
.................................#................#..........................................................#...............#.............#
 | 
			
		||||
..............#............................................................#................................................................
 | 
			
		||||
......#.....................#..........................#........#.....#............#..............#.........................................
 | 
			
		||||
....................#..................................................................................#..................#.................
 | 
			
		||||
....................................#.........#........................................................................................#....
 | 
			
		||||
..........................................................#..............#..................................................................
 | 
			
		||||
................#...............................................................................................#.............#.............
 | 
			
		||||
............................................................................................................................................
 | 
			
		||||
............................#...................#......#.............#..............................................#.......................
 | 
			
		||||
.............#...................................................................................#........................................#.
 | 
			
		||||
......................#..........................................................#.......#..................................................
 | 
			
		||||
#...........................................................#...............................................................................
 | 
			
		||||
.........#.........................#.................#....................................................#.................#...............
 | 
			
		||||
............................................................................................................................................
 | 
			
		||||
...................#.............................................#.....#....................................................................
 | 
			
		||||
........................#.........................#.....................................................................................#...
 | 
			
		||||
.....#........................................................................................................................#.............
 | 
			
		||||
.......................................#...............................................................#.........#..........................
 | 
			
		||||
............................#...........................#...........#......................#..............................#.......#.........
 | 
			
		||||
..#................................#...........#............................................................................................
 | 
			
		||||
.................#..................................#...................#...................................................................
 | 
			
		||||
..........#.................................................#................................................#..............................
 | 
			
		||||
.....................#...............................................................................#......................................
 | 
			
		||||
..............#..................#..............................#.............#....................................#.........#........#.....
 | 
			
		||||
...#.............................................................................................#..........................................
 | 
			
		||||
............................................#..........................................#....................................................
 | 
			
		||||
.........#...............................................#..............................................................#...................
 | 
			
		||||
................#.........#.................................................................................................................
 | 
			
		||||
.....#..................................#............................#..............................#........#..................#..........#
 | 
			
		||||
...............................#............................................................................................................
 | 
			
		||||
....................................................#........#..............................................................................
 | 
			
		||||
........#..........................#.........#...............................................................................#..............
 | 
			
		||||
....................................................................................................................#.......................
 | 
			
		||||
....................#........#......................................#.........#.....#.....#.................................................
 | 
			
		||||
................................................................................................................................#.....#.....
 | 
			
		||||
...............#..........................................#...............#...............................#.................................
 | 
			
		||||
...#..............................#.........................................................................................................
 | 
			
		||||
........................................................................................#............#....................................#.
 | 
			
		||||
............................................................................................................................#...............
 | 
			
		||||
...................................................................................#............................#...........................
 | 
			
		||||
#........#........#........#..........#.........#...........................................................................................
 | 
			
		||||
.............................................................#..............................#............................#..................
 | 
			
		||||
................................#.......................#...................................................................................
 | 
			
		||||
.......................#................................................#...............#.........#..................#................#.....
 | 
			
		||||
............#..................................................................#........................#...................................
 | 
			
		||||
............................................................................................................................................
 | 
			
		||||
......#.....................#.......#......#..............#......#..........................................................................
 | 
			
		||||
.....................#......................................................................................................................
 | 
			
		||||
................................#................................................#.............#...........................#.............#..
 | 
			
		||||
..............................................................................................................#.............................
 | 
			
		||||
............#............#..........................................................................#.............................#.........
 | 
			
		||||
.....#...........#.....................#.............................#.....#........................................#.......................
 | 
			
		||||
................................................#...........#..........................#....................................................
 | 
			
		||||
............................................................................................................................................
 | 
			
		||||
#..........................#.....#................................................................#......................................#..
 | 
			
		||||
..........................................#.................................................................#.............#.................
 | 
			
		||||
................#.........................................................#.................................................................
 | 
			
		||||
.......#..............................#..............................................#..........................................#...........
 | 
			
		||||
........................#...................................#...............................................................................
 | 
			
		||||
............................................................................................................................#...............
 | 
			
		||||
..............................#.....................#.............................#...........#.................#...........................
 | 
			
		||||
...............................................#................#.....#................................................#....................
 | 
			
		||||
...................#...................#.................#.................#.........................#......................................
 | 
			
		||||
@ -9,6 +9,7 @@ mod day07;
 | 
			
		||||
mod day08;
 | 
			
		||||
mod day09;
 | 
			
		||||
mod day10;
 | 
			
		||||
mod day11;
 | 
			
		||||
 | 
			
		||||
const CONTENT01: &'static str = include_str!("input/day01/input.txt");
 | 
			
		||||
const CONTENT02: &'static str = include_str!("input/day02/input.txt");
 | 
			
		||||
@ -20,6 +21,7 @@ const CONTENT07: &'static str = include_str!("input/day07/input.txt");
 | 
			
		||||
const CONTENT08: &'static str = include_str!("input/day08/input.txt");
 | 
			
		||||
const CONTENT09: &'static str = include_str!("input/day09/input.txt");
 | 
			
		||||
const CONTENT10: &'static str = include_str!("input/day10/input.txt");
 | 
			
		||||
const CONTENT11: &'static str = include_str!("input/day11/input.txt");
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
    day01::execute_task01(CONTENT01);
 | 
			
		||||
@ -51,4 +53,7 @@ fn main() {
 | 
			
		||||
    println!();
 | 
			
		||||
    day10::execute_task01(CONTENT10);
 | 
			
		||||
    day10::execute_task02(CONTENT10);
 | 
			
		||||
    println!();
 | 
			
		||||
    day11::execute_task01(CONTENT11);
 | 
			
		||||
    day11::execute_task02(CONTENT11);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user