From cc551122f86703085890b7665ba086e5a2c32221 Mon Sep 17 00:00:00 2001 From: Template Pony Date: Sun, 4 Dec 2022 22:27:56 +0800 Subject: [PATCH] Code for day 3, 4 in golang --- go/3/main.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ go/4/main.go | 57 +++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 go/3/main.go create mode 100644 go/4/main.go diff --git a/go/3/main.go b/go/3/main.go new file mode 100644 index 0000000..5feb026 --- /dev/null +++ b/go/3/main.go @@ -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) +} \ No newline at end of file diff --git a/go/4/main.go b/go/4/main.go new file mode 100644 index 0000000..9218f7b --- /dev/null +++ b/go/4/main.go @@ -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) +} \ No newline at end of file