Wednesday, September 27, 2017

USACO Combination Lock Problem

USACO Combination Lock Problem

USACO 2013 November Contest, Bronze

Problem 1. Combination Lock

http://www.usaco.org/index.php?page=nov13problems

In [1]:
import itertools

def dial_overlap(F,M):
    workfset = set(map(lambda x: x%N,range(F-2,F+3)))
    workmset = set(map(lambda x: x%N,range(M-2,M+3)))
    return(workfset.intersection(workmset))
In [2]:
N=100

f1,f2,f3 = (1,2,3)
m1,m2,m3 = (97,98,99)
In [3]:
o1set=dial_overlap(f1,m1)
o2set=dial_overlap(f2,m2)
o3set=dial_overlap(f3,m3)

overlapping=0
if len(o1set)>0 and len(o2set)>0 and len(o3set)>0:
    for combo in itertools.product(o1set,o2set,o3set):
        overlapping += 1
        # these combinations replace N with zero
        print(combo)

print(250-overlapping, " unique combinations")
    
(99, 0, 1)
249  unique combinations