mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-18 19:13:27 +02:00
51 lines
719 B
Go
51 lines
719 B
Go
package main
|
|
|
|
import (
|
|
"github.com/alexchao26/advent-of-code-go/util"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
input := util.ReadFile("../input.txt")
|
|
lines := strings.Split(input, "\n")
|
|
|
|
var twos, threes int
|
|
for _, boxID := range lines {
|
|
if hasTwo(boxID) {
|
|
twos++
|
|
}
|
|
if hasThree(boxID) {
|
|
threes++
|
|
}
|
|
}
|
|
|
|
fmt.Println("checksum", twos*threes)
|
|
}
|
|
|
|
func hasTwo(box string) bool {
|
|
chars := make(map[rune]int)
|
|
for _, c := range box {
|
|
chars[c]++
|
|
}
|
|
for _, v := range chars {
|
|
if v == 2 {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func hasThree(box string) bool {
|
|
chars := make(map[rune]int)
|
|
for _, c := range box {
|
|
chars[c]++
|
|
}
|
|
for _, v := range chars {
|
|
if v == 3 {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|