Skip to content

Instantly share code, notes, and snippets.

@timblair
Created December 22, 2017 10:10
Show Gist options
  • Save timblair/df84b3efd5bf6d437708cf22fcc4cd48 to your computer and use it in GitHub Desktop.
Save timblair/df84b3efd5bf6d437708cf22fcc4cd48 to your computer and use it in GitHub Desktop.
AoC 2017 Day 21: Grid Joining
func join(gs []grid) grid {
n := int(math.Sqrt(float64(len(gs)))) // Number of subgrids across/down the new grid
sgn := len(gs[0]) // Number of values on one side of a subgrid
cg := newGridBySize(n * sgn) // The new, combined grid to populate
// Add each subgrid to the new, combined grid.
for i, g := range gs {
// Calculate the position of the top-left of the subgrid once placed in
// the new, combined grid. We use this plus the offset within the subgrid
// to place the values in the combined grid.
tly := (i / n) * sgn
tlx := (i % n) * sgn
for j, row := range g {
for k, cell := range row {
cg[tly+j][tlx+k] = cell
}
}
}
return cg
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment