Two Wrongs

Box Cat Brain Teaser

Box Cat Brain Teaser

normalise <- function(m) (m / apply(m, 2, sum))

boxes <- matrix(rep(0.2, 5), nrow=5)

jumping <- matrix(0, nrow=5, ncol=5)
jumping[abs(row(jumping) - col(jumping)) == 1] <- 1
jumping <- normalise(jumping)

daynight <- function(cat_position, peek_box) {
    cat_position[peek_box,] <- 0
    cat_position <- normalise(cat_position)
    print(paste('after peeking in ', peek_box, ':'))
    print(cat_position)
    next_position <- jumping %*% cat_position
    next_position <- normalise(next_position)
    print('after jumping:')
    print(next_position)
}

search <- function(peek_plan) {
    current <- boxes
    for (peek_box in peek_plan) {
        current <- daynight(current, peek_box)
    }
}

## search(c(2,3,4,4,3))