You have a n day streak!

Bash, 102 bytes

find ! -newerat 0-1day -delete
touch -a a
echo You have a $((1+(`stat -c %X-%Y a`)/86400)) day streak!

Warning, do not run in any folder you care about. It deletes any file not accessed in the last day in the working directory.

Uses a file a to store data, using the accessed/modified timestamps.


Bash, 92, 90, 82 bytes

Golfed

grep `date -d-1day -I` h||>h
date -I>>h
echo You have a `uniq h|wc -l` day streak!

EDITS

  • Truncate file, instead of removing it, -8 bytes;
  • Replaced -daystart with ! -newermt to save 2 bytes.

How It Works !

Each time you launch it will append a line with the current date to the file h, e.g:

2017-02-03
2017-02-04
2017-02-05
2017-02-05

It will then use uniq to filter out duplicates (i.e. multiple launches, within the same day), and count the lines to get the streak length.

uniq h|wc -l

To reset a streak, it will grep for 'yesterday' in h, and truncate it if not found.

grep `date -d-1day -I` h||>h

Goruby, 85 Bytes

Run with the interpreter flag -rdate.

c,t=0,Dae.y
op t.ts,?w
dw{c+=1;t=t.p;Fil.f t.ts}
s"You have a #{c} day streak!"

It works by storing a new file for each day on which it's invoked, then counts the number of consecutive files backwards to obtain the length of the streak. It doesn't ever delete files, so it will, eventually, after a very, very, very, very, very long time, fill your hard drive, a handful of bytes at a time.

Here's an ungolfed version of it:

streak, current_date = 0, Date.today
open(current_date.to_s, 'w')
while File.file?(current_date.to_s)
    streak += 1
    current_date = current_date.prev_day;
end
puts "You have a #{streak} day streak!"

Tags:

Date

Code Golf