aboutsummaryrefslogtreecommitdiff
path: root/2019/four.go
blob: 7c901dcf3877f55d340b856440def21a5c3a8f89 (plain) (blame)
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
package main

import (
	"fmt"
	"io/ioutil"
	"math"
	"os"
	"strconv"
)

func main() {
	smallest, largest, total, details := -1, -1, 0, 0
	if len(os.Args) < 2 {
		panic("runtime error: missing operand")
	}
	file, err := ioutil.ReadFile(os.Args[1])
	if err != nil {
		panic(err)
	}
	for i := 0; i < len(file); i++ {
		if string(file[i]) == "-" {
			smallest, err = strconv.Atoi(string(file[0:i]))
			if err != nil {
				panic(err)
			}
			largest, err = strconv.Atoi(string(file[i+1 : len(file)-1]))
			if err != nil {
				panic(err)
			}
			break
		}
	}

	for i := smallest; i <= largest; i++ {
		if criteria(i, false) {
			total++
		}
		if criteria(i, true) {
			details++
		}
	}
	fmt.Println(total)
	fmt.Println(details)
}

func criteria(password int, stage bool) bool {
	prev := 0
	for i := 0; i < 6; i++ {
		if password/int(math.Pow(10, float64(5-i)))%10 < prev {
			return false
		}
		prev = password / int(math.Pow(10, float64(5-i))) % 10
	}
	pascii := strconv.Itoa(password)
	for i := 0; i < len(pascii)-1; i++ {
		if string(pascii[i]) == string(pascii[i+1]) {
			if !(len(pascii)-i > 2 && string(pascii[i]) == string(pascii[i+2])) {
				if !stage { // Gross hack
					return true
				}
				if !(i > 0 && string(pascii[i]) == string(pascii[i-1])) {
					return true
				}
			}
		}
	}
	return false
}