feat: added day 6 part 1 and 2
All checks were successful
Build and run challenges / Challenge for day (1) (push) Successful in 3s
Build and run challenges / Challenge for day (4) (push) Successful in 4s
Build and run challenges / Challenge for day (5) (push) Successful in 5s
Build and run challenges / Challenge for day (3) (push) Successful in 10s
Build and run challenges / Challenge for day (6) (push) Successful in 5s
Build and run challenges / Challenge for day (2) (push) Successful in 3s

Signed-off-by: Louis Vallat <contact@louis-vallat.dev>
This commit is contained in:
Louis Vallat 2024-12-07 23:47:05 +01:00
parent 77362b50a9
commit 38d2473de8
Signed by: louis
SSH Key Fingerprint: SHA256:0iPwDU/PZtEy/K13Oar4TzmcunmI9H5U9IsOR3jyT/Q
3 changed files with 147 additions and 1 deletions

View File

@ -11,7 +11,7 @@ jobs:
name: Challenge for day
strategy:
matrix:
day_number: [1, 2, 3, 4, 5]
day_number: [1, 2, 3, 4, 5, 6]
runs-on: rust-bookworm
steps:
- name: Check out repository code

6
day6/Cargo.toml Normal file
View File

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

140
day6/src/main.rs Normal file
View File

@ -0,0 +1,140 @@
use std::collections::HashSet;
use std::{env, fs};
const DIRECTIONS: [(i32, i32); 4] = [(-1, 0), (0, 1), (1, 0), (0, -1)];
#[derive(Clone, Eq, PartialEq)]
enum Position {
Obstacle,
Visited(HashSet<usize>),
Free,
}
fn read_input(path: &str) -> String {
fs::read_to_string(path).expect("Cannot read file.")
}
fn parse_input(input: &str) -> (Vec<Vec<Position>>, (i32, i32)) {
let mut position = (0, 0);
let map = input
.lines()
.filter(|line| !line.is_empty())
.enumerate()
.map(|(row, line)| {
line.chars()
.enumerate()
.map(|(col, chr)| match chr {
'#' => Position::Obstacle,
'.' => Position::Free,
'^' => {
position = (row as i32, col as i32);
Position::Visited(HashSet::from_iter(vec![0]))
}
_ => unreachable!("Invalid character in input"),
})
.collect()
})
.collect();
(map, position)
}
fn is_in_map(map: &Vec<Vec<Position>>, pos: (i32, i32)) -> bool {
(0..map.len() as i32).contains(&pos.0) && (0..map[pos.0 as usize].len() as i32).contains(&pos.1)
}
fn will_loop(map: &Vec<Vec<Position>>, mut pos: (i32, i32), mut current_direction: usize) -> bool {
let mut map = map.clone();
loop {
let direction = DIRECTIONS[current_direction];
let next_pos = (pos.0 + direction.0, pos.1 + direction.1);
if !is_in_map(&map, next_pos) {
break;
}
match &map[next_pos.0 as usize][next_pos.1 as usize] {
Position::Obstacle => {
current_direction = (current_direction + 1) % DIRECTIONS.len();
}
Position::Free => {
pos = next_pos;
map[pos.0 as usize][pos.1 as usize] =
Position::Visited(HashSet::from_iter(vec![current_direction]));
}
Position::Visited(visits) => {
if visits.contains(&current_direction) {
return true;
}
pos = next_pos;
let mut visits = visits.clone();
visits.insert(current_direction);
map[pos.0 as usize][pos.1 as usize] = Position::Visited(visits);
}
}
}
false
}
fn walk_the_map(map: &Vec<Vec<Position>>, mut pos: (i32, i32), compute_obstacles: bool) -> u32 {
let mut map = map.clone();
let mut current_direction = 0;
let mut visited = 1;
let mut obstacles = 0;
loop {
let direction = DIRECTIONS[current_direction];
let next_pos = (pos.0 + direction.0, pos.1 + direction.1);
if !is_in_map(&map, next_pos) {
break;
}
match &map[next_pos.0 as usize][next_pos.1 as usize] {
Position::Obstacle => {
current_direction = (current_direction + 1) % DIRECTIONS.len();
}
Position::Free => {
map[next_pos.0 as usize][next_pos.1 as usize] = Position::Obstacle;
if compute_obstacles && will_loop(&map, pos, current_direction) {
obstacles += 1;
}
pos = next_pos;
map[pos.0 as usize][pos.1 as usize] =
Position::Visited(HashSet::from_iter(vec![current_direction]));
visited += 1;
}
Position::Visited(visits) => {
pos = next_pos;
let mut visits = visits.clone();
visits.insert(current_direction);
map[pos.0 as usize][pos.1 as usize] = Position::Visited(visits);
}
}
}
if compute_obstacles {
obstacles
} else {
visited
}
}
fn part_1(map: &Vec<Vec<Position>>, pos: (i32, i32)) -> u32 {
walk_the_map(&map, pos, false)
}
fn part_2(map: &Vec<Vec<Position>>, pos: (i32, i32)) -> u32 {
walk_the_map(&map, pos, true)
}
fn main() {
let args: Vec<String> = env::args().collect();
for arg in args.iter().skip(1) {
let input = read_input(&arg);
let (map, start) = parse_input(&input);
println!("[{}]", &arg);
println!("\t[Part 1] => Answer is '{}'.", part_1(&map, start));
println!("\t[Part 2] => Answer is '{}'.", part_2(&map, start));
}
}