Legalize Reversed Date

CJam, 24 bytes

qS/Wf%[_1=:=31^sCs].e<S*

I/O format is dd mm yyyy.

Try it online!

Same byte count, I/O format mm dd yyyy:

qS/Wf%_0=:=1231^s2/.e<S*

Try it online!

Explanation

qS/     e# Read input, split around spaces.
Wf%     e# Reverse each component.
[       e# Set marker for new list.
  _1=   e#   Duplicate reversed strings, extract reversed month.
  :=    e#   Check for equality of the characters. This gives 1 for
        e#   November (11) and 0 for everything else.
  31^   e#   XOR the result with 31, giving 30 for November and 31
        e#   for everything else.
  s     e#   Convert the result to a string, "30"/"31".
  Cs    e#   Push 12, convert to string, "12".
]       e# Wrap "30"/"31" and "12" in a list.
.e<     e# Element-wise minimum. This clamps the day and month to their
        e# respective maxima.
S*      e# Join the result with spaces.

The other version works similarly, except that we start from the integer 1230 or 1231 before converting it to ["12" "30"] or ["12" "31"].


Pyth, 55 53 46 43 41 bytes

APJ_Mczd=HhS,12sH=GhS,sGC@."❤❤ó»î"H%"%02d %02d %s"[GHeJ
APJ_Mczd=hS,12sH=hS,sGC@."❤❤ó»î"H%"%02d %02d %s"[GHeJ
APJ_Mcz\-%"%02d %02d %s"[hS,sGx31q11sHhS,12sHeJ
APJ_Mczdjd[>2+\0hS,sGx31q11sH>2+\0hS,12sHeJ
APJ_Mczdjd.[L\02[`hS,sGx31q11sH`hS,12sHeJ

where ❤❤ were two unprintables, respectively U+001C and U+001F.

Test suite.


Convex, 23 bytes

Byte count assumes CP-1252 encoding.

qS/Wf%_1=:=31^sCs¶.e<S*

I/O format is dd mm yyyy.

Try it online!

This is a direct port of my CJam answer. Convex is heavily based on CJam, and therefore the only difference is the use of Convex's operator which wraps the top two stack elements in a list, saving a byte over [...].

Tags:

Date

Code Golf