diff options
Diffstat (limited to '')
-rw-r--r-- | 2021/13/second.go | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/2021/13/second.go b/2021/13/second.go new file mode 100644 index 0000000..978d521 --- /dev/null +++ b/2021/13/second.go @@ -0,0 +1,98 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +var ( + dots [][]bool + width = -1 + height = -1 +) + +func min(a, b int) int { + if a < b { + return a + } + return b +} + +func foldx(n int) { + for y := 0; y < height; y++ { + if len(dots[y]) < n { + continue + } + for i := 1; i <= n; i++ { + if dots[y][n+i] { + dots[y][n-i] = true + } + } + } + width = n +} + +func foldy(n int) { + for i := 1; i <= n; i++ { + for x := 0; x < len(dots[n+i]); x++ { + if dots[n+i][x] { + dots[n-i][x] = true + } + } + } + height = n +} + +func main() { + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + line := strings.Split(scanner.Text(), ",") + if len(line) < 2 { + break + } + x, _ := strconv.Atoi(line[0]) + y, _ := strconv.Atoi(line[1]) + if y >= height { + dots = append(dots, make([][]bool, y-height+1)...) + height = y + 1 + } + if x >= len(dots[y]) { + dots[y] = append(dots[y], make([]bool, x-len(dots[y])+1)...) + if x >= width { + width = x + 1 + } + } + dots[y][x] = true + } + for y := 0; y < height; y++ { + if len(dots[y]) <= width { + dots[y] = append(dots[y], make([]bool, width-len(dots[y])+1)...) + } + } + for scanner.Scan() { + line := strings.Split(scanner.Text(), "=") + letter := line[0][11] + n, _ := strconv.Atoi(line[1]) + if letter == 'y' { + foldy(n) + } else { + foldx(n) + } + } + for y := 0; y < height; y++ { + w := min(width, len(dots[y])) + for x := 0; x < w; x++ { + if dots[y][x] { + fmt.Printf("#") + } else { + fmt.Printf(".") + } + } + fmt.Println() + } + fmt.Println() + fmt.Println("---") +} |