feat: extract shared functions and solve part two
This commit is contained in:
@@ -10,6 +10,43 @@ func init() {
|
|||||||
registry.Register("2022D3", ParseInput, PartOne, PartTwo)
|
registry.Register("2022D3", ParseInput, PartOne, PartTwo)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildItemSet(items string) map[rune]bool {
|
||||||
|
itemSet := make(map[rune]bool)
|
||||||
|
for _, item := range items {
|
||||||
|
itemSet[item] = true
|
||||||
|
}
|
||||||
|
return itemSet
|
||||||
|
}
|
||||||
|
|
||||||
|
func findCommonItem(itemSet map[rune]bool, items string) rune {
|
||||||
|
for _, item := range items {
|
||||||
|
if itemSet[item] {
|
||||||
|
return item
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func findIntersection(itemSet map[rune]bool, items string) map[rune]bool {
|
||||||
|
intersection := make(map[rune]bool)
|
||||||
|
for _, item := range items {
|
||||||
|
if itemSet[item] {
|
||||||
|
intersection[item] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return intersection
|
||||||
|
}
|
||||||
|
|
||||||
|
func getItemPriority(item rune) int {
|
||||||
|
if item >= 'a' && item <= 'z' {
|
||||||
|
return int(item - 'a' + 1)
|
||||||
|
}
|
||||||
|
if item >= 'A' && item <= 'Z' {
|
||||||
|
return int(item - 'A' + 27)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
func ParseInput(filepath string) []string {
|
func ParseInput(filepath string) []string {
|
||||||
content, _ := os.ReadFile(filepath)
|
content, _ := os.ReadFile(filepath)
|
||||||
return strings.Split(string(content), "\n")
|
return strings.Split(string(content), "\n")
|
||||||
@@ -22,29 +59,29 @@ func PartOne(data []string) int {
|
|||||||
firstCompartment := rucksack[:compartmentSize]
|
firstCompartment := rucksack[:compartmentSize]
|
||||||
secondCompartment := rucksack[compartmentSize:]
|
secondCompartment := rucksack[compartmentSize:]
|
||||||
|
|
||||||
firstCompartmentItems := make(map[rune]bool)
|
firstCompartmentItems := buildItemSet(firstCompartment)
|
||||||
for _, item := range firstCompartment {
|
commonItem := findCommonItem(firstCompartmentItems, secondCompartment)
|
||||||
firstCompartmentItems[item] = true
|
totalPriority += getItemPriority(commonItem)
|
||||||
}
|
|
||||||
|
|
||||||
var commonItem rune
|
|
||||||
for _, item := range secondCompartment {
|
|
||||||
if firstCompartmentItems[item] {
|
|
||||||
commonItem = item
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if commonItem >= 'a' && commonItem <= 'z' {
|
|
||||||
totalPriority += int(commonItem - 'a' + 1)
|
|
||||||
} else if commonItem >= 'A' && commonItem <= 'Z' {
|
|
||||||
totalPriority += int(commonItem - 'A' + 27)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return totalPriority
|
return totalPriority
|
||||||
}
|
}
|
||||||
|
|
||||||
func PartTwo(data []string) int {
|
func PartTwo(data []string) int {
|
||||||
return 0
|
totalPriority := 0
|
||||||
|
for i := 0; i < len(data); i += 3 {
|
||||||
|
if i+2 >= len(data) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
firstRucksack := data[i]
|
||||||
|
secondRucksack := data[i+1]
|
||||||
|
thirdRucksack := data[i+2]
|
||||||
|
|
||||||
|
firstRucksackItems := buildItemSet(firstRucksack)
|
||||||
|
commonInFirstTwo := findIntersection(firstRucksackItems, secondRucksack)
|
||||||
|
badge := findCommonItem(commonInFirstTwo, thirdRucksack)
|
||||||
|
totalPriority += getItemPriority(badge)
|
||||||
|
}
|
||||||
|
|
||||||
|
return totalPriority
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user