-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem13.rs
52 lines (44 loc) · 1.14 KB
/
problem13.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
Problem 13:
Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
*/
use std::fs;
pub fn problem13() -> u128 {
let digits_to_keep = 13;
// Load the numbers, while keeping only the first 13 digits of each
let numbers = get_numbers(digits_to_keep);
let mut s = 0;
for n in numbers {
s += n;
}
// Show only the first 10 digits of the sum
while s >= 10_000_000_000 {
s /= 10
}
s
}
fn get_numbers(digits_to_keep: u8) -> [u128; 100] {
let file_path = "src/problem13.txt";
let contents = fs::read_to_string(file_path)
.expect("Unable to read file.");
let iterator = contents.split("\n");
let mut numbers = [0; 100];
let mut i = 0;
for number in iterator {
let digits: Vec<u128> = number.chars().map(|c| c as u128 - '0' as u128).collect();
for d in 0..digits_to_keep {
numbers[i] *= 10;
numbers[i] += digits[d as usize]
}
i += 1;
}
numbers
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_problem13() {
assert_eq!(problem13(), 5_537_376_230);
}
}