-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday02.go
More file actions
80 lines (65 loc) · 1.91 KB
/
day02.go
File metadata and controls
80 lines (65 loc) · 1.91 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
package main
import (
"flag"
"fmt"
)
func catchUserInput() (string, byte, bool) {
var debug bool
filenamePtr := flag.String("file", "testInput.txt", "Filename containing the program to run")
execPartPtr := flag.String("part", "a", "Which part of the puzzle do you want to calc (a or b)")
flag.BoolVar(&debug, "debug", false, "Turn debug on")
flag.Parse()
switch *execPartPtr {
case "a":
return *filenamePtr, 'a', debug
case "b":
return *filenamePtr, 'b', debug
default:
return *filenamePtr, 'z', debug
}
}
func checkPasswords(filename string, part byte, debug bool) int {
var minNumber, maxNumber int
var passwordChar rune
var password string
var loopCharCount int
var correctPasswordCount int = 0
puzzleInput, _ := readFile(filename)
for _, passwordLine := range puzzleInput {
fmt.Sscanf(passwordLine, "%d-%d %c: %s", &minNumber, &maxNumber, &passwordChar, &password)
if debug {
fmt.Println(passwordLine)
fmt.Printf("min: %d max: %d char: %c password: %s\n", minNumber, maxNumber, passwordChar, password)
}
if part == 'a' {
loopCharCount = 0
for _, loopChar := range password {
if loopChar == passwordChar {
loopCharCount++
}
}
if (loopCharCount >= minNumber) && (loopCharCount <= maxNumber) {
correctPasswordCount++
}
} else {
if (password[minNumber-1] == byte(passwordChar)) && (password[maxNumber-1] != byte(passwordChar)) {
correctPasswordCount++
} else if (password[minNumber-1] != byte(passwordChar)) && (password[maxNumber-1] == byte(passwordChar)) {
correctPasswordCount++
}
}
if debug {
fmt.Println("Char appeared: ", loopCharCount)
}
}
return correctPasswordCount
}
// Main routine
func main() {
filenamePtr, execPart, debug := catchUserInput()
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
} else {
fmt.Printf("Result is: %d\n", checkPasswords(filenamePtr, execPart, debug))
}
}