mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 20:53:30 +02:00
day02 solution
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user