Merge branch 'master' of ssh://git.pzwietering.nl:55555/flip/contests

This commit is contained in:
2023-04-08 22:41:41 +02:00
4 changed files with 1448 additions and 0 deletions

32
projecteuler/089/Cargo.lock generated Normal file
View 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",
]

View 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

File diff suppressed because it is too large Load Diff

View 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);
}