solved part one of day 15
Dieser Commit ist enthalten in:
Ursprung
6fafca3b6b
Commit
68767c83ae
3 geänderte Dateien mit 118 neuen und 0 gelöschten Zeilen
10
examples/2024/15.txt
Normale Datei
10
examples/2024/15.txt
Normale Datei
|
@ -0,0 +1,10 @@
|
||||||
|
########
|
||||||
|
#..O.O.#
|
||||||
|
##@.O..#
|
||||||
|
#...O..#
|
||||||
|
#.#.O..#
|
||||||
|
#...O..#
|
||||||
|
#......#
|
||||||
|
########
|
||||||
|
|
||||||
|
<^^>>>vv<v>>v<<
|
104
src/bin/2024/15.rs
Normale Datei
104
src/bin/2024/15.rs
Normale Datei
|
@ -0,0 +1,104 @@
|
||||||
|
use advent_of_code::{
|
||||||
|
strings::{convert_to_array, line_to_char},
|
||||||
|
Kartesian, KartesianDirection, Map,
|
||||||
|
};
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
use advent_of_code_macros::{include_data, include_example};
|
||||||
|
use log::debug;
|
||||||
|
|
||||||
|
include_data!(DATA 2024 15);
|
||||||
|
|
||||||
|
const WALL: char = '#';
|
||||||
|
const ROBOT: char = '@';
|
||||||
|
const CRATE: char = 'O';
|
||||||
|
const SPACE: char = '.';
|
||||||
|
|
||||||
|
fn instructions_to_dir(instructions: &str) -> Vec<KartesianDirection> {
|
||||||
|
let mut dirs = Vec::with_capacity(instructions.len());
|
||||||
|
for char in instructions.chars() {
|
||||||
|
if char == '\n' {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
dirs.push(match char {
|
||||||
|
'<' => KartesianDirection::Left,
|
||||||
|
'^' => KartesianDirection::Top,
|
||||||
|
'>' => KartesianDirection::Right,
|
||||||
|
'v' => KartesianDirection::Bottom,
|
||||||
|
_ => unreachable!(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
dirs
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_position(map: &Map<char>) -> Kartesian<usize> {
|
||||||
|
let maximum = map.size();
|
||||||
|
let mut coord = Kartesian::new(0, 0);
|
||||||
|
for x in 1..maximum.x - 1 {
|
||||||
|
for y in 1..maximum.y - 1 {
|
||||||
|
coord.x = x;
|
||||||
|
coord.y = y;
|
||||||
|
if map.get(coord) == Some(ROBOT) {
|
||||||
|
return coord;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calc_sum(map: &Map<char>) -> usize {
|
||||||
|
let maximum = map.size();
|
||||||
|
let mut sum = 0;
|
||||||
|
for x in 1..maximum.x - 1 {
|
||||||
|
for y in 1..maximum.y - 1 {
|
||||||
|
if map.get(Kartesian::new(x, y)) == Some(CRATE) {
|
||||||
|
println!("found crate on {}:{}", x, y);
|
||||||
|
sum += 100 * x + y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sum
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let (maze, instructions): (&str, &str) = DATA.split_once("\n\n").unwrap();
|
||||||
|
let mut map = Map::from(convert_to_array::<_, _, '\n'>(maze, line_to_char));
|
||||||
|
let directions = instructions_to_dir(instructions);
|
||||||
|
let mut position = get_position(&map);
|
||||||
|
let mut new;
|
||||||
|
for direction in directions {
|
||||||
|
new = position.move_dir(direction.vector(), direction).unwrap();
|
||||||
|
match map.get(new) {
|
||||||
|
Some(SPACE) => {
|
||||||
|
map.set(position, SPACE);
|
||||||
|
map.set(new, ROBOT);
|
||||||
|
position = new;
|
||||||
|
},
|
||||||
|
Some(CRATE) => {
|
||||||
|
debug!("Trying to move crate to {} on position {}", direction, new);
|
||||||
|
let mut skip = new;
|
||||||
|
while map.get(skip) == Some(CRATE) {
|
||||||
|
skip = skip.move_dir(direction.vector(), direction).unwrap();
|
||||||
|
debug!("Object on {} is {:?}", skip, map.get(skip));
|
||||||
|
}
|
||||||
|
match map.get(skip) {
|
||||||
|
Some(WALL) => {},
|
||||||
|
Some(SPACE) => {
|
||||||
|
map.set(skip, CRATE);
|
||||||
|
map.set(new, ROBOT);
|
||||||
|
map.set(position, SPACE);
|
||||||
|
position = new;
|
||||||
|
},
|
||||||
|
_ => {},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Some(WALL) => {},
|
||||||
|
_ => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("Sum of ocordinates is {}", calc_sum(&map));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
}
|
|
@ -17,6 +17,10 @@ impl<T: Clone + Copy> Map<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn size(&self) -> Kartesian<usize> {
|
||||||
|
self.maximum
|
||||||
|
}
|
||||||
|
|
||||||
/// returns true if coordinates are outside of bounds
|
/// returns true if coordinates are outside of bounds
|
||||||
#[inline]
|
#[inline]
|
||||||
fn outside(&self, coord: Kartesian<usize>) -> bool {
|
fn outside(&self, coord: Kartesian<usize>) -> bool {
|
||||||
|
|
Laden …
Tabelle hinzufügen
In neuem Issue referenzieren