-
Notifications
You must be signed in to change notification settings - Fork 0
/
day04.go
72 lines (58 loc) · 1.31 KB
/
day04.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
package main
import (
"strconv"
"strings"
)
type Elf struct {
From int
To int
}
type Pair struct {
One Elf
Two Elf
}
func calcCoverage(lines []string) int {
var coverage int = 0
elves := extractElves(lines)
for _, e := range elves {
if e.One.From >= e.Two.From && e.One.To <= e.Two.To {
coverage++
} else if e.Two.From >= e.One.From && e.Two.To <= e.One.To {
coverage++
}
}
return coverage
}
func calcCoverageSingle(lines []string) int {
var coverage int = 0
elves := extractElves(lines)
for _, e := range elves {
if (e.One.From >= e.Two.From && e.One.From <= e.Two.To) ||
(e.One.To <= e.Two.To && e.One.To >= e.Two.From) {
coverage++
} else if (e.Two.From >= e.One.From && e.Two.From <= e.One.To) ||
(e.Two.To <= e.One.To && e.Two.To >= e.One.From) {
coverage++
}
}
return coverage
}
func extractElves(lines []string) []Pair {
var elves []Pair
for _, v := range lines {
var pair Pair
both := strings.Split(v, ",")
fromTo1 := strings.Split(both[0], "-")
fromTo2 := strings.Split(both[1], "-")
pair.One = createElf(fromTo1[0], fromTo1[1])
pair.Two = createElf(fromTo2[0], fromTo2[1])
elves = append(elves, pair)
}
return elves
}
func createElf(from string, to string) Elf {
var elf Elf
elf.From, _ = strconv.Atoi(from)
elf.To, _ = strconv.Atoi(to)
return elf
}