simplify somewhat

This commit is contained in:
David 2021-12-27 12:42:02 -07:00
parent b2ff2ac24c
commit bec29a111c
1 changed files with 8 additions and 10 deletions

View File

@ -49,7 +49,7 @@ func (f *floor) Print() {
func (f *floor) Step() bool { func (f *floor) Step() bool {
f.steps++ f.steps++
movedThisStep := map[coord]struct{}{} moved := false
toMove := map[coord]coord{} toMove := map[coord]coord{}
for y := 0; y < f.height; y++ { for y := 0; y < f.height; y++ {
for x := 0; x < f.width; x++ { for x := 0; x < f.width; x++ {
@ -58,16 +58,15 @@ func (f *floor) Step() bool {
if x >= f.width { if x >= f.width {
x = 0 x = 0
} }
next := coord{x: x, y: y} if f.m[current] == '>' && f.m[coord{x: x, y: y}] == '.' {
if _, ok := movedThisStep[current]; !ok && f.m[current] == '>' && f.m[next] == '.' { toMove[current] = coord{x: x, y: y}
toMove[current] = next moved = true
} }
} }
} }
for current, next := range toMove { for current, next := range toMove {
f.m[next] = f.m[current] f.m[next] = f.m[current]
f.m[current] = '.' f.m[current] = '.'
movedThisStep[next] = struct{}{}
} }
toMove = map[coord]coord{} toMove = map[coord]coord{}
for y := 0; y < f.height; y++ { for y := 0; y < f.height; y++ {
@ -77,18 +76,17 @@ func (f *floor) Step() bool {
if y >= f.height { if y >= f.height {
y = 0 y = 0
} }
next := coord{x: x, y: y} if f.m[current] == 'v' && f.m[coord{x: x, y: y}] == '.' {
if _, ok := movedThisStep[current]; !ok && f.m[current] == 'v' && f.m[next] == '.' { toMove[current] = coord{x: x, y: y}
toMove[current] = next moved = true
} }
} }
} }
for current, next := range toMove { for current, next := range toMove {
f.m[next] = f.m[current] f.m[next] = f.m[current]
f.m[current] = '.' f.m[current] = '.'
movedThisStep[next] = struct{}{}
} }
return len(movedThisStep) > 0 return moved
} }
func partOne() { func partOne() {