solved day 16 part 1
Dieser Commit ist enthalten in:
Ursprung
b727a37834
Commit
4f10c7fb5e
10 geänderte Dateien mit 183 neuen und 20 gelöschten Zeilen
1
examples/2024/16.txt
Softlink
1
examples/2024/16.txt
Softlink
|
@ -0,0 +1 @@
|
|||
16c.txt
|
15
examples/2024/16a.txt
Normale Datei
15
examples/2024/16a.txt
Normale Datei
|
@ -0,0 +1,15 @@
|
|||
###############
|
||||
#.......#....E#
|
||||
#.#.###.#.###.#
|
||||
#.....#.#...#.#
|
||||
#.###.#####.#.#
|
||||
#.#.#.......#.#
|
||||
#.#.#####.###.#
|
||||
#...........#.#
|
||||
###.#.#####.#.#
|
||||
#...#.....#.#.#
|
||||
#.#.#.###.#.#.#
|
||||
#.....#...#.#.#
|
||||
#.###.#.#.#.#.#
|
||||
#S..#.....#...#
|
||||
###############
|
17
examples/2024/16b.txt
Normale Datei
17
examples/2024/16b.txt
Normale Datei
|
@ -0,0 +1,17 @@
|
|||
#################
|
||||
#...#...#...#..E#
|
||||
#.#.#.#.#.#.#.#.#
|
||||
#.#.#.#...#...#.#
|
||||
#.#.#.#.###.#.#.#
|
||||
#...#.#.#.....#.#
|
||||
#.#.#.#.#.#####.#
|
||||
#.#...#.#.#.....#
|
||||
#.#.#####.#.###.#
|
||||
#.#.#.......#...#
|
||||
#.#.###.#####.###
|
||||
#.#.#...#.....#.#
|
||||
#.#.#.#####.###.#
|
||||
#.#.#.........#.#
|
||||
#.#.#.#########.#
|
||||
#S#.............#
|
||||
#################
|
27
examples/2024/16c.txt
Normale Datei
27
examples/2024/16c.txt
Normale Datei
|
@ -0,0 +1,27 @@
|
|||
###########################
|
||||
#######################..E#
|
||||
######################..#.#
|
||||
#####################..##.#
|
||||
####################..###.#
|
||||
###################..##...#
|
||||
##################..###.###
|
||||
#################..####...#
|
||||
################..#######.#
|
||||
###############..##.......#
|
||||
##############..###.#######
|
||||
#############..####.......#
|
||||
############..###########.#
|
||||
###########..##...........#
|
||||
##########..###.###########
|
||||
#########..####...........#
|
||||
########..###############.#
|
||||
#######..##...............#
|
||||
######..###.###############
|
||||
#####..####...............#
|
||||
####..###################.#
|
||||
###..##...................#
|
||||
##..###.###################
|
||||
#..####...................#
|
||||
#.#######################.#
|
||||
#S........................#
|
||||
###########################
|
4
examples/2024/README:md
Normale Datei
4
examples/2024/README:md
Normale Datei
|
@ -0,0 +1,4 @@
|
|||
# Examples
|
||||
|
||||
# 16c
|
||||
Thanks to Thanks to https://www.reddit.com/r/adventofcode/comments/1hfhgl1/2024_day_16_part_1_alternate_test_case/
|
|
@ -1,4 +1,5 @@
|
|||
use advent_of_code::{
|
||||
find_character,
|
||||
strings::{convert_to_array, line_to_char},
|
||||
Kartesian, KartesianDirection, Map,
|
||||
};
|
||||
|
@ -30,21 +31,6 @@ fn instructions_to_dir(instructions: &str) -> Vec<KartesianDirection> {
|
|||
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;
|
||||
|
@ -63,7 +49,7 @@ 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 position = find_character(&map, ROBOT);
|
||||
let mut new;
|
||||
for direction in directions {
|
||||
new = position.move_dir(direction.vector(), direction).unwrap();
|
||||
|
|
100
src/bin/2024/16.rs
Normale Datei
100
src/bin/2024/16.rs
Normale Datei
|
@ -0,0 +1,100 @@
|
|||
use std::{cmp::Ordering, collections::HashMap, u32};
|
||||
|
||||
use advent_of_code::{
|
||||
find_character,
|
||||
strings::{convert_to_array, line_to_char},
|
||||
Kartesian, KartesianDirection, Map, KD,
|
||||
};
|
||||
#[allow(unused_imports)]
|
||||
use advent_of_code_macros::{include_data, include_example};
|
||||
|
||||
include_data!(DATA 2024 16);
|
||||
|
||||
const START: Option<char> = Some('S');
|
||||
const WALL: Option<char> = Some('#');
|
||||
const END: Option<char> = Some('E');
|
||||
const SPACE: Option<char> = Some('.');
|
||||
|
||||
fn new_score(old_score: u32, old: KD, new: KD) -> u32 {
|
||||
old_score
|
||||
+ if new == old {
|
||||
1
|
||||
} else if old == new.neg() {
|
||||
2001
|
||||
} else {
|
||||
1001
|
||||
}
|
||||
}
|
||||
|
||||
fn sorter(a: &(Kartesian<usize>, KartesianDirection, u32, Kartesian<usize>), b: &(Kartesian<usize>, KartesianDirection, u32, Kartesian<usize>)) -> Ordering {
|
||||
a.2.cmp(&b.2).reverse()
|
||||
}
|
||||
|
||||
fn search_end(map: &Map<char>, startpos: Kartesian<usize>, last_dir: KartesianDirection) -> u32 {
|
||||
let mut posmap: HashMap<Kartesian<usize>, u32> = HashMap::new();
|
||||
let mut branches = Vec::with_capacity(200);
|
||||
branches.push((startpos, last_dir, 0, startpos));
|
||||
let mut vmap = map.clone();
|
||||
let mut best = u32::MAX;
|
||||
loop {
|
||||
let (newpos, old_dir, score, oldpos) = match branches.pop() {
|
||||
Some(b) => b,
|
||||
None => break,
|
||||
};
|
||||
if !posmap.contains_key(&newpos) {
|
||||
posmap.insert(newpos, score);
|
||||
} else {
|
||||
if score > *posmap.get(&newpos).unwrap() {
|
||||
continue;
|
||||
}
|
||||
posmap.insert(newpos, score);
|
||||
}
|
||||
println!("moved {} with increased score {}", old_dir, score);
|
||||
vmap.set(
|
||||
newpos,
|
||||
match old_dir {
|
||||
KartesianDirection::Left => '←',
|
||||
KartesianDirection::Top => '↑',
|
||||
KartesianDirection::Right => '→',
|
||||
KartesianDirection::Bottom => '↓',
|
||||
_ => unreachable!(),
|
||||
},
|
||||
);
|
||||
for dir in KD::iter(false, false, true) {
|
||||
let after_move = newpos.move_dir(dir.vector(), dir).unwrap();
|
||||
match map.get(after_move) {
|
||||
WALL => {},
|
||||
END => {
|
||||
if best > new_score(score, old_dir, dir) {
|
||||
best = new_score(score, old_dir, dir);
|
||||
}
|
||||
break;
|
||||
},
|
||||
SPACE | START => {
|
||||
let added = new_score(score, old_dir, dir);
|
||||
branches.push((after_move, dir, added, newpos));
|
||||
},
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
branches.sort_by(sorter);
|
||||
}
|
||||
print!("{}", vmap);
|
||||
best
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let map = Map::from(convert_to_array::<_, _, '\n'>(DATA, line_to_char));
|
||||
//println!("Startmap: \n{}", map);
|
||||
let deer = find_character(&map, 'S');
|
||||
let best = search_end(&map, deer, KartesianDirection::Right);
|
||||
println!(
|
||||
"Best Score is: {}",
|
||||
best
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
}
|
|
@ -108,12 +108,12 @@ impl<T: Display + Copy + Default> Display for Map<T> {
|
|||
for v in row.iter() {
|
||||
v.fmt(f)?
|
||||
}
|
||||
for _ in row.len()..=self.maximum.y {
|
||||
for _ in row.len()..self.maximum.y {
|
||||
T::default().fmt(f)?
|
||||
}
|
||||
println!()
|
||||
}
|
||||
for _ in self.data.len()..=self.maximum.x {
|
||||
for _ in self.data.len()..self.maximum.x {
|
||||
for _ in 0..self.maximum.y {
|
||||
T::default().fmt(f)?
|
||||
}
|
||||
|
|
|
@ -135,3 +135,18 @@ impl<T: Clone + Copy> From<Vec<Vec<T>>> for Map<T> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_character<T: Clone + Copy + Default + PartialEq>(map: &Map<T>, character: T) -> 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(character) {
|
||||
return coord;
|
||||
}
|
||||
}
|
||||
}
|
||||
unreachable!("This function does not expect an non-existing character");
|
||||
}
|
||||
|
|
|
@ -88,7 +88,6 @@ fn diff_dir() {
|
|||
|
||||
#[test]
|
||||
fn moving_wrapping() {
|
||||
/*
|
||||
let max: Kartesian<u32> = Kartesian::new(10, 10);
|
||||
let mut pos = Kartesian::new(0, 0);
|
||||
let velocity = Velocity::new(1, 1, KartesianDirection::BottomRight);
|
||||
|
@ -98,7 +97,6 @@ fn moving_wrapping() {
|
|||
pos = pos.wrapping_move_velocity(velocity, max);
|
||||
}
|
||||
assert_eq!(pos, Kartesian::new(0, 0));
|
||||
*/
|
||||
let max = Kartesian::<u32>::new(7, 11);
|
||||
assert_eq!(Kartesian::new(2, 0).wrapping_move_velocity(Velocity::new(1, 3, KartesianDirection::TopLeft), max), Kartesian::new(1, 9));
|
||||
assert_eq!(Kartesian::new(4, 2).wrapping_move_velocity(Velocity::new(3, 2, KartesianDirection::TopRight), max), Kartesian::new(1, 4))
|
||||
|
|
Laden …
Tabelle hinzufügen
In neuem Issue referenzieren