-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday04.go
More file actions
129 lines (111 loc) · 2.79 KB
/
day04.go
File metadata and controls
129 lines (111 loc) · 2.79 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"AdventOfCode-go/advent2017/utils"
"fmt"
"strings"
)
func isAnagram(firstWord string, secondWord string, debug bool) bool {
var wordExists bool
var wordMatches bool = true
firstWordMap := make(map[rune]int)
secondWordMap := make(map[rune]int)
if debug {
fmt.Println("===============")
fmt.Printf("Checking %s against %s\n", firstWord, secondWord)
}
for _, i := range firstWord {
_, wordExists = firstWordMap[i]
if wordExists {
firstWordMap[i]++
} else {
firstWordMap[i] = 1
}
}
for _, j := range secondWord {
_, wordExists = secondWordMap[j]
if wordExists {
secondWordMap[j]++
} else {
secondWordMap[j] = 1
}
}
if debug {
fmt.Println("First Word Map:", firstWordMap)
fmt.Println("Second Word Map:", secondWordMap)
}
for i := range firstWordMap {
if firstWordMap[i] != secondWordMap[i] {
wordMatches = false
}
}
if debug {
fmt.Println(wordMatches)
}
return wordMatches
}
func isValidPassphrase(passphrase string, part byte, debug bool) bool {
//var wordValue int
var wordExists bool
wordsMap := make(map[string]int)
wordsList := strings.Split(passphrase, " ")
for _, i := range wordsList {
if debug {
fmt.Printf("Word: %s Passphrase: %s\n", i, passphrase)
}
//wordsMap[i]++
_, wordExists = wordsMap[i]
if wordExists {
return false
} else {
wordsMap[i]++
}
}
// If this is part a then if no words are repeated, this is a valid passcode
if part == 'a' {
return true
}
/*
In part b, a valid passphrase must contain no two words
that are anagrams of each other - that is, a passphrase is invalid if any
word's letters can be rearranged to form any other word in the passphrase.
*/
for wordPos, firstWord := range wordsList {
//fmt.Printf("wordPos: %d word: %s\n", wordPos, firstWord)
for _, secondWord := range wordsList[wordPos+1:] {
// To be an anagram the lengths of the words must be the same
if len(firstWord) == len(secondWord) {
if secondWord != "" {
// Loop through the letters in firstWord
if debug {
fmt.Println("Testing word:", secondWord)
}
if isAnagram(firstWord, secondWord, debug) {
return false
}
}
}
}
}
return true
}
func countValidPassphrases(filename string, part byte, debug bool) int {
var numberValid int
puzzleInput, _ := utils.ReadFile(filename)
// Loop through passphrases
// if valid passphrase then count++
for i := 0; i < len(puzzleInput); i++ {
if isValidPassphrase(puzzleInput[i], part, debug) {
numberValid++
}
}
return numberValid
}
// Main routine
func main() {
filenamePtr, execPart, debug := utils.CatchUserInput()
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
} else {
fmt.Printf("Result is: %d\n", countValidPassphrases(filenamePtr, execPart, debug))
}
}