feat: added day 18 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 (10) (push) Successful in 3s
Build and run challenges / Challenge for day (11) (push) Successful in 3s
Build and run challenges / Challenge for day (12) (push) Successful in 3s
Build and run challenges / Challenge for day (13) (push) Successful in 3s
Build and run challenges / Challenge for day (14) (push) Successful in 4s
Build and run challenges / Challenge for day (15) (push) Successful in 3s
Build and run challenges / Challenge for day (16) (push) Successful in 3s
Build and run challenges / Challenge for day (17) (push) Successful in 3s
Build and run challenges / Challenge for day (18) (push) Successful in 3s
Build and run challenges / Challenge for day (2) (push) Successful in 4s
Build and run challenges / Challenge for day (4) (push) Successful in 5s
Build and run challenges / Challenge for day (5) (push) Successful in 5s
Build and run challenges / Challenge for day (3) (push) Successful in 11s
Build and run challenges / Challenge for day (6) (push) Successful in 5s
Build and run challenges / Challenge for day (7) (push) Successful in 4s
Build and run challenges / Challenge for day (8) (push) Successful in 4s
Build and run challenges / Challenge for day (9) (push) Successful in 3s
All checks were successful
Build and run challenges / Challenge for day (1) (push) Successful in 3s
Build and run challenges / Challenge for day (10) (push) Successful in 3s
Build and run challenges / Challenge for day (11) (push) Successful in 3s
Build and run challenges / Challenge for day (12) (push) Successful in 3s
Build and run challenges / Challenge for day (13) (push) Successful in 3s
Build and run challenges / Challenge for day (14) (push) Successful in 4s
Build and run challenges / Challenge for day (15) (push) Successful in 3s
Build and run challenges / Challenge for day (16) (push) Successful in 3s
Build and run challenges / Challenge for day (17) (push) Successful in 3s
Build and run challenges / Challenge for day (18) (push) Successful in 3s
Build and run challenges / Challenge for day (2) (push) Successful in 4s
Build and run challenges / Challenge for day (4) (push) Successful in 5s
Build and run challenges / Challenge for day (5) (push) Successful in 5s
Build and run challenges / Challenge for day (3) (push) Successful in 11s
Build and run challenges / Challenge for day (6) (push) Successful in 5s
Build and run challenges / Challenge for day (7) (push) Successful in 4s
Build and run challenges / Challenge for day (8) (push) Successful in 4s
Build and run challenges / Challenge for day (9) (push) Successful in 3s
Signed-off-by: Louis Vallat <contact@louis-vallat.dev>
This commit is contained in:
parent
83e13c5774
commit
e46cd60df1
@ -11,7 +11,7 @@ jobs:
|
||||
name: Challenge for day
|
||||
strategy:
|
||||
matrix:
|
||||
day_number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
|
||||
day_number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
|
||||
runs-on: rust-bookworm
|
||||
steps:
|
||||
- name: Check out repository code
|
||||
|
6
day18/Cargo.toml
Normal file
6
day18/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day18"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
89
day18/src/main.rs
Normal file
89
day18/src/main.rs
Normal file
@ -0,0 +1,89 @@
|
||||
use std::collections::{BTreeSet, HashMap, HashSet};
|
||||
use std::{env, fs};
|
||||
|
||||
const MAP_SIZE: i32 = 71;
|
||||
const SLICE_SIZE: usize = 1024;
|
||||
|
||||
fn read_input(path: &str) -> String {
|
||||
fs::read_to_string(path).expect("Cannot read file.")
|
||||
}
|
||||
|
||||
fn parse_input(input: &str) -> Vec<(i32, i32)> {
|
||||
let mut falling_bytes = Vec::new();
|
||||
|
||||
for line in input.lines().filter(|s| !s.is_empty()) {
|
||||
let (x, y) = line.split_once(',').unwrap();
|
||||
falling_bytes.push((x.parse().unwrap(), y.parse().unwrap()));
|
||||
}
|
||||
|
||||
falling_bytes
|
||||
}
|
||||
|
||||
fn shortest_path(corrupted: &HashSet<(i32, i32)>) -> Option<usize> {
|
||||
let mut positions_cost = HashMap::new();
|
||||
positions_cost.insert((0, 0), 0);
|
||||
|
||||
let mut queue = BTreeSet::new();
|
||||
queue.insert((0, 0, 0));
|
||||
while let Some((cost, x, y)) = queue.pop_first() {
|
||||
if cost > *positions_cost.get(&(x, y)).unwrap() {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (dx, dy) in [(0, -1), (0, 1), (-1, 0), (1, 0)] {
|
||||
if !(0..MAP_SIZE).contains(&(x + dx)) || !(0..MAP_SIZE).contains(&(y + dy)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if corrupted.contains(&(x + dx, y + dy)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (cost + 1) < *positions_cost.get(&(x + dx, y + dy)).unwrap_or(&usize::MAX) {
|
||||
positions_cost.insert((x + dx, y + dy), cost + 1);
|
||||
queue.insert((cost + 1, x + dx, y + dy));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
positions_cost.get(&(MAP_SIZE - 1, MAP_SIZE - 1)).copied()
|
||||
}
|
||||
|
||||
fn find_blocker(corruptions: &Vec<(i32, i32)>) -> (i32, i32) {
|
||||
let (mut low, mut high) = (SLICE_SIZE, corruptions.len() - 1);
|
||||
while (high - low) > 1 {
|
||||
let middle = low + (high - low) / 2;
|
||||
let corrupted = HashSet::from_iter(corruptions[0..=middle].iter().copied());
|
||||
if shortest_path(&corrupted).is_none() {
|
||||
high = middle;
|
||||
} else {
|
||||
low = middle;
|
||||
}
|
||||
}
|
||||
|
||||
for i in low..=high {
|
||||
let corrupted = HashSet::from_iter(corruptions[0..=i].iter().copied());
|
||||
if shortest_path(&corrupted).is_none() {
|
||||
return corruptions[i];
|
||||
}
|
||||
}
|
||||
(0, 0)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
for arg in args.iter().skip(1) {
|
||||
let input = read_input(&arg);
|
||||
let falling_bytes = parse_input(&input);
|
||||
let blocker = find_blocker(&falling_bytes);
|
||||
println!("[{}]", &arg);
|
||||
println!(
|
||||
"\t[Part 1] => Answer is '{}'.",
|
||||
shortest_path(&HashSet::from_iter(
|
||||
falling_bytes[0..SLICE_SIZE].iter().cloned()
|
||||
))
|
||||
.unwrap_or(usize::MAX)
|
||||
);
|
||||
println!("\t[Part 2] => Answer is '{},{}'.", blocker.0, blocker.1);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user