re-organise repo
This commit is contained in:
54
2022/go/day03/day03.go
Normal file
54
2022/go/day03/day03.go
Normal file
@@ -0,0 +1,54 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user