Skip to content

Instantly share code, notes, and snippets.

@PaeP3nguin
Created February 14, 2018 08:14
Show Gist options
  • Save PaeP3nguin/af9426c6491ed430dbe0a5fdb19c7e95 to your computer and use it in GitHub Desktop.
Save PaeP3nguin/af9426c6491ed430dbe0a5fdb19c7e95 to your computer and use it in GitHub Desktop.
Valentine's Day Code Golf
# ██╗ ██╗ █████╗ ██╗ ███████╗███╗ ██╗████████╗██╗███╗ ██╗███████╗ ██╗ ███████╗
# ██║ ██║██╔══██╗██║ ██╔════╝████╗ ██║╚══██╔══╝██║████╗ ██║██╔════╝ ██║ ██╔════╝
# ██║ ██║███████║██║ █████╗ ██╔██╗ ██║ ██║ ██║██╔██╗ ██║█████╗ ╚═╝ ███████╗
# ╚██╗ ██╔╝██╔══██║██║ ██╔══╝ ██║╚██╗██║ ██║ ██║██║╚██╗██║██╔══╝ ╚════██║
# ╚████╔╝ ██║ ██║███████╗███████╗██║ ╚████║ ██║ ██║██║ ╚████║███████╗ ███████║
# ╚═══╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝ ╚══════╝
# ██████╗ ██████╗ ██████╗ ███████╗ ██████╗ ██████╗ ██╗ ███████╗
# ██╔════╝██╔═══██╗██╔══██╗██╔════╝ ██╔════╝ ██╔═══██╗██║ ██╔════╝
# ██║ ██║ ██║██║ ██║█████╗ ██║ ███╗██║ ██║██║ █████╗
# ██║ ██║ ██║██║ ██║██╔══╝ ██║ ██║██║ ██║██║ ██╔══╝
# ╚██████╗╚██████╔╝██████╔╝███████╗ ╚██████╔╝╚██████╔╝███████╗██║
# ╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝ ╚═════╝ ╚═════╝ ╚══════╝╚═╝
# TL;DR:
#
# The ASCII art we're trying to print:
# ♥♥♥♥ ♥♥♥♥
# ♥♥♥♥♥♥♥♥♥ ♥♥♥♥♥♥♥♥♥
# ♥♥♥♥♥♥♥♥♥♥♥♥♥ ♥♥♥♥♥♥♥♥♥♥♥♥♥
# ♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
# ♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
# ♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
# ♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
# ♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
# ♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
# ♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
# ♥♥♥♥♥♥♥♥♥♥♥♥
# ♥♥♥♥♥♥♥♥
# ♥♥♥♥
# ♥♥
#
# The shortest code (that I found) to do it:
# Length: 88 = 3 + 1 newline + 84
# i=0
# for c in 'cimooonljhfdba':a=f'{"♥"*(ord(c)-96):{"^>"[i>2]}15}';print(a+a[::-1]);i+=1
print('Drawing the heart')
print('{:^15}'.format('♥'*3) + '{:^15}'.format('♥'*3)[::-1])
print('{:^15}'.format('♥'*9) + '{:^15}'.format('♥'*9)[::-1])
print('{:^15}'.format('♥'*13) + '{:^15}'.format('♥'*13)[::-1])
print('{:^15}'.format('♥'*15) + '{:^15}'.format('♥'*15)[::-1])
print('{:^15}'.format('♥'*15) + '{:^15}'.format('♥'*15)[::-1])
print('{:^15}'.format('♥'*15) + '{:^15}'.format('♥'*15)[::-1])
print('{:>15}'.format('♥'*14) + '{:>15}'.format('♥'*14)[::-1])
print('{:>15}'.format('♥'*12) + '{:>15}'.format('♥'*12)[::-1])
print('{:>15}'.format('♥'*10) + '{:>15}'.format('♥'*10)[::-1])
print('{:>15}'.format('♥'*8) + '{:>15}'.format('♥'*8)[::-1])
print('{:>15}'.format('♥'*6) + '{:>15}'.format('♥'*6)[::-1])
print('{:>15}'.format('♥'*4) + '{:>15}'.format('♥'*4)[::-1])
print('{:>15}'.format('♥'*2) + '{:>15}'.format('♥'*2)[::-1])
print('{:>15}'.format('♥') + '{:>15}'.format('♥')[::-1])
print('Fitted function (found using Wolfram Alpha)')
f = lambda x: round(-0.00157257*x**4 + 0.084216*x**3 - 1.55846*x**2 + 10.086*x - 5.56294)
for x in range(1, 15):
v = f(x)
if x > 3:
s = '{:>15}'.format('♥' * v)
else:
s = '{:^15}'.format('♥' * v)
print(s + s[::-1])
print('Plain old for loop')
xoxo = [3, 9, 13, 15, 15, 15, 14, 12, 10, 8, 6, 4, 2, 1]
for i, n in enumerate(xoxo):
if i > 2:
a = '{:>15}'.format('♥' * n)
else:
a = '{:^15}'.format('♥' * n)
print(a + a[::-1])
print('Refined golfings')
# These seem short, but they don't forget to include the length of x
x=[3,9,13,15,15,15,14,12,10,8,6,4,2,1]
# Length: 109 (with x inlined)
for i in range(14):z='{:{}15}'.format('♥'*x[i],'^>'[i>2]);print(z+z[::-1])
# Failed experiments with list comprehensions/map
# Length: 118 (with x inlined)
for z in ['{:{}15}'.format('♥'*x[i],'^>'[i>2]) for i in range(14)]:print(z+z[::-1])
# Length: 120 (with x inlined)
[print(z+z[::-1]) for z in ['{:{}15}'.format('♥'*x[i],'^>'[i>2]) for i in range(14)]]
# Length: 126 (with x inlined)
print(*[z+z[::-1]+'\n' for z in ['{:{}15}'.format('♥'*x[i],'^>'[i>2]) for i in range(14)]])
# Length: 127 (with x inlined)
[*map(print,[z+z[::-1] for z in ['{:{}15}'.format('♥'*x[i],'^>'[i>2]) for i in range(14)]])]
print('Final passes')
# Length: 109 = 29 + 1 newline + 79
*x,=map(ord,'cimooonljhfdba')
for i in range(14):a='{:{}15}'.format('♥'*(x[i]-96),'^>'[i>2]);print(a+a[::-1])
# Length: 99 (no f-strings)
for i in range(14):a='{:{}15}'.format('♥'*(ord('cimooonljhfdba'[i])-96),'^>'[i>2]);print(a+a[::-1])
# Length: 90
for i in range(14):a=f"{'♥'*(ord('cimooonljhfdba'[i])-96):{'^>'[i>2]}15}";print(a+a[::-1])
# Length: 97 = 3 + 1 newline + 93 (no f-strings)
i=0
for c in 'cimooonljhfdba':a='{:{}15}'.format('♥'*(ord(c)-96),'^>'[i>2]);print(a+a[::-1]);i+=1
print('The winner, at 88 characters long!')
# Length: 88 = 3 + 1 newline + 84
i=0
for c in 'cimooonljhfdba':a=f'{"♥"*(ord(c)-96):{"^>"[i>2]}15}';print(a+a[::-1]);i+=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment