simplify somewhat

This commit is contained in:
David 2021-12-27 12:42:02 -07:00
parent b2ff2ac24c
commit bec29a111c

View File

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