Fourth day
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package day04
|
||||
|
||||
import (
|
||||
"math"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
utils "git.anthonygueguen.fr/g3po/aoc2023/internal"
|
||||
)
|
||||
|
||||
func GetResult(content []string) []int {
|
||||
points := 0
|
||||
nbr_card := 0
|
||||
re := regexp.MustCompile(`\d+`)
|
||||
distribution := make(map[int]int)
|
||||
distribution[1] = 1
|
||||
|
||||
for i, line := range content {
|
||||
current_card := i + 1
|
||||
if _, ok := distribution[current_card]; !ok {
|
||||
distribution[current_card] = 1
|
||||
}
|
||||
line = strings.Split(line, ":")[1]
|
||||
split := strings.Split(line, "|")
|
||||
set1, set2 := split[0], split[1]
|
||||
winning_num := make(map[int]struct{})
|
||||
num_count := 0
|
||||
for _, n := range re.FindAllString(set1, -1) {
|
||||
num := utils.Must(strconv.Atoi(n))
|
||||
winning_num[num] = struct{}{}
|
||||
}
|
||||
for _, n := range re.FindAllString(set2, -1) {
|
||||
num := utils.Must(strconv.Atoi(n))
|
||||
if _, ok := winning_num[num]; ok {
|
||||
num_count++
|
||||
}
|
||||
}
|
||||
if num_count > 0 {
|
||||
points += int(math.Pow(2.0, float64(num_count-1)))
|
||||
for followin_card := 1; followin_card <= num_count; followin_card++ {
|
||||
distribution_following_card := 1
|
||||
if val, ok := distribution[current_card+followin_card]; ok {
|
||||
distribution_following_card = val
|
||||
}
|
||||
distribution[current_card+followin_card] = distribution_following_card + distribution[current_card]
|
||||
}
|
||||
}
|
||||
}
|
||||
for i := 1; i <= len(content); i++ {
|
||||
if _, ok := distribution[i]; !ok {
|
||||
break
|
||||
}
|
||||
nbr_card += distribution[i]
|
||||
}
|
||||
return []int{points, nbr_card}
|
||||
}
|
||||
Reference in New Issue
Block a user