Draw the Moon Phase
JavaScript (ES6), 121 ... 103 92 bytes
f=(n,i=128)=>i--?f(n,i)+(i%16?'':`
`)+'.#.'[i%16+4*n-~-'31000013'[n&1?i>>4:1]*~-(n&2)>>4]:''
Demo
f=(n,i=128)=>i--?f(n,i)+(i%16?'':`
`)+'.#.'[i%16+4*n-~-'31000013'[n&1?i>>4:1]*~-(n&2)>>4]:''
o.innerHTML=f(0)
<input type="range" oninput="o.innerHTML=f(this.value)" value=0 min=0 max=7><pre id=o>
Jelly, 43 32 bytes
-7 bytes moving from bit-mask to comparison-mask
-2 bytes with some tacit programming improvements
-1 byte moving to the southern hemisphere
-1 byte - use the all-new ɓ
chain separator ...its first ever use!
“>,##‘m0Dm€0ż@€4Ḷ¤Œṙɓ%4>C¹⁸:4¤?Y
Uses the characters 0
for .
and 1
for #
.
All cases in a test suite at Try it online!
How?
Builds a mask capturing the first four phases, and rather than reversing the lines complements the resulting values of the modulo-4 phase result when the phase div-4 is non-zero.
I originally built a bit mask, but the mask values were 0
,8
,12
, and 14
- 0000
, 1000
, 1100
, and 1110
- these have phase
leading ones - so a comparison-mask could be used instead.
“>,##‘m0Dm€0ż@€4Ḷ¤Œṙɓ%4>C¹⁸:4¤?Y - Main link 1: number phase
“>,##‘ - code-page index literal [62,44,35,35]
m0 - reflect -> [62,44,35,35,35,35,44,62]
D - decimalise -> [[6,2],[4,4],[3,5],[3,5],[3,5],[3,5],[4,4],[6,2]]
m€0 - reflect €ach -> [[6,2,2,6],[4,4,4,4],[3,5,5,3],[3,5,5,3],[3,5,5,3],[3,5,5,3],[4,4,4,4],[6,2,2,6]]
¤ - nilad and link(s) as a nilad:
4 - literal 4
Ḷ - lowered range -> [0,1,2,3]
ż@€ - zip (reverse @rguments) for €ach -> [[[0,6],[1,2],[2,2],[3,6]],[[0,4],[1,4],[2,4],[3,4]],[[0,3],[1,5],[2,5],[3,3]],[[0,3],[1,5],[2,5],[3,3]],[[0,3],[1,5],[2,5],[3,3]],[[0,3],[1,5],[2,5],[3,3]],[[0,4],[1,4],[2,4],[3,4]],[[0,6],[1,2],[2,2],[3,6]]]
Œṙ - run-length decode -> [[0,0,0,0,0,0,1,1,2,2,3,3,3,3,3,3],[0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3],[0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3],[0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3],[0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3],[0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3],[0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3],[0,0,0,0,0,0,1,1,2,2,3,3,3,3,3,3]]
- i.e.: 0000001122333333 -
0000111122223333 - Marking out the separate
0001111122222333 - regions as filled up by
0001111122222333 - the phases of the moon in
0001111122222333 - the southern hemisphere.
0001111122222333 -
0000111122223333 -
0000001122333333 -
ɓ - dyadic chain separation & swap arguments, call that m
%4 - phase mod 4
> - greater than? (vectorises across m) 1 if so 0 if not
? - if:
¤ - nilad followed by link(s) as a nilad:
⁸ - link's left argument, phase
:4 - integer divide by 4
C - then: complement
¹ - else: identity (do nothing)
Y - join with newlines
- implicit print
Haskell, 98 90 bytes
f i=do{a<-[3,1,0,0,0,0,1,3];[".#."!!div(i*4+x+[0,a-1,0,1-a]!!mod i 4)16|x<-[0..15]]++"\n"}
It loops through the rows and columns using the list monad (a do
block for rows and a list comprehension for columns) and determines which character for each cell using a expression of the input (i
), offset value for the row (a
), and column index (x
).
Saved 8 bytes by simplifying the subexpression for the true offset.