summaryrefslogtreecommitdiff
path: root/2023
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2023-12-06 10:32:05 +0100
committerSanto Cariotti <santo@dcariotti.me>2023-12-06 10:32:05 +0100
commit2e05ee3b588b6bced1b45c2dfcdb7a2ec4b3819a (patch)
tree1122cb2e743a8762cfa8fe8ca7cf72e793c47505 /2023
parent4301b064d443b13854dd42ef8242d20bef548fe9 (diff)
Add day6
Diffstat (limited to '2023')
-rw-r--r--2023/Cargo.toml2
-rw-r--r--2023/day6/Cargo.toml8
-rw-r--r--2023/day6/example.txt2
-rw-r--r--2023/day6/input.txt3
-rw-r--r--2023/day6/src/lib.rs102
5 files changed, 116 insertions, 1 deletions
diff --git a/2023/Cargo.toml b/2023/Cargo.toml
index 20df693..3a90b12 100644
--- a/2023/Cargo.toml
+++ b/2023/Cargo.toml
@@ -1,2 +1,2 @@
[workspace]
-members = ["day1", "day2", "day3", "day4", "day5"]
+members = ["day1", "day2", "day3", "day4", "day5", "day6"]
diff --git a/2023/day6/Cargo.toml b/2023/day6/Cargo.toml
new file mode 100644
index 0000000..89d04ae
--- /dev/null
+++ b/2023/day6/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "day6"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/2023/day6/example.txt b/2023/day6/example.txt
new file mode 100644
index 0000000..28f5ae9
--- /dev/null
+++ b/2023/day6/example.txt
@@ -0,0 +1,2 @@
+Time: 7 15 30
+Distance: 9 40 200
diff --git a/2023/day6/input.txt b/2023/day6/input.txt
new file mode 100644
index 0000000..f4a8fdc
--- /dev/null
+++ b/2023/day6/input.txt
@@ -0,0 +1,3 @@
+Time: 41 66 72 66
+Distance: 244 1047 1228 1040
+
diff --git a/2023/day6/src/lib.rs b/2023/day6/src/lib.rs
new file mode 100644
index 0000000..b3e33cb
--- /dev/null
+++ b/2023/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);
+ }
+}