Skip to content

Commit

Permalink
Solve 59 in rust
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 27, 2024
1 parent e3511e9 commit 650e185
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Olivia's Project Euler Solutions
| | GraalPy 23.1+ |br| | | |CodeQL| |br| |
| | Browser [#]_ | | |PythonLint| |
+------------+----------------------------+--------+-------------------+
| Rust | 1.69+ |br| | 35 | |Rust| |br| |
| Rust | 1.69+ |br| | 36 | |Rust| |br| |
| | Browser [#]_ | | |Rs-Cov| |br| |
| | | | |RustClippy| |
+------------+----------------------------+--------+-------------------+
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ Problems Solved
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`63` | | | | | |:py-d:`0063`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`67` | | | | | |:py-d:`0067`| |
|:prob:`67` | | | | | |:py-d:`0067`|:rs-d:`0067`|
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`69` | | | | | |:py-d:`0069`|:rs-d:`0069`|
+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
20 changes: 20 additions & 0 deletions docs/src/rust/p0059.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Rust Implementation of Problem 59
=================================

View source code :source:`rust/src/p0059.rs`

Includes
--------

- `utils <./lib/utils.html>`_

Problem Solution
----------------

.. rust:fn:: pub fn p0059::p0059() -> utils::Answer
.. literalinclude:: ../../../rust/src/p0059.rs
:language: rust
:linenos:

.. tags:: cryptography, binary-operator, xor, file-io
21 changes: 21 additions & 0 deletions docs/src/rust/p0067.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Rust Implementation of Problem 67
=================================

View source code :source:`rust/src/p0067.rs`

Includes
--------

- `triangles <./lib/triangles.html>`_
- `utils <./lib/utils.html>`_

Problem Solution
----------------

.. rust:fn:: pub fn p0067::p0067() -> utils::Answer
.. literalinclude:: ../../../rust/src/p0067.rs
:language: rust
:linenos:

.. tags:: combinatorics, path-finding, file-io
1 change: 1 addition & 0 deletions rust/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Problems Solved
- ☒ `45 <./src/p0045.rs>`__
- ☒ `53 <./src/p0053.rs>`__
- ☒ `59 <./src/p0059.rs>`__
- ☒ `67 <./src/p0067.rs>`__
- ☒ `69 <./src/p0069.rs>`__
- ☒ `76 <./src/p0076.rs>`__
- ☒ `77 <./src/p0077.rs>`__
Expand Down
2 changes: 2 additions & 0 deletions rust/src/include/problems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::p0037::p0037;
use crate::p0045::p0045;
use crate::p0053::p0053;
use crate::p0059::p0059;
use crate::p0067::p0067;
use crate::p0069::p0069;
use crate::p0076::p0076;
use crate::p0077::p0077;
Expand Down Expand Up @@ -54,6 +55,7 @@ pub fn get_problem<'b>(n: usize) -> Option<ProblemRef<'b>> {
45 => Some(( &45, p0045, false)),
53 => Some(( &53, p0053, false)),
59 => Some(( &59, p0059, false)),
67 => Some(( &67, p0067, false)),
69 => Some(( &69, p0069, false)),
76 => Some(( &76, p0076, false)),
77 => Some(( &77, p0077, false)),
Expand Down
1 change: 1 addition & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod p0037;
pub mod p0045;
pub mod p0053;
pub mod p0059;
pub mod p0067;
pub mod p0069;
pub mod p0076;
pub mod p0077;
Expand Down
2 changes: 2 additions & 0 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub mod p0037;
pub mod p0045;
pub mod p0053;
pub mod p0059;
pub mod p0067;
pub mod p0069;
pub mod p0076;
pub mod p0077;
Expand Down Expand Up @@ -85,6 +86,7 @@ seq!(N in 01..=20 {
#[case::problem_45(45)]
#[case::problem_53(53)]
#[case::problem_59(59)]
#[case::problem_67(67)]
#[case::problem_69(69)]
#[case::problem_76(76)]
#[case::problem_77(77)]
Expand Down
39 changes: 10 additions & 29 deletions rust/src/p0018.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Project Euler Problem 18
Project Euler Problem 67
Thinking from the bottom up got the answer
Expand All @@ -8,38 +8,19 @@ Problem:
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.
.. code-block::
3
7 4
2 4 6
8 5 9 3
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:
.. code-block::
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
Find the maximum total from top to bottom in triangle.txt (right click and 'Save Link/Target As...'), a 15K text file
containing a triangle with one-hundred rows.
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)
NOTE: This is a much more difficult version of Problem 18. It is not possible to try every route to solve this problem,
as there are 2^99 altogether! If you could check one trillion (10^12) routes every second it would take over twenty
billion years to check them all. There is an efficient algorithm to solve it. ;o)
*/
use crate::include::triangles::reduce_triangle;
use crate::include::utils::Answer;
Expand Down
35 changes: 35 additions & 0 deletions rust/src/p0067.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Project Euler Problem 67
Thinking from the bottom up got the answer
Problem:
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 in triangle.txt (right click and 'Save Link/Target As...'), a 15K text file
containing a triangle with one-hundred rows.
NOTE: This is a much more difficult version of Problem 18. It is not possible to try every route to solve this problem,
as there are 2^99 altogether! If you could check one trillion (10^12) routes every second it would take over twenty
billion years to check them all. There is an efficient algorithm to solve it. ;o)
*/
use crate::include::triangles::reduce_triangle;
use crate::include::utils::{get_data_file,Answer};


pub fn p0018() -> Answer {
let rows: Vec<Vec<u8>> = vec![];
for line in get_data_file("p0067_triangle.txt").trim().lines() {
rows.push(line.split(' ').map(|x| x.parse::<u8>()).collect::<Vec<u8>>());
}
return Answer::Int(reduce_triangle(rows).into());
}

0 comments on commit 650e185

Please sign in to comment.