Jump to content

Recommended Posts

Posted (edited)

Hmm... yeah let's see how this goes.

Might be a flop.

 

If you don't know what a codegolf is, it's essentially a challenge to make a program that does something in the least amount of code (usually counted in, like, bytes or characters). You can use any programming language, and basically.. yeah. Uhh.. I'd say that the poster of the challenge can add additional restrictions or change the rules a bit (such as least amount of lines instead of characters/bytes, or not allowed to use loops or something).

Also... there are languages specially made for codegolfing. I'd say don't use those, unless the challenge poster specifies otherwise (basically, use stuff like Python, JS, whatever). Feel free to ask any questions or clarifications. If you think a rule or something should be changed, let me know (with a valid reason).

Sorry if this is a bad explanation..

 

Anyway, I guess either after a time has passed either since the challenge was posed or since the last answer, idk. Or community/challenge-poster says so, idk? Sorry.

 

This might work better as a blog.. and I might do that. I'm just worried about polluting the blog space, unless I do, like, a weekly or few times a week challenge or something and users can pose challenges and stuff. This way, someone can come back a month later with a solution that beats the current one.

 

Anyway, here's the first challenge:

Allowed languages: any non-codegolf-y ones (ask if needed).
Type: fewest characters.

--Challenge--
Output scrolling "marquee-style" scrolling text
(preferably in the terminal/console, but feel free to use graphics).
The text should be for a user-inputted string, and the "box" should also be of a user-inputed width
Your choice on the units. Could be number of characters, pixels, or something else.
Just be sure to specify in code or in your post so it's easier to test
(though in-code would "waste" characters lol).

Idealy, it should be readable (as in, you can make out the output. The code can be a mess lol.)
Not sure if this will be a requirement though...

(See GIF for an idea of what I mean of scrolling)

By user input, I mean reading from a function like prompt() [JS], input() [Python], or std::getline() [C++]


--Restrictions--
- No outside libraries aside from any required or standard ones that are essentially always used.
UPDATE: actually, you can use libraries as long as it doesn't.. go against the "spirit of the rules."

- No using existing functions that already make the task trivial
  (for instance, if there is a function somewhere that already does this or makes it
  SIGNIFICANTLY easier, please do not use it. You may ask me for clarification or if
  you can use something or not.)

 

Feel free to ask for clarification, and also feel free to reuse this template for your codegolf challenge!

 

Uhh yeah good luck and I'll see how this goes...

 

edit: oops, forgot the gif example lol!

Spoiler

SwiftUI marquee sample - Stack Overflow

 

Edited by Usseewa
Posted (edited)

JS:

Spoiler

function setup() {
  createCanvas(400, 400);
}
let message = prompt("please enter text");
let x = 100;
function draw() {
  if (x===400) {
    x=0
  }
  else{
    x ++ 
  background(220);
  text(message, x, 100, 50, 500);
  }
}

15 lines

@Usseewa, what do we think?

Edited by Adonalsium Will Return
Posted (edited)

Hmm.. So in the future note that this challenge is counting the characters, not the number of lines.

Also, this is p5.js, right? I'm actually not very familiar with it, so idk.

But I ran it through the online editor and it doesn't actually meet the requirements, but it's a start. You actually need to take user input (you can use the prompt() function, with JS) for the text to be displayed, as well as the width. You will need to make it look more like the GIF I attached in the first post (scrolling text, that wraps around and is possibly displayed multiple times as needed to fill the width).

Good start though! I always find it helps to start out with something like that and then work off of it.

 

EDIT: i see the GIF is actually different from what I meant.. idk it's hard to explain but i can share my code if needed

 

@Adonalsium Will Return (Next time maybe make a new post as it's a bit easier idk)

That's better. Getting there. Now you need adjustable width and.. multiple instances of the text (tho it may be hard).

 

For instance:

| Hello |
| ello  |
| llo H |
| lo He |
| o Hel |
|  Hell |
| Hello |
| ello  |
...


| Hello  He |
| ello  Hel |
| llo  Hell |
| lo  Hello |
| o  Hello  |
|   Hello   |
| Hello  H  |
...

| He |
| el |
| ll |
| lo |
| o  |
|  H |
...

| H |
| e |
| l |
| l |
| o |
|   |
| H |
...

(Note that the spacing between instances is entirely optional but is there to improve readability.)

(Treat those examples as if they were an animation or something)

 

Nice job so far tho

Edited by Usseewa
Posted
Just now, Fizz9 said:

I WILL DO THIS GIVE ME A LITTLE WHILE

take your time don't worry foobarfizzbuzzbarbazquxwhatever...

HAHAHAHAHHAHAHAHAH

we got all the damn time in the world....mdnndnndndndndkddkdkekkeekaajjajwjw

it's not even first solver, cuz if someone posts a solution before you, then later you post a shorter solution, the someone ELSE posts a shorter solution... etc. etc.

 

JUST REMEMBER IT'S BASED ON CHARACTERS, NOT NUMBER OF LINESS NANWNWJWJNSNZNSNSNS

Posted
Just now, Adonalsium Will Return said:

@Usseewa, Adjustable width?

(quoting + pinging is actually a waste of time but i don't mind)

(it only sends a notif for the quote)

 

asjustable width meaning see how the examples i gave are of different widths? different number of characters that can be contained within it? So prompt the user for the width and use that.

For you, it might be useful to use width as a pixel value rather than number of characters (since whateeverrbrnrbrbeb)

 

 

EDITTTTTTT:

also btw the width stays the same after the user only enters it once at the start yk? not changing width while you run program

but each time you run the user enters a width and they can enter a different or whatever cram

Posted

Alright I was going to use p5js too, but that has too many extra characters

Instead:

let w=prompt("Width in chars")
let c=0;
let s="Hello "
setInterval(function(){
	let t=""
  	for(let i=0;i<w;i++){
    	t+=s[(i+c)%6]
    }
  console.clear()
  console.log(t)
  c++
}, 200)

 

Posted
20 minutes ago, KaladinsSenseOfHumourSpren said:

Alright I was going to use p5js too, but that has too many extra characters

Instead:

let w=prompt("Width in chars")
let c=0;
let s="Hello "
setInterval(function(){
	let t=""
  	for(let i=0;i<w;i++){
    	t+=s[(i+c)%6]
    }
  console.clear()
  console.log(t)
  c++
}, 200)

 

good pretty sure that works except...

you need to also prompt for the text (the "Hello")

You can just concat a space after or smt.

 

Once you do that, repost and please post the number of chars too. Just put it in a text editor or something. Not sure how whitespace is counted, but spaces/tabs DO count (cuz I assume basically all text editors/IDEs count them lol)

Or smth like google docs / ms word might work too ¯\_(ツ)_/¯

I'll probably double check using vscode at some point

 

also since you're going for the no semicolons approach (depends on if newlines are counted which probably aren't actually) but anyway you have an extra semi on line 2 and then the last line has an unneeded space after the comma.

also hint the prompt doesn't need any text/message :3

 

(btw everyone the challenge is still open and will be for a bit since we can usually optimize stuff, plus different languages might yield (lol) shorter programs)

 

also sorry if I'm being weird...

 

 

also lol c++ i love doing that

 

 

oh sorry if i made it seem like something so

i used Hello in examples but u just as placeholder

it could be any text

user inputs it

each time program run

 

BYEEEEEE

Posted
10 minutes ago, Usseewa said:

good pretty sure that works except...

you need to also prompt for the text (the "Hello")

You can just concat a space after or smt.

 

Once you do that, repost and please post the number of chars too. Just put it in a text editor or something. Not sure how whitespace is counted, but spaces/tabs DO count (cuz I assume basically all text editors/IDEs count them lol)

Or smth like google docs / ms word might work too ¯\_(ツ)_/¯

I'll probably double check using vscode at some point

 

also since you're going for the no semicolons approach (depends on if newlines are counted which probably aren't actually) but anyway you have an extra semi on line 2 and then the last line has an unneeded space after the comma.

also hint the prompt doesn't need any text/message :3

 

(btw everyone the challenge is still open and will be for a bit since we can usually optimize stuff, plus different languages might yield (lol) shorter programs)

 

also sorry if I'm being weird...

 

 

also lol c++ i love doing that

Hmm

let w=prompt()
let c=0
let s=prompt()+" "
setInterval(function(){
let t=""
for(let i=0;i<w;i++){
t+=s[(i+c)%s.length]
}
console.clear()
console.log(t)
c++
}, 200)

It's 155 characters, or 144 without spaces

Posted
52 minutes ago, KaladinsSenseOfHumourSpren said:

Hmm

let w=prompt()
let c=0
let s=prompt()+" "
setInterval(function(){
let t=""
for(let i=0;i<w;i++){
t+=s[(i+c)%s.length]
}
console.clear()
console.log(t)
c++
}, 200)

It's 155 characters, or 144 without spaces

good job that works!

it's actually 173 according to vscode and 162 according to notepad.

But that's cuz of the damn line breaks..

So we remove... 22 chars from the 173 and get 151 which is also what the google docs says?

how'd you get 155?

 

(also you can optimize it further)

maybe I'm wrong it doesn't even matter ...

 

anyway other people you can still post your solutions

Posted
8 minutes ago, Usseewa said:

good job that works!

it's actually 173 according to vscode and 162 according to notepad.

But that's cuz of the damn line breaks..

So we remove... 22 chars from the 173 and get 151 which is also what the google docs says?

how'd you get 155?

 

(also you can optimize it further)

maybe I'm wrong it doesn't even matter ...

 

anyway other people you can still post your solutions

Oh never mind I'd meant 151

let w=prompt()
let c=0
let s=prompt()+" "
setInterval(function(){
let t=""
for(let i=0;i<w;i++){
t+=s[(i+c)%s.length]
}
console.clear()
console.log(t)
c++
},200)

There, 150 now

If I really wanted to try and minimise characters I could make the animation really fast

let w=prompt()
let c=0
let s=prompt()+" "
setInterval(function(){
let t=""
for(let i=0;i<w;i++){
t+=s[(i+c)%s.length]
}
console.clear()
console.log(t)
c++
},9)

It would technically be valid and save me two characters, but...

A frame every 9 milliseconds would be way too fast

Posted
Just now, KaladinsSenseOfHumourSpren said:

Oh never mind I'd meant 151

let w=prompt()
let c=0
let s=prompt()+" "
setInterval(function(){
let t=""
for(let i=0;i<w;i++){
t+=s[(i+c)%s.length]
}
console.clear()
console.log(t)
c++
},200)

There, 150 now

If I really wanted to try and minimise characters I could make the animation really fast

let w=prompt()
let c=0
let s=prompt()+" "
setInterval(function(){
let t=""
for(let i=0;i<w;i++){
t+=s[(i+c)%s.length]
}
console.clear()
console.log(t)
c++
},9)

It would technically be valid and save me two characters, but...

A frame every 9 milliseconds would be way too fast

look into.. like anonymous functions or arrow functions or whatever the heck they're called. they don't use the "function" keyword i don't think so i think u can save characters

hope that's not cueating..

Posted
3 minutes ago, Usseewa said:

look into.. like anonymous functions or arrow functions or whatever the heck they're called. they don't use the "function" keyword i don't think so i think u can save characters

hope that's not cueating..

Ohhhhh those

I'd completely forgotten those existed

Need to look up how they work again

 

I implemented it, but because I need to attach it to a variable, the character count went up...

Posted
1 hour ago, KaladinsSenseOfHumourSpren said:

Ohhhhh those

I'd completely forgotten those existed

Need to look up how they work again

 

I implemented it, but because I need to attach it to a variable, the character count went up...

okay damnit i'll just share my version optimized from yours

let w=prompt()
let c=0
let s=prompt()+" "
setInterval(()=>{
let t=""
for(let i=0;i<w;i++)
t+=s[(i+c)%s.length]
console.clear()
console.log(t)
c++
},200)

142 chars

Note how I used the arrow function in the setInterval parameter, not a var decl like you did. Plus, since the for loop is only one statement, i removed the unnecessary curly braces.

 

(Note that I've never really used strict mode for JS, so I don't know everything that is/isn't allowed with that if you're trying it but...)

 

You can remove all the let statements (doesn't work in strict mode, but works "normally")

w=prompt()
c=0
s=prompt()+" "
setInterval(()=>{
t=""
for(i=0;i<w;i++)
t+=s[(i+c)%s.length]
console.clear()
console.log(t)
c++
},200)

122 chars

 

If you are using strict mode, you can combine the let statements (though I think strict requires semicolons. Not sure tho.)

let w,c,s,t,i

then same code as above.

 

okbye

 

anyway that's 122 chars can ya beat it

sorry lol.. im getting tired

also u can kinda ignore everything about strict mode... maybe... idkjfajf

Posted
2 minutes ago, Usseewa said:

Note how I used the arrow function in the setInterval parameter, not a var decl like you did. Plus, since the for loop is only one statement, i removed the unnecessary curly braces.

Ohhh

W3schools said they need to be attached to a variable...

3 minutes ago, Usseewa said:

Plus, since the for loop is only one statement, i removed the unnecessary curly braces.

You can do that?

3 minutes ago, Usseewa said:

You can remove all the let statements (doesn't work in strict mode, but works "normally")

I'd completely forgotten you could do that in JS

I used to do that a lot some years ago, then spent ages un-learning it

Posted
2 minutes ago, KaladinsSenseOfHumourSpren said:

W3schools said they need to be attached to a variable...

6 minutes ago, Usseewa said:

i mean.. i use it all the time like i showed when i use APIs

like..

await promise.then((r) => { ... })) or something if i recall..

3 minutes ago, KaladinsSenseOfHumourSpren said:

You can do that?

7 minutes ago, Usseewa said:

yeah, why not? i forget if i learned that from JS or some other languages. but yeah, languages usually probably let you forgo braces for single-statement blocks. Same with if statements and the like.

4 minutes ago, KaladinsSenseOfHumourSpren said:

I'd completely forgotten you could do that in JS

I used to do that a lot some years ago, then spent ages un-learning it

yeah..

Posted
3 minutes ago, Usseewa said:
8 minutes ago, KaladinsSenseOfHumourSpren said:
12 minutes ago, Usseewa said:

i mean.. i use it all the time like i showed when i use APIs

like..

await promise.then((r) => { ... })) or something if i recall..

Ah

I'd forgotten

3 minutes ago, Usseewa said:

yeah, why not? i forget if i learned that from JS or some other languages. but yeah, languages usually probably let you forgo braces for single-statement blocks. Same with if statements and the like.

I'd never known this.

Posted
1 minute ago, KaladinsSenseOfHumourSpren said:
5 minutes ago, Usseewa said:
9 minutes ago, KaladinsSenseOfHumourSpren said:
14 minutes ago, Usseewa said:

i mean.. i use it all the time like i showed when i use APIs

like..

await promise.then((r) => { ... })) or something if i recall..

Ah

I'd forgotten

holy quadruple quote :3

1 minute ago, KaladinsSenseOfHumourSpren said:

I'd never known this.

coolios

 

on another note.. do you know ternary if statements and null-coalescing operators?

they're soooooo fun

and i never knew about them till php, but now know that basically every language has them (or ataleast ternary-if)

even python, tho it's styled differently:

regular:

a ? b : c

where a is condition, b will be returned if a is true, otherwise c

for python:

b if a else c

 

anyway yeahhhhhhhh

 

also null is just

a ?? b

 

basically it returns b if a is null(ish)/undefined, otherwise a. useful for default values or whatever sometimes to protect from errors

Posted
1 minute ago, Usseewa said:

do you know ternary if statements

Yup, though I don't use them much

2 minutes ago, Usseewa said:

null-coalescing operators?

Nope

2 minutes ago, Usseewa said:

also null is just

a ?? b

 

basically it returns b if a is null(ish)/undefined, otherwise a. useful for default values or whatever sometimes to protect from errors

Ah.

Posted
19 hours ago, Usseewa said:
19 hours ago, KaladinsSenseOfHumourSpren said:
19 hours ago, Usseewa said:
19 hours ago, KaladinsSenseOfHumourSpren said:
19 hours ago, Usseewa said:

i mean.. i use it all the time like i showed when i use APIs

like..

await promise.then((r) => { ... })) or something if i recall..

Expand  

Ah

I'd forgotten

holy quadruple quote :3

HOLY QUINTUPLE QUOTE

Posted (edited)

Ok here is my answer in Kona. For the record I would not characterize the K family (and thus Kona) to be made for code-golfing by the scale at which they are used within the finance sector. In any case, here it is: 

f:{(y-#a)_'(!#a)!\:a:((.$_ceil y*%#x)*#j)#j:(x," ")}

I am going to try and explain, more or less, how it functions. Note: in Kona, functions are evaluated right-to-left.

First, we begin with 

j:(x," ")

Which essentially just appends an empty space at the end of the string, and is then assigned to the variable j. Moving on, we have the following: 

a:((.$_ceil y*%#x)*#j)#j:(x," ")

The part (.$_ceil y*%#x)*#j can be broken down in the following way: #x is the number of elements in x, which in our case, since we are applying it to a string, is the number of characters; followed by y*%#x, which just means we divide our desired length by the number of characters in our string. The _ceil is simply the cealing function, and the .$ is used to turn a floating into an integer (if decimal part is zero).  All of it is then multiplied by the length of our j (which again is the string with an empty space at the end), and then named "a". Here is a couple of examples of it in use: 
 

  {((.$_ceil y*%#x)*#j)#j:(x," ")}["Hello";12]
"Hello Hello Hello "
  {((.$_ceil y*%#x)*#j)#j:(x," ")}["Hello";6]
"Hello Hello "
  {((.$_ceil y*%#x)*#j)#j:(x," ")}["Hello";4]
"Hello "
  {((.$_ceil y*%#x)*#j)#j:(x," ")}["Hello";15]
"Hello Hello Hello "

Once we have our "a", we consider the following part: 

(!#a)!\:a

First off, !#a is a vector of integers from 0 up to (#a)-1 (the length of a); e.g. if a is "Hello " as our third example above, then !#a is simply 0 1 2 3. The !\: is a tad more difficult; the ! in its dyadic form is a rotate function; e.g. 2!"Hello" is "lloHe"; and since our (!#a) is a vector, we use the \: to apply all to our a; for example,

  1 2 3!\:"Hello"
("elloH"
 "lloHe"
 "loHel")

Finally, (y-#a)_' is simply removing the first (y-#a) characters from each row of our column vector a. 

Here are a few examples of the whole function in action: 
 

  f["Hello";8]
("Hello He"
 "ello Hel"
 "llo Hell"
 "lo Hello"
 "o Hello "
 " Hello H"
 "Hello He"
 "ello Hel"
 "llo Hell"
 "lo Hello"
 "o Hello "
 " Hello H")
  f["Hello";2]
("He"
 "el"
 "ll"
 "lo"
 "o "
 " H")
  f["Hello";15]
("Hello Hello Hel"
 "ello Hello Hell"
 "llo Hello Hello"
 "lo Hello Hello "
 "o Hello Hello H"
 " Hello Hello He"
 "Hello Hello Hel"
 "ello Hello Hell"
 "llo Hello Hello"
 "lo Hello Hello "
 "o Hello Hello H"
 " Hello Hello He"
 "Hello Hello Hel"
 "ello Hello Hell"
 "llo Hello Hello"
 "lo Hello Hello "
 "o Hello Hello H"
 " Hello Hello He")
  f["Hello";5]
("Hello"
 "ello "
 "llo H"
 "lo He"
 "o Hel"
 " Hell")

I hope everything was clear enough. Anyways the character count (without the f: which was added purely for clarity) is 50; and I am sure if I had a bit more time to think about it I could reduce it quite a lot. For more info on Kona here is the GitHub repository: kevinlawler/kona: Open-source implementation of the K programming language

Edited by KonaUser
Clarity
Posted
1 hour ago, KonaUser said:

Ok here is my answer in Kona. For the record I would not characterize the K family (and thus Kona) to be made for code-golfing by the scale at which they are used within the finance sector. In any case, here it is: 

f:{(y-#a)_'(!#a)!\:a:((.$_ceil y*%#x)*#j)#j:(x," ")}

I am going to try and explain, more or less, how it functions. Note: in Kona, functions are evaluated right-to-left.

First, we begin with 

j:(x," ")

Which essentially just appends an empty space at the end of the string, and is then assigned to the variable j. Moving on, we have the following: 

a:((.$_ceil y*%#x)*#j)#j:(x," ")

The part (.$_ceil y*%#x)*#j can be broken down in the following way: #x is the number of elements in x, which in our case, since we are applying it to a string, is the number of characters; followed by y*%#x, which just means we divide our desired length by the number of characters in our string. The _ceil is simply the cealing function, and the .$ is used to turn a floating into an integer (if decimal part is zero).  All of it is then multiplied by the length of our j (which again is the string with an empty space at the end), and then named "a". Here is a couple of examples of it in use: 
 

  {((.$_ceil y*%#x)*#j)#j:(x," ")}["Hello";12]
"Hello Hello Hello "
  {((.$_ceil y*%#x)*#j)#j:(x," ")}["Hello";6]
"Hello Hello "
  {((.$_ceil y*%#x)*#j)#j:(x," ")}["Hello";4]
"Hello "
  {((.$_ceil y*%#x)*#j)#j:(x," ")}["Hello";15]
"Hello Hello Hello "

Once we have our "a", we consider the following part: 

(!#a)!\:a

First off, !#a is a vector of integers from 0 up to (#a)-1 (the length of a); e.g. if a is "Hello " as our third example above, then !#a is simply 0 1 2 3. The !\: is a tad more difficult; the ! in its dyadic form is a rotate function; e.g. 2!"Hello" is "lloHe"; and since our (!#a) is a vector, we use the \: to apply all to our a; for example,

  1 2 3!\:"Hello"
("elloH"
 "lloHe"
 "loHel")

Finally, (y-#a)_' is simply removing the first (y-#a) characters from each row of our column vector a. 

Here are a few examples of the whole function in action: 
 

  f["Hello";8]
("Hello He"
 "ello Hel"
 "llo Hell"
 "lo Hello"
 "o Hello "
 " Hello H"
 "Hello He"
 "ello Hel"
 "llo Hell"
 "lo Hello"
 "o Hello "
 " Hello H")
  f["Hello";2]
("He"
 "el"
 "ll"
 "lo"
 "o "
 " H")
  f["Hello";15]
("Hello Hello Hel"
 "ello Hello Hell"
 "llo Hello Hello"
 "lo Hello Hello "
 "o Hello Hello H"
 " Hello Hello He"
 "Hello Hello Hel"
 "ello Hello Hell"
 "llo Hello Hello"
 "lo Hello Hello "
 "o Hello Hello H"
 " Hello Hello He"
 "Hello Hello Hel"
 "ello Hello Hell"
 "llo Hello Hello"
 "lo Hello Hello "
 "o Hello Hello H"
 " Hello Hello He")
  f["Hello";5]
("Hello"
 "ello "
 "llo H"
 "lo He"
 "o Hel"
 " Hell")

I hope everything was clear enough. Anyways the character count (without the f: which was added purely for clarity) is 50; and I am sure if I had a bit more time to think about it I could reduce it quite a lot. For more info on Kona here is the GitHub repository: kevinlawler/kona: Open-source implementation of the K programming language

Did you make this account just for the codegolf? Lol.

Anyway... sorry I don't quite understand your explanation (not your fault, I just don't have the time to go over it). Where can I run this code to test it?

 

Also, welcome to the Shard lol

Posted
5 minutes ago, Usseewa said:

Did you make this account just for the codegolf? Lol.

Anyway... sorry I don't quite understand your explanation (not your fault, I just don't have the time to go over it). Where can I run this code to test it?

 

Also, welcome to the Shard lol

Yes I did just make it for this post 🤣 I just love golfing!
You can try it out here! (TIO link)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...