summaryrefslogtreecommitdiff
path: root/2023/Rust
diff options
context:
space:
mode:
Diffstat (limited to '2023/Rust')
-rw-r--r--2023/Rust/input/day6.prod2
-rw-r--r--2023/Rust/input/day6.test2
-rw-r--r--2023/Rust/src/day6.rs92
3 files changed, 96 insertions, 0 deletions
diff --git a/2023/Rust/input/day6.prod b/2023/Rust/input/day6.prod
new file mode 100644
index 0000000..71523ae
--- /dev/null
+++ b/2023/Rust/input/day6.prod
@@ -0,0 +1,2 @@
+Time: 60 94 78 82
+Distance: 475 2138 1015 1650
diff --git a/2023/Rust/input/day6.test b/2023/Rust/input/day6.test
new file mode 100644
index 0000000..28f5ae9
--- /dev/null
+++ b/2023/Rust/input/day6.test
@@ -0,0 +1,2 @@
+Time: 7 15 30
+Distance: 9 40 200
diff --git a/2023/Rust/src/day6.rs b/2023/Rust/src/day6.rs
new file mode 100644
index 0000000..02dcac9
--- /dev/null
+++ b/2023/Rust/src/day6.rs
@@ -0,0 +1,92 @@
+use std::{collections::HashMap, marker::PhantomData};
+
+#[derive(Debug)]
+struct Race<State = Part1> {
+ times: Vec<u64>,
+ distances: Vec<u64>,
+ state: std::marker::PhantomData<State>,
+}
+
+struct Part1;
+struct Part2;
+
+impl From<&str> for Race<Part1> {
+ fn from(value: &str) -> Self {
+ let (times, distances) = value.split_once("\n").unwrap();
+ let (_, times) = times.split_once(':').unwrap();
+ let (_, distances) = distances.split_once(':').unwrap();
+ let times = times
+ .split_whitespace()
+ .map(|x| x.parse::<u64>().unwrap())
+ .collect::<Vec<_>>();
+ let distances = distances
+ .split_whitespace()
+ .map(|x| x.parse::<u64>().unwrap())
+ .collect::<Vec<_>>();
+ Self {
+ times,
+ distances,
+ state: PhantomData::<Part1>,
+ }
+ }
+}
+
+impl From<&str> for Race<Part2> {
+ fn from(value: &str) -> Self {
+ let (times, distances) = value.split_once("\n").unwrap();
+ let (_, times) = times.split_once(':').unwrap();
+ let (_, distances) = distances.split_once(':').unwrap();
+ let times = times
+ .chars()
+ .filter(|ch| ch.is_digit(10))
+ .collect::<String>()
+ .parse::<u64>()
+ .unwrap();
+ let distances = distances
+ .chars()
+ .filter(|ch| ch.is_digit(10))
+ .collect::<String>()
+ .parse::<u64>()
+ .unwrap();
+ Self {
+ times: vec![times],
+ distances: vec![distances],
+ state: PhantomData::<Part2>,
+ }
+ }
+}
+
+fn solve_part_one(data: &str) -> u64 {
+ let race: Race<Part1> = Race::from(data);
+ let mut cnt = 0;
+ let mut ans = 1;
+ for (i, _) in race.times.iter().enumerate() {
+ for t in 0..=race.times[i] {
+ if (race.times[i] - t) * t > race.distances[i] {
+ cnt += 1;
+ }
+ }
+ ans *= cnt;
+ cnt = 0;
+ }
+ ans
+}
+fn solve_part_two(data: &str) -> u64 {
+ let race: Race<Part2> = Race::from(data);
+ let mut ans = 0;
+ for t in 0..=race.times[0] {
+ if (race.times[0] - t) * t > race.distances[0] {
+ ans += 1;
+ }
+ }
+ ans
+}
+
+fn main() {
+ let test = include_str!("../input/day6.test");
+ let prod = include_str!("../input/day6.prod");
+ println!("part_1 test: {:?}", solve_part_one(test));
+ println!("part_1 prod {:?}", solve_part_one(prod));
+ println!("part_2 test: {:?}", solve_part_two(test));
+ println!("part_2 prod {:?}", solve_part_two(prod));
+}