1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- #!/usr/bin/ruby
- # Author: Daniel "Trizen" Șuteu
- # License: GPLv3
- # Website: https://github.com/trizen
- # Your task has been made easy, as the encryption key consists of three lower case characters.
- # Using cipher.txt (right click and 'Save Link/Target As...'), a file containing the encrypted
- # ASCII codes, and the knowledge that the plain text must contain common English words, decrypt
- # the message and find the sum of the ASCII values in the original text.
- # https://projecteuler.net/problem=59
- # Runtime: 0.369s
- var enc = eval('[' + %f'p059_cipher.txt'.read + ']')
- var p_len = 3 # password length
- var pass = [] # decoded password
- for p in (^p_len) {
- for c in ('a'.ord .. 'z'.ord) {
- var dec = []
- for i in (p..enc.end `by` p_len) {
- dec << chr(enc[i] ^ c)
- }
- # The number of non-alpha and non-space chars
- var count = dec.join.count(/[^a-z ]/i)
- # The ratio must be less than 10%
- if (count/dec.len < 0.1) {
- pass << c
- break
- }
- }
- }
- var dec = []
- for i in (^enc) {
- dec << enc[i]^pass[i % p_len]
- }
- say dec.sum
|