broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    now = x['now']
    window = sum(mg for t, mg in x['history'] if now - t <= 1440)
    remaining = max(0, x['max_mg'] - window)
    if x['history']:
        last = max(t for t, mg in x['history'])
        if now - last < x['min_interval']:
            return {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': window, 'remaining_mg': remaining}
    if window + x['request_mg'] > x['max_mg']:
        return {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': window, 'remaining_mg': remaining}
    return {'allowed': True, 'reason': 'OK', 'window_mg': window, 'remaining_mg': remaining}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3100,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1100}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1401, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 610,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 340, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 335, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1100, 'max_mg': 3100, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1100}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 50, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3200,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1200}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1402, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 620,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 400, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 300},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 330, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1200, 'max_mg': 3200, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1200}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3050, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 100, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3300,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1300}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1403, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 630,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 460, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 360},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 325, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1300, 'max_mg': 3300, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1300}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3100, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 150, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3400,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1400}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1404, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 640,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 520, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 420},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 320, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1400, 'max_mg': 3400, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1400}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3150, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 200, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3500,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1500}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1405, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 650,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 580, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 480},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 315, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1500, 'max_mg': 3500, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1500}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3200, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 250, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    now = x['now']
    window = sum(mg for t, mg in x['history'] if t // 1440 == now // 1440)
    remaining = max(0, x['max_mg'] - window)
    if x['history']:
        last = max(t for t, mg in x['history'])
        if now - last < x['min_interval']:
            return {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': window, 'remaining_mg': remaining}
    if window + x['request_mg'] > x['max_mg']:
        return {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': window, 'remaining_mg': remaining}
    return {'allowed': True, 'reason': 'OK', 'window_mg': window, 'remaining_mg': remaining}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3100,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1100}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1401, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 610,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 340, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 335, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1100, 'max_mg': 3100, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1100}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 50, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3200,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1200}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1402, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 620,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 400, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 300},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 330, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1200, 'max_mg': 3200, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1200}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3050, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 100, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3300,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1300}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1403, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 630,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 460, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 360},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 325, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1300, 'max_mg': 3300, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1300}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3100, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 150, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3400,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1400}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1404, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 640,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 520, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 420},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 320, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1400, 'max_mg': 3400, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1400}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3150, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 200, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3500,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1500}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1405, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 650,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 580, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 480},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 315, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1500, 'max_mg': 3500, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1500}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3200, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 250, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A dose given exactly 24 hours ago still blocks a new dose.

Input {history [[t_min, mg], ...] (any order), now, request_mg, max_mg, min_interval}. Doses with now - t < 1440 minutes count toward the rolling window (a dose exactly 24 h old has expired). The request is refused with TOO_SOON when now minus the most recent dose time is below min_interval, otherwise with DAILY_LIMIT when window + request exceeds max_mg; else OK. Return {allowed, reason, window_mg, remaining_mg = max(0, max_mg - window)}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    now = x['now']
    window = sum(mg for t, mg in x['history'] if now - t < 1440)
    remaining = max(0, x['max_mg'] - window)
    if x['history']:
        last = x['history'][-1][0]
        if now - last < x['min_interval']:
            return {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': window, 'remaining_mg': remaining}
    if window + x['request_mg'] > x['max_mg']:
        return {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': window, 'remaining_mg': remaining}
    return {'allowed': True, 'reason': 'OK', 'window_mg': window, 'remaining_mg': remaining}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3100,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1100}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1401, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 610,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 340, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 335, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1100, 'max_mg': 3100, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1100}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 50, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3200,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1200}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1402, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 620,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 400, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 300},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 330, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1200, 'max_mg': 3200, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1200}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3050, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 100, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3300,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1300}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1403, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 630,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 460, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 360},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 325, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1300, 'max_mg': 3300, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1300}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3100, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 150, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3400,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1400}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1404, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 640,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 520, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 420},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 320, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1400, 'max_mg': 3400, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1400}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3150, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 200, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3500,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1500}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1405, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 650,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 580, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 480},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 315, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1500, 'max_mg': 3500, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1500}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3200, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 250, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    now = x['now']
    window = sum(mg for t, mg in x['history'] if now - t < 1440)
    remaining = max(0, x['max_mg'] - window)
    if x['history']:
        last = min(t for t, mg in x['history'])
        if now - last < x['min_interval']:
            return {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': window, 'remaining_mg': remaining}
    if window + x['request_mg'] > x['max_mg']:
        return {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': window, 'remaining_mg': remaining}
    return {'allowed': True, 'reason': 'OK', 'window_mg': window, 'remaining_mg': remaining}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3100,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1100}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1401, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 610,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 340, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 335, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1100, 'max_mg': 3100, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1100}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 50, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3200,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1200}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1402, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 620,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 400, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 300},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 330, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1200, 'max_mg': 3200, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1200}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3050, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 100, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3300,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1300}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1403, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 630,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 460, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 360},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 325, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1300, 'max_mg': 3300, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1300}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3100, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 150, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3400,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1400}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1404, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 640,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 520, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 420},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 320, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1400, 'max_mg': 3400, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1400}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3150, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 200, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3500,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1500}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1405, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 650,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 580, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 480},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 315, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1500, 'max_mg': 3500, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1500}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3200, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 250, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
An early dose charted late in the list makes a too-soon request look allowed.

Input {history [[t_min, mg], ...] (any order), now, request_mg, max_mg, min_interval}. Doses with now - t < 1440 minutes count toward the rolling window (a dose exactly 24 h old has expired). The request is refused with TOO_SOON when now minus the most recent dose time is below min_interval, otherwise with DAILY_LIMIT when window + request exceeds max_mg; else OK. Return {allowed, reason, window_mg, remaining_mg = max(0, max_mg - window)}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    now = x['now']
    window = sum(mg for t, mg in x['history'] if now - t < 1440)
    remaining = max(0, x['max_mg'] - window)
    if x['history']:
        last = max(t for t, mg in x['history'])
        if now - last <= x['min_interval']:
            return {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': window, 'remaining_mg': remaining}
    if window + x['request_mg'] > x['max_mg']:
        return {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': window, 'remaining_mg': remaining}
    return {'allowed': True, 'reason': 'OK', 'window_mg': window, 'remaining_mg': remaining}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3100,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1100}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1401, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 610,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 340, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 335, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1100, 'max_mg': 3100, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1100}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 50, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3200,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1200}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1402, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 620,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 400, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 300},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 330, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1200, 'max_mg': 3200, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1200}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3050, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 100, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3300,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1300}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1403, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 630,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 460, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 360},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 325, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1300, 'max_mg': 3300, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1300}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3100, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 150, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3400,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1400}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1404, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 640,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 520, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 420},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 320, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1400, 'max_mg': 3400, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1400}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3150, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 200, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3500,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1500}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1405, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 650,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 580, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 480},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 315, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1500, 'max_mg': 3500, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1500}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3200, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 250, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    now = x['now']
    window = sum(mg for t, mg in x['history'] if now - t < 1440)
    remaining = max(0, x['max_mg'] - window)
    if x['history']:
        last = max(t for t, mg in x['history'])
        if now - last < x['min_interval'] - 30:
            return {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': window, 'remaining_mg': remaining}
    if window + x['request_mg'] > x['max_mg']:
        return {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': window, 'remaining_mg': remaining}
    return {'allowed': True, 'reason': 'OK', 'window_mg': window, 'remaining_mg': remaining}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3100,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1100}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1401, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 610,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 340, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 335, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1100, 'max_mg': 3100, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1100}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 50, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3200,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1200}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1402, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 620,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 400, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 300},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 330, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1200, 'max_mg': 3200, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1200}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3050, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 100, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3300,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1300}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1403, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 630,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 460, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 360},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 325, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1300, 'max_mg': 3300, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1300}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3100, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 150, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3400,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1400}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1404, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 640,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 520, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 420},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 320, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1400, 'max_mg': 3400, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1400}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3150, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 200, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3500,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1500}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1405, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 650,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 580, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 480},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 315, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1500, 'max_mg': 3500, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1500}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3200, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 250, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A request made exactly at the minimum interval is refused as too soon.

Input {history [[t_min, mg], ...] (any order), now, request_mg, max_mg, min_interval}. Doses with now - t < 1440 minutes count toward the rolling window (a dose exactly 24 h old has expired). The request is refused with TOO_SOON when now minus the most recent dose time is below min_interval, otherwise with DAILY_LIMIT when window + request exceeds max_mg; else OK. Return {allowed, reason, window_mg, remaining_mg = max(0, max_mg - window)}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    now = x['now']
    window = sum(mg for t, mg in x['history'] if now - t < 1440)
    remaining = max(0, x['max_mg'] - window)
    if x['history']:
        last = max(t for t, mg in x['history'])
        if now - last < x['min_interval']:
            return {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': window, 'remaining_mg': remaining}
    if window >= x['max_mg']:
        return {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': window, 'remaining_mg': remaining}
    return {'allowed': True, 'reason': 'OK', 'window_mg': window, 'remaining_mg': remaining}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3100,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1100}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1401, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 610,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 340, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 335, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1100, 'max_mg': 3100, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1100}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 50, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3200,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1200}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1402, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 620,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 400, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 300},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 330, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1200, 'max_mg': 3200, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1200}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3050, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 100, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3300,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1300}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1403, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 630,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 460, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 360},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 325, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1300, 'max_mg': 3300, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1300}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3100, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 150, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3400,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1400}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1404, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 640,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 520, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 420},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 320, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1400, 'max_mg': 3400, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1400}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3150, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 200, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3500,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1500}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1405, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 650,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 580, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 480},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 315, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1500, 'max_mg': 3500, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1500}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3200, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 250, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    now = x['now']
    window = sum(mg for t, mg in x['history'] if now - t < 1440)
    remaining = max(0, x['max_mg'] - window)
    if x['history']:
        last = max(t for t, mg in x['history'])
        if now - last < x['min_interval']:
            return {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': window, 'remaining_mg': remaining}
    if window + x['request_mg'] >= x['max_mg']:
        return {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': window, 'remaining_mg': remaining}
    return {'allowed': True, 'reason': 'OK', 'window_mg': window, 'remaining_mg': remaining}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3100,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1100}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1401, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 610,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 340, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 335, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1100, 'max_mg': 3100, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1100}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 50, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3200,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1200}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1402, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 620,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 400, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 300},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 330, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1200, 'max_mg': 3200, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1200}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3050, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 100, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3300,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1300}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1403, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 630,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 460, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 360},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 325, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1300, 'max_mg': 3300, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1300}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3100, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 150, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3400,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1400}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1404, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 640,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 520, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 420},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 320, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1400, 'max_mg': 3400, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1400}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3150, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 200, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})],
 [('dose exactly 24 hours old has expired',
   {'history': [[0, 1000], [600, 1000], [1200, 1000]],
    'now': 1440,
    'request_mg': 1000,
    'max_mg': 3500,
    'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1500}),
  ('rolling window spans midnight',
   {'history': [[1300, 1000], [1405, 1000]],
    'now': 1700,
    'request_mg': 1000,
    'max_mg': 2500,
    'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 2000, 'remaining_mg': 500}),
  ('unsorted history most recent dose',
   {'history': [[500, 500], [100, 500], [300, 500]],
    'now': 650,
    'request_mg': 500,
    'max_mg': 4000,
    'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 1500, 'remaining_mg': 2500}),
  ('interval exactly met',
   {'history': [[100, 650]], 'now': 580, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 480},
   {'allowed': True, 'reason': 'OK', 'window_mg': 650, 'remaining_mg': 2350}),
  ('interval shortly before minimum',
   {'history': [[100, 650]], 'now': 315, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': False, 'reason': 'TOO_SOON', 'window_mg': 650, 'remaining_mg': 2350}),
  ('request reaches maximum exactly',
   {'history': [[0, 1000], [300, 1000]], 'now': 600, 'request_mg': 1500, 'max_mg': 3500, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 2000, 'remaining_mg': 1500}),
  ('over limit remaining clamped',
   {'history': [[0, 2000], [400, 1500]], 'now': 900, 'request_mg': 500, 'max_mg': 3200, 'min_interval': 240},
   {'allowed': False, 'reason': 'DAILY_LIMIT', 'window_mg': 3500, 'remaining_mg': 0}),
  ('first dose control',
   {'history': [], 'now': 250, 'request_mg': 650, 'max_mg': 3000, 'min_interval': 240},
   {'allowed': True, 'reason': 'OK', 'window_mg': 0, 'remaining_mg': 3000})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A dose that would push the 24-hour total past the maximum is approved.

Input {history [[t_min, mg], ...] (any order), now, request_mg, max_mg, min_interval}. Doses with now - t < 1440 minutes count toward the rolling window (a dose exactly 24 h old has expired). The request is refused with TOO_SOON when now minus the most recent dose time is below min_interval, otherwise with DAILY_LIMIT when window + request exceeds max_mg; else OK. Return {allowed, reason, window_mg, remaining_mg = max(0, max_mg - window)}.

split: open-access