day02 solution

This commit is contained in:
alexchao26
2020-09-06 20:56:52 -04:00
parent 62efd77655
commit 31a8f178be
6 changed files with 438 additions and 1 deletions
+50
View File
@@ -0,0 +1,50 @@
package main
import (
"adventofcode/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
}