Compare commits
10 Commits
f18a87ba4d
...
ff59a34ac1
| Author | SHA1 | Date | |
|---|---|---|---|
| ff59a34ac1 | |||
| ea301ea3cb | |||
| a28244231a | |||
| e7d501f936 | |||
| ce8daa9e3d | |||
|
|
49784eea22 | ||
|
|
091ef1f518 | ||
| fedb474b4b | |||
|
|
5bb1e5b31c | ||
|
|
3af4295850 |
25
atcoder/beginner_contests/abc050/Cargo.lock
generated
Normal file
25
atcoder/beginner_contests/abc050/Cargo.lock
generated
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "abc050"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"proconio",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lazy_static"
|
||||||
|
version = "1.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proconio"
|
||||||
|
version = "0.4.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "91c333be3af2936f8e810300bc74fe4d0cc168ebc04ab02a28c5b1060fa1bd59"
|
||||||
|
dependencies = [
|
||||||
|
"lazy_static",
|
||||||
|
]
|
||||||
12
atcoder/beginner_contests/abc050/Cargo.toml
Normal file
12
atcoder/beginner_contests/abc050/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[package]
|
||||||
|
name = "abc050"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# dependencies added to new project
|
||||||
|
[dependencies]
|
||||||
|
proconio = "0.4.3"
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
lto = true
|
||||||
|
panic = 'abort'
|
||||||
19
atcoder/beginner_contests/abc050/src/bin/a.rs
Normal file
19
atcoder/beginner_contests/abc050/src/bin/a.rs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
use proconio::input;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
input! {
|
||||||
|
a: u32,
|
||||||
|
op: char,
|
||||||
|
b: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut result = a as i32;
|
||||||
|
|
||||||
|
match op {
|
||||||
|
'+' => result += b as i32,
|
||||||
|
'-' => result -= b as i32,
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{}", result);
|
||||||
|
}
|
||||||
17
atcoder/beginner_contests/abc050/src/bin/b.rs
Normal file
17
atcoder/beginner_contests/abc050/src/bin/b.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
use proconio::input;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
input! {
|
||||||
|
n: usize,
|
||||||
|
ts: [i32; n],
|
||||||
|
m: usize,
|
||||||
|
pxs: [(i32, i32); m],
|
||||||
|
};
|
||||||
|
|
||||||
|
let max_time: i32 = ts.iter().sum();
|
||||||
|
|
||||||
|
for (p, x) in pxs {
|
||||||
|
let time_saved = ts[p as usize - 1] - x;
|
||||||
|
println!("{}", max_time - time_saved);
|
||||||
|
}
|
||||||
|
}
|
||||||
3
atcoder/beginner_contests/abc050/src/bin/c.rs
Normal file
3
atcoder/beginner_contests/abc050/src/bin/c.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fn main() {
|
||||||
|
unimplemented!();
|
||||||
|
}
|
||||||
3
atcoder/beginner_contests/abc050/src/bin/d.rs
Normal file
3
atcoder/beginner_contests/abc050/src/bin/d.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fn main() {
|
||||||
|
unimplemented!();
|
||||||
|
}
|
||||||
7
projecteuler/086/Cargo.lock
generated
Normal file
7
projecteuler/086/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
8
projecteuler/086/Cargo.toml
Normal file
8
projecteuler/086/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
52
projecteuler/086/src/main.rs
Normal file
52
projecteuler/086/src/main.rs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
fn does_cuboid_have_integer_shortest_path(a: u64, b: u64, c: u64) -> bool {
|
||||||
|
assert!(a >= b);
|
||||||
|
assert!(b >= c);
|
||||||
|
|
||||||
|
let d = a * a + (b + c).pow(2);
|
||||||
|
let root = (d as f64).sqrt() as u64;
|
||||||
|
|
||||||
|
d == root * root
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_solution(old_solution: u64, max_m: u64) -> u64 {
|
||||||
|
let mut current_number_of_solutions = 0;
|
||||||
|
|
||||||
|
for h in 1..=max_m {
|
||||||
|
for w in 1..=h {
|
||||||
|
if does_cuboid_have_integer_shortest_path(max_m, h, w) {
|
||||||
|
current_number_of_solutions += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
current_number_of_solutions + old_solution
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, this is Patrick!");
|
||||||
|
let now = Instant::now();
|
||||||
|
|
||||||
|
let max_value = 1000000;
|
||||||
|
|
||||||
|
let mut number_of_solutions = vec![0];
|
||||||
|
let mut m = 1;
|
||||||
|
|
||||||
|
while let Some(&n) = number_of_solutions.last() {
|
||||||
|
if n > max_value {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
number_of_solutions.push(new_solution(n, m));
|
||||||
|
m += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"The least value of M such that the number of solutions first exceeds {} is: {}",
|
||||||
|
max_value,
|
||||||
|
number_of_solutions.len() - 1,
|
||||||
|
);
|
||||||
|
|
||||||
|
println!("Time passed: {:?}", Instant::now() - now);
|
||||||
|
}
|
||||||
7
projecteuler/087/Cargo.lock
generated
Normal file
7
projecteuler/087/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
8
projecteuler/087/Cargo.toml
Normal file
8
projecteuler/087/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
62
projecteuler/087/src/main.rs
Normal file
62
projecteuler/087/src/main.rs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
fn primes(max_prime: u32) -> Vec<u32> {
|
||||||
|
let mut prime_bools = vec![true; max_prime as usize];
|
||||||
|
prime_bools[0] = false;
|
||||||
|
prime_bools[1] = false;
|
||||||
|
|
||||||
|
for p in 2..=(max_prime as f32).powf(0.5) as usize + 1 {
|
||||||
|
if !prime_bools[p] {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut i = p;
|
||||||
|
while i + p < max_prime as usize {
|
||||||
|
i += p;
|
||||||
|
prime_bools[i] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
prime_bools
|
||||||
|
.into_iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter_map(|(n, b)| match b {
|
||||||
|
false => None,
|
||||||
|
true => Some(n as u32),
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, this is Patrick!");
|
||||||
|
let now = Instant::now();
|
||||||
|
|
||||||
|
let mut prime_power_triples = HashSet::new();
|
||||||
|
let max_value = 50000000;
|
||||||
|
|
||||||
|
let primes_power_two = primes(f32::powf(max_value as f32, 0.5) as u32 + 1);
|
||||||
|
let primes_power_three = primes(f32::powf(max_value as f32, 1.0 / 3.0) as u32 + 1);
|
||||||
|
let primes_power_four = primes(f32::powf(max_value as f32, 0.25) as u32 + 1);
|
||||||
|
|
||||||
|
for twos in primes_power_two.iter() {
|
||||||
|
for threes in primes_power_three.iter() {
|
||||||
|
for fours in primes_power_four.iter() {
|
||||||
|
let n = twos * twos + threes * threes * threes + fours * fours * fours * fours;
|
||||||
|
if n < max_value {
|
||||||
|
prime_power_triples.insert(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"Number of prime power triples under {}: {:?}",
|
||||||
|
max_value,
|
||||||
|
prime_power_triples.len()
|
||||||
|
);
|
||||||
|
|
||||||
|
// println!("They are: {:?}", prime_power_triples);
|
||||||
|
|
||||||
|
println!("Time passed: {:?}", Instant::now() - now);
|
||||||
|
}
|
||||||
25
projecteuler/088/Cargo.lock
generated
Normal file
25
projecteuler/088/Cargo.lock
generated
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "autocfg"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-traits"
|
||||||
|
version = "0.2.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
]
|
||||||
9
projecteuler/088/Cargo.toml
Normal file
9
projecteuler/088/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
num-traits = "0.2.15"
|
||||||
79
projecteuler/088/src/main.rs
Normal file
79
projecteuler/088/src/main.rs
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
use num_traits::{identities::One, Num};
|
||||||
|
use std::{
|
||||||
|
cmp::min,
|
||||||
|
collections::{hash_map::RandomState, HashSet},
|
||||||
|
time::Instant,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn product<T: Num + Copy>(v: &Vec<T>) -> T {
|
||||||
|
v.iter().fold(One::one(), |acc, &v_x| acc * v_x)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn naive_solution(k: u32) -> u32 {
|
||||||
|
let max_ps = 2 * k;
|
||||||
|
let mut numbers = vec![1; k as usize];
|
||||||
|
let mut result = u32::MAX;
|
||||||
|
let mut idx = k as usize - 1;
|
||||||
|
|
||||||
|
while numbers[0] <= k {
|
||||||
|
let p = product(&numbers);
|
||||||
|
let s = numbers.iter().sum::<u32>();
|
||||||
|
|
||||||
|
if p == s {
|
||||||
|
result = min(result, p);
|
||||||
|
return result;
|
||||||
|
} else if p > max_ps || s > max_ps {
|
||||||
|
idx -= 1;
|
||||||
|
} else {
|
||||||
|
numbers[idx] += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ChatGPT failing once again
|
||||||
|
fn _chatgpt_solution(k: u32) -> u32 {
|
||||||
|
let mut prod = vec![2 * k; k as usize + 1];
|
||||||
|
let mut sums = vec![2 * k; k as usize + 1];
|
||||||
|
|
||||||
|
prod[1] = 1; // product of a single number is 1
|
||||||
|
sums[1] = 1; // sum of a single number is 1
|
||||||
|
|
||||||
|
// iterate through all possible combinations of numbers
|
||||||
|
for i in 2..k + 1 {
|
||||||
|
for j in (i..2 * k + 1).rev() {
|
||||||
|
for p in 1..=j - i {
|
||||||
|
let q = j - i - p;
|
||||||
|
if q >= i && prod[p as usize] <= j / prod[q as usize] {
|
||||||
|
let s = sums[p as usize] + q;
|
||||||
|
if s < sums[j as usize] {
|
||||||
|
sums[j as usize] = s;
|
||||||
|
prod[j as usize] = prod[p as usize] * q;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sums[k as usize]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, this is Patrick!");
|
||||||
|
let now = Instant::now();
|
||||||
|
|
||||||
|
let max_k = 12;
|
||||||
|
let solution = naive_solution;
|
||||||
|
|
||||||
|
let results: Vec<u32> = ((2..=max_k).into_iter().map(|k| solution(k))).collect();
|
||||||
|
let hash_results: HashSet<u32, RandomState> = HashSet::from_iter(results.into_iter());
|
||||||
|
let result: u32 = hash_results.into_iter().sum();
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"The sum of all minimal product-sum numbers for 2 <= k <= {} is: {}",
|
||||||
|
max_k, result,
|
||||||
|
);
|
||||||
|
|
||||||
|
println!("Time passed: {:?}", Instant::now() - now);
|
||||||
|
}
|
||||||
32
projecteuler/089/Cargo.lock
generated
Normal file
32
projecteuler/089/Cargo.lock
generated
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"nom",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "minimal-lexical"
|
||||||
|
version = "0.2.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "nom"
|
||||||
|
version = "7.1.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
"minimal-lexical",
|
||||||
|
]
|
||||||
9
projecteuler/089/Cargo.toml
Normal file
9
projecteuler/089/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
nom = "7.1.3"
|
||||||
1000
projecteuler/089/roman.txt
Normal file
1000
projecteuler/089/roman.txt
Normal file
File diff suppressed because it is too large
Load Diff
407
projecteuler/089/src/main.rs
Normal file
407
projecteuler/089/src/main.rs
Normal file
@@ -0,0 +1,407 @@
|
|||||||
|
use nom::{
|
||||||
|
branch::alt,
|
||||||
|
character::complete::{char, multispace1},
|
||||||
|
multi::{many1, separated_list1},
|
||||||
|
IResult, Parser,
|
||||||
|
};
|
||||||
|
use std::{time::Instant, vec};
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||||
|
enum RomanNum {
|
||||||
|
I,
|
||||||
|
V,
|
||||||
|
X,
|
||||||
|
L,
|
||||||
|
C,
|
||||||
|
D,
|
||||||
|
M,
|
||||||
|
Empty,
|
||||||
|
}
|
||||||
|
use RomanNum::*;
|
||||||
|
type RomanNumeral = Vec<RomanNum>;
|
||||||
|
|
||||||
|
impl RomanNum {
|
||||||
|
fn to_digit(&self) -> u32 {
|
||||||
|
match self {
|
||||||
|
I => 1,
|
||||||
|
V => 5,
|
||||||
|
X => 10,
|
||||||
|
L => 50,
|
||||||
|
C => 100,
|
||||||
|
D => 500,
|
||||||
|
M => 1000,
|
||||||
|
Empty => 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function is not correct at all but I'm not gonna bother fixing it
|
||||||
|
#[deprecated]
|
||||||
|
fn _is_minimal(number: &RomanNumeral) -> bool {
|
||||||
|
let mut last_num = Empty;
|
||||||
|
let mut occ = 0;
|
||||||
|
|
||||||
|
for num in number.iter() {
|
||||||
|
if *num == last_num {
|
||||||
|
occ += 1;
|
||||||
|
} else {
|
||||||
|
last_num = *num;
|
||||||
|
occ = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if occ > 3 && (last_num == I || last_num == X || last_num == C) {
|
||||||
|
return false;
|
||||||
|
// } else if occ > 1 && (last_num == V || last_num == L || last_num == D) {
|
||||||
|
// return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
#[deprecated]
|
||||||
|
fn _reduce(original: &RomanNumeral) -> RomanNumeral {
|
||||||
|
let mut last_num = Empty;
|
||||||
|
let mut occ = 0;
|
||||||
|
let mut result = vec![];
|
||||||
|
|
||||||
|
let mut number = original.clone();
|
||||||
|
number.push(Empty);
|
||||||
|
|
||||||
|
for num in number.iter() {
|
||||||
|
if *num == last_num {
|
||||||
|
occ += 1;
|
||||||
|
|
||||||
|
if occ == 10 {
|
||||||
|
match last_num {
|
||||||
|
I => result.push(X),
|
||||||
|
X => result.push(C),
|
||||||
|
C => result.push(M),
|
||||||
|
M => {
|
||||||
|
for _ in 0..occ {
|
||||||
|
result.push(M);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
occ -= 10;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if occ < 4 {
|
||||||
|
for _ in 0..occ {
|
||||||
|
result.push(last_num);
|
||||||
|
}
|
||||||
|
} else if occ == 4 {
|
||||||
|
match last_num {
|
||||||
|
I => {
|
||||||
|
result.push(I);
|
||||||
|
result.push(V);
|
||||||
|
}
|
||||||
|
X => {
|
||||||
|
result.push(X);
|
||||||
|
result.push(L);
|
||||||
|
}
|
||||||
|
C => {
|
||||||
|
result.push(C);
|
||||||
|
result.push(D);
|
||||||
|
}
|
||||||
|
M => {
|
||||||
|
for _ in 0..occ {
|
||||||
|
result.push(M);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
} else if occ == 5 {
|
||||||
|
match last_num {
|
||||||
|
I => result.push(V),
|
||||||
|
X => result.push(L),
|
||||||
|
C => result.push(D),
|
||||||
|
M => {
|
||||||
|
for _ in 0..occ {
|
||||||
|
result.push(M);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
} else if occ >= 6 && occ <= 8 {
|
||||||
|
match last_num {
|
||||||
|
I => {
|
||||||
|
result.push(V);
|
||||||
|
for _ in 0..occ - 5 {
|
||||||
|
result.push(I);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
X => {
|
||||||
|
result.push(L);
|
||||||
|
for _ in 0..occ - 5 {
|
||||||
|
result.push(X);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
C => {
|
||||||
|
result.push(D);
|
||||||
|
for _ in 0..occ - 5 {
|
||||||
|
result.push(C);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
M => {
|
||||||
|
for _ in 0..occ {
|
||||||
|
result.push(M);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
} else if occ == 9 {
|
||||||
|
match last_num {
|
||||||
|
I => {
|
||||||
|
result.push(I);
|
||||||
|
result.push(X);
|
||||||
|
}
|
||||||
|
X => {
|
||||||
|
result.push(X);
|
||||||
|
result.push(C);
|
||||||
|
}
|
||||||
|
C => {
|
||||||
|
result.push(C);
|
||||||
|
result.push(M);
|
||||||
|
}
|
||||||
|
M => {
|
||||||
|
for _ in 0..occ {
|
||||||
|
result.push(M);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
last_num = *num;
|
||||||
|
occ = 1;
|
||||||
|
}
|
||||||
|
// println!("{:?}", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
#[deprecated]
|
||||||
|
fn _reduce_extra(numeral: &RomanNumeral) -> RomanNumeral {
|
||||||
|
if numeral.len() <= 3 {
|
||||||
|
return numeral.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut result = vec![];
|
||||||
|
let mut window = vec![Empty, numeral[0], numeral[1]];
|
||||||
|
let mut i = 2;
|
||||||
|
|
||||||
|
while i < numeral.len() {
|
||||||
|
result.push(window[0]);
|
||||||
|
window[0] = window[1];
|
||||||
|
window[1] = window[2];
|
||||||
|
window[2] = numeral[i];
|
||||||
|
|
||||||
|
if window[0] == V && window[1] == I && window[2] == V {
|
||||||
|
result.push(I);
|
||||||
|
result.push(X);
|
||||||
|
|
||||||
|
window[1] = Empty;
|
||||||
|
window[2] = Empty;
|
||||||
|
} else if window[0] == L && window[1] == X && window[2] == L {
|
||||||
|
result.push(X);
|
||||||
|
result.push(C);
|
||||||
|
|
||||||
|
window[1] = Empty;
|
||||||
|
window[2] = Empty;
|
||||||
|
} else if window[0] == D && window[1] == C && window[2] == D {
|
||||||
|
result.push(C);
|
||||||
|
result.push(M);
|
||||||
|
|
||||||
|
window[1] = Empty;
|
||||||
|
window[2] = Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
result.push(window[0]);
|
||||||
|
result.push(window[1]);
|
||||||
|
result.push(window[2]);
|
||||||
|
|
||||||
|
result.into_iter().filter(|&n| n != Empty).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn roman_to_decimal(number: &RomanNumeral) -> u32 {
|
||||||
|
let mut result: i32 = 0;
|
||||||
|
let mut last;
|
||||||
|
let mut current = Empty;
|
||||||
|
|
||||||
|
for &num in number.iter() {
|
||||||
|
last = current;
|
||||||
|
current = num;
|
||||||
|
|
||||||
|
if last.to_digit() < current.to_digit() {
|
||||||
|
result -= last.to_digit() as i32;
|
||||||
|
} else {
|
||||||
|
result += last.to_digit() as i32;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result += current.to_digit() as i32;
|
||||||
|
result as u32
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decimal_to_roman(number: u32) -> RomanNumeral {
|
||||||
|
let mut n = number;
|
||||||
|
let mut result = vec![];
|
||||||
|
|
||||||
|
while n > 0 {
|
||||||
|
while n >= 1000 {
|
||||||
|
result.push(M);
|
||||||
|
n -= 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
if n >= 900 {
|
||||||
|
result.push(C);
|
||||||
|
result.push(M);
|
||||||
|
n -= 900;
|
||||||
|
}
|
||||||
|
|
||||||
|
if n >= 500 {
|
||||||
|
result.push(D);
|
||||||
|
n -= 500;
|
||||||
|
}
|
||||||
|
|
||||||
|
if n >= 400 {
|
||||||
|
result.push(C);
|
||||||
|
result.push(D);
|
||||||
|
n -= 400;
|
||||||
|
}
|
||||||
|
|
||||||
|
while n >= 100 {
|
||||||
|
result.push(C);
|
||||||
|
n -= 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
if n >= 90 {
|
||||||
|
result.push(X);
|
||||||
|
result.push(C);
|
||||||
|
n -= 90;
|
||||||
|
}
|
||||||
|
|
||||||
|
if n >= 50 {
|
||||||
|
result.push(L);
|
||||||
|
n -= 50;
|
||||||
|
}
|
||||||
|
|
||||||
|
if n >= 40 {
|
||||||
|
result.push(X);
|
||||||
|
result.push(L);
|
||||||
|
n -= 40;
|
||||||
|
}
|
||||||
|
|
||||||
|
while n >= 10 {
|
||||||
|
result.push(X);
|
||||||
|
n -= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
if n == 9 {
|
||||||
|
result.push(I);
|
||||||
|
result.push(X);
|
||||||
|
n -= 9;
|
||||||
|
}
|
||||||
|
|
||||||
|
if n >= 5 {
|
||||||
|
result.push(V);
|
||||||
|
n -= 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
if n == 4 {
|
||||||
|
result.push(I);
|
||||||
|
result.push(V);
|
||||||
|
n -= 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
while n > 0 {
|
||||||
|
result.push(I);
|
||||||
|
n -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_input(input: &str) -> IResult<&str, Vec<RomanNumeral>> {
|
||||||
|
let (input, result) = separated_list1(
|
||||||
|
multispace1,
|
||||||
|
many1(
|
||||||
|
alt((
|
||||||
|
char('I'),
|
||||||
|
char('V'),
|
||||||
|
char('X'),
|
||||||
|
char('L'),
|
||||||
|
char('C'),
|
||||||
|
char('D'),
|
||||||
|
char('M'),
|
||||||
|
))
|
||||||
|
.map(|c| match c {
|
||||||
|
'I' => I,
|
||||||
|
'V' => V,
|
||||||
|
'X' => X,
|
||||||
|
'L' => L,
|
||||||
|
'C' => C,
|
||||||
|
'D' => D,
|
||||||
|
'M' => M,
|
||||||
|
_ => Empty,
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)(input)?;
|
||||||
|
|
||||||
|
Ok((input, result))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, this is Patrick!");
|
||||||
|
let now = Instant::now();
|
||||||
|
|
||||||
|
let input_text = include_str!("../roman.txt");
|
||||||
|
|
||||||
|
// let res = parse_input(input_text);
|
||||||
|
// println!("{:?}", res);
|
||||||
|
|
||||||
|
let (_, numerals) = parse_input(input_text).unwrap();
|
||||||
|
let mut result = 0;
|
||||||
|
|
||||||
|
// for mut numeral in numerals {
|
||||||
|
// let start = numeral.len();
|
||||||
|
// let mut s = start;
|
||||||
|
// loop {
|
||||||
|
// numeral = reduce(&numeral);
|
||||||
|
// numeral = reduce_extra(&numeral);
|
||||||
|
// if s == numeral.len() {
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// s = numeral.len();
|
||||||
|
// }
|
||||||
|
// result += start - numeral.len();
|
||||||
|
// println!("{:?} , {}", numeral, start - numeral.len());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Thanks chat gpt for giving me the direction of a solution that should work (although let the record state that the specific solution it generated was not correct at all)
|
||||||
|
for numeral in numerals {
|
||||||
|
let l = numeral.len();
|
||||||
|
let dec_version = roman_to_decimal(&numeral);
|
||||||
|
let rom_version = decimal_to_roman(dec_version);
|
||||||
|
|
||||||
|
// println!(
|
||||||
|
// "{:?} calculated to be {} then represented as {:?}",
|
||||||
|
// numeral, &dec_version, &rom_version,
|
||||||
|
// );
|
||||||
|
|
||||||
|
debug_assert!(l >= rom_version.len());
|
||||||
|
|
||||||
|
result += l - rom_version.len();
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("The number of characters that can be saved is: {}", result);
|
||||||
|
|
||||||
|
println!("Time passed: {:?}", Instant::now() - now);
|
||||||
|
}
|
||||||
7
projecteuler/090/Cargo.lock
generated
Normal file
7
projecteuler/090/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
8
projecteuler/090/Cargo.toml
Normal file
8
projecteuler/090/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
10
projecteuler/090/src/main.rs
Normal file
10
projecteuler/090/src/main.rs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, this is Patrick!");
|
||||||
|
let now = Instant::now();
|
||||||
|
// We can't just loop through all possibilities because that would something in the order of
|
||||||
|
// (10*9*8*7*6*5)^2, which is a really big scary number
|
||||||
|
|
||||||
|
println!("Time passed: {:?}", Instant::now() - now);
|
||||||
|
}
|
||||||
@@ -4,5 +4,5 @@ fn main() {
|
|||||||
println!("Hello, this is Patrick!");
|
println!("Hello, this is Patrick!");
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
|
|
||||||
println!("Time passed: {:?}", now);
|
println!("Time passed: {:?}", Instant::now() - now);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user