-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.go
99 lines (86 loc) · 2.46 KB
/
day02.go
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
package main
import (
"fmt"
"io/ioutil"
"log"
"path/filepath"
"strconv"
"strings"
)
// Read the input file.
func getInput() []string {
filepath, err := filepath.Abs("2020/inputs/day02.txt")
if err != nil {
log.Fatal(err)
}
inputVal, err := ioutil.ReadFile(filepath)
if err != nil {
log.Fatal(err)
}
return strings.Split(string(inputVal), "\n")
}
// Count the number of true results in a slice.
func nTrue(results []bool) int {
n := 0
for _, element := range results {
if element {
n++
}
}
return n
}
// Password policy specifies occurrence thresholds (min, max)
// for the provided (letter).
func partOne(inputVal []string) (int, int) {
valid := 0
for _, element := range inputVal {
// Parse the input string into its component parts.
inputString := strings.Split(element, " ")
minMax := strings.Split(inputString[0], "-")
letter := strings.Replace(inputString[1], ":", "", 1)
password := inputString[2]
// Convert the min/max thresholds to integers
min, _ := strconv.Atoi(minMax[0])
max, _ := strconv.Atoi(minMax[1])
// Evaluate the password
nOccurrences := strings.Count(password, letter)
if nOccurrences >= min && nOccurrences <= max {
valid++
}
}
invalid := len(inputVal) - valid
return valid, invalid
}
// Password policy specifies index positions (position1, position2)
// for the provided (letter).
func partTwo(inputVal []string) (int, int) {
valid := 0
for _, element := range inputVal {
var results []bool
// Parse the input string into its component parts.
inputString := strings.Split(element, " ")
indexPositions := strings.Split(inputString[0], "-")
letter := strings.Replace(inputString[1], ":", "", 1)
password := inputString[2]
// Convert the required index positions to integers
// the policy is indexed at 1 so we need to subtract 1.
position1, _ := strconv.Atoi(indexPositions[0])
position2, _ := strconv.Atoi(indexPositions[1])
// Evaluate the password
results = append(results, string(password[position1-1]) == letter)
results = append(results, string(password[position2-1]) == letter)
n := nTrue(results)
if n == 1 {
valid++
}
}
invalid := len(inputVal) - valid
return valid, invalid
}
func main() {
inputVal := getInput()
partOneValid, partOneInvalid := partOne(inputVal)
partTwoValid, partTwoInvalid := partTwo(inputVal)
fmt.Printf("PartOne - Valid: %d, Invalid: %d\n", partOneValid, partOneInvalid)
fmt.Printf("PartTwo - Valid: %d, Invalid: %d\n", partTwoValid, partTwoInvalid)
}