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++ moved := false 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 } 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] = '.' } 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 } 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] = '.' } return moved } 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) }