]> fbox.kageds.com Git - adventofcode.git/blob - 2022/go/day03/day03.go
day02
[adventofcode.git] / 2022 / go / day03 / day03.go
1 package day03
2
3 import (
4 "strings"
5 // "fmt"
6
7 mapset "github.com/deckarep/golang-set/v2"
8
9 )
10
11 func Part1(input string) int {
12 var sum int = 0
13 lines := strings.Split(input, "\n")
14 for _, line := range lines {
15 c1 := []rune(line[:len(line)/2])
16 c1set := mapset.NewSet[rune](c1...)
17
18 c2 := []rune(line[len(line)/2:])
19 c2set := mapset.NewSet[rune](c2...)
20 for _, v := range c1set.Intersect(c2set).ToSlice() {
21 sum += getValue(v)
22 }
23 }
24 return sum
25 }
26
27 func getValue(v rune) int {
28 if v >= 'a' && v <= 'z' {
29 return int(v) - int('a') + 1
30 }
31 if v >= 'A' && v <= 'Z' {
32 return int(v) - int('A') + 27
33 }
34 return 0
35 }
36
37 func Part2(input string) int {
38 var sum int = 0
39 var idx int = 0
40 lines := strings.Split(input, "\n")
41 for idx=0; idx < len(lines); idx += 3 {
42 c1 := []rune(lines[idx])
43 c1set := mapset.NewSet[rune](c1...)
44 c2 := []rune(lines[idx+1])
45 c2set := mapset.NewSet[rune](c2...)
46 c3 := []rune(lines[idx+2])
47 c3set := mapset.NewSet[rune](c3...)
48 for _, v := range c1set.Intersect(c2set).Intersect(c3set).ToSlice() {
49 sum += getValue(v)
50 }
51
52 }
53 return sum
54 }