summaryrefslogtreecommitdiff
path: root/2023/rust/day6/src
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2024-02-09 11:42:04 +0100
committerSanto Cariotti <santo@dcariotti.me>2024-02-09 11:42:04 +0100
commitdb2686c798378738020a0951a521358586e21aa0 (patch)
tree8d585ceb9bdc302ae10a2458cc343be40d60acb7 /2023/rust/day6/src
parent67dff579e91c13ef40ecb17d14497d145861c5e7 (diff)
Add day2 in OCaml
Diffstat (limited to '2023/rust/day6/src')
-rw-r--r--2023/rust/day6/src/lib.rs102
1 files changed, 102 insertions, 0 deletions
diff --git a/2023/rust/day6/src/lib.rs b/2023/rust/day6/src/lib.rs
new file mode 100644
index 0000000..b3e33cb
--- /dev/null
+++ b/2023/rust/day6/src/lib.rs
@@ -0,0 +1,102 @@
+pub fn part1(input: &str) -> u64 {
+ let mut res: u64 = 1;
+
+ let td: Vec<Vec<_>> = input
+ .trim_end()
+ .split('\n')
+ .map(|x| {
+ x.split(':')
+ .nth(1)
+ .unwrap()
+ .trim()
+ .replace(" ", " ")
+ .replace(" ", " ")
+ .replace(" ", " ")
+ .split(' ')
+ .map(|y| y.parse::<u64>().unwrap())
+ .collect()
+ })
+ .collect();
+
+ assert_eq!(td.len(), 2);
+ let (t, d) = (&td[0], &td[1]);
+
+ for i in 0..t.len() {
+ let mut r = 0;
+ for j in 0..t[i] {
+ if (t[i] - j) * j > d[i] {
+ r += 1;
+ }
+ }
+ res *= r;
+ }
+
+ res
+}
+
+pub fn part2(input: &str) -> u64 {
+ let mut res: u64 = 1;
+
+ let td: Vec<Vec<_>> = input
+ .trim_end()
+ .split('\n')
+ .map(|x| {
+ x.split(':')
+ .nth(1)
+ .unwrap()
+ .trim()
+ .replace(" ", "")
+ .split(' ')
+ .map(|y| y.parse::<u64>().unwrap())
+ .collect()
+ })
+ .collect();
+
+ assert_eq!(td.len(), 2);
+ let (t, d) = (&td[0], &td[1]);
+
+ for i in 0..t.len() {
+ let mut r = 0;
+ for j in 0..t[i] {
+ if (t[i] - j) * j > d[i] {
+ r += 1;
+ }
+ }
+ res *= r;
+ }
+
+ res
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn example_part1() {
+ let input = include_str!("../example.txt");
+ let result = part1(input);
+ assert_eq!(result, 288);
+ }
+
+ #[test]
+ fn input_part1() {
+ let input = include_str!("../input.txt");
+ let result = part1(input);
+ assert_eq!(result, 74698);
+ }
+
+ #[test]
+ fn example_part2() {
+ let input = include_str!("../example.txt");
+ let result = part2(input);
+ assert_eq!(result, 71503);
+ }
+
+ #[test]
+ fn input_part2() {
+ let input = include_str!("../input.txt");
+ let result = part2(input);
+ assert_eq!(result, 27563421);
+ }
+}