#!/usr/bin/env node
# Require node fs
fs = require("fs")
# Happy birthday ;)
CAKECONSTANT = Math.round("""
,/
,( ),
Y
|-|
| |
_..--'''@|''''| |@'''|''''--.._
.' @_/-//-//>/>'/ @ '.
( @ /_<//<'/----------^-)
|'._ @ //|###########|
|~ ''--..@|',|}}}}}}}}}}}|
| ~ ~ |/ |###########|
| ~~ ~ ~|./|{{{{{{{{{{{|
'._ ~ ~ ~ |,/`````````````
''--.~.|/
""".length / 250 + Math.random())
# Some constants
OLD = "./data/CoffeeJunkie_oldcake.txt"
NEW = "./data/CoffeeJunkie_newcake.txt"
# How much cake do I want?
wantCake = (total_c, rest_c, total_p, rest_p) ->
round = total_p - rest_p
fairness = rest_c // rest_p
switchMemory() if round is 0
fairest_round = tryToRemember(total_p)
tryToMemorize(fairness)
if round >= fairest_round then fairness - CAKECONSTANT else total_c // 2
# Ok I should try to remember the last cake...
switchMemory = () ->
try
fs.renameSync(NEW, OLD)
catch error
# What happend with the last cake?
tryToRemember = (rounds) ->
try
last_cake = fs.readFileSync(OLD, "utf-8")
last_cake.trim().split(" ").map(
(i) -> parseInt(i)
).reduce(
(x, y, z, li) -> if y > li[x] then z else x
0
)
catch error
rounds / 2
# Watch what happens!
tryToMemorize = (fairness) ->
try
fs.appendFileSync(NEW, " " + fairness)
catch error
# Coffee is ready, so... GO!
console.log(wantCake(process.argv[2..]...))