Skip to content

Commit

Permalink
add day 3 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Matrikss committed Dec 3, 2022
1 parent 639e66a commit 0cd1f52
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions day3.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ func check(e error) {
}
}

func join_multi(s [][]string) string {
res := ""
for i := 0; i < len(s); i++ {
for j := 0; j < len(s[i]); j++ {
res += s[i][j]
}
}
return res
}

func main() {

priorities := map[string]int{}
Expand All @@ -35,15 +45,24 @@ func main() {

lines := strings.Split(string(input), "\r\n")

sum := 0
sum1 := 0
for _, line := range lines {
f_half := line[:len(line)/2]
s_half := line[len(line)/2:]

regex := regexp.MustCompile("[" + f_half + "]")
sum += priorities[regex.FindStringSubmatch(s_half)[0]]
sum1 += priorities[regex.FindStringSubmatch(s_half)[0]]
}

sum2 := 0
for i := 0; i < len(lines); i += 3 {
regex := regexp.MustCompile("[" + lines[i] + "]")
common := regex.FindAllStringSubmatch(lines[i+1], -1)
regex = regexp.MustCompile("[" + lines[i+2] + "]")
common = regex.FindAllStringSubmatch(join_multi(common), -1)
sum2 += priorities[common[0][0]]
}

fmt.Println("part 1: ", sum)
fmt.Println("part 2: ", 0)
fmt.Println("part 1: ", sum1)
fmt.Println("part 2: ", sum2)
}

0 comments on commit 0cd1f52

Please sign in to comment.