- commit
- 55822b915dae0838a0e2d40c53e9f4cbe48ecaa4
- parent
- 5427d198e5119c3279ef48950b13c31ef01337d9
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2023-12-07 08:12
2023-12-06
Diffstat
| A | 2023/06/input.txt | 2 | ++ |
| A | 2023/06/solution.rs | 44 | ++++++++++++++++++++++++++++++++++++++++++++ |
| A | 2023/06/test.txt | 2 | ++ |
3 files changed, 48 insertions, 0 deletions
diff --git a/2023/06/input.txt b/2023/06/input.txt
@@ -0,0 +1,2 @@ -1 1 Time: 51 92 68 90 -1 2 Distance: 222 2031 1126 1225
diff --git a/2023/06/solution.rs b/2023/06/solution.rs
@@ -0,0 +1,44 @@
-1 1 #[path = "../lib.rs"]
-1 2 mod lib;
-1 3
-1 4 fn parse_line(line: &str) -> Vec<u64> {
-1 5 return line
-1 6 .split_whitespace()
-1 7 .skip(1)
-1 8 .map(|s| s.parse().unwrap())
-1 9 .collect();
-1 10 }
-1 11
-1 12 fn count_games(times: &Vec<u64>, dists: &Vec<u64>) -> u64 {
-1 13 let mut results = vec![];
-1 14
-1 15 for i in 0..times.len() {
-1 16 // how many integer x can satisfy
-1 17 // (times[i] - x) * x > dists[i]
-1 18 let p = (times[i] as f64) / 2.0;
-1 19 let q = p * p - (dists[i] as f64);
-1 20 assert!(q >= 0.0);
-1 21 let q2 = q.sqrt();
-1 22 assert!(q2 < p);
-1 23 let x1 = (p - q2).floor() as u64;
-1 24 let x2 = (p + q2).ceil() as u64;
-1 25 results.push(x2 - x1 - 1);
-1 26 }
-1 27
-1 28 return results.iter().product();
-1 29 }
-1 30
-1 31 fn main() {
-1 32 let mut lines = lib::iter_input();
-1 33 let line1 = lines.next().unwrap();
-1 34 let line2 = lines.next().unwrap();
-1 35
-1 36 let times1 = parse_line(&line1);
-1 37 let dists1 = parse_line(&line2);
-1 38
-1 39 let times2 = parse_line(&line1.replace(" ", "").replace(":", ": "));
-1 40 let dists2 = parse_line(&line2.replace(" ", "").replace(":", ": "));
-1 41
-1 42 println!("part2: {}", count_games(×1, &dists1));
-1 43 println!("part2: {}", count_games(×2, &dists2));
-1 44 }
diff --git a/2023/06/test.txt b/2023/06/test.txt
@@ -0,0 +1,2 @@ -1 1 Time: 7 15 30 -1 2 Distance: 9 40 200