2023 Days 4 and 6
This commit is contained in:
parent
9cc373290b
commit
069aa736dc
7 changed files with 404 additions and 0 deletions
56
2023/04/part1/main.go
Normal file
56
2023/04/part1/main.go
Normal 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
|
||||
}
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue