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
43ba56e237
commit
8024f31c8c
7 changed files with 117 additions and 0 deletions
15
2023/day06/Cargo.toml
Normal file
15
2023/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
2023/day06/benches/benches.rs
Normal file
3
2023/day06/benches/benches.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
day06::bench();
|
||||
}
|
||||
2
2023/day06/input.txt
Normal file
2
2023/day06/input.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Time: 40 92 97 90
|
||||
Distance: 215 1064 1505 1100
|
||||
2
2023/day06/input_small.txt
Normal file
2
2023/day06/input_small.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Time: 7 15 30
|
||||
Distance: 9 40 200
|
||||
83
2023/day06/src/lib.rs
Normal file
83
2023/day06/src/lib.rs
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
use helper::{Day, IteratorExt, 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)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Race {
|
||||
time: u64,
|
||||
distance: u64,
|
||||
}
|
||||
|
||||
fn parse(input: &str) -> Vec<Race> {
|
||||
let lines = input.lines().collect_array::<2>().unwrap();
|
||||
lines[0]
|
||||
.split_ascii_whitespace()
|
||||
.zip(lines[1].split_ascii_whitespace())
|
||||
.skip(1)
|
||||
.map(|(time, distance)| Race {
|
||||
time: time.parse().unwrap(),
|
||||
distance: distance.parse().unwrap(),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn part1(input: &str) -> u64 {
|
||||
let races = parse(input);
|
||||
|
||||
races
|
||||
.iter()
|
||||
.map(|race| {
|
||||
let t = race.time;
|
||||
|
||||
(0..t)
|
||||
.filter(|&i| {
|
||||
let t_run = t - i;
|
||||
let d = t_run * i;
|
||||
|
||||
d > race.distance
|
||||
})
|
||||
.count() as u64
|
||||
})
|
||||
.product()
|
||||
}
|
||||
|
||||
fn part2(_input: &str) -> u64 {
|
||||
0
|
||||
}
|
||||
|
||||
helper::tests! {
|
||||
day06 Day06;
|
||||
part1 {
|
||||
small => 0;
|
||||
default => 0;
|
||||
}
|
||||
part2 {
|
||||
small => 0;
|
||||
default => 0;
|
||||
}
|
||||
}
|
||||
helper::benchmarks! {}
|
||||
3
2023/day06/src/main.rs
Normal file
3
2023/day06/src/main.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
day06::main();
|
||||
}
|
||||
9
Cargo.lock
generated
9
Cargo.lock
generated
|
|
@ -170,6 +170,15 @@ dependencies = [
|
|||
"nom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day06"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"divan",
|
||||
"helper",
|
||||
"nom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "divan"
|
||||
version = "0.1.4"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue