Skip to content

Instantly share code, notes, and snippets.

View jason-ash's full-sized avatar

Jason Ash jason-ash

View GitHub Profile
@jason-ash
jason-ash / chocolates.py
Created October 5, 2020 18:17
Solves the fivethirtyeight Riddler from Oct 2, 2020.
from typing import Optional
def model(dark: int, milk: int, last: Optional[str] = None) -> float:
"""
Solves the Riddler Classic using dynamic programming. We start with a given number
of chocolates in the bag, and we draw one at a time. We track the last chocolate we
drew, and if we draw the same one, we eat it. Otherwise, we put it back in the bag
and reset the "last" value to None.
"""
Solves the Riddler Classic from October 2, 2020.
https://fivethirtyeight.com/features/can-you-eat-all-the-chocolates/
"""
from __future__ import annotations
from functools import lru_cache
from typing import List, NamedTuple, Optional, Tuple
class ChocolateBag(NamedTuple):
@jason-ash
jason-ash / 20181026-riddler.py
Created October 28, 2018 01:35
Solution to the fivethirtyeight riddler from Oct 26, 2018
import numpy as np
from itertools import groupby, product
def model():
"""
Solves the Riddler Classic from October 26, 2018.
First, generate all valid hands. We only care about suits,
so we represent them using numbers 0, 1, 2, 3. There are
4096 unique hands we could generate, such as [0,0,0,0,0,0],