From b2ff2ac24c26a62f4e749226b1d3c99ed4771e8c Mon Sep 17 00:00:00 2001 From: David Ashby Date: Mon, 27 Dec 2021 12:30:38 -0700 Subject: [PATCH] day 25 done --- 25/main.go | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 25/main.go diff --git a/25/main.go b/25/main.go new file mode 100644 index 0000000..6c1bb79 --- /dev/null +++ b/25/main.go @@ -0,0 +1,112 @@ +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) +}