Skip to content

Commit

Permalink
day_15 part_1
Browse files Browse the repository at this point in the history
  • Loading branch information
0qln committed Dec 19, 2024
1 parent 27fbe43 commit 730db50
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/day_15/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::collections::LinkedList;

use itertools::Itertools;
use regex::Regex;

use crate::day_6::{self, Dir, Map, Pos};

pub mod part_1;
pub mod part_2;

pub fn parse(input: &str) -> (Map, Vec<isize>) {
let (map, moves) = input.split_once("\r\n\r\n").unwrap();
let map = day_6::parse(map);
let moves = moves.chars().filter_map(|c| match c {
'^' => Some(3),
'>' => Some(4),
'v' => Some(1),
'<' => Some(2),
_ => None,
}).collect_vec();
(map, moves)
}
10 changes: 10 additions & 0 deletions src/day_15/part_1/dummy_input_0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
########
#..O.O.#
##@.O..#
#...O..#
#.#.O..#
#...O..#
#......#
########

<^^>>>vv<v>>v<<^
21 changes: 21 additions & 0 deletions src/day_15/part_1/dummy_input_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
##########
#..O..O.O#
#......O.#
#.OO..O.O#
#[email protected].#
#O#..O...#
#O..O..O.#
#.OO.O.OO#
#....O...#
##########

<vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^
vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v
><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<
<<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^
^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><
^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^
>^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^
<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>
^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>
v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^
44 changes: 44 additions & 0 deletions src/day_15/part_1/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use itertools::Itertools;

use crate::{
day_6::{self, get_dir, Dir, Map, Pos},
day_8::setoff,
};

use super::parse;

#[cfg(test)]
mod test;

pub fn solve(input: &str) -> usize {
let (mut map, moves) = parse(input);
let mut curr = map.find(|&ch| ch == '@').unwrap();
for m in moves {
let m = get_dir(m);
if move_stuff(&mut map, curr, m) {
curr = setoff(curr, m);
}
}

map.find_all(|&ch| ch == 'O')
.map(|(l, t)| t * 100 + l)
.sum()
}

fn move_stuff(map: &mut Map, curr: Pos, m: Dir) -> bool {
match map.get(curr) {
Some('@' | 'O') => {
let next = setoff(curr, m);
let can_move = move_stuff(map, next, m);
if can_move {
let curr_chr = map.get(curr).unwrap();
map.set(curr, '.');
map.set(next, curr_chr);
}
can_move
}
Some('#') | None => false,
Some('.') => true,
c => panic!("Invalid char: {:?}", c),
}
}
23 changes: 23 additions & 0 deletions src/day_15/part_1/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use super::solve;
use crate::include_using_path;

#[test]
fn dummy_input_0() {
let input = include_using_path!("dummy_input_0.txt");
let result = solve(input);
assert_eq!(result, 2028);
}

#[test]
fn dummy_input_1() {
let input = include_using_path!("dummy_input_1.txt");
let result = solve(input);
assert_eq!(result, 10092);
}

#[test]
fn solve_puzzle() {
let input = include_using_path!("input.txt");
let result = solve(input);
println!("Result: {}", result);
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod day_10;
mod day_12;
mod day_13;
mod day_14;
mod day_15;

macro_rules! include_using_path {
($relative_path:expr) => {{
Expand Down

0 comments on commit 730db50

Please sign in to comment.