Intro
In most fonts all of the uppercase alphabet characters besides BDO have single lines approximately touching some of the four corners of the character's bounding rectangle: ACEFGHIJKLMNPQRSTUVWXYZ.
For example, the two legs of an A "touch" the bottom left and bottom right corners of the character. Likewise, C touches its top and bottom right corners (it is a bit curved but it's close enough). L only touches it's top left and bottom right corners with single lines. The bottom left corner of L is a vertex, not the end of a single line.
Here is a table of what characters touch which corners according to the Stack Exchange font I (and hopefully you) see. 1 is for upper left, 2 is for upper right, 3 lower left, 4 lower right.
A: 3 4
C: 2 4
E: 2 4
F: 2 3
G: 2
H: 1 2 3 4
I: 1 2 3 4
J: 1 3
K: 1 2 3 4
L: 1 4
M: 3 4
N: 2 3
P: 3
Q: 4
R: 3 4
S: 2 3
T: 1 2
U: 1 2
V: 1 2
W: 1 2
X: 1 2 3 4
Y: 1 2
Z: 1 4
Setup
Pretend like these corner touching lines extend in the direction of the corner that they touch so that arrangements of these characters on a grid can be "connected".
For example, all the characters in
A
C X
are connected because the bottom left of A and top right of C connect, and the bottom right of A and top left of X connect.
However,
CAX
has no connections because connections only occur diagonally from one character to the next.
Challenge
Write the shortest program possible (in bytes) that outputs all of the characters in ACEFGHIJKLMNPQRSTUVWXYZ in one big fully connected tree, according to the rules above. Each character must appear exactly once. Use spaces for empty space.
Example
Everything in this 23-letter tree can be reached from anything else via the diagonal connections defined above:
Q
A J R
C U S Y
I M N
E H X
F L T
G Z K P
V W
Notes
- You may hardcode your solution.
- Your output should only contain
ACEFGHIJKLMNPQRSTUVWXYZ, spaces, and newlines.BDOwill not be used. - Leading/trailing spaces are fine as long as all the connections are properly positioned.
- The output grid should not be larger than 30 by 30 characters (including newlines and spaces).
- Only corner connections are considered. The bottom of
Ydoes not connect to anything. You must use the corner connections from the table above. - Not all connectable corners need to connect to something. Connectable and non-connectable corners may border each other.
- Output to stdout. There is no input.
- Including a connectivity graph made with slashes as Peter Taylor has done is a helpful touch but not required.
Update:
githubhagocyte has made an alphabet tree validity checker over on Github.
-
\$\begingroup\$ You may hardcode your solution? \$\endgroup\$edc65– edc652014年08月07日 10:48:16 +00:00Commented Aug 7, 2014 at 10:48
-
2\$\begingroup\$ @edc65 You may hardcode your solution. \$\endgroup\$Calvin's Hobbies– Calvin's Hobbies2014年08月07日 10:53:56 +00:00Commented Aug 7, 2014 at 10:53
-
7\$\begingroup\$ Funny how those 2 phrases only have 1 different character :P \$\endgroup\$Teun Pronk– Teun Pronk2014年08月07日 10:57:51 +00:00Commented Aug 7, 2014 at 10:57
-
1\$\begingroup\$ @githubphagocyte, that would be better as an addendum to the question than as an answer, given that it's not an answer. \$\endgroup\$Peter Taylor– Peter Taylor2014年08月07日 13:59:05 +00:00Commented Aug 7, 2014 at 13:59
-
1\$\begingroup\$ Nice golf, @Calvin'sHobbies. This one's a lot of fun to see the answers for. \$\endgroup\$Jordan– Jordan2014年08月07日 17:44:37 +00:00Commented Aug 7, 2014 at 17:44
12 Answers 12
Python, 49
print' '.join(' MQRCNAF\n XZHLKSIP\n0円GJWVUYET')
Example:
>>> print' '.join(' MQRCNAF\n XZHLKSIP\n0円GJWVUYET')
M Q R C N A F
X Z H L K S I P
G J W V U Y E T
I think it connects up properly now, but I may have missed something again.
-
\$\begingroup\$
F4looks connected toG1, which is invalid \$\endgroup\$Tymric– Tymric2014年08月07日 11:28:49 +00:00Commented Aug 7, 2014 at 11:28 -
\$\begingroup\$ @Timmy But F2 is connected to A3. It all looks connected to me. \$\endgroup\$Calvin's Hobbies– Calvin's Hobbies2014年08月07日 11:33:53 +00:00Commented Aug 7, 2014 at 11:33
-
\$\begingroup\$ @Calvin'sHobbies I think I misunderstood the requirements. Is it allowed to have adjacent corners that aren't connected? \$\endgroup\$Tymric– Tymric2014年08月07日 11:55:53 +00:00Commented Aug 7, 2014 at 11:55
-
2\$\begingroup\$ @Timmy Sure. Those pairs of corners just don't count as connected, but the two letters may be connected in another way. \$\endgroup\$Calvin's Hobbies– Calvin's Hobbies2014年08月07日 11:57:36 +00:00Commented Aug 7, 2014 at 11:57
GolfScript (41 chars)
'QZENRPMALHIFKSXJTUVWYGC'8/{' '*n' '+:n}/
Connectivity graph:
Q Z E N R P M A
\ \ \ / / \ / / \ / \
L H I F K S X J
\ / \ / \ / / \ / / /
T U V W Y G C
Marbelous (削除) 164 (削除ここまで) (削除) 158 (削除ここまで) 143
used bmarks' tree since it almost perfectly optimized for Marbelous. Code in this case is just the ascci codes for all characters (including spaces and newlines) from left to right, delimited by spaces.
43 20 46 20 50 20 4D 20 51 20 52 20 45 20 41 14 20 58 20 48 20 4e 20 4C 20 4B 20 5A 20 49 20 53 14 47 20 59 20 56 20 20 20 55 20 4A 20 54 20 57
Output:
C F P M Q R E A
X H N L K Z I S
G Y V U J T W
A better Marbelous approach (削除) 135 (削除ここまで) 129
This one outputs the same tree with one extra space before and after each line, it works by feeding the literals into a subroutine that prints a space before printing the literal. And just printing a space if the literal is a space (20 HEX)
57
54
4A
55
20
56
59
47
14
53
49
5A
4B
4C
4E
48
58
20
14
41
45
52
51
4D
50
46
43
Sp
Sp:
20 I0
.. =V
-
\$\begingroup\$ This answer will get down to 96 when spaces between cells are no longer required for Marbelous source code, which will be the case later today. \$\endgroup\$overactor– overactor2014年08月07日 15:16:30 +00:00Commented Aug 7, 2014 at 15:16
-
1\$\begingroup\$ Using languages or language features which postdate the question is considered cheating. \$\endgroup\$Peter Taylor– Peter Taylor2014年08月07日 16:41:02 +00:00Commented Aug 7, 2014 at 16:41
-
4\$\begingroup\$ @PeterTaylor For now this answer only uses features that were implemented when the question was asked. \$\endgroup\$overactor– overactor2014年08月07日 17:31:48 +00:00Commented Aug 7, 2014 at 17:31
BrainF*ck 669
Made this one for giggles. Outputs the exact same as the example.
(削除) Will provide another solution later. (削除ここまで)
Can't think of a clever way to do this in Lua so I'll just stick to this one :)
++++[->++++++++<]>..[-->+++++<]>+.>++++++++++.[->+++<]>++.[->++<]>+.-[-->+<]>.+++++[->++<]>.--[->++++<]>...>-[--->+<]>---.>++++++++++.+[->++++++<]>+.-[-->+<]>-.>-[--->+<]>.[----->++<]>--...>-[--->+<]>--.+[--->+<]>++++.--[->+++<]>-.[->+++<]>-.[->+++<]>++.++++[->++<]>+.-[->++++<]>.++++++[->++<]>+.-----[->++++<]>.[-->+++++<]>--.>++++++++++.[->+++++++<]>-.-[-->+<]>--.++++[->++<]>.[->++++<]>.[++++>---<]>.>++++++++++.[->+++<]>++.+++[->++<]>.[-->+<]>---.++++++[->++<]>.[-->+<]>------.>-[--->+<]>-.>++++++++++.[->+++++++<]>+.+[->++++<]>...--[->+++<]>.[--->+<]>++.--[-->+++++<]>.---[->++++<]>.[-->+++++<]>.>++++++++++.[->+++<]>++.....>+[--->++<]>.+[--->+<]>+++.---[->+++<]>.
Output
Q
A J R
C U S Y
I M N
E H X
F L T
G Z K P
V W
-
2\$\begingroup\$ No matter how crazy the codegolf challenge, there's almost always a brainfuck answer. Gotta love it +1 \$\endgroup\$Pharap– Pharap2014年08月07日 12:34:47 +00:00Commented Aug 7, 2014 at 12:34
-
\$\begingroup\$ @AndoDaan Nope it wouldn't. I didn't use that though. I made my own brainf*ck like I always do. I did use that a lot and learned most of the tricks I always use when writing BF so I wouldn't be surprised if it looks simular. \$\endgroup\$Teun Pronk– Teun Pronk2014年08月07日 14:38:42 +00:00Commented Aug 7, 2014 at 14:38
-
\$\begingroup\$ Not simular, identical. Both 669 bytes in length. Both using the same character by character method(no optimization at all), and also both identical with where all the plussy, miney and other thingy symbols go. \$\endgroup\$AndoDaan– AndoDaan2014年08月07日 14:51:13 +00:00Commented Aug 7, 2014 at 14:51
-
2\$\begingroup\$ Must be a coincidence then. I participate for the fun of the challenge and I don't see the fun of generating a BF. The only way that even could be fun a little bit is when you wrote the generator yourself. \$\endgroup\$Teun Pronk– Teun Pronk2014年08月07日 14:54:34 +00:00Commented Aug 7, 2014 at 14:54
PHP 46
This was more like puzzle solving rather than programming, so my answer is more like puzzle solution rather than code. However it's a valid PHP program, so I'm submitting it.
C A Q S R P M J
X Z I F K N H
G T U V W Y E L
Update the same in Perl. Length still remains 46:
print"CAQSRPMJ
XZIFKNH
GTUVWYEL"=~s/\S/$& /rg
HTML, 55
code
<pre>C F P M Q R E A
X H N L K Z I S
G Y V U J T W
output:
C F P M Q R E A
X H N L K Z I S
G Y V U J T W
Bash+coreutils, 46
sed 's/\S/ &/g'<<<"CAQSRPMJ
XZIFKNH
GTUVWYEL"
Now shamelessly borrowing @core1024 optimal tree:
Output:
$ ./alphatree.sh
C A Q S R P M J
X Z I F K N H
G T U V W Y E L
$
STATA 63
Edit: now my own solution. Should be all the letters.
di 'C F P M Q R E A' di ' X H N L K Z I S' di 'G Y V U J T W'
C F P M Q R E A
X H N L K Z I S
G Y V U J T W
Pyth, 32
jd"QAPMFRLZ\nUINKSHXJ\n0円GTCVEWY
Output:
Q A P M F R L Z
U I N K S H X J
G T C V E W Y
Connections, thanks to @githubphagocyte's checker:
Q A P M F R L Z
\ / \ / / \ / / \ \ \
U I N K S H X J
/ \ / / \ / / \ / \ /
G T C V E W Y
Combines @grc's null byte trick and Pyth's extremely short syntax. Made my own grid for the hell of it.
Explanation:
j is python's string join. d is space. 0円 is the escape sequence for the null byte. It is a NOP when printed, so the third line has exactly two spaces in front. Also, note that strings can be EOL-terminated in Pyth, as well as quote-terminated.
Javascript 83
I'll begin with hardcoding YOUR solution
console.log(' Q\n A J R\nC U S Y\n I M N\nE H X\n F L T\nG Z K P\n V W')
-
\$\begingroup\$ make it shorter with
alertinstead ofconsole.log\$\endgroup\$Zaenille– Zaenille2014年08月07日 15:07:18 +00:00Commented Aug 7, 2014 at 15:07 -
2\$\begingroup\$ @MarkGabriel I'd like to make it shorter in some smarter way. Alert has not the right font to display ascii art. \$\endgroup\$edc65– edc652014年08月07日 15:20:14 +00:00Commented Aug 7, 2014 at 15:20
-
1\$\begingroup\$ I see. Forgot about alert's formatting being different. :) \$\endgroup\$Zaenille– Zaenille2014年08月08日 00:28:05 +00:00Commented Aug 8, 2014 at 0:28
PHP, 69
<?php echo preg_replace("/(\w)/",'1円 ',"CAQSRPMJ
XZIFKNH
GTUVWYEL");
gives
C A Q S R P M J
X Z I F K N H
G T U V W Y E L
-
\$\begingroup\$ You have no diagonal connections at all in your output \$\endgroup\$edc65– edc652014年08月11日 07:11:56 +00:00Commented Aug 11, 2014 at 7:11
-
\$\begingroup\$ @MartinBüttner - thank you, it cost me a byte to fix. \$\endgroup\$Victory– Victory2014年08月11日 22:33:28 +00:00Commented Aug 11, 2014 at 22:33
-
\$\begingroup\$ The problem with this (and many other) response is that the output is shorter than the code (see core1024 php) \$\endgroup\$edc65– edc652014年08月11日 22:43:01 +00:00Commented Aug 11, 2014 at 22:43
-
\$\begingroup\$ You can shave two bytes by replacing your regex with
"/\w/",'0円 '\$\endgroup\$ATaco– ATaco2016年11月07日 02:30:14 +00:00Commented Nov 7, 2016 at 2:30
Vyxal, 28 bytes
«⟑Eƈ↓Z↔+τ3⁄4ꜝdB_ḣC«3/ƛṄ\$꘍&›⇧,
Yes, I stole @isaacg's grid.
«...« # Compressed string 'qapmfrlzuinkshxjgtcvewy ' (Note trailing space)
3/ # Into 3 pieces
ƛ # Map...
Ṅ # Join by spaces
\$꘍ # Prepend (register) spaces
&› # Increment register
⇧, # Output as uppercase.
Explore related questions
See similar questions with these tags.