Skip to content

Instantly share code, notes, and snippets.

@jfkelley
jfkelley / 538_splitting_spheres.scala
Created June 20, 2020 18:32
Solves 538's riddler class for Jun 19, 2020
// All of the ways that a subset of xs can sum to target
def sumTo(xs: List[Long], target: Long, acc: Set[Long]): LazyList[Set[Long]] = {
if (target == 0) {
LazyList(acc)
} else if (target < 0) {
LazyList.empty
} else xs match {
case head :: rest => {
sumTo(rest, target - head, acc + head) ++ sumTo(rest, target, acc)
}
@jfkelley
jfkelley / 538_coin_flip_game.py
Last active February 21, 2020 20:35
Solves 538's riddler classic for 2/21/2020
def pWin(p, f, t, cache):
if (f, t) in cache:
return cache[(f, t)]
if t > f:
return 1.0
if t < f * -2:
return 0.0
one = p * pWin(p, f-1, t+1, cache) + (1-p) * pWin(p, f-1, t-1, cache)
two = 0.5 * pWin(p, f-1, t+2, cache) + 0.5 * pWin(p, f-1, t-2, cache)
x = max(one, two)
@jfkelley
jfkelley / 538_count_bridge_bids.py
Created February 22, 2019 19:24
Solves 538 Riddler Classic for Feb 22 2019
counts = {}
enable_doubling = False # change to true for extra credit
def count_possible_bids(current_bid, n_passes, n_doubles):
if (n_passes == 3 and current_bid != -1) or n_passes == 4:
return 1
if (current_bid, n_passes, n_doubles) in counts:
return counts[(current_bid, n_passes, n_doubles)]
result = 0
for n in range(current_bid + 1, 28):
@jfkelley
jfkelley / 538_liar_ages.py
Created February 22, 2019 18:59
Solves 538 Riddler Express for Feb 22 2019
statements = [
('a', 'b', lambda x: x > 20),
('b', 'c', lambda x: x > 18),
('c', 'd', lambda x: x < 22),
('d', 'e', lambda x: x != 17),
('e', 'a', lambda x: x > 21),
('a', 'd', lambda x: x > 16),
('b', 'e', lambda x: x < 20),
('c', 'a', lambda x: x == 19),
('d', 'b', lambda x: x == 20),
def threes(board):
for x in [0, 1, 2]:
yield [board[x][0], board[x][1], board[x][2]]
yield [board[0][x], board[1][x], board[2][x]]
yield [board[0][0], board[1][1], board[2][2]]
yield [board[0][2], board[1][1], board[2][0]]
def is_done(board):
for th in threes(board):
if th[0] is not None and th[0] == th[1] and th[1] == th[2]:

Keybase proof

I hereby claim:

  • I am jfkelley on github.
  • I am joefkelley (https://keybase.io/joefkelley) on keybase.
  • I have a public key whose fingerprint is 37CA 2BA0 7ECE 3F14 5B87 4436 54B6 4CF0 1AEE D71B

To claim this, I am signing this object:

200
J1
J2
J3
J4
J5
J6
J7
J8
J9