day 25 done

This commit is contained in:
David 2021-12-27 12:30:38 -07:00
parent 4a9afc8de6
commit b2ff2ac24c
1 changed files with 112 additions and 0 deletions

112
25/main.go Normal file
View File

@ -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)
}