Add solution for problem-59 and a C solution for problem 40
This commit is contained in:
65
problem-40.c
Normal file
65
problem-40.c
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
#include<stdlib.h>
|
||||||
|
#include<stdio.h>
|
||||||
|
#include<string.h>
|
||||||
|
|
||||||
|
#define MAX_NUM 1000000
|
||||||
|
|
||||||
|
char* LETTERS = "0123456789";
|
||||||
|
|
||||||
|
|
||||||
|
int digits(int* res, int num)
|
||||||
|
{
|
||||||
|
int len = 0;
|
||||||
|
while (num > 0) {
|
||||||
|
len++;
|
||||||
|
res = (int*)realloc(res, len * sizeof(int));
|
||||||
|
res[len-1] = num % 10;
|
||||||
|
num /= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
char* str;
|
||||||
|
int* digs;
|
||||||
|
int nums, j, result;
|
||||||
|
int digit = 0, i = 0, num = 1;
|
||||||
|
|
||||||
|
str = malloc(MAX_NUM * sizeof(int));
|
||||||
|
digs = malloc(0 * sizeof(int));
|
||||||
|
nums = digits(digs, num);
|
||||||
|
|
||||||
|
while (digit < MAX_NUM) {
|
||||||
|
str[digit] = digs[nums-1-i];
|
||||||
|
digit++;
|
||||||
|
i++;
|
||||||
|
if (i == nums) {
|
||||||
|
/* refill digs and reset i */
|
||||||
|
num++;
|
||||||
|
nums = digits(digs, num);
|
||||||
|
i = 0;
|
||||||
|
/* DEBUG
|
||||||
|
printf("num = %d str = ", num);
|
||||||
|
for (j=0; j<digit; j++)
|
||||||
|
printf("%d", str[j]);
|
||||||
|
printf("\n");
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result = str[0] *
|
||||||
|
str[9] *
|
||||||
|
str[99] *
|
||||||
|
str[999] *
|
||||||
|
str[9999] *
|
||||||
|
str[99999] *
|
||||||
|
str[999999];
|
||||||
|
|
||||||
|
printf("The result is %d\n", result);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
51
problem-59.py
Normal file
51
problem-59.py
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import itertools
|
||||||
|
import requests
|
||||||
|
import string
|
||||||
|
|
||||||
|
URL = "https://projecteuler.net/project/resources/p059_cipher.txt"
|
||||||
|
|
||||||
|
def get_ciphertext(url):
|
||||||
|
resp = requests.get(url)
|
||||||
|
return resp.text
|
||||||
|
|
||||||
|
|
||||||
|
def generate_keys(letters, length):
|
||||||
|
perms = itertools.permutations(letters, length)
|
||||||
|
yield from perms
|
||||||
|
|
||||||
|
|
||||||
|
def ints_to_string(numbers):
|
||||||
|
return [chr(num) for num in numbers]
|
||||||
|
|
||||||
|
|
||||||
|
def string_to_ints(letters):
|
||||||
|
return [ord(letter) for letter in letters]
|
||||||
|
|
||||||
|
|
||||||
|
def xor_lists(xs, ys):
|
||||||
|
if len(xs) != len(ys):
|
||||||
|
raise Exception("Lists must be the same length")
|
||||||
|
|
||||||
|
return [a ^ b for a, b in zip(xs, ys)]
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
text = get_ciphertext(URL)
|
||||||
|
cipher = [int(num) for num in text.split(',')]
|
||||||
|
letters = string.ascii_lowercase
|
||||||
|
common_words = ["the", "of", "to", "and", "in", "is", "it", "that"]
|
||||||
|
for perm in generate_keys(letters, 3):
|
||||||
|
key = "".join(list(perm))
|
||||||
|
cycled = string_to_ints(list(key * (len(cipher) // len(key))))
|
||||||
|
decrypt = "".join(ints_to_string(xor_lists(cipher, cycled)))
|
||||||
|
if all(word in decrypt for word in common_words):
|
||||||
|
print("Decrypted passage:")
|
||||||
|
print(decrypt)
|
||||||
|
print("Key: {0}".format(key))
|
||||||
|
print("Sum: {0}".format(sum(xor_lists(cipher, cycled))))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user