repl.it
@pyelias/

AOC 2019-4

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import collections

BASE = 10

def count_all_without_same(count, lo_dig):
    if count < 1:
        return 1
    elif count == 1:
        return BASE - lo_dig

    res = 0
    for d in range(lo_dig, BASE):
        res += count_all_without_same(count - 1, d)
    return res

def count_without_same(hi, lo_dig=1):
    if len(hi) < 1:
        return 0
    elif len(hi) == 1:
        return max(0, hi[0] - lo_dig)
    
    res = 0
    res += count_without_same(hi[1:], max(lo_dig, hi[0]))
    for d in range(lo_dig, hi[0]):
        res += count_all_without_same(len(hi) - 1, d)

    return res

def count_all_with_same(count, lo_dig):
    if count < 2:
        return 0
    
    res = 0
    for d in range(lo_dig, BASE):
        res += count_all_with_same(count - 1, d + 1)
        res += count_all_without_same(count - 2, d)
    return res

def count_with_same(hi, lo_dig=1):
    if len(hi) < 2:
        return 0
    
    res = 0
    if hi[0] == hi[1]:
        res += count_without_same(hi[2:], max(lo_dig, hi[0]))
    elif hi[0] < hi[1]:
        res += count_all_without_same(len(hi) - 2, hi[0])
    
    res += count_with_same(hi[1:], max(lo_dig, hi[0] + 1))
    for d in range(lo_dig, hi[0]):
        res += count_all_without_same(len(hi) - 2, d)
        res += count_all_with_same(len(hi) - 1, d + 1)

    return res

def valid(l):
    curr = 0
    for d in l:
        if d < curr:
            return False
        curr = d
    counts = collections.Counter(l)
    return 2 in counts.values()

c = 0
for i in range(123257, 647016):
    if valid(list(map(int, str(i)))):
        c += 1
print(c)
# idk why this is broken
print(count_with_same([6, 4, 7, 0, 1, 6]))
print(count_with_same([6, 4, 7, 0, 1, 6]) - count_with_same([1, 2, 3, 2, 5, 7]))
?