-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
87 lines (69 loc) · 1.35 KB
/
main.go
File metadata and controls
87 lines (69 loc) · 1.35 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"encoding/csv"
"fmt"
"math"
"os"
"path"
"strconv"
)
func parse() (int, int) {
input, err := os.Open(path.Join("2019", "4", "input.txt"))
if err != nil {
panic(err)
}
csvReader := csv.NewReader(input)
row, err := csvReader.Read()
if err != nil {
panic(err)
}
min, err := strconv.Atoi(row[0])
if err != nil {
panic(err)
}
max, err := strconv.Atoi(row[1])
if err != nil {
panic(err)
}
return min, max
}
func intAtIndex(val, index int) int {
return (val / int(math.Pow(10, float64(5-index)))) % 10
}
func numPasswords(min, max int) int {
ret := 0
for i := min; i <= max; i++ {
double := false
for j := 1; j < 6; j++ {
if intAtIndex(i, j) == intAtIndex(i, j-1) {
double = true
break
}
}
if !double {
continue
}
nonDecreasing := true
for j := 0; j < 6; j++ {
if intAtIndex(i, j) < intAtIndex(i, j-1) {
nonDecreasing = false
break
}
}
if !nonDecreasing {
continue
}
ret += 1
}
return ret
}
func main() {
fmt.Printf("%d%d%d%d%d%d\n", intAtIndex(123456, 0), intAtIndex(123456, 1),
intAtIndex(123456, 2), intAtIndex(123456, 3), intAtIndex(123456, 4),
intAtIndex(123456, 5))
fmt.Println(numPasswords(111111, 111111))
fmt.Println(numPasswords(223450, 223450))
fmt.Println(numPasswords(123789, 123789))
min, max := parse()
fmt.Println(numPasswords(min, max))
}