#CoffeeJunkie, Coffeescript (Node.js)
#!/usr/bin/env node
# Require node fs
fs = require("fs")
# Happy birthday ;)
CAKECONSTANT = Math.round("""
,
( )
|-|
| |
_..--'''@| |@'''--.._
.' @_/-//-//>/>'/ @ '.
( @ /_<//<'/----------^-)
|'._ @ //|###########|
|~ ''--..@|',|}}}}}}}}}}}|
| ~ ~ |/ |###########|
| ~~ ~ ~|./|{{{{{{{{{{{|
'._ ~ ~ ~ |,/`````````````
''--.~.|/
""".length / 250 + Math.random())
# Some constants
OLD = "./data/oldcake.txt"
NEW = "./data/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..]...))
What exactly is a cake without a good cup of coffee?
The CoffeeJunkie prefers coffee over a slice of cake, but nevertheless wants to try some. He will always be fair to other participants and will try to remember what happened to the last cake. However, his excessive coffee consume has weakened his memories...