Happy Christmas Eve! πŸŽ„

Merry Christmas Eve, all!

Run the code below to produce some Christmas trees:

do i=3,9
do k=1,i
print*,repeat(' ',i-k)//repeat('*',2*k-1)
enddo
print*,repeat(' ',i-1)//'*'
if (i<9) write(*,*)
enddo
end

(This is my code golf solution with 123 characters. Apparently, it is possible to do it with less than 70 characters!)

8 Likes

Reminds me of the old obfustication challenges:

write(*,'(*(a/),a/)')([(repeat(' ',i-k)//repeat('*',2*k-1),k=1,i),repeat(' ',i-1)//'*'],i=3,9)
end
2 Likes

Your code works with ifort, but with gfortran I get:

Fortran runtime error: Argument NCOPIES of REPEAT intrinsic is negative (its value is -1)
write(*,'(*(a/),a/)')([(repeat(' ',i-k)//repeat('*',2*k-1),k=1,i),repeat(' ',i-1)//'*'],i=3,9)

Just having fun, but it shows how hard you can make something be to debug, doesn’t it? But …

do i=3,9
do k=1,i
write(*,*)i-k,2*k-1
enddo
write(*,*)i-1
enddo
end

should see what it does to nvfortran! The description I have on hand doesn’t say zero is disallowed, do not see a negative. Now I actually have to think about it:>

use iso_fortran_env
open(output_unit,encoding="UTF-8")
write(*,*)char(127876,selected_char_kind("ISO_10646"))
end

114 characters :grin:

3 Likes

Starting from your proposition, in 93 characters:

print'(*(a/))',((repeat(' ',i-k+1)//repeat('*',2*k-1),k=1,i),repeat(' ',i)//'*','',i=3,9)
end
1 Like

All nice!

No one run the obfuscation version with gfortran unless you are curious whether you will get a bus error or fill /var/crash first. Does not seem right to give or receive a bug report this time of year, so some other time – but a real doozie.

Assuming your terminal emulator handles ANSI in-line control sequences, this one should be safe to run with gfortran

!write(*,'(*(a,/),a,/)')([(repeat(' ',i-k)//repeat('*',2*k-1),k=1,i),repeat(' ',i-1)//'*'],i=3,9)
!write(*,'(*(i0,/),i0/)')([(i-k,2*k-1,k=1,i),i-1],i=3,9)
implicit none
integer, allocatable :: seed(:)
integer,parameter    :: first=1,last=8
integer              :: i,k,n,r
real                 :: rand_val
   call random_seed(size = n)
   allocate(seed(n))
   call random_seed(get=seed)
   write(*,'(a,a,*(a,/),a,/)')char(27)//'[H'//char(27)//'[J'
   do
      call random_number(rand_val)
      r=first+floor((last+1-first)*rand_val)
      write(*,'(a,*(a,/),a,/)')char(27)//'[H',([&
      &( repeat(' ',min(8,max(0,i-k)))//repeat(char(33+r+k),min(17,max(0,2*k-1))) ,k=1,i),&
      &repeat(' ',min(max(0,i-1),9) )//'*'],i=8,9)
   enddo
end

Merry Christmas!

1 Like

An β€œimproved” version (53 characters):

open(unit=6,encoding="UTF-8")
print*,char(127876,4)
end
$ gfortran -std=f2018 -Wall -Wextra -pedantic essai2.f90 && ./a.out
 πŸŽ„