哪一年


9

2014年12月,2015年几乎已经开始。但是,似乎并非每个人都意识到这一点。

乔恩:“ 2009年肯定是漫长的一年” |  加菲猫看着日历|  加菲猫:“我们必须得到一个新的日历”

但是PPCG的人们来救助!

输入项

您的程序将获得一个像墙一样的网格,其中12月一个月的日子与11月和1月的日子有所不同。每个星期从星期日开始,到星期六结束。

一个月内的天以表示#。月份以外的日子用(空格,ASCII码32)表示。每周都有单独的一行。行之间用换行符(\n)分隔。您的程序可能要求省略或在输入末尾添加换行符。

例如,这是2009年12月的输入:

  #####
#######
#######
#######
#####  

日历始终是十二月。

任务

有了输入,您必须找到与日历关联的年份。由于日历与多个年份匹配,因此您必须返回最接近2015年的年份(2015年之前)。(2015年本身除外)。

您的程序必须在<2015年的任何年份中产生正确的输出,但那些12月的日历布局等于接近2015年的另一年(2015年前)的日历输出除外。

如果年份<2014,则还必须计算与2014年的年份差值。例如,对于2012年,差值为2

输出量

程序的输出必须为:

  • 文本:(Your calendar is for <year>.注意:由于最初被拼写为“日历”,因此我也将接受该拼写。)
  • 后跟换行符(\n\r\n)。
  • 后跟文字: It's almost 2015.
  • 如果年份<2014,则必须在其后加上文本:You're <difference> years behind.必须在单独的一行上。
  • 后跟换行符(\n\r\n)。
  • 后跟文字: Go buy a new calendar!
  • (可选)后跟换行符(\n\r\n)。

规则

  • 您可以选择是要接收日历作为命令行参数(例如yourprogram.exe <calendar>)还是提示用户输入。
  • 您可能会认为您的程序不会收到无效的输入。无效的输入包括没有年份的日历布局。
  • 最短的代码(以任何语言表示的字节数)为准。
  • 任何非标准的命令行参数(运行脚本通常不需要的参数)都计入总字符数。

  • 您的程序不得执行的操作:

    • 取决于任何外部资源。
    • 取决于具有特定的文件名。
    • 输出所需输出以外的任何内容。
    • 需要非常长的时间才能运行。如果您的程序在普通家庭用户的计算机上运行一分钟以上,则该程序无效。
  • 在发布此挑战之前,不得使用没有公开可用的编译器/解释器的编程语言来编写您的程序。

例子

输入:

  #####
#######
#######
#######
#####  

输出:

Your calendar is for 2009.
It's almost 2015.
You're 5 years behind.
Go buy a new calendar!

输入:

 ######
#######
#######
#######
####   

输出:

Your calendar is for 2014.
It's almost 2015.
Go buy a new calendar!

输入:

      #
#######
#######
#######
#######
##     

输出:

Your calendar is for 2012.
It's almost 2015.
You're 2 years behind.
Go buy a new calendar!

ew 设法在2014年UTC结束前(SE使用的)将其发布6秒钟。另外,大家新年快乐!
user2428118 2015年

假设假设2016年和2009年的形状相同。那么哪一个是最接近的呢?(即您是说按绝对距离最接近,还是最接近但早于2015年?)
Sp3000

@ SP3000后者。我已编辑帖子以明确说明。
user2428118 2015年

2
真的有必要在输出中以两种不同的方式拼写日历吗?
feersum'1

3
“您可以选择是否要接收分数作为命令行参数(例如yourprogram.exe 2/5)” ...什么?
feersum'1

Answers:


2

CJam,126个字节

"Your calendar is for "2e3q'##"DE9AB6C"=~+".
It's almost "2015_(".
You're 5 years behind"9@5$-:TtTg*".
Go buy a new calendar!"

3

Python 3,178个字节

s=input();i=42157313>>s.index("#")*4&15;print("Your calendar is for %d.\nIt's almost 2015.\n%sGo buy a new calendar!"%(2014-i,(i>0)*("You're %d year%s behind.\n"%(i,"s"*(i>1)))))

一个基于第一个的位置的简单查找表#

展开:

s=input()
i=42157313>>s.index("#")*4&15
print("Your calendar is for %d.\nIt's almost 2015.\n%sGo buy a new calendar!"\
    %(2014-i,(i>0)*("You're %d year%s behind.\n"%(i,"s"*(i>1)))))

2

Perl-187

$ARGV[0]=~/^( *)/;my@a=(7,8,3..5,0,6);my$b=($a[length$1]+2006);print"Your calendar is for $b.\nIt's almost 2015.\n".($b<2014?"You're ".2014-$b." years behind.\nGo buy a new calendar!":"")

“。” 和“-”具有相同的运算符优先级,因此您需要在“ 2014- $ b”前后加上括号。
nutki'1


@nutki Aah,我以为我已经解决了。
KSFT 2015年

2

Perl 5:137 143

#!perl -p
$_="Your calendar is for ".(2014-($%=w834506&s/#/?/r)).".
It's almost 2015.
".("You're $% years behind.
")x!!$%."Go buy a new calendar!"

以前的方法:

#!perl -p
/#/;$_="Your calendar is for ".(2014-($b=1558279/9**"@-"%9)).".
It's almost 2015.
".("You're $b years behind.
")x!!$b."Go buy a new calendar!"

标准输入的日历(当然只有第一行很重要)

perl 2014.pl <<<" ######"

2

C#235

缩小:

class P{static void Main(string[] a){var y=new[]{1,0,5,4,3,8,2}[a[0].IndexOf('#')];var z=2014-y;System.Console.Write("Your calendar is for "+z+"\nIt's almost 2015.\n"+(z>0?"You're "+z+" years behind.":"")+"\nGo buy a new calendar!");}}

不打高尔夫球

class P
{
    static void Main(string[] a)
    {
        var y = new[]{1,0,5,4,3,8,2}[a[0].IndexOf('#')];
        var z = 2014-y; 
        System.Console.Write("Your calendar is for "+z+"\nIt's almost 2015.\n"+(z>0 ? "You're "+z+" years behind.":"")+"\nGo buy a new calendar!");
    }
}

好吧,语言很冗长:)


1
您的班级和Main不必公开。另外,您可以轻松地重命名ProgramP。那应该为您节省一些字符:)
ProgramFOX 2015年

确实有20个字符,谢谢:)
Manuel Schweigert

1
您可以通过更改y计算方式来节省一些字符,如果您为字符串建立索引,则可以保存描述数组所需的语法(即var y = "1054382"[index]-480是ASCII 48)。y也可以内联到z计算中以进一步节省费用。
VisualMelon 2015年

2

C#,384363个 325字节

C#时间,请告诉我是否错过其中一项规则等。

string a(string s){var l=s.IndexOf('#');var x=(DayOfWeek)Enum.Parse(typeof(DayOfWeek),""+l);l=1;for(;;){var y=DateTime.Now.Year-l;var t=(new DateTime(y,12,1).DayOfWeek==x)?"Your calendar is for "+y+"\nIt's almost 2015\n"+((y < 2014)?"You're "+--l+"years behind\n":"")+"Go buy a new calendar":null;if(t!=null){return t;}l++;}}

输入项

" ######" 
"#######"  
"#######" 
"#######" 
"#####  "

输出量

"Your calendar is for 2014
 It's almost 2015
 Go buy a new calendar

输入2

"  #####"
"#######"  
"#######"
"#######" 
"#####  "

输出2

"Your calendar is for 2009
 It's almost 2015
 You're 5 years behind
 Go buy a new calendar"

编辑:更新,设法删除一些字节


您的输出中缺少标点符号。
泰特斯

2

Java,243个字节

这是一种冗长的语言:-)

class A{public static void main(String[]s){int y=2005+new int[]{8,9,4,5,0,6,7}[s[0].indexOf("#")],d=2014-y;System.out.print("Your calendar is for "+y+".\nIt's almost 2015.\n"+(d>0?"You're "+d+" years behind.\n":"")+"Go buy a new calendar!");}}

未缩小

class A {
    public static void main(String[] s) {
        int y = 2005 + new int[]{8,9,4,5,0,6,7}[s[0].indexOf("#")],
            d = 2014 - y;
        System.out.print("Your calendar is for " + y + ".\nIt's almost 2015.\n"
            + (d > 0 ? "You're " + d + " years behind.\n" : "") + "Go buy a new calendar!");
    }
}

1

的JavaScript(ES6),199个 170字节

我还不习惯编写ES6,所以任何提示将不胜感激:

查找表,170字节

a=(i)=>{y=[8,9,4,5,0,6,7][i.search('#')]+2005,f=2014-y;return`Your calendar is for ${y}.\nIt's almost 2015.\n${f?`You're ${f} years behind.\n`:''}Go buy a new calendar!`}

原始的199字节

a=i=>{y=2015,f=-1;do{f++;d=new Date(`${y--}-12-01`)}while(d.getDay()!=i.search('#'))return`Your calendar is for ${y}.\nIt's almost 2015.\n${f?`You're ${f} years behind.\n`:''}Go buy a new calendar!`}

未缩小

a = i => {
    y = 2015;
    f = -1;
    do {
        f++;
        d = new Date(`${y--}-12-01`);
    } while (d.getDay() != i.search('#'));
    return `Your calendar is for ${y}.\nIt's almost 2015.\n${f ? `You're ${f} years behind.\n` : ''}Go buy a new calendar!`;
}

的JavaScript(ES5),212 ×182字节

我还在下面包含了我的原始版本

查找数组,182字节

function a(i){y=[8,9,4,5,0,6,7][i.indexOf('#')]+2005,f=2014-y;return"Your calendar is for "+y+".\nIt's almost 2015.\n"+(f?"You're "+f+" years behind.\n":'')+"Go buy a new calendar!"}

未缩小

function a(i) {
    y = [8,9,4,5,0,6,7][i.indexOf('#')] + 2005;
    f = 2014 - y;
    return "Your calendar is for " + y + ".\nIt's almost 2015.\n" + (f ? "You're " + f + " years behind.\n" : '') + "Go buy a new calendar!";
}

原始的212字节

function a(i){y=2015,f=-1;do{f++;d=new Date(y--+"-12-01")}while(d.getDay()!=i.indexOf('#'));return"Your calendar is for "+y+".\nIt's almost 2015.\n"+(f?"You're "+f+" years behind.\n":'')+"Go buy a new calendar!"}

未缩小

function a(i) {
    y = 2015;
    f = -1;
    do {
        f++;
        d = new Date(y-- + "-12-01");
    } while (d.getDay() != i.indexOf('#'));
    return "Your calendar is for "+y+".\nIt's almost 2015.\n" + (f ? "You're "+f+" years behind.\n" : '') + "Go buy a new calendar!";
}

1

CoffeeScript,211 177字节

类似于我的PHP答案,但是CoffeeScript没有do-while循环,也没有简短的三元运算符:

a=(i)->y=2015;f=-1;loop(f++;d=new Date y--+"-12-01";break if d.getDay()==i.indexOf '#');"Your calendar is for "+y+".\nIt's almost 2015.\n"+(if f then"You're "+f+" years behind.\n"else'')+'Go buy a new calendar!'

不败

a = (i)->
    y = 2015
    f = -1
    loop
        f++
        d = new Date y-- + "-12-01"
        break if d.getDay() == i.indexOf '#'
    "Your calendar is for " + y + ".\nIt's almost 2015.\n" + (if f then "You're " + f + " years behind.\n" else '') + 'Go buy a new calendar!'

通过使用查找表来缩短:

a=(i)->y=[8,9,4,5,0,6,7][i.indexOf '#']+2005;f=2014-y;"Your calendar is for "+y+".\nIt's almost 2015.\n"+(if f then"You're "+f+" years behind.\n"else'')+'Go buy a new calendar!'

1

PHP,215个 181字节

查找表,181字节

由于数组语法较短,因此仅适用于PHP 5.4+:

function a($i){$y=2005+[8,9,4,5,0,6,7][strpos($i,'#')];$f=2014-$y;echo "Your calendar is for $y.\nIt's almost 2015.\n".($f?"You're $f years behind.\n":'')."Go buy a new calendar!";}

未缩小

function a($input) {
    $year = 2005 + [8,9,4,5,0,6,7][strpos($input, '#')];
    $difference = 2014 - $year;
    echo "Your calendar is for $year.\nIt's almost 2015.\n" . ($difference ? "You're $difference years behind.\n" : '') . "Go buy a new calendar!";
}

原始的215字节

适用于大多数(如果不是全部)PHP 5版本:

<?function a($c){$y=2015;$f=-1;do{$f++;$d=strtotime(--$y."-12-1");}while(date(w,$d)!=strpos($c,'#'));echo"Your calendar is for $y.\nIt's almost 2015.\n".($f?"You're $f years behind.\n":'')."Go buy a new calendar!";}

未缩小

<?php

function a($input) {
    $year = 2015;
    $difference = -1;
    do {
        $difference++;
        $date = strtotime(--$year . "-12-1");
    } while (date('w', $date) != strpos($input, '#'));
    echo "Your calendar is for $year.\nIt's almost 2015.\n" . ($difference ? "You're $difference years behind.\n" : '') . "Go buy a new calendar!";
}

1

红宝石174

def a(i)y=2005+[8,9,4,5,0,6,7][i.index('#')];d=2014-y;puts"Your calendar is for #{y}.\nIt's almost 2015.\n"+(d>0?"You're #{d} years behind.\n":'')+"Go buy a new calendar!"end

1

PHP,145字节

结束标记后面有两个换行符,因为PHP将忽略第一个

Your calendar is for <?=2014-$y=_1054382[1+strspn($argv[1]," ")],".
It´s almost 2015.",$y?"
You're $y years behind.":""?>

Go buy a new calendar!

从命令行参数获取输入;

需要PHP 5.6(2014年12月18日发布)或更高版本来索引字符串文字。


1

SmileBASIC,159个字节

DEF C C
Y=VAL("2834501"[INSTR(C,"#")])?"Your calender is for ";2014-Y;".
?"It's almost 2015.
IF Y THEN?"You're ";Y;" years behind.
?"Go buy a new calendar!
END

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.