2023 Days 4 and 6

This commit is contained in:
Tristan 2023-12-06 11:02:00 +00:00 committed by Tristan Beedell
parent 9cc373290b
commit 069aa736dc
7 changed files with 404 additions and 0 deletions

56
2023/04/part1/main.go Normal file
View file

@ -0,0 +1,56 @@
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
}