Monday, January 07, 2019

Fun with Swift at Christmas

You may not have heard of the IOCCC.




Years ago I discovered them through my old friend Jim Hague.   Jim won the T-shirt in the 1986 International Obfuscated C Code Contest with a fiendish entry; a C program written entirely in morse code that, when you run it, prints out its own program listing.  In morse code!

You have to pretty smart to pull off anything like that.  But of course Jim is.

Another entry, from 1998, was a C program by Ian Phillipps that looks like a mass of gibberish.  But when you run it, it prints out the Christmas Carol 'The Twelve Days of Christmas'.  Every verse, immaculately punctuated, with each partridge, calling bird and gold ring in its proper place.

A few years ago I translated this into Delphi just for fun.  And now, here's a Swift version.  I'm not sure how Ian wrote the original C version.  All I know is he's much brighter than me!

So go ahead and paste this into a Swift playground and be amazed!

import Foundation

@discardableResult func t_ (_ t: Int, _ x: Int, _ a: UnsafePointer<Int8>, _ s: inout String) -> Int {
    if 1<t { if t<3 { t_(-79,-13,a+t_(-87,1-x,t_(-86,0,a+1, &s)+a, &s), &s
    ); }; if t<x {t_(t+1,x,a, &s)}; return t_(-94,-27+t,a, &s) != 0
    && t==2 ? (x<13 ? t_(2,x+1, "%s %d %d\r", &s) : 9) : 16 }
    else if t<0 { if t < -72 {return t_(x,t,"@n'+,#'/*{}w+/w#cdnr/+," +
     "{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l+,/n{n+,/+#n+,/#;#q#n+,/+k#;*+,/'r :'" +
     "d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'" +
     "/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){nl]!/n{n#'; r{#w'r nc{nl]" +
     "'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; " +
     ":{nl]'/*{q#'ld;r'}{nlwb!/*de}'c ;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'" +
     "/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/", &s)} else
     if t < -50 {if x == a [0] {s += String (Character (UnicodeScalar (Int (
     a[31]))!)); return Int (a[31])} else {return t_(-65,x,a+1,&s)}} else {
     return t_((a [0] == 47 ? 1 : 0)+t,x,a+1, &s)}} else if 0<t {return t_(2,2,"%s", 
     &s)} else { return a [0] == 47 ? 1 :  t_(0,t_(-61,Int (a[0]),
     "!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\ruwloca-O;m " +
     ".vpbks,fxntdCeghiry", &s),a+1, &s) != 0 ? 1 : 0}
}

var str = ""
t_ (1, 10000, "", &str)
print (str)




Labels: , ,