-
Notifications
You must be signed in to change notification settings - Fork 1
/
day04_cleaning.go
134 lines (106 loc) · 3.02 KB
/
day04_cleaning.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
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
func findCompletePairsOverlap(filePath string) int {
input, _ := os.ReadFile(filePath)
lines := strings.Split(strings.TrimSpace(string(input)), "\n")
totalOverlaps := 0
for _, line := range lines {
sections := strings.Split(line, ",")
elf1Range := sections[0]
inclusiveRange := strings.Split(elf1Range, "-")
elf1Lo, _ := strconv.Atoi(inclusiveRange[0])
elf1Hi, _ := strconv.Atoi(inclusiveRange[1])
elf2Range := sections[1]
inclusiveRange = strings.Split(elf2Range, "-")
elf2Lo, _ := strconv.Atoi(inclusiveRange[0])
elf2Hi, _ := strconv.Atoi(inclusiveRange[1])
sectionsToClean := make(map[int]void)
var longestHi, longestLo, shortestHi, shortestLo int
if (elf2Hi - elf2Lo) > (elf1Hi - elf1Lo) {
longestHi = elf2Hi
longestLo = elf2Lo
shortestHi = elf1Hi
shortestLo = elf1Lo
} else {
longestHi = elf1Hi
longestLo = elf1Lo
shortestHi = elf2Hi
shortestLo = elf2Lo
}
for i := longestLo; i <= longestHi; i++ {
sectionsToClean[i] = member
}
numSectionsForLongestHi := len(sectionsToClean)
for i := shortestLo; i <= shortestHi; i++ {
sectionsToClean[i] = member
}
if len(sectionsToClean) == numSectionsForLongestHi {
totalOverlaps++
}
}
return totalOverlaps
}
func findPartialPairsOverlap(filePath string) int {
input, _ := os.ReadFile(filePath)
lines := strings.Split(strings.TrimSpace(string(input)), "\n")
totalOverlaps := 0
for _, line := range lines {
sections := strings.Split(line, ",")
elf1Range := sections[0]
inclusiveRange := strings.Split(elf1Range, "-")
elf1Lo, _ := strconv.Atoi(inclusiveRange[0])
elf1Hi, _ := strconv.Atoi(inclusiveRange[1])
elf2Range := sections[1]
inclusiveRange = strings.Split(elf2Range, "-")
elf2Lo, _ := strconv.Atoi(inclusiveRange[0])
elf2Hi, _ := strconv.Atoi(inclusiveRange[1])
sectionsToClean := make(map[int]void)
var longestHi, longestLo, shortestHi, shortestLo int
if (elf2Hi - elf2Lo) > (elf1Hi - elf1Lo) {
longestHi = elf2Hi
longestLo = elf2Lo
shortestHi = elf1Hi
shortestLo = elf1Lo
} else {
longestHi = elf1Hi
longestLo = elf1Lo
shortestHi = elf2Hi
shortestLo = elf2Lo
}
for i := longestLo; i <= longestHi; i++ {
sectionsToClean[i] = member
}
numSectionsForLongestHi := len(sectionsToClean)
for i := shortestLo; i <= shortestHi; i++ {
sectionsToClean[i] = member
if len(sectionsToClean) == numSectionsForLongestHi {
totalOverlaps++
break
} else {
numSectionsForLongestHi++
}
}
}
return totalOverlaps
}
func day04() {
// Part 1
result := findCompletePairsOverlap("2022/data/day04_sample.txt")
if result != 2 {
panic("Part 1 example is failing")
}
result = findCompletePairsOverlap("2022/data/day04_input.txt")
fmt.Println("Part 1:", result)
// Part 2
// result = findPartialPairsOverlap("2022/data/day04_sample.txt")
// if result != 4 {
// panic("Part 2 example is failing")
// }
result = findPartialPairsOverlap("2022/data/day04_input.txt")
fmt.Println("Part 2:", result)
}