broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(promo, ride, now_day):
    reason = None
    if now_day > promo['expires_day']:
        reason = 'expired'
    elif promo['uses_left'] <= 0:
        reason = 'exhausted'
    elif promo['first_ride_only'] and ride['rider_rides'] > 0:
        reason = 'not_first'
    elif ride['fare'] < promo['min_fare']:
        reason = 'min_fare'
    disc = 0
    if reason is None:
        if promo['kind'] == 'pct':
            disc = ride['fare'] * promo['value'] // 100
            if promo['cap']:
                disc = min(disc, promo['cap'])
        else:
            disc = min(promo['value'], ride['fare'])
    credit = min(ride['credits'], ride['fare'] - disc)
    return {'discount': disc, 'credit_used': credit, 'due': ride['fare'] - disc - credit, 'reason': reason}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 3,
     'value': 25},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 5}, 99],
   {'credit_used': 1099, 'discount': 0, 'due': 0, 'reason': None}),
  ('partial repair probe: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 3,
     'value': 10},
    {'credits': 5000, 'fare': 1000, 'rider_rides': 0}, 100],
   {'credit_used': 1000, 'discount': 0, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 1,
     'value': 25},
    {'credits': 0, 'fare': 1099, 'rider_rides': 0}, 99],
   {'credit_used': 0, 'discount': 0, 'due': 1099, 'reason': None}),
  ('normal control 1',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 1,
     'value': 50},
    {'credits': 250, 'fare': 3216, 'rider_rides': 0}, 100],
   {'credit_used': 250, 'discount': 800, 'due': 2166, 'reason': None}),
  ('normal control 2',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 0,
     'value': 10},
    {'credits': 0, 'fare': 2400, 'rider_rides': 0}, 101],
   {'credit_used': 0, 'discount': 0, 'due': 2400, 'reason': 'expired'}),
  ('normal control 3',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 0,
     'value': 50},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 1}, 100],
   {'credit_used': 1099, 'discount': 0, 'due': 0, 'reason': 'exhausted'}),
  ('normal control 4',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 1000},
    {'credits': 5000, 'fare': 2400, 'rider_rides': 0}, 101],
   {'credit_used': 2400, 'discount': 0, 'due': 0, 'reason': 'expired'})],
 [('regression: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 3,
     'value': 25},
    {'credits': 0, 'fare': 2400, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 2400, 'reason': None}),
  ('partial repair probe: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 1,
     'value': 25},
    {'credits': 5000, 'fare': 1050, 'rider_rides': 0}, 100],
   {'credit_used': 1050, 'discount': 0, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 10},
    {'credits': 250, 'fare': 1000, 'rider_rides': 0}, 100],
   {'credit_used': 250, 'discount': 0, 'due': 750, 'reason': None}),
  ('normal control 1',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 1,
     'value': 500},
    {'credits': 250, 'fare': 1467, 'rider_rides': 5}, 100],
   {'credit_used': 250, 'discount': 500, 'due': 717, 'reason': None}),
  ('normal control 2',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 0,
     'value': 50},
    {'credits': 0, 'fare': 2400, 'rider_rides': 1}, 99],
   {'credit_used': 0, 'discount': 0, 'due': 2400, 'reason': 'exhausted'}),
  ('normal control 3',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 10},
    {'credits': 0, 'fare': 2260, 'rider_rides': 0}, 99],
   {'credit_used': 0, 'discount': 226, 'due': 2034, 'reason': None}),
  ('normal control 4',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 2500},
    {'credits': 5000, 'fare': 1050, 'rider_rides': 5}, 101],
   {'credit_used': 1050, 'discount': 0, 'due': 0, 'reason': 'expired'})],
 [('regression: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 10},
    {'credits': 0, 'fare': 1050, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1050, 'reason': None}),
  ('partial repair probe: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 1,
     'value': 25},
    {'credits': 0, 'fare': 1099, 'rider_rides': 5}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1099, 'reason': None}),
  ('second regression',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 1,
     'value': 25},
    {'credits': 0, 'fare': 1000, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1000, 'reason': None}),
  ('normal control 1',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 0,
     'value': 500},
    {'credits': 0, 'fare': 2400, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 2400, 'reason': 'exhausted'}),
  ('normal control 2',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 0,
     'value': 2500},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 1}, 100],
   {'credit_used': 1099, 'discount': 0, 'due': 0, 'reason': 'exhausted'}),
  ('normal control 3',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 1000},
    {'credits': 0, 'fare': 1099, 'rider_rides': 1}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1099, 'reason': 'exhausted'}),
  ('normal control 4',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 0,
     'value': 50},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 0}, 100],
   {'credit_used': 1099, 'discount': 0, 'due': 0, 'reason': 'exhausted'})],
 [('regression: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 1,
     'value': 25},
    {'credits': 250, 'fare': 626, 'rider_rides': 0}, 100],
   {'credit_used': 250, 'discount': 0, 'due': 376, 'reason': None}),
  ('partial repair probe: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 50},
    {'credits': 5000, 'fare': 3687, 'rider_rides': 0}, 100],
   {'credit_used': 3687, 'discount': 0, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 10},
    {'credits': 250, 'fare': 1099, 'rider_rides': 1}, 100],
   {'credit_used': 250, 'discount': 0, 'due': 849, 'reason': None}),
  ('normal control 1',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 3,
     'value': 10},
    {'credits': 5000, 'fare': 1000, 'rider_rides': 0}, 100],
   {'credit_used': 900, 'discount': 100, 'due': 0, 'reason': None}),
  ('normal control 2',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 25},
    {'credits': 0, 'fare': 700, 'rider_rides': 5}, 101],
   {'credit_used': 0, 'discount': 0, 'due': 700, 'reason': 'expired'}),
  ('normal control 3',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 25},
    {'credits': 250, 'fare': 1099, 'rider_rides': 0}, 101],
   {'credit_used': 250, 'discount': 0, 'due': 849, 'reason': 'expired'}),
  ('normal control 4',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 25},
    {'credits': 0, 'fare': 1394, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1394, 'reason': 'exhausted'})],
 [('regression: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 50},
    {'credits': 0, 'fare': 3621, 'rider_rides': 0}, 99],
   {'credit_used': 0, 'discount': 0, 'due': 3621, 'reason': None}),
  ('partial repair probe: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 3,
     'value': 10},
    {'credits': 250, 'fare': 2400, 'rider_rides': 1}, 100],
   {'credit_used': 250, 'discount': 0, 'due': 2150, 'reason': None}),
  ('second regression',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 1,
     'value': 25},
    {'credits': 5000, 'fare': 1050, 'rider_rides': 1}, 100],
   {'credit_used': 1050, 'discount': 0, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 0,
     'value': 1000},
    {'credits': 0, 'fare': 1099, 'rider_rides': 5}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1099, 'reason': 'exhausted'}),
  ('normal control 2',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 1000},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 0}, 99],
   {'credit_used': 99, 'discount': 1000, 'due': 0, 'reason': None}),
  ('normal control 3',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 0,
     'value': 2500},
    {'credits': 250, 'fare': 1050, 'rider_rides': 1}, 99],
   {'credit_used': 250, 'discount': 0, 'due': 800, 'reason': 'exhausted'}),
  ('normal control 4',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 1000},
    {'credits': 5000, 'fare': 2400, 'rider_rides': 0}, 101],
   {'credit_used': 2400, 'discount': 0, 'due': 0, 'reason': 'expired'})]]
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

N = 1
observations = []
def solve(promo, ride, now_day):
    reason = None
    if now_day > promo['expires_day']:
        reason = 'expired'
    elif promo['uses_left'] <= 0:
        reason = 'exhausted'
    elif promo['first_ride_only'] and ride['rider_rides'] > 0:
        reason = 'not_first'
    elif ride['fare'] < promo['min_fare']:
        reason = 'min_fare'
    disc = 0
    if reason is None:
        if promo['kind'] == 'pct':
            disc = ride['fare'] * promo['value'] // 100
            if promo['cap'] is not None and promo['cap'] > 0:
                disc = min(disc, promo['cap'])
        else:
            disc = min(promo['value'], ride['fare'])
    credit = min(ride['credits'], ride['fare'] - disc)
    return {'discount': disc, 'credit_used': credit, 'due': ride['fare'] - disc - credit, 'reason': reason}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 3,
     'value': 25},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 5}, 99],
   {'credit_used': 1099, 'discount': 0, 'due': 0, 'reason': None}),
  ('partial repair probe: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 3,
     'value': 10},
    {'credits': 5000, 'fare': 1000, 'rider_rides': 0}, 100],
   {'credit_used': 1000, 'discount': 0, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 1,
     'value': 25},
    {'credits': 0, 'fare': 1099, 'rider_rides': 0}, 99],
   {'credit_used': 0, 'discount': 0, 'due': 1099, 'reason': None}),
  ('normal control 1',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 1,
     'value': 50},
    {'credits': 250, 'fare': 3216, 'rider_rides': 0}, 100],
   {'credit_used': 250, 'discount': 800, 'due': 2166, 'reason': None}),
  ('normal control 2',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 0,
     'value': 10},
    {'credits': 0, 'fare': 2400, 'rider_rides': 0}, 101],
   {'credit_used': 0, 'discount': 0, 'due': 2400, 'reason': 'expired'}),
  ('normal control 3',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 0,
     'value': 50},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 1}, 100],
   {'credit_used': 1099, 'discount': 0, 'due': 0, 'reason': 'exhausted'}),
  ('normal control 4',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 1000},
    {'credits': 5000, 'fare': 2400, 'rider_rides': 0}, 101],
   {'credit_used': 2400, 'discount': 0, 'due': 0, 'reason': 'expired'})],
 [('regression: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 3,
     'value': 25},
    {'credits': 0, 'fare': 2400, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 2400, 'reason': None}),
  ('partial repair probe: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 1,
     'value': 25},
    {'credits': 5000, 'fare': 1050, 'rider_rides': 0}, 100],
   {'credit_used': 1050, 'discount': 0, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 10},
    {'credits': 250, 'fare': 1000, 'rider_rides': 0}, 100],
   {'credit_used': 250, 'discount': 0, 'due': 750, 'reason': None}),
  ('normal control 1',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 1,
     'value': 500},
    {'credits': 250, 'fare': 1467, 'rider_rides': 5}, 100],
   {'credit_used': 250, 'discount': 500, 'due': 717, 'reason': None}),
  ('normal control 2',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 0,
     'value': 50},
    {'credits': 0, 'fare': 2400, 'rider_rides': 1}, 99],
   {'credit_used': 0, 'discount': 0, 'due': 2400, 'reason': 'exhausted'}),
  ('normal control 3',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 10},
    {'credits': 0, 'fare': 2260, 'rider_rides': 0}, 99],
   {'credit_used': 0, 'discount': 226, 'due': 2034, 'reason': None}),
  ('normal control 4',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 2500},
    {'credits': 5000, 'fare': 1050, 'rider_rides': 5}, 101],
   {'credit_used': 1050, 'discount': 0, 'due': 0, 'reason': 'expired'})],
 [('regression: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 10},
    {'credits': 0, 'fare': 1050, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1050, 'reason': None}),
  ('partial repair probe: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 1,
     'value': 25},
    {'credits': 0, 'fare': 1099, 'rider_rides': 5}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1099, 'reason': None}),
  ('second regression',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 1,
     'value': 25},
    {'credits': 0, 'fare': 1000, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1000, 'reason': None}),
  ('normal control 1',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 0,
     'value': 500},
    {'credits': 0, 'fare': 2400, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 2400, 'reason': 'exhausted'}),
  ('normal control 2',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 0,
     'value': 2500},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 1}, 100],
   {'credit_used': 1099, 'discount': 0, 'due': 0, 'reason': 'exhausted'}),
  ('normal control 3',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 1000},
    {'credits': 0, 'fare': 1099, 'rider_rides': 1}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1099, 'reason': 'exhausted'}),
  ('normal control 4',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 0,
     'value': 50},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 0}, 100],
   {'credit_used': 1099, 'discount': 0, 'due': 0, 'reason': 'exhausted'})],
 [('regression: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 1,
     'value': 25},
    {'credits': 250, 'fare': 626, 'rider_rides': 0}, 100],
   {'credit_used': 250, 'discount': 0, 'due': 376, 'reason': None}),
  ('partial repair probe: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 50},
    {'credits': 5000, 'fare': 3687, 'rider_rides': 0}, 100],
   {'credit_used': 3687, 'discount': 0, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 10},
    {'credits': 250, 'fare': 1099, 'rider_rides': 1}, 100],
   {'credit_used': 250, 'discount': 0, 'due': 849, 'reason': None}),
  ('normal control 1',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 3,
     'value': 10},
    {'credits': 5000, 'fare': 1000, 'rider_rides': 0}, 100],
   {'credit_used': 900, 'discount': 100, 'due': 0, 'reason': None}),
  ('normal control 2',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 25},
    {'credits': 0, 'fare': 700, 'rider_rides': 5}, 101],
   {'credit_used': 0, 'discount': 0, 'due': 700, 'reason': 'expired'}),
  ('normal control 3',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 25},
    {'credits': 250, 'fare': 1099, 'rider_rides': 0}, 101],
   {'credit_used': 250, 'discount': 0, 'due': 849, 'reason': 'expired'}),
  ('normal control 4',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 25},
    {'credits': 0, 'fare': 1394, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1394, 'reason': 'exhausted'})],
 [('regression: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 50},
    {'credits': 0, 'fare': 3621, 'rider_rides': 0}, 99],
   {'credit_used': 0, 'discount': 0, 'due': 3621, 'reason': None}),
  ('partial repair probe: explicit zero cap',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 3,
     'value': 10},
    {'credits': 250, 'fare': 2400, 'rider_rides': 1}, 100],
   {'credit_used': 250, 'discount': 0, 'due': 2150, 'reason': None}),
  ('second regression',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 1,
     'value': 25},
    {'credits': 5000, 'fare': 1050, 'rider_rides': 1}, 100],
   {'credit_used': 1050, 'discount': 0, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 0,
     'value': 1000},
    {'credits': 0, 'fare': 1099, 'rider_rides': 5}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1099, 'reason': 'exhausted'}),
  ('normal control 2',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 1000},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 0}, 99],
   {'credit_used': 99, 'discount': 1000, 'due': 0, 'reason': None}),
  ('normal control 3',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 0,
     'value': 2500},
    {'credits': 250, 'fare': 1050, 'rider_rides': 1}, 99],
   {'credit_used': 250, 'discount': 0, 'due': 800, 'reason': 'exhausted'}),
  ('normal control 4',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 1000},
    {'credits': 5000, 'fare': 2400, 'rider_rides': 0}, 101],
   {'credit_used': 2400, 'discount': 0, 'due': 0, 'reason': 'expired'})]]
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 paused campaign with cap 0 still gives full percentage discounts.

Validate a promo in order: expired if now_day is after expires_day (the expiry day itself is valid), exhausted if no uses remain, not_first if first-ride-only and the rider has prior rides, min_fare if the fare (cents) is below min_fare. A valid pct promo takes value% of the fare (floored), capped at cap cents when cap is not None (a cap of 0 means a zero discount); a flat promo takes value but never more than the fare. Ride credits then cover up to the remaining fare. Return discount, credit used, amount due and reason.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(promo, ride, now_day):
    reason = None
    if now_day > promo['expires_day']:
        reason = 'expired'
    elif promo['uses_left'] <= 0:
        reason = 'exhausted'
    elif promo['first_ride_only'] and ride['rider_rides'] > 0:
        reason = 'not_first'
    elif ride['fare'] < promo['min_fare']:
        reason = 'min_fare'
    disc = 0
    if reason is None:
        if promo['kind'] == 'pct':
            disc = ride['fare'] * promo['value'] // 100
            if promo['cap'] is not None:
                disc = min(disc, promo['cap'])
        else:
            disc = promo['value']
    credit = min(ride['credits'], ride['fare'] - disc)
    return {'discount': disc, 'credit_used': credit, 'due': ride['fare'] - disc - credit, 'reason': reason}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: flat promo above fare',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 3,
     'value': 2500},
    {'credits': 0, 'fare': 2400, 'rider_rides': 0}, 99],
   {'credit_used': 0, 'discount': 2400, 'due': 0, 'reason': None}),
  ('partial repair probe: flat promo above fare',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 1,
     'value': 2500},
    {'credits': 0, 'fare': 700, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 700, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 2500},
    {'credits': 0, 'fare': 1099, 'rider_rides': 0}, 99],
   {'credit_used': 0, 'discount': 1099, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 1,
     'value': 1000},
    {'credits': 0, 'fare': 1099, 'rider_rides': 1}, 100],
   {'credit_used': 0, 'discount': 1000, 'due': 99, 'reason': None}),
  ('normal control 2',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 0,
     'value': 10},
    {'credits': 0, 'fare': 1000, 'rider_rides': 5}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1000, 'reason': 'exhausted'}),
  ('normal control 3',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 1,
     'value': 1000},
    {'credits': 250, 'fare': 1099, 'rider_rides': 0}, 101],
   {'credit_used': 250, 'discount': 0, 'due': 849, 'reason': 'expired'}),
  ('normal control 4',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 10},
    {'credits': 0, 'fare': 1000, 'rider_rides': 1}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1000, 'reason': 'not_first'})],
 [('regression: flat promo above fare',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 3,
     'value': 2500},
    {'credits': 0, 'fare': 1000, 'rider_rides': 0}, 99],
   {'credit_used': 0, 'discount': 1000, 'due': 0, 'reason': None}),
  ('partial repair probe: flat promo above fare',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 1,
     'value': 1000},
    {'credits': 250, 'fare': 1000, 'rider_rides': 1}, 100],
   {'credit_used': 0, 'discount': 1000, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 3,
     'value': 2500},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 1099, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 3,
     'value': 25},
    {'credits': 0, 'fare': 700, 'rider_rides': 5}, 100],
   {'credit_used': 0, 'discount': 175, 'due': 525, 'reason': None}),
  ('normal control 2',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 500},
    {'credits': 250, 'fare': 700, 'rider_rides': 0}, 101],
   {'credit_used': 250, 'discount': 0, 'due': 450, 'reason': 'expired'}),
  ('normal control 3',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 1000},
    {'credits': 5000, 'fare': 1050, 'rider_rides': 5}, 100],
   {'credit_used': 1050, 'discount': 0, 'due': 0, 'reason': 'not_first'}),
  ('normal control 4',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 0,
     'value': 500},
    {'credits': 0, 'fare': 1000, 'rider_rides': 5}, 99],
   {'credit_used': 0, 'discount': 0, 'due': 1000, 'reason': 'exhausted'})],
 [('regression: flat promo above fare',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 2500},
    {'credits': 5000, 'fare': 1342, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 1342, 'due': 0, 'reason': None}),
  ('partial repair probe: flat promo above fare',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 3,
     'value': 2500},
    {'credits': 5000, 'fare': 821, 'rider_rides': 5}, 100],
   {'credit_used': 0, 'discount': 821, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 2500},
    {'credits': 5000, 'fare': 2400, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 2400, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 500},
    {'credits': 250, 'fare': 1099, 'rider_rides': 0}, 100],
   {'credit_used': 250, 'discount': 500, 'due': 349, 'reason': None}),
  ('normal control 2',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 1000},
    {'credits': 5000, 'fare': 1394, 'rider_rides': 0}, 99],
   {'credit_used': 394, 'discount': 1000, 'due': 0, 'reason': None}),
  ('normal control 3',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 25},
    {'credits': 250, 'fare': 1605, 'rider_rides': 1}, 101],
   {'credit_used': 250, 'discount': 0, 'due': 1355, 'reason': 'expired'}),
  ('normal control 4',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 0,
     'value': 10},
    {'credits': 250, 'fare': 700, 'rider_rides': 5}, 101],
   {'credit_used': 250, 'discount': 0, 'due': 450, 'reason': 'expired'})],
 [('regression: flat promo above fare',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 2500},
    {'credits': 0, 'fare': 2400, 'rider_rides': 0}, 99],
   {'credit_used': 0, 'discount': 2400, 'due': 0, 'reason': None}),
  ('partial repair probe: flat promo above fare',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 1,
     'value': 1000},
    {'credits': 250, 'fare': 1000, 'rider_rides': 1}, 99],
   {'credit_used': 0, 'discount': 1000, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 1,
     'value': 2500},
    {'credits': 5000, 'fare': 1262, 'rider_rides': 1}, 100],
   {'credit_used': 0, 'discount': 1262, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 1000},
    {'credits': 0, 'fare': 1050, 'rider_rides': 5}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1050, 'reason': 'exhausted'}),
  ('normal control 2',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 1,
     'value': 25},
    {'credits': 250, 'fare': 1050, 'rider_rides': 0}, 99],
   {'credit_used': 250, 'discount': 262, 'due': 538, 'reason': None}),
  ('normal control 3',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 2500},
    {'credits': 5000, 'fare': 1050, 'rider_rides': 0}, 101],
   {'credit_used': 1050, 'discount': 0, 'due': 0, 'reason': 'expired'}),
  ('normal control 4',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 3,
     'value': 25},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 1}, 100],
   {'credit_used': 825, 'discount': 274, 'due': 0, 'reason': None})],
 [('regression: flat promo above fare',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 1,
     'value': 2500},
    {'credits': 0, 'fare': 1099, 'rider_rides': 5}, 99],
   {'credit_used': 0, 'discount': 1099, 'due': 0, 'reason': None}),
  ('partial repair probe: flat promo above fare',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 1,
     'value': 1000},
    {'credits': 5000, 'fare': 1000, 'rider_rides': 5}, 99],
   {'credit_used': 0, 'discount': 1000, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 2500},
    {'credits': 5000, 'fare': 1050, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 1050, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 500},
    {'credits': 0, 'fare': 1099, 'rider_rides': 1}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1099, 'reason': 'not_first'}),
  ('normal control 2',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 3,
     'value': 10},
    {'credits': 250, 'fare': 2400, 'rider_rides': 5}, 101],
   {'credit_used': 250, 'discount': 0, 'due': 2150, 'reason': 'expired'}),
  ('normal control 3',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 1,
     'value': 500},
    {'credits': 250, 'fare': 1000, 'rider_rides': 5}, 101],
   {'credit_used': 250, 'discount': 0, 'due': 750, 'reason': 'expired'}),
  ('normal control 4',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 2500},
    {'credits': 250, 'fare': 855, 'rider_rides': 0}, 100],
   {'credit_used': 250, 'discount': 0, 'due': 605, 'reason': 'exhausted'})]]
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

N = 1
observations = []
def solve(promo, ride, now_day):
    reason = None
    if now_day > promo['expires_day']:
        reason = 'expired'
    elif promo['uses_left'] <= 0:
        reason = 'exhausted'
    elif promo['first_ride_only'] and ride['rider_rides'] > 0:
        reason = 'not_first'
    elif ride['fare'] < promo['min_fare']:
        reason = 'min_fare'
    disc = 0
    if reason is None:
        if promo['kind'] == 'pct':
            disc = ride['fare'] * promo['value'] // 100
            if promo['cap'] is not None:
                disc = min(disc, promo['cap'])
        else:
            disc = min(promo['value'], ride['fare'] - 1)
    credit = min(ride['credits'], ride['fare'] - disc)
    return {'discount': disc, 'credit_used': credit, 'due': ride['fare'] - disc - credit, 'reason': reason}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: flat promo above fare',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 3,
     'value': 2500},
    {'credits': 0, 'fare': 2400, 'rider_rides': 0}, 99],
   {'credit_used': 0, 'discount': 2400, 'due': 0, 'reason': None}),
  ('partial repair probe: flat promo above fare',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 1,
     'value': 2500},
    {'credits': 0, 'fare': 700, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 700, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 2500},
    {'credits': 0, 'fare': 1099, 'rider_rides': 0}, 99],
   {'credit_used': 0, 'discount': 1099, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 1,
     'value': 1000},
    {'credits': 0, 'fare': 1099, 'rider_rides': 1}, 100],
   {'credit_used': 0, 'discount': 1000, 'due': 99, 'reason': None}),
  ('normal control 2',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 0,
     'value': 10},
    {'credits': 0, 'fare': 1000, 'rider_rides': 5}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1000, 'reason': 'exhausted'}),
  ('normal control 3',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 1,
     'value': 1000},
    {'credits': 250, 'fare': 1099, 'rider_rides': 0}, 101],
   {'credit_used': 250, 'discount': 0, 'due': 849, 'reason': 'expired'}),
  ('normal control 4',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 10},
    {'credits': 0, 'fare': 1000, 'rider_rides': 1}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1000, 'reason': 'not_first'})],
 [('regression: flat promo above fare',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 3,
     'value': 2500},
    {'credits': 0, 'fare': 1000, 'rider_rides': 0}, 99],
   {'credit_used': 0, 'discount': 1000, 'due': 0, 'reason': None}),
  ('partial repair probe: flat promo above fare',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 1,
     'value': 1000},
    {'credits': 250, 'fare': 1000, 'rider_rides': 1}, 100],
   {'credit_used': 0, 'discount': 1000, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 3,
     'value': 2500},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 1099, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 3,
     'value': 25},
    {'credits': 0, 'fare': 700, 'rider_rides': 5}, 100],
   {'credit_used': 0, 'discount': 175, 'due': 525, 'reason': None}),
  ('normal control 2',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 500},
    {'credits': 250, 'fare': 700, 'rider_rides': 0}, 101],
   {'credit_used': 250, 'discount': 0, 'due': 450, 'reason': 'expired'}),
  ('normal control 3',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 1000},
    {'credits': 5000, 'fare': 1050, 'rider_rides': 5}, 100],
   {'credit_used': 1050, 'discount': 0, 'due': 0, 'reason': 'not_first'}),
  ('normal control 4',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 0,
     'value': 500},
    {'credits': 0, 'fare': 1000, 'rider_rides': 5}, 99],
   {'credit_used': 0, 'discount': 0, 'due': 1000, 'reason': 'exhausted'})],
 [('regression: flat promo above fare',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 2500},
    {'credits': 5000, 'fare': 1342, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 1342, 'due': 0, 'reason': None}),
  ('partial repair probe: flat promo above fare',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 3,
     'value': 2500},
    {'credits': 5000, 'fare': 821, 'rider_rides': 5}, 100],
   {'credit_used': 0, 'discount': 821, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 2500},
    {'credits': 5000, 'fare': 2400, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 2400, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 500},
    {'credits': 250, 'fare': 1099, 'rider_rides': 0}, 100],
   {'credit_used': 250, 'discount': 500, 'due': 349, 'reason': None}),
  ('normal control 2',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 1000},
    {'credits': 5000, 'fare': 1394, 'rider_rides': 0}, 99],
   {'credit_used': 394, 'discount': 1000, 'due': 0, 'reason': None}),
  ('normal control 3',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 25},
    {'credits': 250, 'fare': 1605, 'rider_rides': 1}, 101],
   {'credit_used': 250, 'discount': 0, 'due': 1355, 'reason': 'expired'}),
  ('normal control 4',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 0,
     'value': 10},
    {'credits': 250, 'fare': 700, 'rider_rides': 5}, 101],
   {'credit_used': 250, 'discount': 0, 'due': 450, 'reason': 'expired'})],
 [('regression: flat promo above fare',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 2500},
    {'credits': 0, 'fare': 2400, 'rider_rides': 0}, 99],
   {'credit_used': 0, 'discount': 2400, 'due': 0, 'reason': None}),
  ('partial repair probe: flat promo above fare',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 1,
     'value': 1000},
    {'credits': 250, 'fare': 1000, 'rider_rides': 1}, 99],
   {'credit_used': 0, 'discount': 1000, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 1,
     'value': 2500},
    {'credits': 5000, 'fare': 1262, 'rider_rides': 1}, 100],
   {'credit_used': 0, 'discount': 1262, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 1000},
    {'credits': 0, 'fare': 1050, 'rider_rides': 5}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1050, 'reason': 'exhausted'}),
  ('normal control 2',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 1,
     'value': 25},
    {'credits': 250, 'fare': 1050, 'rider_rides': 0}, 99],
   {'credit_used': 250, 'discount': 262, 'due': 538, 'reason': None}),
  ('normal control 3',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 2500},
    {'credits': 5000, 'fare': 1050, 'rider_rides': 0}, 101],
   {'credit_used': 1050, 'discount': 0, 'due': 0, 'reason': 'expired'}),
  ('normal control 4',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 3,
     'value': 25},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 1}, 100],
   {'credit_used': 825, 'discount': 274, 'due': 0, 'reason': None})],
 [('regression: flat promo above fare',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 1,
     'value': 2500},
    {'credits': 0, 'fare': 1099, 'rider_rides': 5}, 99],
   {'credit_used': 0, 'discount': 1099, 'due': 0, 'reason': None}),
  ('partial repair probe: flat promo above fare',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 1,
     'value': 1000},
    {'credits': 5000, 'fare': 1000, 'rider_rides': 5}, 99],
   {'credit_used': 0, 'discount': 1000, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 2500},
    {'credits': 5000, 'fare': 1050, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 1050, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 500},
    {'credits': 0, 'fare': 1099, 'rider_rides': 1}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1099, 'reason': 'not_first'}),
  ('normal control 2',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 3,
     'value': 10},
    {'credits': 250, 'fare': 2400, 'rider_rides': 5}, 101],
   {'credit_used': 250, 'discount': 0, 'due': 2150, 'reason': 'expired'}),
  ('normal control 3',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 1,
     'value': 500},
    {'credits': 250, 'fare': 1000, 'rider_rides': 5}, 101],
   {'credit_used': 250, 'discount': 0, 'due': 750, 'reason': 'expired'}),
  ('normal control 4',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 2500},
    {'credits': 250, 'fare': 855, 'rider_rides': 0}, 100],
   {'credit_used': 250, 'discount': 0, 'due': 605, 'reason': 'exhausted'})]]
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 25.00 credit code on a 7.00 ride shows -18.00 due.

Validate a promo in order: expired if now_day is after expires_day (the expiry day itself is valid), exhausted if no uses remain, not_first if first-ride-only and the rider has prior rides, min_fare if the fare (cents) is below min_fare. A valid pct promo takes value% of the fare (floored), capped at cap cents when cap is not None (a cap of 0 means a zero discount); a flat promo takes value but never more than the fare. Ride credits then cover up to the remaining fare. Return discount, credit used, amount due and reason.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(promo, ride, now_day):
    reason = None
    if now_day > promo['expires_day']:
        reason = 'expired'
    elif promo['uses_left'] <= 0:
        reason = 'exhausted'
    elif promo['first_ride_only'] and ride['rider_rides'] > 0:
        reason = 'not_first'
    elif ride['fare'] < promo['min_fare']:
        reason = 'min_fare'
    disc = 0
    if reason is None:
        if promo['kind'] == 'pct':
            disc = ride['fare'] * promo['value'] // 100
            if promo['cap'] is not None:
                disc = min(disc, promo['cap'])
        else:
            disc = min(promo['value'], ride['fare'])
    credit = min(ride['credits'], ride['fare'])
    return {'discount': disc, 'credit_used': credit, 'due': ride['fare'] - disc - credit, 'reason': reason}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: credits after discount',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 10},
    {'credits': 5000, 'fare': 2400, 'rider_rides': 1}, 100],
   {'credit_used': 2160, 'discount': 240, 'due': 0, 'reason': None}),
  ('partial repair probe: credits after discount',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 1,
     'value': 50},
    {'credits': 5000, 'fare': 3459, 'rider_rides': 1}, 100],
   {'credit_used': 1730, 'discount': 1729, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 2500},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 0}, 99],
   {'credit_used': 0, 'discount': 1099, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 0,
     'value': 25},
    {'credits': 5000, 'fare': 1000, 'rider_rides': 5}, 100],
   {'credit_used': 1000, 'discount': 0, 'due': 0, 'reason': 'exhausted'}),
  ('normal control 2',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 10},
    {'credits': 250, 'fare': 700, 'rider_rides': 5}, 99],
   {'credit_used': 250, 'discount': 0, 'due': 450, 'reason': 'min_fare'}),
  ('normal control 3',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 1,
     'value': 2500},
    {'credits': 0, 'fare': 700, 'rider_rides': 0}, 101],
   {'credit_used': 0, 'discount': 0, 'due': 700, 'reason': 'expired'}),
  ('normal control 4',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 0,
     'value': 50},
    {'credits': 0, 'fare': 1050, 'rider_rides': 5}, 101],
   {'credit_used': 0, 'discount': 0, 'due': 1050, 'reason': 'expired'})],
 [('regression: credits after discount',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 1,
     'value': 50},
    {'credits': 5000, 'fare': 3646, 'rider_rides': 5}, 100],
   {'credit_used': 1823, 'discount': 1823, 'due': 0, 'reason': None}),
  ('partial repair probe: credits after discount',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 1,
     'value': 2500},
    {'credits': 250, 'fare': 3192, 'rider_rides': 0}, 100],
   {'credit_used': 250, 'discount': 2500, 'due': 442, 'reason': None}),
  ('second regression',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 50},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 0}, 99],
   {'credit_used': 799, 'discount': 300, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 1000},
    {'credits': 0, 'fare': 2400, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 1000, 'due': 1400, 'reason': None}),
  ('normal control 2',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 3,
     'value': 2500},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 5}, 99],
   {'credit_used': 1099, 'discount': 0, 'due': 0, 'reason': 'not_first'}),
  ('normal control 3',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 25},
    {'credits': 0, 'fare': 2400, 'rider_rides': 0}, 99],
   {'credit_used': 0, 'discount': 600, 'due': 1800, 'reason': None}),
  ('normal control 4',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 0,
     'value': 500},
    {'credits': 0, 'fare': 632, 'rider_rides': 5}, 101],
   {'credit_used': 0, 'discount': 0, 'due': 632, 'reason': 'expired'})],
 [('regression: credits after discount',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 1,
     'value': 2500},
    {'credits': 250, 'fare': 1099, 'rider_rides': 1}, 100],
   {'credit_used': 0, 'discount': 1099, 'due': 0, 'reason': None}),
  ('partial repair probe: credits after discount',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 10},
    {'credits': 250, 'fare': 2836, 'rider_rides': 0}, 99],
   {'credit_used': 250, 'discount': 283, 'due': 2303, 'reason': None}),
  ('second regression',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 1000},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 1}, 100],
   {'credit_used': 99, 'discount': 1000, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 10},
    {'credits': 0, 'fare': 1099, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 109, 'due': 990, 'reason': None}),
  ('normal control 2',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 0,
     'value': 500},
    {'credits': 5000, 'fare': 1050, 'rider_rides': 5}, 100],
   {'credit_used': 1050, 'discount': 0, 'due': 0, 'reason': 'exhausted'}),
  ('normal control 3',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 50},
    {'credits': 5000, 'fare': 1275, 'rider_rides': 0}, 101],
   {'credit_used': 1275, 'discount': 0, 'due': 0, 'reason': 'expired'}),
  ('normal control 4',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 3,
     'value': 500},
    {'credits': 0, 'fare': 1050, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 500, 'due': 550, 'reason': None})],
 [('regression: credits after discount',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 2500},
    {'credits': 5000, 'fare': 2400, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 2400, 'due': 0, 'reason': None}),
  ('partial repair probe: credits after discount',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 500},
    {'credits': 5000, 'fare': 2400, 'rider_rides': 0}, 99],
   {'credit_used': 1900, 'discount': 500, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 2500},
    {'credits': 250, 'fare': 1000, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 1000, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 3,
     'value': 25},
    {'credits': 0, 'fare': 1050, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 262, 'due': 788, 'reason': None}),
  ('normal control 2',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 0,
     'value': 50},
    {'credits': 5000, 'fare': 1000, 'rider_rides': 1}, 100],
   {'credit_used': 1000, 'discount': 0, 'due': 0, 'reason': 'exhausted'}),
  ('normal control 3',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 1,
     'value': 2500},
    {'credits': 0, 'fare': 700, 'rider_rides': 1}, 99],
   {'credit_used': 0, 'discount': 0, 'due': 700, 'reason': 'not_first'}),
  ('normal control 4',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 2500},
    {'credits': 5000, 'fare': 1050, 'rider_rides': 1}, 100],
   {'credit_used': 1050, 'discount': 0, 'due': 0, 'reason': 'exhausted'})],
 [('regression: credits after discount',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 1,
     'value': 25},
    {'credits': 5000, 'fare': 1050, 'rider_rides': 1}, 99],
   {'credit_used': 788, 'discount': 262, 'due': 0, 'reason': None}),
  ('partial repair probe: credits after discount',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 1,
     'value': 25},
    {'credits': 250, 'fare': 3234, 'rider_rides': 0}, 99],
   {'credit_used': 250, 'discount': 808, 'due': 2176, 'reason': None}),
  ('second regression',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 1,
     'value': 25},
    {'credits': 5000, 'fare': 2400, 'rider_rides': 0}, 100],
   {'credit_used': 1800, 'discount': 600, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 3,
     'value': 500},
    {'credits': 0, 'fare': 1050, 'rider_rides': 0}, 101],
   {'credit_used': 0, 'discount': 0, 'due': 1050, 'reason': 'expired'}),
  ('normal control 2',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 50},
    {'credits': 0, 'fare': 1000, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1000, 'reason': 'exhausted'}),
  ('normal control 3',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 10},
    {'credits': 0, 'fare': 2400, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 240, 'due': 2160, 'reason': None}),
  ('normal control 4',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 1000},
    {'credits': 0, 'fare': 2400, 'rider_rides': 5}, 99],
   {'credit_used': 0, 'discount': 0, 'due': 2400, 'reason': 'not_first'})]]
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

N = 1
observations = []
def solve(promo, ride, now_day):
    reason = None
    if now_day > promo['expires_day']:
        reason = 'expired'
    elif promo['uses_left'] <= 0:
        reason = 'exhausted'
    elif promo['first_ride_only'] and ride['rider_rides'] > 0:
        reason = 'not_first'
    elif ride['fare'] < promo['min_fare']:
        reason = 'min_fare'
    disc = 0
    if reason is None:
        if promo['kind'] == 'pct':
            disc = ride['fare'] * promo['value'] // 100
            if promo['cap'] is not None:
                disc = min(disc, promo['cap'])
        else:
            disc = min(promo['value'], ride['fare'])
    credit = min(ride['credits'], ride['fare'] - disc) if disc == 0 else 0
    return {'discount': disc, 'credit_used': credit, 'due': ride['fare'] - disc - credit, 'reason': reason}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: credits after discount',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 10},
    {'credits': 5000, 'fare': 2400, 'rider_rides': 1}, 100],
   {'credit_used': 2160, 'discount': 240, 'due': 0, 'reason': None}),
  ('partial repair probe: credits after discount',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 1,
     'value': 50},
    {'credits': 5000, 'fare': 3459, 'rider_rides': 1}, 100],
   {'credit_used': 1730, 'discount': 1729, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 2500},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 0}, 99],
   {'credit_used': 0, 'discount': 1099, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 0,
     'value': 25},
    {'credits': 5000, 'fare': 1000, 'rider_rides': 5}, 100],
   {'credit_used': 1000, 'discount': 0, 'due': 0, 'reason': 'exhausted'}),
  ('normal control 2',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 10},
    {'credits': 250, 'fare': 700, 'rider_rides': 5}, 99],
   {'credit_used': 250, 'discount': 0, 'due': 450, 'reason': 'min_fare'}),
  ('normal control 3',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 1,
     'value': 2500},
    {'credits': 0, 'fare': 700, 'rider_rides': 0}, 101],
   {'credit_used': 0, 'discount': 0, 'due': 700, 'reason': 'expired'}),
  ('normal control 4',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 0,
     'value': 50},
    {'credits': 0, 'fare': 1050, 'rider_rides': 5}, 101],
   {'credit_used': 0, 'discount': 0, 'due': 1050, 'reason': 'expired'})],
 [('regression: credits after discount',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 1,
     'value': 50},
    {'credits': 5000, 'fare': 3646, 'rider_rides': 5}, 100],
   {'credit_used': 1823, 'discount': 1823, 'due': 0, 'reason': None}),
  ('partial repair probe: credits after discount',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 1,
     'value': 2500},
    {'credits': 250, 'fare': 3192, 'rider_rides': 0}, 100],
   {'credit_used': 250, 'discount': 2500, 'due': 442, 'reason': None}),
  ('second regression',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 50},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 0}, 99],
   {'credit_used': 799, 'discount': 300, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 1000},
    {'credits': 0, 'fare': 2400, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 1000, 'due': 1400, 'reason': None}),
  ('normal control 2',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 3,
     'value': 2500},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 5}, 99],
   {'credit_used': 1099, 'discount': 0, 'due': 0, 'reason': 'not_first'}),
  ('normal control 3',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 25},
    {'credits': 0, 'fare': 2400, 'rider_rides': 0}, 99],
   {'credit_used': 0, 'discount': 600, 'due': 1800, 'reason': None}),
  ('normal control 4',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 0,
     'value': 500},
    {'credits': 0, 'fare': 632, 'rider_rides': 5}, 101],
   {'credit_used': 0, 'discount': 0, 'due': 632, 'reason': 'expired'})],
 [('regression: credits after discount',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 1,
     'value': 2500},
    {'credits': 250, 'fare': 1099, 'rider_rides': 1}, 100],
   {'credit_used': 0, 'discount': 1099, 'due': 0, 'reason': None}),
  ('partial repair probe: credits after discount',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 10},
    {'credits': 250, 'fare': 2836, 'rider_rides': 0}, 99],
   {'credit_used': 250, 'discount': 283, 'due': 2303, 'reason': None}),
  ('second regression',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 1000},
    {'credits': 5000, 'fare': 1099, 'rider_rides': 1}, 100],
   {'credit_used': 99, 'discount': 1000, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 10},
    {'credits': 0, 'fare': 1099, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 109, 'due': 990, 'reason': None}),
  ('normal control 2',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 0,
     'value': 500},
    {'credits': 5000, 'fare': 1050, 'rider_rides': 5}, 100],
   {'credit_used': 1050, 'discount': 0, 'due': 0, 'reason': 'exhausted'}),
  ('normal control 3',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 50},
    {'credits': 5000, 'fare': 1275, 'rider_rides': 0}, 101],
   {'credit_used': 1275, 'discount': 0, 'due': 0, 'reason': 'expired'}),
  ('normal control 4',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 3,
     'value': 500},
    {'credits': 0, 'fare': 1050, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 500, 'due': 550, 'reason': None})],
 [('regression: credits after discount',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 2500},
    {'credits': 5000, 'fare': 2400, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 2400, 'due': 0, 'reason': None}),
  ('partial repair probe: credits after discount',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 3,
     'value': 500},
    {'credits': 5000, 'fare': 2400, 'rider_rides': 0}, 99],
   {'credit_used': 1900, 'discount': 500, 'due': 0, 'reason': None}),
  ('second regression',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 3,
     'value': 2500},
    {'credits': 250, 'fare': 1000, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 1000, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 3,
     'value': 25},
    {'credits': 0, 'fare': 1050, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 262, 'due': 788, 'reason': None}),
  ('normal control 2',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 0,
     'value': 50},
    {'credits': 5000, 'fare': 1000, 'rider_rides': 1}, 100],
   {'credit_used': 1000, 'discount': 0, 'due': 0, 'reason': 'exhausted'}),
  ('normal control 3',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 1,
     'value': 2500},
    {'credits': 0, 'fare': 700, 'rider_rides': 1}, 99],
   {'credit_used': 0, 'discount': 0, 'due': 700, 'reason': 'not_first'}),
  ('normal control 4',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 2500},
    {'credits': 5000, 'fare': 1050, 'rider_rides': 1}, 100],
   {'credit_used': 1050, 'discount': 0, 'due': 0, 'reason': 'exhausted'})],
 [('regression: credits after discount',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 1,
     'value': 25},
    {'credits': 5000, 'fare': 1050, 'rider_rides': 1}, 99],
   {'credit_used': 788, 'discount': 262, 'due': 0, 'reason': None}),
  ('partial repair probe: credits after discount',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 1,
     'value': 25},
    {'credits': 250, 'fare': 3234, 'rider_rides': 0}, 99],
   {'credit_used': 250, 'discount': 808, 'due': 2176, 'reason': None}),
  ('second regression',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'pct',
     'min_fare': 0,
     'uses_left': 1,
     'value': 25},
    {'credits': 5000, 'fare': 2400, 'rider_rides': 0}, 100],
   {'credit_used': 1800, 'discount': 600, 'due': 0, 'reason': None}),
  ('normal control 1',
   [{'cap': 0,
     'expires_day': 100,
     'first_ride_only': False,
     'kind': 'flat',
     'min_fare': 0,
     'uses_left': 3,
     'value': 500},
    {'credits': 0, 'fare': 1050, 'rider_rides': 0}, 101],
   {'credit_used': 0, 'discount': 0, 'due': 1050, 'reason': 'expired'}),
  ('normal control 2',
   [{'cap': None,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 1050,
     'uses_left': 0,
     'value': 50},
    {'credits': 0, 'fare': 1000, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 0, 'due': 1000, 'reason': 'exhausted'}),
  ('normal control 3',
   [{'cap': 800,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'pct',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 10},
    {'credits': 0, 'fare': 2400, 'rider_rides': 0}, 100],
   {'credit_used': 0, 'discount': 240, 'due': 2160, 'reason': None}),
  ('normal control 4',
   [{'cap': 300,
     'expires_day': 100,
     'first_ride_only': True,
     'kind': 'flat',
     'min_fare': 1000,
     'uses_left': 1,
     'value': 1000},
    {'credits': 0, 'fare': 2400, 'rider_rides': 5}, 99],
   {'credit_used': 0, 'discount': 0, 'due': 2400, 'reason': 'not_first'})]]
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 rider with credits and a promo is charged negative amounts and loses credit balance.

Validate a promo in order: expired if now_day is after expires_day (the expiry day itself is valid), exhausted if no uses remain, not_first if first-ride-only and the rider has prior rides, min_fare if the fare (cents) is below min_fare. A valid pct promo takes value% of the fare (floored), capped at cap cents when cap is not None (a cap of 0 means a zero discount); a flat promo takes value but never more than the fare. Ride credits then cover up to the remaining fare. Return discount, credit used, amount due and reason.

split: open-access