# Background

I drink too much coffee. Eventually it will probably kill me.

*Or will it..?*

Anyway, there's no way to know.

*Or is there...?*


# The Discovery of the Formula 

I proudly announce my discovery of a formula for measuring the life-span of coffee drinkers!

For

* ```h``` is a health number assigned to each person (8 digit date of birth YYYYMMDD)


* ```CAFE``` is a cup of *regular* coffee


* ```DECAF``` is a cup of *decaffeinated* coffee


To determine the life-time coffee limits:

* Drink cups of coffee (i.e. add to ```h```) until any part of the health number includes `DEAD`


* If the test subject can survive drinking five thousand cups wihout being ```DEAD``` then coffee has no ill effect on them

# Kata Task

Given the test subject's date of birth, return an array describing their life-time coffee limits

```[ regular limit , decaffeinated limit ]```

## Notes 

* The limits are ```0``` if the subject is unaffected as described above

* At least 1 cup must be consumed (Just thinking about coffee cannot kill you!)

# Examples

* John was born 19/Jan/1950 so ```h=19500119```. His coffee limits are ```[2645, 1162]``` which is only about 1 cup per week. You better cut back John...How are you feeling? You don't look so well.


* Susan (11/Dec/1965) is unaffected by decaffeinated coffee, but regular coffee is very bad for her ```[111, 0]```. Just stick to the decaf Susan.


* Elizabeth (28/Nov/1964) is allergic to decaffeinated coffee. Dead after only 11 cups ```[0, 11]```. Read the label carefully Lizzy! Is it worth the risk?


* Peter (4/Sep/1965) can drink as much coffee as he likes ```[0, 0]```. You're a legend Peter!!



Hint: https://en.wikipedia.org/wiki/Hexadecimal 



*Note: A life-span prediction formula this accurate has got to be worth a lot of money to somebody! I am willing to sell my copyright to the highest bidder. Contact me via the discourse section of this Kata.*

Difficulty: EASY
Tags: Ad-hoc

Starter code:
def coffee_limits(year, month, day):
	

Solutions:
def coffee_limits(year, month, day):
	h = int(f'{year:04}{month:02}{day:02}')
	return [limit(h, 51966), limit(h, 912559)]

def limit(h, c):
	for i in range(1, 5000):
		h += c
		if 'DEAD' in f'{h:X}':
			return i
	return 0


def coffee_limits(year, month, day):
	health = int(f'{year:04d}{month:02d}{day:02d}')
	drinks = (int(d, 16) for d in ('cafe', 'decaf'))
	return [next((cup for cup in range(1, 5001) if 'dead' in f'{health + drink * cup:x}'), 0) for drink in drinks]


(CAFE, DECAF) = (51966, 912559)

def coffee_limits(y, m, d):
	n = int(''.join([str(y), str(m).zfill(2), str(d).zfill(2)]))
	A = lambda x: next((i for i in range(1, 5001) if 'dead' in hex(n + i * x)[2:]), 0)
	return [A(CAFE), A(DECAF)]


import re

def coffee_limits(year, month, day):
	h_cafe = str(year)
	if len(str(month)) == 1:
		h_cafe += '0' + str(month)
	else:
		h_cafe += str(month)
	if len(str(day)) == 1:
		h_cafe += '0' + str(day)
	else:
		h_cafe += str(day)
	h_cafe = int(h_cafe)
	h_decaf = h_cafe
	n = 0
	cafe = 0
	decaf = 0
	print(h_cafe)
	while n < 5000:
		h_cafe += 51966
		h_decaf += 912559
		n += 1
		if 'dead' in hex(h_cafe):
			if cafe == 0:
				cafe = n
		elif 'dead' in hex(h_decaf):
			if decaf == 0:
				decaf = n
	return [cafe, decaf]


def coffee_limits(year, month, day):
	health_no = int('{:04}{:02}{:02}'.format(year, month, day))
	(cafe, decaf) = (health_no, health_no)
	cups = 0
	for i in range(5000):
		cafe += 51966
		cups += 1
		if 'DEAD' in '{:08X}'.format(cafe):
			break
	cafe = 0 if cups == 5000 else cups
	cups = 0
	for i in range(5000):
		decaf += 912559
		cups += 1
		if 'DEAD' in '{:08X}'.format(decaf):
			break
	decaf = 0 if cups == 5000 else cups
	return [cafe, decaf]


def decToHex(n):
	hexChart = {0: '0', 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9', 10: 'A', 11: 'B', 12: 'C', 13: 'D', 14: 'E', 15: 'F'}
	hexValue = ''
	while n:
		hexValue = hexChart[n % 16] + hexValue
		n //= 16
	return hexValue

def hexToDec(string):
	hex = '0123456789ABCDEF'
	total = 0
	for (index, i) in enumerate(string):
		value = hex.index(i)
		power = len(string) - (index + 1)
		total += value * 16 ** power
	return total

def coffee_limits(year, month, day):
	cafe = hexToDec('CAFE')
	cafeCups = 0
	h = int(str(year) + str(month).zfill(2) + str(day).zfill(2))
	for i in range(5000):
		h += cafe
		cafeCups += 1
		if 'DEAD' in decToHex(h):
			break
	h = int(str(year) + str(month).zfill(2) + str(day).zfill(2))
	print(h)
	decaf = hexToDec('DECAF')
	decafCups = 0
	for i in range(5000):
		h += decaf
		decafCups += 1
		if 'DEAD' in decToHex(h):
			break
	return [cafeCups % 5000, decafCups % 5000]


def coffee_limits(year, month, day):
	CAFE = 51966
	DECAF = 912559
	cafes_allowed = []
	decafs_allowed = []
	year = str(year)
	if month < 10:
		month = '0' + str(month)
	else:
		month = str(month)
	if day < 10:
		print(day)
		day = '0' + str(day)
	else:
		day = str(day)
	health_number = int(year + month + day)
	for i in range(1, 5001):
		CAFE_total = health_number + i * CAFE
		DECAF_total = health_number + i * DECAF
		CAFE_hex_total = hex(CAFE_total)
		DECAF_hex_total = hex(DECAF_total)
		if 'dead' in CAFE_hex_total:
			cafes_allowed.append(i)
		if 'dead' in DECAF_hex_total:
			decafs_allowed.append(i)
	print(cafes_allowed)
	if not cafes_allowed:
		cafes_allowed = 0
	else:
		cafes_allowed = cafes_allowed[0]
	if not decafs_allowed:
		decafs_allowed = 0
	else:
		decafs_allowed = decafs_allowed[0]
	return [cafes_allowed, decafs_allowed]


def test(number, coffee):
	for i in range(1, 5001):
		if 'dead' in hex(number + i * coffee):
			return i
	return 0

def coffee_limits(year, month, day):
	number = year * 10000 + month * 100 + day
	return [test(number, coffee) for coffee in (51966, 912559)]


def coffee_limits(year, month, day):
	h = year * 10000 + month * 100 + day
	c = 0
	for i in range(1, 5001):
		if 'dead' in hex(h + i * 51966):
			c = i
			break
	d = 0
	for i in range(1, 5001):
		if 'dead' in hex(h + i * 912559):
			d = i
			break
	return [c, d]