Skip to content

Instantly share code, notes, and snippets.

Hello World
var enigma = Enigma(rotor0: swissK[0], rotor1: swissK[1], rotor2: swissK[2], plugboard: plugboard, key: (.A, .A, .A))
let message = "HELLOWORLD"
let message2 = "AAAAAAAAAA"
let ciphered = enigma.cipher(message)
let ciphered2 = enigma.cipher(message2)
var _enigma = Enigma(rotor0: swissK[0], rotor1: swissK[1], rotor2: swissK[2], plugboard: plugboard, key: (.A, .A, .A))
let deciphered = _enigma.cipher(ciphered)
let deciphered2 = _enigma.cipher(ciphered2)
// rotor
let ETW_K: Cipher = { token in
switch token {
case .A: return .Q
case .B: return .W
case .C: return .E
case .D: return .R
case .E: return .T
case .F: return .Z
case .G: return .U
public struct RotorBox {
...
public mutating func cipher(_ target: Token) -> Token {
defer {
// 関数を抜ける際に回転
rotate()
}
public struct RotorBox {
public struct Rotor {
let forward: Cipher
let backward: Cipher
var position: Token
init(_ forward: @escaping Cipher, position: Token) {
self.forward = forward
public enum Token: Character {
case A = "A", B = "B", C = "C", D = "D", E = "E", F = "F"
case G = "G", H = "H", I = "I", J = "J", K = "K", L = "L"
case M = "M", N = "N", O = "O", P = "P", Q = "Q", R = "R"
case S = "S", T = "T", U = "U", V = "V", W = "W", X = "X"
case Y = "Y", Z = "Z"
}
public typealias Cipher = (Token) -> (Token)
public struct Enigma {
private var rotorBox: RotorBox
private let plugboard: Cipher
public typealias Key = (Token, Token, Token)
public init(rotor0: @escaping Cipher, rotor1: @escaping Cipher, rotor2: @escaping Cipher, plugboard: @escaping Cipher, key: Key) {
self.rotorBox = RotorBox(
rotor0: RotorBox.Rotor(rotor0, position: key.0),
rotor1: RotorBox.Rotor(rotor1, position: key.1),
import Foundation
enum Token: Character {
case A = "A"
case B = "B"
case C = "C"
case D = "D"
case E = "E"
case F = "F"
case G = "G"
@KentaKudo
KentaKudo / brain_tester_of_the_day_20170703.py
Last active July 3, 2017 13:58
Take the digits 1,2,3 up to 9 in numerical order and put either a plus sign or a minus sign or neither between the digits to make a sum that adds up to 100. For example, one way of achieving this is: 1 + 2 + 34 - 5 + 67 - 8 + 9 = 100, which uses six plusses and minuses. What is the fewest number of plusses and minuses you need to do this?
# pow(3, 8) = 6561
options = ['', '+', '-']
for i in options:
for j in options:
for k in options:
for l in options:
for m in options:
for n in options:
for o in options:
for p in options: