Files
adventofcode/2022/go/day03/day03.go
2023-11-16 10:48:53 +00:00

55 lines
1.1 KiB
Go

package day03
import (
"strings"
// "fmt"
mapset "github.com/deckarep/golang-set/v2"
)
func Part1(input string) int {
var sum int = 0
lines := strings.Split(input, "\n")
for _, line := range lines {
c1 := []rune(line[:len(line)/2])
c1set := mapset.NewSet[rune](c1...)
c2 := []rune(line[len(line)/2:])
c2set := mapset.NewSet[rune](c2...)
for _, v := range c1set.Intersect(c2set).ToSlice() {
sum += getValue(v)
}
}
return sum
}
func getValue(v rune) int {
if v >= 'a' && v <= 'z' {
return int(v) - int('a') + 1
}
if v >= 'A' && v <= 'Z' {
return int(v) - int('A') + 27
}
return 0
}
func Part2(input string) int {
var sum int = 0
var idx int = 0
lines := strings.Split(input, "\n")
for idx=0; idx < len(lines); idx += 3 {
c1 := []rune(lines[idx])
c1set := mapset.NewSet[rune](c1...)
c2 := []rune(lines[idx+1])
c2set := mapset.NewSet[rune](c2...)
c3 := []rune(lines[idx+2])
c3set := mapset.NewSet[rune](c3...)
for _, v := range c1set.Intersect(c2set).Intersect(c3set).ToSlice() {
sum += getValue(v)
}
}
return sum
}