adventofcode/2022/day03/main.go

75 lines
1.2 KiB
Go
Raw Normal View History

2022-12-03 13:14:41 +00:00
package main
import (
"fmt"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
fmt.Println("# Part 1")
fmt.Println(part1(inp))
fmt.Println("# Part 2")
fmt.Println(part2(inp))
}
func part1(inp []string) int {
var ret int
for i := range inp {
sack := inp[i]
c1, c2 := sack[:len(sack)/2], sack[len(sack)/2:]
var dupe byte
for _, c1w := range c1 {
for _, c2w := range c2 {
if c1w == c2w {
dupe = byte(c1w)
break
}
}
if dupe != 0 {
break
}
}
if dupe >= 'a' {
ret += int(dupe - 'a' + 1)
} else if dupe >= 'A' {
ret += int(dupe - 'A' + 27)
}
}
return ret
}
func part2(inp []string) int {
var ret int
elves := []string{"", "", ""}
for i := range inp {
elves[i%3] = inp[i]
if i%3 == 2 {
var badge byte
// Check 'em
for _, e1 := range elves[0] {
for _, e2 := range elves[1] {
for _, e3 := range elves[2] {
if e1 == e2 && e2 == e3 {
badge = byte(e1)
}
}
if badge != 0 {
break
}
}
if badge != 0 {
break
}
}
if badge >= 'a' {
ret += int(badge - 'a' + 1)
} else if badge >= 'A' {
ret += int(badge - 'A' + 27)
}
}
}
return ret
}