feat: 18 - maximum path sum I
This commit is contained in:
parent
d2e0a5cd91
commit
5accea1fb2
3 changed files with 76 additions and 9 deletions
|
@ -11,7 +11,7 @@ I originally started the challenge in [the-honk](https://github.com/newtykins/th
|
|||
|
||||
## Challenge Completion
|
||||
|
||||
### <!-- completed -->21<!-- completed --> out of 100 public challenges completed.
|
||||
### <!-- completed -->22<!-- completed --> out of 100 public challenges completed.
|
||||
|
||||
- [x] 1 - [Multiples of 3 or 5](src/bin/1.rs)
|
||||
- [x] 2 - [Even Fibonacci numbers](src/bin/2.rs)
|
||||
|
@ -30,7 +30,7 @@ I originally started the challenge in [the-honk](https://github.com/newtykins/th
|
|||
- [x] 15 - [Lattice paths](src/bin/15.rs)
|
||||
- [x] 16 - [Power digit sum](src/bin/16.rs)
|
||||
- [x] 17 - [Number letter counts](src/bin/17.rs)
|
||||
- [ ] 18 - Maximum path sum I
|
||||
- [x] 18 - [Maximum path sum I](src/bin/18.rs)
|
||||
- [ ] 19 - Counting Sundays
|
||||
- [ ] 20 - Factorial digit sum
|
||||
- [ ] 21 - Amicable numbers
|
||||
|
|
|
@ -6,9 +6,8 @@ If all the numbers from 1 to 1000 (one thousand) inclusive were written out in w
|
|||
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
|
||||
*/
|
||||
|
||||
use std::ops::Div;
|
||||
|
||||
use phf::phf_map;
|
||||
use std::ops::Div;
|
||||
|
||||
const TRANSLATIONS: phf::Map<u16, &'static str> = phf_map! {
|
||||
0u16 => "",
|
||||
|
@ -58,7 +57,10 @@ fn number_to_words(number: usize) -> String {
|
|||
n -= hundreds * 100;
|
||||
|
||||
if hundreds > 0 {
|
||||
out = format!("{out}{}hundred", TRANSLATIONS.get(&(hundreds as u16)).unwrap());
|
||||
out = format!(
|
||||
"{out}{}hundred",
|
||||
TRANSLATIONS.get(&(hundreds as u16)).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
// Tens
|
||||
|
@ -67,11 +69,11 @@ fn number_to_words(number: usize) -> String {
|
|||
|
||||
if tens > 0 {
|
||||
let translation: &str;
|
||||
let and = if hundreds > 0 {"and"} else {""};
|
||||
let and = if hundreds > 0 { "and" } else { "" };
|
||||
|
||||
if n % 10 > 0 && tens == 1 {
|
||||
translation = TRANSLATIONS.get(&((10 + (n % 10)) as u16)).unwrap();
|
||||
|
||||
|
||||
n -= n % 10;
|
||||
} else {
|
||||
translation = TRANSLATIONS.get(&(tens as u16 * 10)).unwrap();
|
||||
|
@ -82,7 +84,7 @@ fn number_to_words(number: usize) -> String {
|
|||
|
||||
// Remainder
|
||||
if n > 0 {
|
||||
let and = if hundreds > 0 && tens <= 0 {"and"} else {""};
|
||||
let and = if hundreds > 0 && tens <= 0 { "and" } else { "" };
|
||||
out = format!("{out}{and}{}", TRANSLATIONS.get(&(n as u16)).unwrap());
|
||||
}
|
||||
|
||||
|
@ -104,5 +106,8 @@ fn letter_count(upper_bound: usize) -> usize {
|
|||
fn main() {
|
||||
let sum = letter_count(1000);
|
||||
|
||||
println!("{} letters would be used to write out all of the numbers from 1 to 1000!", sum);
|
||||
println!(
|
||||
"{} letters would be used to write out all of the numbers from 1 to 1000!",
|
||||
sum
|
||||
);
|
||||
}
|
||||
|
|
62
src/bin/18.rs
Normal file
62
src/bin/18.rs
Normal file
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
Problem 18 - Maximum path sum I
|
||||
|
||||
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
|
||||
3 7 4
|
||||
2 4 6
|
||||
8 5 9 3
|
||||
That is, 3 + 7 + 4 + 9 = 23.
|
||||
Find the maximum total from top to bottom of the triangle below:
|
||||
75
|
||||
95 64
|
||||
17 47 82
|
||||
18 35 87 10
|
||||
20 04 82 47 65
|
||||
19 01 23 75 03 34
|
||||
88 02 77 73 07 63 67
|
||||
99 65 04 28 06 16 70 92
|
||||
41 41 26 56 83 40 80 70 33
|
||||
41 48 72 33 47 32 37 16 94 29
|
||||
53 71 44 65 25 43 91 52 97 51 14
|
||||
70 11 33 28 77 73 17 78 39 68 17 57
|
||||
91 71 52 38 17 14 91 43 58 50 27 29 48
|
||||
63 66 04 68 89 53 67 30 73 16 69 87 40 31
|
||||
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
|
||||
NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67 , is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
|
||||
*/
|
||||
|
||||
fn maximum_path_sum(triangle: Vec<Vec<usize>>) -> usize {
|
||||
let mut tri = triangle.clone();
|
||||
|
||||
for row in (0..(triangle.len() - 1)).rev() {
|
||||
for col in 0..(row + 1) {
|
||||
tri[row][col] = tri[row][col] + std::cmp::max(tri[row + 1][col], tri[row + 1][col + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
return tri[0][0];
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let triangle: Vec<Vec<usize>> = vec![
|
||||
vec![75],
|
||||
vec![95, 64],
|
||||
vec![17, 47, 82],
|
||||
vec![18, 35, 87, 10],
|
||||
vec![20, 4, 82, 47, 65],
|
||||
vec![19, 1, 23, 75, 3, 34],
|
||||
vec![88, 2, 77, 73, 7, 63, 67],
|
||||
vec![99, 65, 4, 28, 6, 16, 70, 92],
|
||||
vec![41, 41, 26, 56, 83, 40, 80, 70, 33],
|
||||
vec![41, 48, 72, 33, 47, 32, 37, 16, 94, 29],
|
||||
vec![53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14],
|
||||
vec![70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57],
|
||||
vec![91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48],
|
||||
vec![63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31],
|
||||
vec![4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23],
|
||||
];
|
||||
|
||||
let sum = maximum_path_sum(triangle);
|
||||
|
||||
println!("The maximum path sum of the triangle is {}!", sum);
|
||||
}
|
Loading…
Reference in a new issue