AoC Day 17, Part 2, Solved, also Mess City (Population: ME)

This commit is contained in:
🐙PiperYxzzy
2023-12-17 19:50:57 +02:00
parent 9c6a14c0ea
commit 627470b17e

View File

@@ -65,6 +65,16 @@ func removeDuplicates(paths []Path) []Path {
} }
func run(part2 bool, input string) any { func run(part2 bool, input string) any {
// when you're ready to do part 2, remove this "not implemented" block
maxBeforeTurning := 3
minBeforeTurning := 0
if part2 {
maxBeforeTurning = 10
minBeforeTurning = 4
}
grid := make([][]WeightedCell, 0) grid := make([][]WeightedCell, 0)
for v, l := range strings.Split(input, "\n") { for v, l := range strings.Split(input, "\n") {
if l == "" { if l == "" {
@@ -75,53 +85,61 @@ func run(part2 bool, input string) any {
for h, c := range strings.Split(l, "") { for h, c := range strings.Split(l, "") {
m, _ := strconv.Atoi(c) m, _ := strconv.Atoi(c)
wc := WeightedCell{v, h, m, wc := WeightedCell{v, h, m,
[]int{math.MaxInt, math.MaxInt, math.MaxInt}, make([]int, 0),
[]int{math.MaxInt, math.MaxInt, math.MaxInt}, make([]int, 0),
[]int{math.MaxInt, math.MaxInt, math.MaxInt}, make([]int, 0),
[]int{math.MaxInt, math.MaxInt, math.MaxInt}} make([]int, 0)}
for t := 0; t < maxBeforeTurning; t++ {
if v == 0 && h == 0 {
wc.N = append(wc.N, m)
wc.S = append(wc.S, m)
wc.E = append(wc.E, m)
wc.W = append(wc.W, m)
} else {
wc.N = append(wc.N, math.MaxInt)
wc.S = append(wc.S, math.MaxInt)
wc.E = append(wc.E, math.MaxInt)
wc.W = append(wc.W, math.MaxInt)
}
}
gridLine = append(gridLine, wc) gridLine = append(gridLine, wc)
} }
grid = append(grid, gridLine) grid = append(grid, gridLine)
} }
// when you're ready to do part 2, remove this "not implemented" block
if part2 {
return "not implemented"
}
// solve part 1 here // solve part 1 here
grid[0][0].N = []int{grid[0][0].HeatLoss, grid[0][0].HeatLoss, grid[0][0].HeatLoss}
grid[0][0].S = []int{grid[0][0].HeatLoss, grid[0][0].HeatLoss, grid[0][0].HeatLoss}
grid[0][0].E = []int{grid[0][0].HeatLoss, grid[0][0].HeatLoss, grid[0][0].HeatLoss}
grid[0][0].W = []int{grid[0][0].HeatLoss, grid[0][0].HeatLoss, grid[0][0].HeatLoss}
paths := []Path{ paths := []Path{
{0, 0, 'E', 3, grid[0][0].HeatLoss, []Step{{0, 0}}}, {0, 0, 'E', maxBeforeTurning, grid[0][0].HeatLoss, []Step{{0, 0}}},
{0, 0, 'S', 3, grid[0][0].HeatLoss, []Step{{0, 0}}}, {0, 0, 'S', maxBeforeTurning, grid[0][0].HeatLoss, []Step{{0, 0}}},
} }
var p Path var p Path
for len(paths) > 0 { for len(paths) > 0 {
p, paths = paths[0], paths[1:] p, paths = paths[0], paths[1:]
// Check L and R
stepsTaken := maxBeforeTurning - p.CurrentRemaining
if stepsTaken >= minBeforeTurning {
if p.CurrentV == len(grid)-1 && p.CurrentH == len(grid[0])-1 { if p.CurrentV == len(grid)-1 && p.CurrentH == len(grid[0])-1 {
break break
} }
// Check L and R
if p.CurrentDirection == 'N' || p.CurrentDirection == 'S' { if p.CurrentDirection == 'N' || p.CurrentDirection == 'S' {
// E & W // E & W
eH := p.CurrentH + 1 eH := p.CurrentH + 1
if eH < len(grid[p.CurrentV]) { if eH < len(grid[p.CurrentV]) {
eC := grid[p.CurrentV][eH] eC := grid[p.CurrentV][eH]
if eC.HeatLoss+p.HeatLoss < eC.E[2] { if eC.HeatLoss+p.HeatLoss < eC.E[maxBeforeTurning-1] {
eC.E[2] = eC.HeatLoss + p.HeatLoss eC.E[maxBeforeTurning-1] = eC.HeatLoss + p.HeatLoss
eClone := make([]Step, len(p.Steps)) eClone := make([]Step, len(p.Steps))
copy(eClone, p.Steps) copy(eClone, p.Steps)
eClone = append(eClone, Step{p.CurrentV, eH}) eClone = append(eClone, Step{p.CurrentV, eH})
ePath := Path{p.CurrentV, eH, 'E', 2, eC.E[2], eClone} ePath := Path{p.CurrentV, eH, 'E', maxBeforeTurning - 1, eC.E[maxBeforeTurning-1], eClone}
paths = append(paths, ePath) paths = append(paths, ePath)
} }
} }
@@ -129,14 +147,14 @@ func run(part2 bool, input string) any {
wH := p.CurrentH - 1 wH := p.CurrentH - 1
if wH >= 0 { if wH >= 0 {
wC := grid[p.CurrentV][wH] wC := grid[p.CurrentV][wH]
if wC.HeatLoss+p.HeatLoss < wC.W[2] { if wC.HeatLoss+p.HeatLoss < wC.W[maxBeforeTurning-1] {
wC.W[2] = wC.HeatLoss + p.HeatLoss wC.W[maxBeforeTurning-1] = wC.HeatLoss + p.HeatLoss
wClone := make([]Step, len(p.Steps)) wClone := make([]Step, len(p.Steps))
copy(wClone, p.Steps) copy(wClone, p.Steps)
wClone = append(wClone, Step{p.CurrentV, wH}) wClone = append(wClone, Step{p.CurrentV, wH})
wPath := Path{p.CurrentV, wH, 'W', 2, wC.W[2], wClone} wPath := Path{p.CurrentV, wH, 'W', maxBeforeTurning - 1, wC.W[maxBeforeTurning-1], wClone}
paths = append(paths, wPath) paths = append(paths, wPath)
} }
} }
@@ -144,14 +162,14 @@ func run(part2 bool, input string) any {
nV := p.CurrentV - 1 nV := p.CurrentV - 1
if nV >= 0 { if nV >= 0 {
nC := grid[nV][p.CurrentH] nC := grid[nV][p.CurrentH]
if nC.HeatLoss+p.HeatLoss < nC.N[2] { if nC.HeatLoss+p.HeatLoss < nC.N[maxBeforeTurning-1] {
nC.N[2] = nC.HeatLoss + p.HeatLoss nC.N[maxBeforeTurning-1] = nC.HeatLoss + p.HeatLoss
nClone := make([]Step, len(p.Steps)) nClone := make([]Step, len(p.Steps))
copy(nClone, p.Steps) copy(nClone, p.Steps)
nClone = append(nClone, Step{nV, p.CurrentH}) nClone = append(nClone, Step{nV, p.CurrentH})
nPath := Path{nV, p.CurrentH, 'N', 2, nC.N[2], nClone} nPath := Path{nV, p.CurrentH, 'N', maxBeforeTurning - 1, nC.N[maxBeforeTurning-1], nClone}
paths = append(paths, nPath) paths = append(paths, nPath)
} }
} }
@@ -159,20 +177,21 @@ func run(part2 bool, input string) any {
sV := p.CurrentV + 1 sV := p.CurrentV + 1
if sV < len(grid) { if sV < len(grid) {
sC := grid[sV][p.CurrentH] sC := grid[sV][p.CurrentH]
if sC.HeatLoss+p.HeatLoss < sC.S[2] { if sC.HeatLoss+p.HeatLoss < sC.S[maxBeforeTurning-1] {
sC.S[2] = sC.HeatLoss + p.HeatLoss sC.S[maxBeforeTurning-1] = sC.HeatLoss + p.HeatLoss
sClone := make([]Step, len(p.Steps)) sClone := make([]Step, len(p.Steps))
copy(sClone, p.Steps) copy(sClone, p.Steps)
sClone = append(sClone, Step{sV, p.CurrentH}) sClone = append(sClone, Step{sV, p.CurrentH})
sPath := Path{sV, p.CurrentH, 'S', 2, sC.S[2], sClone} sPath := Path{sV, p.CurrentH, 'S', maxBeforeTurning - 1, sC.S[maxBeforeTurning-1], sClone}
paths = append(paths, sPath) paths = append(paths, sPath)
} }
} }
} else { } else {
panic("bad direction") panic("bad direction")
} }
}
if p.CurrentRemaining > 0 { if p.CurrentRemaining > 0 {
// Take one step forward // Take one step forward
@@ -248,8 +267,9 @@ func run(part2 bool, input string) any {
}) })
} }
print := false
if print {
fmt.Println("Printing solve:") fmt.Println("Printing solve:")
for v := range grid { for v := range grid {
for h := range grid[v] { for h := range grid[v] {
printed := false printed := false
@@ -268,6 +288,7 @@ func run(part2 bool, input string) any {
} }
fmt.Println() fmt.Println()
} }
}
// Are we not counting the heat loss at position [0][0]? // Are we not counting the heat loss at position [0][0]?