aboutsummaryrefslogtreecommitdiff
path: root/challenge-113/abigail/awk/ch-1.awk
blob: 950a1052d0fbde1037e599774e00b1e6a3fd325e (plain)
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
#!/usr/bin/awk

#
# See ../README.md
#

#
# Run as: awk -f ch-1.awk < input-file
#

#     
# For a description of the algorithm, and the proof why this is correct:
# https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html          
# 

BEGIN {
    split ("1 2 1 2 5 2 1 2 1", gcds)
}

{
    N = $1
    D = $2
    if (D == 0) {
        print (N >= 100 || N % 10 == 0 ? 1 : 0)
        next
    }
    if (N >= 10 * D) {
        print 1
        next
    }
    for (i = 0; i < D / gcds [D]; i ++) {
        T = N - 10 * i - D
        if (T >= 0 && T % D == 0) {
            print 1
            next
        }
    }
    print 0
}