-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (112 loc) · 3.11 KB
/
main.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
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
130
131
132
133
134
135
136
137
138
package main
import (
"bufio"
"flag"
"fmt"
"os"
"reflect"
"strconv"
"strings"
)
var (
methodP *string
)
func parseFlags() {
methodP = flag.String("method", "all", "The method/part that should be run, valid are p1,p2 and test")
flag.Parse()
}
func main() {
parseFlags()
switch *methodP {
case "all":
fmt.Println("Silver:" + PartOne("input"))
fmt.Println("Gold:" + PartTwo("input"))
case "p1":
fmt.Println("Silver:" + PartOne("input"))
case "p2":
fmt.Println("Gold:" + PartTwo("input"))
}
}
func PartOne(filename string) string {
input := readInput(filename)
totalPairs := 0
for _, v := range input {
// get both assignments
assignments := strings.Split(v, ",")
firstElf := assignments[0]
secondElf := assignments[1]
firstElfAssignments := strings.Split(firstElf, "-")
firstElfStart, _ := strconv.Atoi(firstElfAssignments[0])
firstElfEnd, _ := strconv.Atoi(firstElfAssignments[1])
firstElfMap := make(map[int]bool)
for i := firstElfStart; i < firstElfEnd+1; i++ {
firstElfMap[i] = true
}
secondElfAssignments := strings.Split(secondElf, "-")
secondElfStart, _ := strconv.Atoi(secondElfAssignments[0])
secondElfEnd, _ := strconv.Atoi(secondElfAssignments[1])
secondElfMap := make(map[int]bool)
for i := secondElfStart; i < secondElfEnd+1; i++ {
secondElfMap[i] = true
}
// Now get intersection
intersectionMap := make(map[int]bool)
for section := range firstElfMap {
if secondElfMap[section] {
intersectionMap[section] = true
}
}
// check if it is equal to either of the elfs assignments
if reflect.DeepEqual(intersectionMap, firstElfMap) || reflect.DeepEqual(intersectionMap, secondElfMap) {
totalPairs++
}
}
return strconv.Itoa(totalPairs)
}
func PartTwo(filename string) string {
input := readInput(filename)
totalPairs := 0
for _, v := range input {
// get both assignments
assignments := strings.Split(v, ",")
firstElf := assignments[0]
secondElf := assignments[1]
firstElfAssignments := strings.Split(firstElf, "-")
firstElfStart, _ := strconv.Atoi(firstElfAssignments[0])
firstElfEnd, _ := strconv.Atoi(firstElfAssignments[1])
firstElfMap := make(map[int]bool)
for i := firstElfStart; i < firstElfEnd+1; i++ {
firstElfMap[i] = true
}
secondElfAssignments := strings.Split(secondElf, "-")
secondElfStart, _ := strconv.Atoi(secondElfAssignments[0])
secondElfEnd, _ := strconv.Atoi(secondElfAssignments[1])
secondElfMap := make(map[int]bool)
for i := secondElfStart; i < secondElfEnd+1; i++ {
secondElfMap[i] = true
}
// Now get intersection
intersectionMap := make(map[int]bool)
for section := range firstElfMap {
if secondElfMap[section] {
intersectionMap[section] = true
}
}
// check if we have any overlap
if len(intersectionMap) > 0 {
totalPairs++
}
}
return strconv.Itoa(totalPairs)
}
// Read data from input.txt
// Return the string, so that we can deal with it however
func readInput(filename string) []string {
var input []string
f, _ := os.Open(filename + ".txt")
scanner := bufio.NewScanner(f)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}