aoc/2023/04/part1/main.go

57 lines
780 B
Go
Raw Permalink Normal View History

2023-12-06 11:02:00 +00:00
package main
import (
"bufio"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin);
total := 0;
for text, err := reader.ReadString('\n'); err == nil; text, err = reader.ReadString('\n') {
points := score(text);
total += points;
}
println(total);
}
func score(text string) (score int) {
gameStr := strings.Trim(strings.ReplaceAll(strings.Split(text, ": ")[1], " ", " "), "\n");
nums := strings.Split(gameStr," | ");
winners := strings.Split(nums[0]," ");
draw := strings.Split(nums[1]," ");
amt := 0;
for _, n := range draw {
match := false;
for _, w := range winners {
match = n == w;
if match {
break;
}
}
if match {
amt++;
if amt == 1 {
score = 1;
} else {
score *= 2;
}
}
}
return
}