mirror of
https://github.com/Noratrieb/advent-of-code.git
synced 2026-01-14 17:45:02 +01:00
d6p1
This commit is contained in:
parent
c4518496d3
commit
e9a6c19c2e
8 changed files with 279 additions and 6 deletions
9
2024/Cargo.lock
generated
9
2024/Cargo.lock
generated
|
|
@ -161,6 +161,15 @@ dependencies = [
|
||||||
"rustc-hash",
|
"rustc-hash",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day06"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"divan",
|
||||||
|
"helper",
|
||||||
|
"nom",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "divan"
|
name = "divan"
|
||||||
version = "0.1.16"
|
version = "0.1.16"
|
||||||
|
|
|
||||||
15
2024/day06/Cargo.toml
Normal file
15
2024/day06/Cargo.toml
Normal 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
|
||||||
3
2024/day06/benches/benches.rs
Normal file
3
2024/day06/benches/benches.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
fn main() {
|
||||||
|
day06::bench();
|
||||||
|
}
|
||||||
130
2024/day06/input.txt
Normal file
130
2024/day06/input.txt
Normal file
|
|
@ -0,0 +1,130 @@
|
||||||
|
......#...........#..............................#..............................#...........................##...............#....
|
||||||
|
.............#..............#........#......##...................................................#............##...#..........#...
|
||||||
|
...#..................#..............#.........#...#.........................................................................#..#.
|
||||||
|
.....#..........#...................#.....................#..........#...........#......#..#...................#..................
|
||||||
|
...#...............................................##..................#............#.....#...................#...................
|
||||||
|
.#.#..#...................#.##.................#..........#................................#......#.........##..................#.
|
||||||
|
....................#...................#....#.........#....................#....................................................#
|
||||||
|
..........#......#...................................................................................................#....#.......
|
||||||
|
.#...............#.........#...........#...............#...................#......#...............................................
|
||||||
|
............#...........#........#..##.......#........#.....#................................##...................................
|
||||||
|
...................#.#.............###.................##.................#.......................#.....#..............##.........
|
||||||
|
.........................#...#............................#......#.......................#........................................
|
||||||
|
.....................#..............................................#.......#......##......................................#......
|
||||||
|
............................#.#............................................................................#.................#....
|
||||||
|
.#.....#.......................................................#..........#.......................................................
|
||||||
|
..............#..............................#.....#....#........#....................#....#.#...........#.#..................#...
|
||||||
|
........................#...#...##......#...........................#.............#............................#..................
|
||||||
|
...#........................................................#...#...........................#...#...............#........#........
|
||||||
|
.........#..#..........#.....................................#..................................................................#.
|
||||||
|
...........................................#.........#....#........................................#..#...........#...............
|
||||||
|
..#..........#...........................................................................................#..............#.........
|
||||||
|
...#.......#......................................#...............#...............................................................
|
||||||
|
.#........#......#.#....................................#...#...........#..........#........#.#.......#.......................#...
|
||||||
|
......................#...........#......##....................#..................#......#..............................#.......#.
|
||||||
|
..............#......................#..............#...#.#............................................................#.#........
|
||||||
|
..............#..................#...#..................#...#.....#...#...............#...........................................
|
||||||
|
.............#............................##..#...#...............................................................##..............
|
||||||
|
..#............................#.......#.#....................................#...................................................
|
||||||
|
.......#.....#..........#......#.....#...................................#...........................#......#....#..............#.
|
||||||
|
.#.................#.............................................#................#.............#.....#....................#......
|
||||||
|
..............#.........#..#.....#...........#..............................#.................................#............#......
|
||||||
|
....#.#............#............................................................#..........#......................................
|
||||||
|
.........................................#.......#......#....................................#.....................#...#......#...
|
||||||
|
....#.............................#....................................#..........#................................#..............
|
||||||
|
.........#..#......#..............................................................#....................#..........................
|
||||||
|
.......................................................##...#........#......................................................#.#..#
|
||||||
|
..........................#.......##..##................................................................................#.........
|
||||||
|
..................................##..................................................................#..........................#
|
||||||
|
.#..................................................#..............#.....................#.....#..................................
|
||||||
|
.......##........................#......................................................#..........#..............................
|
||||||
|
.........#....#....#.........#.#........................#...#.....................................................#.....#.........
|
||||||
|
....#.............................#....#....................#.....................#.....#......#.....................#..........#.
|
||||||
|
............................................................................................#..........#......#...................
|
||||||
|
..................................................................................#..........................#.#.....#.........#..
|
||||||
|
..................................................#...................................................#......#.....#............#.
|
||||||
|
..........................................^#................................................................................#.....
|
||||||
|
...............#..........#.............................................#.....#....#...................#................#.........
|
||||||
|
#..#....#........#........#.........#.......#.................#.......................#.#.............#...................#.......
|
||||||
|
..#................................................................#......................................#.......................
|
||||||
|
........#......................#.....................##.#..........#.............................#.............................#..
|
||||||
|
....#..........#........................#.........................................................................................
|
||||||
|
......#...#.#.#.........#.................#.....#.....#...#......#..................................................##........#...
|
||||||
|
...#....#....................#...................................................................#......#...............#.........
|
||||||
|
..#.......#.........................#..........#........#............................#......#....................#................
|
||||||
|
.................................#.......................................#....................................................#..#
|
||||||
|
......................#...#........................#.......................#.............#........................................
|
||||||
|
......#.......#...................................................................................................#...............
|
||||||
|
.............#............#......#............................#...........................................##......................
|
||||||
|
..........................#.....................................................................................#.................
|
||||||
|
....#..............................#......................#........................................#....#...........#.............
|
||||||
|
................#.......#.........................................................................................................
|
||||||
|
.............#............#........#.....#.........#.#.#............................#..........#..............#....#.......#.....#
|
||||||
|
..#.....#...............................................................................................#...............#.........
|
||||||
|
.......#...........#............#................#.......................................#..........................#......#......
|
||||||
|
..........#........................................#......................#............#....................................#..#..
|
||||||
|
.......#......#..............................#........................................................#.#.................#.......
|
||||||
|
.#.................#..#.....................................................................................#...........##........
|
||||||
|
#...................................#........................................#..............#.##........#..#.#.............#......
|
||||||
|
......#...............#........#....#.........#.........#...#.......................#...........................#............#....
|
||||||
|
...#...................................................................................#.......................................#..
|
||||||
|
.....#.......................................#................#...............................................................#...
|
||||||
|
.#................................................##................................#....................#...............#........
|
||||||
|
.................................................#..#..................#.......#........................#...............#.....#...
|
||||||
|
.....##.#.............................#.....................#................................................................#...#
|
||||||
|
...........#....................#...........#...............#.................................................#.......#.......#..#
|
||||||
|
........#....................................................#.....#....#.........................................#.....#.#.......
|
||||||
|
...............#..........................#............#.#.................................................#......................
|
||||||
|
...........#......#.....................#...................................................................#.#..............#....
|
||||||
|
......#...#.........................#..........................................................#..................................
|
||||||
|
.....................#................................................#..#........................................................
|
||||||
|
..............#.................#............#....................#.#.......................................#............#........
|
||||||
|
............#.....................#................#......#..........................#.#.........#............#...................
|
||||||
|
........................................................................#.....................#...................................
|
||||||
|
.............................#....................................................................................................
|
||||||
|
.........................#...#..........................................#............................................#............
|
||||||
|
..#...............................#.....#.....#.............#.......#..#.................##.#.......................#.........#.#.
|
||||||
|
......................................#...........................................................................................
|
||||||
|
...........#.....................................#................#...#.........................#........................#........
|
||||||
|
..#...#..#..........................#.......#....#......................................................#................#........
|
||||||
|
.....................................#...#.#........#...............#..#......#.....................................#...#.........
|
||||||
|
.#..............#..............................................................#.........................##......................#
|
||||||
|
..........#..................................#....#.........#...#.#..........#...#...................................##...........
|
||||||
|
...............#.....................................................................................................#............
|
||||||
|
.#.......#....................#...#..........................#.#...............#......................#........#.................#
|
||||||
|
.............#....................#........................#..................#......#....#....#..................................
|
||||||
|
........#..............................................................#.....................#....................................
|
||||||
|
.......................#..............#.............#......#.........................................#.........................#..
|
||||||
|
.......#.......#.#........#.......................#..#.......................................................#.#..................
|
||||||
|
...............#..#....#.......................#.....#..............................#..................#.....................#....
|
||||||
|
......#..............#...........................#............................##...........................#......................
|
||||||
|
.......................................#........#................................#..........................#.....................
|
||||||
|
.#.....................................#............#.............................................................#..#............
|
||||||
|
..................#..................#.........................#............#..........#.........................#...#............
|
||||||
|
.......................................................#..............#..........................................................#
|
||||||
|
...#....#..#...#..#...................##....................#.....................#...................#.....#..........#..........
|
||||||
|
.......................#........#....................##.................#.......................................................#.
|
||||||
|
........#.......#.......................#.#......##...##........#..##.........................................#...............##..
|
||||||
|
....#..........................#.........##..#......#...........................#........#........#...#...........................
|
||||||
|
...#..........................#.....#.....................................#.......................................................
|
||||||
|
....................................##.......#..............................#.............#...#......#.............#..............
|
||||||
|
.#..............................#...............................................................................................#.
|
||||||
|
.....#................................................................................##.....#..............#.#...................
|
||||||
|
...........................................................................................#....#................#................
|
||||||
|
...#..#...................................................#....#.......#.......#.................................#........#.......
|
||||||
|
.#....#....................#..............................................................................#.............#.......#.
|
||||||
|
.........#.................#.......................................#...........#......#.......#............#............#.........
|
||||||
|
........................................#...............................#.........................................................
|
||||||
|
..............#....#......................#..........#........#.....................................#...........#..........#......
|
||||||
|
......##....#.............#..........#......................#.........................................##..........................
|
||||||
|
....................#.............................#.....................##.#..........#..#......................###.........#.....
|
||||||
|
.#.......................................##........................#.....#.....#..#...............................................
|
||||||
|
..............................#............................#.......................................................##.............
|
||||||
|
.........#.............................#..............#......#.....#..............#.#............................#........#....#..
|
||||||
|
............#..............#.............................#..........................#.............................#...............
|
||||||
|
.................#...........#..............#.....#..............................#.............#................#.....#...........
|
||||||
|
........#.....................#........#.....................................#..........................#......#.#................
|
||||||
|
.....................................................#..#........................#.##..#.........#................................
|
||||||
|
......#..............................................#..........................#...............................#..........#......
|
||||||
|
.................................................#.................##.......................#...................................#.
|
||||||
|
....................#.....#.......#.............#................................#..........#...#.................................
|
||||||
10
2024/day06/input_small.txt
Normal file
10
2024/day06/input_small.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
....#.....
|
||||||
|
.........#
|
||||||
|
..........
|
||||||
|
..#.......
|
||||||
|
.......#..
|
||||||
|
..........
|
||||||
|
.#..^.....
|
||||||
|
........#.
|
||||||
|
#.........
|
||||||
|
......#...
|
||||||
95
2024/day06/src/lib.rs
Normal file
95
2024/day06/src/lib.rs
Normal 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
3
2024/day06/src/main.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
fn main() {
|
||||||
|
day06::main();
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{borrow::Cow, process};
|
use std::{borrow::Cow, process};
|
||||||
|
|
||||||
use clap::{Arg, ArgMatches, Command};
|
use clap::{value_parser, Arg, ArgMatches, Command};
|
||||||
|
|
||||||
use crate::{Day, Variant};
|
use crate::{Day, Variant};
|
||||||
|
|
||||||
|
|
@ -49,10 +49,13 @@ fn create_variant_subcommands(mut part: Command, variants: &[Variant]) -> Comman
|
||||||
Command::new(v.name)
|
Command::new(v.name)
|
||||||
.about(format!("Run the {} variant", v.name))
|
.about(format!("Run the {} variant", v.name))
|
||||||
.arg(Arg::new("input").short('i').long("input"))
|
.arg(Arg::new("input").short('i').long("input"))
|
||||||
|
.arg(Arg::new("iter").long("iter").value_parser(value_parser!(usize)))
|
||||||
})
|
})
|
||||||
.for_each(|cmd| part = part.clone().subcommand(cmd));
|
.for_each(|cmd| part = part.clone().subcommand(cmd));
|
||||||
} else {
|
} else {
|
||||||
part = part.arg(Arg::new("input").short('i').long("input"));
|
part = part
|
||||||
|
.arg(Arg::new("input").short('i').long("input"))
|
||||||
|
.arg(Arg::new("iter").long("iter").value_parser(value_parser!(usize)));
|
||||||
}
|
}
|
||||||
|
|
||||||
part
|
part
|
||||||
|
|
@ -63,21 +66,26 @@ fn dispatch_root_subcommand<D: Day>(
|
||||||
variants: &[Variant],
|
variants: &[Variant],
|
||||||
matches: &ArgMatches,
|
matches: &ArgMatches,
|
||||||
) -> ! {
|
) -> ! {
|
||||||
|
let iter = matches.get_one::<usize>("iter").unwrap_or(&1);
|
||||||
|
|
||||||
if variants.len() > 1 {
|
if variants.len() > 1 {
|
||||||
let subcommand = matches.subcommand().unwrap();
|
let subcommand = matches.subcommand().unwrap();
|
||||||
let variant = variants.iter().find(|v| v.name == subcommand.0).unwrap();
|
let variant = variants.iter().find(|v| v.name == subcommand.0).unwrap();
|
||||||
let input = get_input(subcommand.1, default_input);
|
let input = get_input(subcommand.1, default_input);
|
||||||
execute::<D>(variant, &input);
|
execute::<D>(variant, &input, *iter);
|
||||||
} else {
|
} else {
|
||||||
let input = get_input(matches, default_input);
|
let input = get_input(matches, default_input);
|
||||||
execute::<D>(&variants[0], &input);
|
execute::<D>(&variants[0], &input, *iter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute<D: Day>(variant: &Variant, input: &str) -> ! {
|
fn execute<D: Day>(variant: &Variant, input: &str, iter: usize) -> ! {
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
let input = D::pad_input(input);
|
let input = D::pad_input(input);
|
||||||
let result = (variant.f)(&input);
|
let mut result = 0;
|
||||||
|
for _ in 0..iter {
|
||||||
|
result = (variant.f)(&input);
|
||||||
|
}
|
||||||
let err = writeln!(std::io::stdout(), "{result}");
|
let err = writeln!(std::io::stdout(), "{result}");
|
||||||
if let Err(err) = err {
|
if let Err(err) = err {
|
||||||
if err.kind() != std::io::ErrorKind::BrokenPipe {
|
if err.kind() != std::io::ErrorKind::BrokenPipe {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue