~talos/advent-of-code

035860b27a27e98b891eee629ea245cc394d933d — Jordan Newport 5 months ago 21dc1cd
Day 21
1 files changed, 24 insertions(+), 52 deletions(-)

M 2021/21/solution.go
M 2021/21/solution.go => 2021/21/solution.go +24 -52
@@ 109,73 109,44 @@ func main() {
		9:1,
	}

	// 1D case
	/*
	memoize := make(map[int]int)
	for i := 29; i >= 0; i-- {
		if i >= 21 {
			memoize[i] = 1
		} else {
			for k, v := range freq {
				memoize[i] += memoize[i+k] * v
			}
		}
	}
	*/

	memoize := make(map[vec4]vec4)
	memoize := make(map[vec5]vec5)
	// scores
	for i := 30; i >= 0; i-- {
		for j := 30; j >= 0; j-- {
			// positions
			for k := 10; k >= 1; k-- {
				for l := 10; l >= 1; l-- {
					if i >= 21 {
						memoize[vec4{i,j,k,l}] = vec4{1, 0, k, l}
					} else if j >= 21 {
						memoize[vec4{i,j,k,l}] = vec4{0, 1, k, l}
					} else {
						for ki, vi := range freq {
							new_position_1 := (k + ki - 1) % 10 + 1
							new_score_1 := i + new_position_1
							product := vi
							if new_score_1 >= 21 {
								current := memoize[vec4{i,j,k,l}]
								target := memoize[vec4{new_score_1, j, new_position_1, l}]
								memoize[vec4{i,j,k,l}] = vec4{current.w + target.w * product, current.x + target.x * product, new_position_1, l}
								if i == 20 && j == 20 {
									fmt.Println(k, l, ki, new_position_1, new_score_1, product, current, memoize[vec4{i,j,k,l}])
								}
							} else {
								for kj, vj := range freq {
									new_position_2 := (l + kj - 1) % 10 + 1
									new_score_2 := j + new_position_2
									product *= vj
									current := memoize[vec4{i,j,k,l}]
									target := memoize[vec4{new_score_1, new_score_2, new_position_1, new_position_2}]
									memoize[vec4{i,j,k,l}] = vec4{current.w + target.w * product, current.x + target.x * product, new_position_1, new_position_2}
									if i == 20 && j == 20 {
										fmt.Println(k, l, ki, kj, new_position_1, new_position_2, new_score_1, new_score_2, product, current, memoize[vec4{i,j,k,l}])
									}
					for player := 2; player >= 1; player-- {
						if i >= 21 {
							memoize[vec5{i,j,k,l,player}] = vec5{1, 0, k, l, player}
						} else if j >= 21 {
							memoize[vec5{i,j,k,l,player}] = vec5{0, 1, k, l, player}
						} else {
							for key, value := range freq {
								if player == 1 {
									new_position := (k + key - 1) % 10 + 1
									new_score := i + new_position
									current := memoize[vec5{i,j,k,l,player}]
									target := memoize[vec5{new_score, j, new_position, l, player ^ 3}]
									memoize[vec5{i,j,k,l,player}] = vec5{current.w + target.w * value, current.x + target.x * value, new_position, l, player}
								} else {
									new_position := (l + key - 1) % 10 + 1
									new_score := j + new_position
									current := memoize[vec5{i,j,k,l,player}]
									target := memoize[vec5{i, new_score, k, new_position, player ^ 3}]
									memoize[vec5{i,j,k,l,player}] = vec5{current.w + target.w * value, current.x + target.x * value, k, new_position, player}
								}
							}
						}
						// fmt.Println(i, j, k, l, player, memoize[vec5{i,j,k,l,player}])
					}
					// fmt.Println(i, j, k, l, memoize[vec4{i,j,k,l}])
				}
			}
		}
	}

	fmt.Println(memoize[vec4{0, 0, pos1, pos2}])
	fmt.Println(memoize[vec5{0, 0, pos1, pos2, 1}])

	/*
	fmt.Println()
	for k, v := range memoize {
		fmt.Println(k, ":", v)
	}
	*/
	// fmt.Println(wins1, wins2)
}

type pair struct {


@@ 183,9 154,10 @@ type pair struct {
	y int
}

type vec4 struct {
type vec5 struct {
	w int // score
	x int // score
	y int // pos
	z int // pos
	player int
}