-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03p2.go
57 lines (43 loc) · 1.18 KB
/
03p2.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
package main
import (
"fmt"
"aoc2022/utils"
)
func D03P2() {
lines := utils.ReadLines("./inputs/03.txt")
priorityTotal := 0
numGroups := len(lines) / 3
var elfGroups []map[string]string
for i := 0; i < numGroups; i++ {
elfGroup := make(map[string]string)
elfGroup["ElfOne"] = lines[i*3]
elfGroup["ElfTwo"] = lines[i*3+1]
elfGroup["ElfThree"] = lines[i*3+2]
sharedItem := compareElves(elfGroup["ElfOne"], elfGroup["ElfTwo"], elfGroup["ElfThree"])
elfGroup["SharedItem"] = string(sharedItem)
// Print Items numberic value
itemPriority := sharedItem - 96
// Check if item is uppercase
if itemPriority < 0 {
itemPriority = itemPriority + 58
}
priorityTotal += int(itemPriority)
elfGroup["SharedItemPriority"] = fmt.Sprintf("%v", itemPriority)
elfGroups = append(elfGroups, elfGroup)
}
fmt.Printf("Priority Total: %v\n", priorityTotal)
}
func compareElves(elfOne string, elfTwo string, elfThree string) rune {
for _, elfOneItem := range elfOne {
for _, elfTwoItem := range elfTwo {
if elfOneItem == elfTwoItem {
for _, elfThreeItem := range elfThree {
if elfOneItem == elfThreeItem {
return elfOneItem
}
}
}
}
}
return 0
}