initial commit

This commit is contained in:
Jakob Lechner 2024-12-07 17:45:16 +01:00
commit cf7d87c04d
34 changed files with 4282 additions and 0 deletions

1
day6/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

7
day6/Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day6"
version = "0.1.0"

6
day6/Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "day6"
version = "0.1.0"
edition = "2021"
[dependencies]

130
day6/input Normal file
View file

@ -0,0 +1,130 @@
........#........................................#......#........#................................................................
....................................#......#.....#............#.............#..........#..........................................
......................#.......................................................#...................................................
.......#..#..#....#...#...#....#..............#......#.......#...#................#.......#.......................................
......................#....##...#.......#....#.......................................#....................#.......................
...#............................#........................................#..........................#.....................#.......
....................#............#...............#......#.........#...........#...................................................
............................#......#...#................#.............#...........................................................
.....#..#.........#....................#......................................................#........................#.........#
.........#..##.#.........#.............................................#...........#........#....................##...............
...............#....#.........................##......#.....................#..............................................#......
..................##...................................#...........#........#....#.............#..................#........#.#....
....................................#...................#..............................#............#.............................
.........#.....#................#..........................................#...................................#..............#...
...#....................#...................................#..##...................#.......#......................###.........#..
....................#............#....#.##....#.........#......#...#........................#.......................#..........#..
..............#...................................................................................#....................#..........
.........#................#..............................#............................#...#.................#...............#.....
............................................................................................#...#............................#....
............#....................#................................#....#...............................#....#.....................
........................................#.........#..................................................#..#..................#......
.............#.#............................#..#.....#............................................#....#..........................
................................................................................................#.........#..#..............#..#..
...........#..........#.#..#................#.#..#...#.........#..........................................#..........#..#.........
.............................#....................#.......#....#.....#....#......#....................#..#...............##.......
..........#..............................................................................#....#.........#..#................#.....
..#..#...............................................................#.......#........#...........................#...............
...........................................................................#...##....................#.#....##....................
.......................................#...............................................#.....................#.........#..........
.......................................................#.......#....#................#.....................................#......
.............#................#...................#.................#....................#..................#.#.........#.........
.....................................................................#.....................................#......................
........................#..........................................#....#..#.#..........................................#.........
............#.......#..................#.................................................#..............#.......................#.
.........................................................#...............#...#....#...........#.................................#.
.........................#..........................#..#........................................................#.................
...............#....................................#.......#......................................#............#.................
.#......................................................................................................#.........................
#...#........................................................#.................#....#....................#...........#............
..#.........#................................................................#................................#.............#.....
..................................#.........................................................................#................#....
..............#..............................................#........#..................................#........................
......#............................................#.................................#............................#...............
.......##..#.......................##............#...#...................#.#..........................................##..........
.#......#.....................................................................#..#..........................#......#.............#
.................#.....................#........##..#.........#........#................#.........................................
...........#.....#..........#........#............................................................................................
.........................#......#.......................................#..............................#..........................
............#............................................................#..............#..............................#..........
..................#.........#...........................................................................................#.........
#.#..................................#....................#......................#.............#.................................#
....#................#.................#...................#...........#......................................#...................
................#........................................................................................#....#.#.......#.....##..
..........#...#.......................................#........#.......................................#...#......................
.......#..##........................#......##.........................#.........#.......#.............................#.....#.....
................#...............................................#....#..........#.....#.........#.........#.........#.............
...............................#............#....................................#......#......................................#..
.#..#..................#............................................#....#............#...............##...#..........#...........
....#.............................................................................................................................
.............................................................#...........................#..........#.............................
.#........#..................#.....#.............#.....................................................#...#........#...........#.
.......................#........................................#.....#............................#.#..................#.........
................#....#................#.......#............................#.......#.................#............................
....#.........#....#........#.....................#........................#............#.........................................
.......#.......#.....................................................................##...........#...............................
...........#.........................................................#..........#............#....................................
..................#.............................#.......................................................................#.........
................#.....#........#.....#...#..........#.....................................#.....#........................#........
..........................................#.........#...........#.................................................................
...#.......................................................................................................#......................
....#..............#...........#..................................................#.................#.................#...........
.#................#.....#.#.................................................................#.........................#...........
............#.........................##....................................#..............#......................................
...##...........#...#............#..........................................................................#.....................
............................................#......................#......#.......................................................
............#............................................................................................................#........
...................##..............#.#....#.##...................................................#..................#.............
..#...................................................................................#.........#.........................#.....#.
........................#..............................................#......#................................................#..
................#............#............................#.#...................#.....................#...........................
..................#......#................#.#......................#...................#...#......................................
..#................................................##...................................................................#.........
...........................#......................................................................#......#...#......#.............
........................#...#........#......#.......#..........#.............................#........#.....#.....................
.................................................#..............................................#......#.....#....................
.....#....#.................#......#........#.#..............^...........................#...................#..#.................
.............................................................................#................................#...................
#..........................#..#..............#.......#..........................#.................................................
............#.............................................................................#...................#...................
..................#.............................#.........................................#................#.........#......#.....
...#...............................#.....#......#............###.#.#.....................................#....#.............#.....
...........#...........#...........................#..............................................................................
............#..........................................#.....#.............#..........................#....................#.....#
........................#......#..#............................#.......................................................#..........
..#...#...#.......#.#..........................................#.............#.........#....#..................#...........#......
..................#.......#.....................................................................................#........#........
......##................#...........................................#..............##.................................#...........
.................#................................................................................#.#....................#........
....................#.........#..........#...............#...#...#.#.#............................................................
#..................#.#..........#..#...................................................................................#..........
..........#........................................................#..........##..........................#..##...................
...........#...................................................................#..................................................
..................#........#............................................#..................#....#.......................#..#......
............#...................#......#..........................................................................................
...........................#.....##..........#.#..............#......................#.............#.......#...........#..........
............#..................................................#.......#.........#.#..................#..............##...........
#..................................................#...#......#..#......................#.............#........#............#.....
....#..................#..........#.........#.........................................#..................#................#.......
...#.................#.............................................................................#....................#.........
..........#.................................................................#............#....................#.#.#.....#.........
.....................#......................#...........#........#................................................................
.....#.......................................................................#......................#...#......#..................
.............##........#.....................#...........##........#............#.....#.....................#...............#.....
.....#.........#....#.................#....#...........#.......##..........#.........#.#......................................#...
.......#..................##.......#...#.#...#...................................#.......................................#.....#..
...............#.......#.................#........................................................................................
......#.......#.....#...............................#...........#.......#......................##...#....#........................
.##..........................##..................##................#..#....#...##.................................................
........#..............................#.#......#........#...............#....#........#.#........................................
...............................#..#.....................#.#...................#...................................................
.....................#.................................................##...#.......#.................##...............#.......#..
.............#..........................#.................................#..............#.......#..........#........#............
.........................................#...................#.........................................................#..........
...............................#..........#..............................#............#.....#.....................................
.............#..........................#....................#................................#...#............#..................
....#......#........#.......#......#................................................#...#......................................##.
..#...................................#........#.....................................#...#......#.........#..#..........#.........
.......#.........#................................................................................................................
...........#...............................##.........................................#..#....................#.....#.#.......##..
.........................#..#...............#............................#.............#..........................#..............#

115
day6/src/main.rs Normal file
View file

@ -0,0 +1,115 @@
use std::fs::File;
use std::io::{self, BufRead};
use std::collections::{HashMap, HashSet};
fn main() {
let file = File::open("input").unwrap();
let mut obstacles: HashSet<(i16, i16)> = HashSet::new();
let mut size_x: i16 = 0;
let mut size_y: i16 = 0;
let mut start_pos: (i16, i16) = (-1, -1);
for (line_idx, line_result) in io::BufReader::new(file).lines().enumerate() {
size_y += 1;
let line = match line_result {
Ok(content) => content,
Err(e) => {
eprintln!("Error reading line: {}", e);
continue;
}
};
let mut line_size_x: i16 = 0;
for (i, c) in line.chars().enumerate() {
line_size_x += 1;
if c == '^' {
start_pos = (i.try_into().unwrap(), line_idx.try_into().unwrap());
}
if c == '#' {
obstacles.insert((i.try_into().unwrap(), line_idx.try_into().unwrap()));
}
}
if line_size_x > size_x {
size_x = line_size_x;
}
};
let visited_positions = run(&obstacles, start_pos, size_x, size_y);
println!("The guard visited {} distinct positions", visited_positions.unwrap());
let mut effective_obstructions: u16 = 0;
for x in 0..size_x {
for y in 0..size_y {
let mut obstacles_new = obstacles.clone();
obstacles_new.insert((x, y));
match run(&obstacles_new, start_pos, size_x, size_y) {
Some(_) => {},
None => effective_obstructions += 1,
}
}
}
println!("Effective obstructions: {}", effective_obstructions);
}
fn run(
obstacles: &HashSet<(i16, i16)>,
start_pos: (i16, i16),
size_x: i16,
size_y: i16,
) -> Option<usize>
{
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
enum Direction {
Up,
Down,
Left,
Right,
}
let mut guard_pos: (i16, i16) = start_pos;
let mut direction: Direction = Direction::Up;
let mut visited: HashMap<(i16, i16), HashSet<Direction>> = HashMap::new();
loop {
let new_position = match direction {
Direction::Up => (guard_pos.0, guard_pos.1 - 1),
Direction::Down => (guard_pos.0, guard_pos.1 + 1),
Direction::Left => (guard_pos.0 - 1, guard_pos.1),
Direction::Right => (guard_pos.0 + 1, guard_pos.1),
};
if new_position.0 < 0 || new_position.0 > size_x || new_position.1 < 0 || new_position.1 > size_y {
break;
}
/*
visited
.entry(guard_pos)
.or_insert_with(HashSet::new)
.insert(direction);
*/
if let Some(directions) = visited.get_mut(&guard_pos) {
if !directions.contains(&direction) {
directions.insert(direction);
} else {
return None;
}
} else {
let mut new_set = HashSet::new();
new_set.insert(direction);
visited.insert(guard_pos, new_set);
}
if obstacles.contains(&new_position) {
direction = match direction {
Direction::Up => Direction::Right,
Direction::Down => Direction::Left,
Direction::Left => Direction::Up,
Direction::Right => Direction::Down
};
}
else {
guard_pos = new_position;
}
}
Some(visited.keys().len())
}