You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
1.8 KiB
Go
100 lines
1.8 KiB
Go
package day4
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"gitea.paas.celticinfo.fr/oabrivard/aoc2023/utils"
|
|
)
|
|
|
|
func WinningNumbersSum(lines []string) int {
|
|
result := 0
|
|
var re1 = regexp.MustCompile(`Card\s+([0-9]+):(.*)`)
|
|
|
|
for _, l := range lines {
|
|
matches := re1.FindStringSubmatch(l)
|
|
|
|
if len(matches) < 3 {
|
|
fmt.Printf("'%s'\n", l)
|
|
}
|
|
|
|
restOfLine := matches[2]
|
|
lists := strings.Split(restOfLine, "|")
|
|
|
|
winnings := make([]bool, 101)
|
|
a := utils.ParseIntArray(lists[0], " ")
|
|
for _, w := range a {
|
|
winnings[w] = true
|
|
}
|
|
|
|
count := 0
|
|
cards := utils.ParseIntArray(lists[1], " ")
|
|
for _, c := range cards {
|
|
if winnings[c] {
|
|
count++
|
|
}
|
|
}
|
|
|
|
if count > 0 {
|
|
result += utils.IntPow(2, count-1)
|
|
}
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func ScratchCopyCount(lines []string) int {
|
|
matchingCount := make([]int, len(lines))
|
|
var re1 = regexp.MustCompile(`Card\s+([0-9]+):(.*)`)
|
|
|
|
for i, l := range lines {
|
|
matches := re1.FindStringSubmatch(l)
|
|
|
|
if len(matches) < 3 {
|
|
fmt.Printf("Error at line %d : '%s'\n", i, l)
|
|
}
|
|
|
|
//card := matches[1]
|
|
restOfLine := matches[2]
|
|
lists := strings.Split(restOfLine, "|")
|
|
|
|
winnings := make([]bool, 101)
|
|
a := utils.ParseIntArray(lists[0], " ")
|
|
for _, w := range a {
|
|
winnings[w] = true
|
|
}
|
|
|
|
count := 0
|
|
cards := utils.ParseIntArray(lists[1], " ")
|
|
for _, c := range cards {
|
|
if winnings[c] {
|
|
count++
|
|
}
|
|
}
|
|
|
|
matchingCount[i] = count
|
|
}
|
|
|
|
copyCount := make([]int, len(lines))
|
|
for i := range copyCount {
|
|
copyCount[i] = 1 // initial deck count for 1 copy
|
|
}
|
|
|
|
for i, v := range matchingCount {
|
|
if v > 0 {
|
|
for j := copyCount[i]; j > 0; j-- { // this loop is to take the previous copies into account
|
|
for k := 0; k < v; k++ {
|
|
copyCount[i+k+1]++
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
result := 0
|
|
for _, v := range copyCount {
|
|
result += v
|
|
}
|
|
return result
|
|
}
|