This commit is contained in:
nora 2024-12-07 12:49:38 +01:00
parent c4518496d3
commit e9a6c19c2e
8 changed files with 279 additions and 6 deletions

9
2024/Cargo.lock generated
View file

@ -161,6 +161,15 @@ dependencies = [
"rustc-hash",
]
[[package]]
name = "day06"
version = "0.1.0"
dependencies = [
"divan",
"helper",
"nom",
]
[[package]]
name = "divan"
version = "0.1.16"

15
2024/day06/Cargo.toml Normal file
View file

@ -0,0 +1,15 @@
[package]
name = "day06"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
nom.workspace = true
helper.workspace = true
divan.workspace = true
[[bench]]
name = "benches"
harness = false

View file

@ -0,0 +1,3 @@
fn main() {
day06::bench();
}

130
2024/day06/input.txt Normal file
View file

@ -0,0 +1,130 @@
......#...........#..............................#..............................#...........................##...............#....
.............#..............#........#......##...................................................#............##...#..........#...
...#..................#..............#.........#...#.........................................................................#..#.
.....#..........#...................#.....................#..........#...........#......#..#...................#..................
...#...............................................##..................#............#.....#...................#...................
.#.#..#...................#.##.................#..........#................................#......#.........##..................#.
....................#...................#....#.........#....................#....................................................#
..........#......#...................................................................................................#....#.......
.#...............#.........#...........#...............#...................#......#...............................................
............#...........#........#..##.......#........#.....#................................##...................................
...................#.#.............###.................##.................#.......................#.....#..............##.........
.........................#...#............................#......#.......................#........................................
.....................#..............................................#.......#......##......................................#......
............................#.#............................................................................#.................#....
.#.....#.......................................................#..........#.......................................................
..............#..............................#.....#....#........#....................#....#.#...........#.#..................#...
........................#...#...##......#...........................#.............#............................#..................
...#........................................................#...#...........................#...#...............#........#........
.........#..#..........#.....................................#..................................................................#.
...........................................#.........#....#........................................#..#...........#...............
..#..........#...........................................................................................#..............#.........
...#.......#......................................#...............#...............................................................
.#........#......#.#....................................#...#...........#..........#........#.#.......#.......................#...
......................#...........#......##....................#..................#......#..............................#.......#.
..............#......................#..............#...#.#............................................................#.#........
..............#..................#...#..................#...#.....#...#...............#...........................................
.............#............................##..#...#...............................................................##..............
..#............................#.......#.#....................................#...................................................
.......#.....#..........#......#.....#...................................#...........................#......#....#..............#.
.#.................#.............................................#................#.............#.....#....................#......
..............#.........#..#.....#...........#..............................#.................................#............#......
....#.#............#............................................................#..........#......................................
.........................................#.......#......#....................................#.....................#...#......#...
....#.............................#....................................#..........#................................#..............
.........#..#......#..............................................................#....................#..........................
.......................................................##...#........#......................................................#.#..#
..........................#.......##..##................................................................................#.........
..................................##..................................................................#..........................#
.#..................................................#..............#.....................#.....#..................................
.......##........................#......................................................#..........#..............................
.........#....#....#.........#.#........................#...#.....................................................#.....#.........
....#.............................#....#....................#.....................#.....#......#.....................#..........#.
............................................................................................#..........#......#...................
..................................................................................#..........................#.#.....#.........#..
..................................................#...................................................#......#.....#............#.
..........................................^#................................................................................#.....
...............#..........#.............................................#.....#....#...................#................#.........
#..#....#........#........#.........#.......#.................#.......................#.#.............#...................#.......
..#................................................................#......................................#.......................
........#......................#.....................##.#..........#.............................#.............................#..
....#..........#........................#.........................................................................................
......#...#.#.#.........#.................#.....#.....#...#......#..................................................##........#...
...#....#....................#...................................................................#......#...............#.........
..#.......#.........................#..........#........#............................#......#....................#................
.................................#.......................................#....................................................#..#
......................#...#........................#.......................#.............#........................................
......#.......#...................................................................................................#...............
.............#............#......#............................#...........................................##......................
..........................#.....................................................................................#.................
....#..............................#......................#........................................#....#...........#.............
................#.......#.........................................................................................................
.............#............#........#.....#.........#.#.#............................#..........#..............#....#.......#.....#
..#.....#...............................................................................................#...............#.........
.......#...........#............#................#.......................................#..........................#......#......
..........#........................................#......................#............#....................................#..#..
.......#......#..............................#........................................................#.#.................#.......
.#.................#..#.....................................................................................#...........##........
#...................................#........................................#..............#.##........#..#.#.............#......
......#...............#........#....#.........#.........#...#.......................#...........................#............#....
...#...................................................................................#.......................................#..
.....#.......................................#................#...............................................................#...
.#................................................##................................#....................#...............#........
.................................................#..#..................#.......#........................#...............#.....#...
.....##.#.............................#.....................#................................................................#...#
...........#....................#...........#...............#.................................................#.......#.......#..#
........#....................................................#.....#....#.........................................#.....#.#.......
...............#..........................#............#.#.................................................#......................
...........#......#.....................#...................................................................#.#..............#....
......#...#.........................#..........................................................#..................................
.....................#................................................#..#........................................................
..............#.................#............#....................#.#.......................................#............#........
............#.....................#................#......#..........................#.#.........#............#...................
........................................................................#.....................#...................................
.............................#....................................................................................................
.........................#...#..........................................#............................................#............
..#...............................#.....#.....#.............#.......#..#.................##.#.......................#.........#.#.
......................................#...........................................................................................
...........#.....................................#................#...#.........................#........................#........
..#...#..#..........................#.......#....#......................................................#................#........
.....................................#...#.#........#...............#..#......#.....................................#...#.........
.#..............#..............................................................#.........................##......................#
..........#..................................#....#.........#...#.#..........#...#...................................##...........
...............#.....................................................................................................#............
.#.......#....................#...#..........................#.#...............#......................#........#.................#
.............#....................#........................#..................#......#....#....#..................................
........#..............................................................#.....................#....................................
.......................#..............#.............#......#.........................................#.........................#..
.......#.......#.#........#.......................#..#.......................................................#.#..................
...............#..#....#.......................#.....#..............................#..................#.....................#....
......#..............#...........................#............................##...........................#......................
.......................................#........#................................#..........................#.....................
.#.....................................#............#.............................................................#..#............
..................#..................#.........................#............#..........#.........................#...#............
.......................................................#..............#..........................................................#
...#....#..#...#..#...................##....................#.....................#...................#.....#..........#..........
.......................#........#....................##.................#.......................................................#.
........#.......#.......................#.#......##...##........#..##.........................................#...............##..
....#..........................#.........##..#......#...........................#........#........#...#...........................
...#..........................#.....#.....................................#.......................................................
....................................##.......#..............................#.............#...#......#.............#..............
.#..............................#...............................................................................................#.
.....#................................................................................##.....#..............#.#...................
...........................................................................................#....#................#................
...#..#...................................................#....#.......#.......#.................................#........#.......
.#....#....................#..............................................................................#.............#.......#.
.........#.................#.......................................#...........#......#.......#............#............#.........
........................................#...............................#.........................................................
..............#....#......................#..........#........#.....................................#...........#..........#......
......##....#.............#..........#......................#.........................................##..........................
....................#.............................#.....................##.#..........#..#......................###.........#.....
.#.......................................##........................#.....#.....#..#...............................................
..............................#............................#.......................................................##.............
.........#.............................#..............#......#.....#..............#.#............................#........#....#..
............#..............#.............................#..........................#.............................#...............
.................#...........#..............#.....#..............................#.............#................#.....#...........
........#.....................#........#.....................................#..........................#......#.#................
.....................................................#..#........................#.##..#.........#................................
......#..............................................#..........................#...............................#..........#......
.................................................#.................##.......................#...................................#.
....................#.....#.......#.............#................................#..........#...#.................................

View file

@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...

95
2024/day06/src/lib.rs Normal file
View file

@ -0,0 +1,95 @@
use helper::{Day, Variants};
pub fn main() {
helper::main::<Day06>(include_str!("../input.txt"));
}
struct Day06;
helper::define_variants! {
day => crate::Day06;
part1 {
basic => crate::part1;
}
part2 {
basic => crate::part2;
}
}
impl Day for Day06 {
fn part1() -> Variants {
part1_variants!(construct_variants)
}
fn part2() -> Variants {
part2_variants!(construct_variants)
}
}
fn part1(input: &str) -> u64 {
let input = input.as_bytes();
let width = input.iter().position(|&byte| byte == b'\n').unwrap() + 1; // account for newline
let mut guard_pos = input
.iter()
.position(|&byte| byte == b'^' || byte == b'>' || byte == b'<' || byte == b'v')
.unwrap();
let mut guard_state = input[guard_pos];
let mut reached_tiles = vec![false; input.len()];
reached_tiles[guard_pos] = true;
loop {
let new_pos = match guard_state {
b'^' => guard_pos.checked_sub(width),
b'v' => guard_pos.checked_add(width),
b'<' => guard_pos.checked_sub(1),
b'>' => guard_pos.checked_add(1),
_ => unreachable!(),
};
match new_pos {
None => {
break;
}
Some(new_pos) if input.len() <= new_pos => {
break;
}
Some(new_pos) if input[new_pos] == b'\n' => {
break;
}
Some(new_pos) => {
if input[new_pos] == b'#' {
guard_state = match guard_state {
b'^' => b'>',
b'v' => b'<',
b'<' => b'^',
b'>' => b'v',
_ => unreachable!(),
}
} else {
reached_tiles[new_pos] = true;
guard_pos = new_pos;
}
}
}
}
reached_tiles.iter().filter(|reached| **reached).count() as u64
}
fn part2(_input: &str) -> u64 {
0
}
helper::tests! {
day06 Day06;
part1 {
small => 41;
default => 4454;
}
part2 {
small => 0;
default => 0;
}
}
helper::benchmarks! {}

3
2024/day06/src/main.rs Normal file
View file

@ -0,0 +1,3 @@
fn main() {
day06::main();
}