| Bytes | Lang | Time | Link |
|---|---|---|---|
| 118 | C MSVC | 240809T171705Z | jdt |
| nan | Java 22 | 240808T151507Z | mastaH |
| 102 | Fortran GFortran cpp | 240815T140317Z | roblogic |
| 063 | Rexx Regina | 240815T125517Z | int 21h |
| 047 | Japt | 240808T190204Z | Shaggy |
| 091 | Haskell | 240810T092753Z | pxeger |
| 103 | Python | 240810T091053Z | pxeger |
| 096 | Swift | 240809T175534Z | TKDKid10 |
| 063 | JavaScript | 240808T030639Z | noodle p |
| 067 | PowerShell | 240808T070148Z | user3141 |
| 145 | Rust | 240809T191202Z | DPD- |
| 058 | gnuplot | 240808T164957Z | int 21h |
| 087 | Kotlin script mode | 240809T161904Z | InSync |
| 085 | Perl 5 | 240809T091532Z | Dom Hast |
| 058 | PARI/GP | 240809T091007Z | alephalp |
| 062 | Lua | 240809T085127Z | ouflak |
| 198 | Progress Openedge ABL | 240809T081903Z | Crelda |
| 270 | Haskell | 240809T074754Z | Rocko |
| 134 | C++ GCC | 240808T051944Z | bb94 |
| 065 | Lua | 240808T165430Z | chunes |
| 249 | VBScript | 240808T084948Z | Neil |
| 065 | Wolfram Language Mathematica | 240808T073220Z | att |
| 077 | Google Sheets | 240808T094632Z | doubleun |
| 068 | Ruby | 240808T075112Z | G B |
| 060 | R | 240808T062453Z | pajonk |
| 124 | Raku Perl 6 rakudo | 240808T051046Z | bb94 |
| 061 | Uiua | 240808T025342Z | noodle p |
| 068 | Python | 240808T013510Z | Albert.L |
C (MSVC), 118 bytes
#define day /##/
#define x /##/
#define print(x)puts("24 hours");}
int main() {
day = "Monday"
x = day.length
print(x)
Java 22, 210 159 bytes
Modified after @noodle person comment
class C{String day,length="24 hours";void print(String s){System.out.print(s);}void main(){String x;C day=new C();day.
day = "Monday";
x = day.length;
print(x);
}}
Un-golfed
class C {
String day, length = "24 hours";
void print(String s) { System.out.print(s); }
void main() {
String x;
C day = new C();
day.
day = "Monday";
x = day.length;
print(x);
}
}
Must be compiled and run with --release 22 and --enable-preview
javac --release 22 --enable-preview C.java
java --enable-preview C
Fortran (GFortran) -cpp, 102 bytes
#define print(x)print*,"24 hours"
#define x !
#define day !
day = "Monday"
x = day.length
print(x)
end
Zsh attempt failed - Fortran works though! The -cpp flag allows me to #define away the problematic code. print(x) is a (poorly named) macro that prints the text in a way the compiler understands. The assignments for x = and day = just become comments (!).
Rexx (Regina), 63 bytes
Here are two solutions in Rexx. Thanks to its flexible syntax the solutions are quite compact.
Since Rexx does not have print function/statement, it is defined in both cases within the program.
In the first and shorter solution (below) the user-defined print function takes no argument (x is ignored), prints the hardcoded string 24 hours and stops the execution. No need to care about return statement, which is normally required. Although print is formally a function, the way it is defined it could only run once, which is enough for the task.
day = "Monday"
x = day.length
print(x)
print:say"24 hours"
exit
Rexx (Regina), 71 bytes
This longer solution is actually more exciting. This time the print function prints the content of the global variable x. The content of x comes from day., which is a separate from day variable. My guess is that since length is not defined it is simply ignored and, so, day.length evaluates to day..
day.="24 hours"
day = "Monday"
x = day.length
print(x)
print:say x
exit
Haskell, 91 bytes
main=print"24 hours"where{_._=0;length=0;day = "Monday";
x = day.length;
print(x)=putStr x}
Thanks to WheatWizard
Python, 103 bytes
import gc
gc.get_referents(str.__dict__)[0]["length"]="24 hours"
day = "Monday"
x = day.length
print(x)
Just for fun - a version with no syntax trickery. Uses implementation-specific behaviour, but works on CPython 3.12.2. str.__dict__ is an immutable proxy but it has a hidden reference to the underlying mutable dictionary, which we can access using the gc module.
Swift, 101 96 bytes
Assuming I am allowed to answer my own question...
extension String{var length:String{"24 hours"}}
var x="",
day = "Monday"
x = day.length
print(x)
The Swift compiler on tio.run doesn't support Swift v6.0 implicit returning, but a different one does. Try it here.
JavaScript, 63 bytes
print=_=>alert`24 hours`
day = "Monday"
x = day.length
print(x)
Now using alert rather than console.log, as suggested by Shaggy.
PowerShell, 67 bytes
filter print{'24 hours'}
$day = "Monday"
$x = $day.length
print($x)
This requires a modification to the original code by adding $ in front of the variables; functionally, I don't see any difference to the allowed adding of semicolons at the line ends, or replacing the "=" assignment operator with a different one.
Straightforward: 'filter' is basically the same as 'function' (but with automatic pipeline processing, which is not the point here), but has only 6 instead of 8 characters. It will ignore any arguments and just return the static string '24 hours'.
PowerShell, 167 165 185 171 bytes
165 Found two spaces that could be removed
185 print now generates the output
171 thanks to @julian who golfed the C#
Add-Type 'public class c{static void print(int x){System.Console.Write("24 hours");}public static void d(){int x;var
day = "Monday";
x = day.Length;
print(x);
}}'
[c]::d()
This takes the original code as C# and compiles it into a class c that can be used by PowerShell. Some further golfing of the C# might be possible ...
Rust, 145 bytes
macro_rules!x{($a:stmt;$b:tt=$c:expr;$e:stmt)=>{$a;$b=();print!("24 hours")}}fn main(){let(day,x);x!{
day = "Monday";
x = day.length;
print(x)
}}
the macro x! rewrites the code to:
day = "Monday";
x = ();
print!("24 hours")
gnuplot, 58 bytes
length="\r24 hours"
day = "Monday"
x = day.length
print(x)
Try it online! (Note that due to a bug, the output from both TIO and gnuplot.io is different from the output produced on a local Windows machine, see the screenshot below.)
How this works:
The dot operator concatenates two strings. This way the string x contains "Monday\r24 hours". When printing x, first "Monday" appears, then the symbol \r (carriage return without linefeed) moves the cursor to the start of the same line and then printing "24 hours" over the string "Monday" overwrites it.
Edit: since this answer has received so many upvotes, I have decided to try and print the string "Monday\r24 hours" from the Linux OS. The result in Python can be checked online here.
Kotlin (script mode), 87 bytes
val print={_:Int->print("24 hours")}
var x=0
val day = "Monday"
x = day.length
print(x)
A port of @noodleperson's JS answer.
Perl 5, 85 bytes
sub day:lvalue{$x="24 hours"}sub x:lvalue{$x}
day = "Monday";
x = day.length
print(x)
Explanation
Works by defining two lvalue subroutines which can be assigned to, day and x, with the desired value hardcoded to "24 hours" for day. day.length actually concatenates the output of day and length but since $_ is undefined, length outputs undef which is coerced to the empty string.
PARI/GP, 58 bytes
a.length="24 hours"
day = "Monday"
x = day.length
print(x)
PARI/GP's member function syntax is a bit weird. When we define a member function a.length="24 hours", we actually define a new function _.length = (a) -> "24 hours". This function is associated with _.length instead of a, so we can call it from any object, not just a.
Progress Openedge ABL, 198 bytes
def var day as char.
def var x as char.
def temp-table day
FIELD length as char init "24 Hours".
function print char(x as char):
message x.
end.
create day.
day = "Monday".
x = day.length.
print(x).
Works by defining each component of the code. First instance of day is read as a normal variable. day.length is read as the field 'length' in the table 'day'.
Try It
You may need to switch the dojo to run on v12.2.12 to avoid an error with the site
Haskell, 270 bytes
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE DataKinds #-}
#define print(x) in putStrLn x
import GHC.Records
instance GHC.Records.HasField "length" String String where getField=const"24 hours"
main=let
day = "Monday"
x = day.length
print(x)
C++ (GCC), 168 135 134 bytes
Thanks to G B for −33 bytes.
#include<cstdio>
#define print(Z)puts("24 hours");}
struct A{A*length;A(char*){}};main(){A*x,
day = "Monday";
x = day.length;
print(x)
Lua, 84 65 bytes
day={length="24 hours"}os.
day = "Monday"
x = day.length
print(x)
-19 thanks to Daniel Schepler
VBScript, 263 249 bytes
class o
public length
property let day(x)
end property
property get day
me.length="24 hours"
set day=me
end property
function print(x)
end function
function x
day = "Monday"
x = day.length
print(x)
end function
end class
set x=new o
wscript.echo x.x
day is a date function in VBScript. print is apparently also a function, but I don't know what it does. Wrapping everything in a class allows me to declare local properties to override them, so the only statement that actually achieves anything is x = day.length, which reads the me.length set earlier and returns it as the result of x.x, which we echo at the end of the script.
Wolfram Language (Mathematica), 65 bytes
print _^:=Print@"24 hours"
day = "Monday"
x = day.length
print(x)
I've taken the liberty of generalizing "This program... must provide its output... by utilizing a function called print" slightly. Strictly speaking, Mathematica doesn't exactly have functions; instead, it has a collection of definitions describing transformations on an expression. During evaluation, these are applied until no more such definitions are applicable.
print _^:=Print@"24 hours"
Add a definition to the symbol print: when it's multiplied with anything, print 24 hours.
day = "Monday"
Assign "Monday" to the symbol day.
x = day.length
Assign the dot product of day and length to the symbol x. There is no built-in rule for evaluating the dot product of a string and a symbol.
print(x)
Multiply print with x.
Probably against the spirit of the rules, but line 1 could simply be print:=Print@"24 hours" (-3 bytes): print 24 hours when print is encountered.
Google Sheets, 77 bytes
=let(print,lambda(_,"24 hours"),_,
day = "Monday"&
x = day.length,
print(x)
)
Ruby, 68 bytes
def print a;puts"24 hours"end
day = "Monday"
x = day.length
print(x)
For reference, previous solution:
Ruby, 82 bytes
String.class_eval{def length;"24 hours"end}
day = "Monday"
x = day.length
print(x)
R, 60 bytes
day.length="24 hours"
day = "Monday"
x = day.length
print(x)
. is just a part of variable name in R.
Raku (Perl 6) (rakudo), 126 124 bytes
use MONKEY-TYPING;augment class Str {method length {"24 hours"}};my \day:=my \x:=$;
day = "Monday";
x = day.length;
print(x)
Uiua, 61 bytes
print=ggg&p
day = "Monday"
x = day.length
print(x)."24 hours"
First, let's look at what the given program says in Uiua:
day = "Monday" # Assign 'day' to the string.
x = day.length # Assign 'x' to a function which pushes the value
# of 'day' and pushes twice the length of an array.
print(x) # Call a function 'print' after calling the function 'x'.
x has to take an array as input, so we put it on the stack. x has three outputs, all of which are useless, so print has to three values and print "24 hours". We kill two birds with one stone by using "24 hours" as the array x takes. print is defined to pop three stack values and print a string.
If you prefer, the ."24 hours" section can be moved to its own line at the start of the program, but this way is a byte shorter and as far as I can tell is perfectly allowed.
Python, 68 bytes
class day:length="24 hours";\
day = "Monday"
x = day.length
print(x)
Not sure the backslash is cheating or not.
