113 lines
2.0 KiB
Go
113 lines
2.0 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
start := time.Now()
|
||
|
partOne()
|
||
|
duration := time.Since(start)
|
||
|
fmt.Printf("p1: %s\n", duration)
|
||
|
}
|
||
|
|
||
|
func makeScanner(test bool) *bufio.Scanner {
|
||
|
var f *os.File
|
||
|
if test {
|
||
|
f, _ = os.Open("inputs/testinput")
|
||
|
} else {
|
||
|
f, _ = os.Open("inputs/input")
|
||
|
}
|
||
|
reader := bufio.NewReader(f)
|
||
|
return bufio.NewScanner(reader)
|
||
|
}
|
||
|
|
||
|
type coord struct {
|
||
|
x int
|
||
|
y int
|
||
|
}
|
||
|
|
||
|
type floor struct {
|
||
|
steps int
|
||
|
width int
|
||
|
height int
|
||
|
m map[coord]rune
|
||
|
}
|
||
|
|
||
|
func (f *floor) Print() {
|
||
|
for y := 0; y < f.height; y++ {
|
||
|
for x := 0; x < f.width; x++ {
|
||
|
fmt.Printf("%s", string(f.m[coord{x: x, y: y}]))
|
||
|
}
|
||
|
fmt.Printf("\n")
|
||
|
}
|
||
|
fmt.Printf("\n")
|
||
|
}
|
||
|
|
||
|
func (f *floor) Step() bool {
|
||
|
f.steps++
|
||
|
movedThisStep := map[coord]struct{}{}
|
||
|
toMove := map[coord]coord{}
|
||
|
for y := 0; y < f.height; y++ {
|
||
|
for x := 0; x < f.width; x++ {
|
||
|
current := coord{x: x, y: y}
|
||
|
x += 1
|
||
|
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
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
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++ {
|
||
|
for x := 0; x < f.width; x++ {
|
||
|
current := coord{x: x, y: y}
|
||
|
y += 1
|
||
|
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
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
for current, next := range toMove {
|
||
|
f.m[next] = f.m[current]
|
||
|
f.m[current] = '.'
|
||
|
movedThisStep[next] = struct{}{}
|
||
|
}
|
||
|
return len(movedThisStep) > 0
|
||
|
}
|
||
|
|
||
|
func partOne() {
|
||
|
scanner := makeScanner(false)
|
||
|
f := &floor{
|
||
|
m: make(map[coord]rune),
|
||
|
}
|
||
|
y := 0
|
||
|
for scanner.Scan() {
|
||
|
line := scanner.Text()
|
||
|
f.width = len(line)
|
||
|
for x, v := range line {
|
||
|
f.m[coord{x: x, y: y}] = v
|
||
|
}
|
||
|
y++
|
||
|
}
|
||
|
f.height = y
|
||
|
for f.Step() {
|
||
|
}
|
||
|
fmt.Println(f.steps)
|
||
|
}
|