Scala 494 ที่ไม่มีบรรทัดใหม่ 520 พร้อมบรรทัดใหม่:
def k(i:Int,d:Int=0):(Int,Int)=if(i<(7-d))(d,i+1)else k(i-(7-d),d+1)
def t(i:Char)=(if(i=='y')i-4 else
if(i=='z')i+2 else
if(i=='j')i+14 else
if(i>='v')i+3 else
if(i>'i')i-1 else i)-'a'
def q(p:(Int,Int),i:Int,c:Char)=if(p._1==i||p._1+p._2==i)""+c else" "
def g(r:Int,c:Char)={val p=k(t(c.toLower))
print((r match{case 1=>q(p,3,'\\')+q(p,4,'|')+q(p,5,'/')
case 2=>q(p,2,'-')+"o"+q(p,6,'-')
case 3=>q(p,1,'/')+q(p,0,'|')+q(p,7,'\\')})+" ")}
for(w<-readLine.split(" ")){println;for(r<-(1 to 3)){w.map(c=>g(r,c));println}}
ungolfed:
def toClock (i: Int, depth: Int=0) : (Int, Int) = {
if (i < (7 - depth)) (depth, i+1) else toClock (i - (7-depth), depth + 1)}
def toIdx (i: Char) = {
(if (i == 'y') i - 4 else
if (i == 'z') i + 2 else
if (i == 'j') i + 14 else
if (i >= 'v') i + 3 else
if (i > 'i') i - 1 else i ) - 'a'}
def p2c (pair: (Int, Int), i: Int, c: Char) = {
if (pair._1 == i || pair._1 + pair._2 == i) ""+c else " "
}
def printGrid (row: Int, c: Char) = {
val idx = toIdx (c.toLower)
val pair = toClock (idx)
row match {
case 1 => { print(
p2c (pair, 3, '\\') +
p2c (pair, 4, '|') +
p2c (pair, 5, '/') + " ")
}
case 2 => { print(
p2c (pair, 2, '-') + "o" +
p2c (pair, 6, '-') + " ")
}
case 3 => { print(
p2c (pair, 1, '/') +
p2c (pair, 0, '|') +
p2c (pair, 7, '\\') + " ")
}
}
}
val worte = "This is Code Golf"
(1 to 3).map (row => {worte.map (c => printGrid (row, c));println})
คำอธิบาย:
ฉันสังเกตรูปแบบนาฬิกา แต่ไม่ใช่กับ 12 ชั่วโมง แต่ 8 และเวลาเริ่มต้นคือ 0 คือเวลา 6 นาฬิกาและ a, b, c เป็นรหัสแรกโดยมีธงแรก (หนึ่ง) ทางทิศใต้
เนื่องจากการตั้งค่าสถานะ 1 และ 2 แยกไม่ออกเราจึงสามารถเรียงลำดับชุดค่าผสมทั้งหมดด้วยจำนวนที่ต่ำกว่าสำหรับค่าสถานะแรกก่อน แต่น่าเสียดายที่ลำดับที่ดีตั้งแต่ต้นถูกรบกวนเมื่อ j ไม่ได้ติดตามฉัน แต่ k, l, m และหลังจากนั้นมันก็ยุ่งเหยิง
ดังนั้นฉันจึงจัดเรียงแป้นสำหรับการแมปใหม่:
val iis = is.map {i =>
if (i == 'y') i - 4 else
if (i == 'z') i + 2 else
if (i == 'j') i + 14 else
if (i >= 'v') i + 3 else
if (i > 'i') i - 1 else i }.map (_ - 'a')
iis.zipWithIndex .sortBy (_._1) .map (p => (p._1, ('a' + p._2).toChar))
Vector((97,a), (98, b), (99, c), (100,d), (101,e), (102,f), (103,g),
(104,h), (105,i), (106,k), (107,l), (108,m), (109,n),
(110,o), (111,p), (112,q), (113,r), (114,s),
(115,t), (116,u), (117,y), -------
------- (120,j), (121,v),
(122,w), (123,x),
(124,z))
ถ้าเราย่อตัวอักษร 'a' จากตัวละครทุกตัวเราจะได้ตัวเลขจาก (0 ถึง 7 + 6 + 5 + ... + 1) เราสามารถแมปตัวเลขของตารางตัวละคร
3 4 5 \ | / |
2 6 - o - - o - o
1 0 7 / | \ (2, ) (2,2)
คู่ของตัวเลขสองตัวสามารถจับคู่สองธงโดยที่หมายเลขแรกคือดัชนี 0 ถึง 6 สำหรับธงแรกและธงที่สองไม่ใช่หมายเลขจาก 1 ถึง 7 สำหรับธงที่สอง แต่สำหรับระยะทางจาก ธงแรกที่สอง (2,2) จะหมายถึงธงแรกคือ WEST และที่สองคือสองขั้นตอนตามเข็มนาฬิกาจากที่นั่นไปที่ NORTH
def toClock (i: Int, depth: Int=0) : (Int, Int) = {
if (i < (7 - depth)) (depth, i+1) else toClock (i - (7-depth), depth + 1)}
Vector( (0,1), (0,2), (0,3), (0,4), (0,5), (0,6), (0,7),
(1,1), (1,2), (1,3), (1,4), (1,5), (1,6),
(2,1), (2,2), (2,3), (2,4), (2,5),
(3,1), (3,2), (3,3),
(4,2), (4,3),
(5,1), (5,2),
(6,1))