Clojure, 417 319 bytes
(defn cannon[n](let[a(map #(char(+ 65 %))(iterate #(if(> % 24)0(inc %))0))m1(reverse(reduce #(concat %(repeat %2(- n %2)))[](range 0(inc n))))p1(map-indexed #(str(apply str(repeat %2 " "))(nth a %))m1)m2(reverse(reduce #(concat %(repeat %2(-(* 2 %2)2)))[](reverse(range 0(inc n)))))p2(reverse(map-indexed #(str(apply str (repeat %2 " "))(nth a(+(count p1)%)))m2))](doseq[x(reverse(map #(str % %2)p1 p2))](println x))))
At some point I got tangled up in reverse
calls and gave up on the idea to make it as short as possible. I just wanted to have a working solution. Here you go...
Sort of ungolfed
(defn cannon [n]
(let [a (map #(char (+ 65 %)) (iterate #(if (> % 24) 0 (inc %)) 0))
m1 (reverse (reduce #(concat % (repeat %2 (- n %2))) [] (range 0 (inc n))))
p1 (map-indexed #(str (apply str (repeat %2 " ")) (nth a %)) m1)
m2 (reverse (reduce #(concat % (repeat %2 (- (* 2 %2) 2))) [] (reverse (range 0 (inc n)))))
p2 (reverse (map-indexed #(str (apply str (repeat %2 " ")) (nth a (+ (count p1) %))) m2))]
(doseq [x (reverse (map #(str % %2) p1 p2))] (println x))))
Update
Motivated by Olivier's comment, I managed to cut multiple reverse
calls and apply some general golfing tricks to cut characters. Also I created aliases for reverse
, map-indexed
, concat
, repeat
and str
because I used them multiple times each.
(defn c[n](let[a(map #(char(+ 65 %))(iterate #(if(> % 24)0(inc %))0))k #(reduce %[](range 0(inc n)))r #(apply str(repeat % " "))rv reverse m map-indexed c concat t repeat s str p(m #(s(r %2)(nth a %))(rv(k #(c %(t %2(- n %2))))))](rv(map #(s % %2)p(rv(m #(s(r %2)(nth a(+(count p)%)))(k #(c %(t %2(-(* 2 %2)2))))))))))
Ungolfed
(defn c [n]
(let [a (map
#(char (+ 65 %))
(iterate
#(if (> % 24) 0 (inc %))
0))
k #(reduce
%
[]
(range 0 (inc n)))
r #(apply str (repeat % " "))
rv reverse
m map-indexed
c concat
t repeat
s str
p (m
#(s
(r %2)
(nth a %))
(rv (k #(c % (t %2 (- n %2))))))]
(rv
(map
#(s % %2)
p
(rv
(m
#(s
(r %2)
(nth a (+ (count p) %)))
(k #(c % (t %2 (- (* 2 %2) 2))))))))))
Creates the function c
which accepts the value n and returns a list of lines.
7Closely related. – Dom Hastings – 2018-02-28T09:07:34.973
2Why are O and P in the same level in the example? If I read the spec correctly, it seems it should go up one treetop for P and descend by one for Q. – Skyler – 2018-02-28T15:09:06.857
2@Skyler At every tick, the alphabet goes 1 to the right and N vertically. N decreases every tick as well. Between
O
andP
, the tick goes 1 to the right, but 0 up- or down-wards. – Olivier Grégoire – 2018-02-28T15:23:11.503@Skyler that depends on where you imagine the ball - in the centre of each letter, or moving from the bottom-left to the top-right point of each vertical block of letters (and later top-left to bottom-right); in either case, I admit there's a bit of discontinuity at the apex, which I ignored for the sake of keeping it easy to golf – ngn – 2018-02-28T15:48:07.633
4Looks like alphabet cannons are now canon. – Carl Witthoft – 2018-02-28T16:22:58.580
What is the maximum
n
that we need to support? – Dom Hastings – 2018-02-28T20:04:43.5331@DomHastings if it makes any difference for your code: 15 (strictly less than an arbitrary not-too-high power of 2 that I just made up) – ngn – 2018-02-28T20:19:26.297
2
@ngn Hah, I was tinkering with @TonHospel's Perl solution and came up with 1 byte less, but it only supports up to 14!
– Dom Hastings – 2018-02-28T20:30:14.913@DomHastings what!? ...diffing and trying to figure out how 14 is special... So, is
(A..Z)x9
supposed to return nine copies of the alphabet? I'm not very familiar with Perl. When I tried it, it returned one copy. – ngn – 2018-02-28T20:41:58.110Yeah, if you do
print((A..Z)x9)
you'll get 9 copies. Otherwise it just replicates the result of callingprint
9 times. I accept 15 as the maximum though. I'll try and think about other ways to reduce byte count instead! Thanks! – Dom Hastings – 2018-02-28T20:50:13.3271@DomHastings silly me... parentheses, of couse. I don't mind lowering the limit, but I think your improvement for n<=14 is still interesting out-of-the-box thinking and would be appreciated as-is :) – ngn – 2018-02-28T20:57:34.970