aoc22/go/3/main.go

85 lines
1.9 KiB
Go

package main
import (
"bufio"
"github.com/emirpasic/gods/sets/hashset"
"os"
"path/filepath"
)
var (
f = "../../3.input.txt"
// why go why is an element in a string int32, but uint8 in a interface{}
p = map[int32]int{}
)
func must(e error) {
if e != nil {
panic(e)
}
}
func main() {
fp, err := filepath.Abs(f)
must(err)
rf, err := os.Open(fp)
must(err)
s := bufio.NewScanner(rf)
s.Split(bufio.ScanLines)
for i, c := range "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" {
p[c] = i + 1
}
pScore := 0
for s.Scan() {
line := s.Text()
half := len(line)/2
c1, c2 := line[:half], line[half:]
s1, s2 := hashset.New(), hashset.New()
for i := 0; i < len(c1); i++ {
s1.Add(c1[i])
s2.Add(c2[i])
}
shared := s1.Intersection(s2).Values()[0].(byte) // I am cancer (the interface{} thats uint8)
// println(string(shared))
s := int32(shared)
pScore = pScore + p[s]
}
println("parts score:", pScore)
rf.Close()
rf, err = os.Open(fp)
must(err)
s = bufio.NewScanner(rf)
commonScore := 0
for s.Scan() {
lines3 := [3]string{}
var s3 [3]*hashset.Set
lines3[0] = s.Text()
s.Scan()
lines3[1] = s.Text()
s.Scan()
lines3[2] = s.Text()
for i := 0; i < 3; i++ {
s3[i] = hashset.New()
}
for i := 0; i < 3; i++ {
for _, c := range lines3[i] {
s3[i].Add(c)
}
}
common := s3[0].Intersection(s3[1].Intersection(s3[2])).Values()[0].(int32) // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
// println(string(common))
// c := int32(common)
commonScore = commonScore + p[common]
}
println("common score among 3:", commonScore)
}