Code for day 3, 4 in golang
This commit is contained in:
parent
c8f7b64102
commit
cc551122f8
85
go/3/main.go
Normal file
85
go/3/main.go
Normal file
@ -0,0 +1,85 @@
|
||||
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)
|
||||
}
|
57
go/4/main.go
Normal file
57
go/4/main.go
Normal file
@ -0,0 +1,57 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
f = "../../4.input.txt"
|
||||
)
|
||||
|
||||
func must(e error) {
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
|
||||
func conv(s string) int {
|
||||
i, err := strconv.Atoi(s)
|
||||
must(err)
|
||||
return i
|
||||
}
|
||||
|
||||
func main() {
|
||||
fp, err := filepath.Abs(f)
|
||||
must(err)
|
||||
rf, err := os.Open(fp)
|
||||
must(err)
|
||||
s := bufio.NewScanner(rf)
|
||||
s.Split(bufio.ScanLines)
|
||||
|
||||
split := strings.Split
|
||||
containCount := 0
|
||||
overlapCount := 0
|
||||
for s.Scan() {
|
||||
l := s.Text()
|
||||
p := split(l, ",")
|
||||
ids1 := split(p[0], "-")
|
||||
ids2 := split(p[1], "-")
|
||||
id1 := []int{conv(ids1[0]), conv(ids1[1])}
|
||||
id2 := []int{conv(ids2[0]), conv(ids2[1])}
|
||||
|
||||
if id1[0] <= id2 [0] && id1[1] >= id2[1] ||
|
||||
id1[0] >= id2 [0] && id1[1] <= id2[1] {
|
||||
containCount = containCount + 1
|
||||
}
|
||||
if id1[0] <= id2[1] && id1[1] >= id2[0] {
|
||||
overlapCount = overlapCount + 1
|
||||
}
|
||||
}
|
||||
|
||||
println("contains count:", containCount)
|
||||
println("overlaps count:", overlapCount)
|
||||
}
|
Loading…
Reference in New Issue
Block a user