aboutsummaryrefslogtreecommitdiff
path: root/2021/13/first.go
blob: 34ab6fdf67b06a7a5e1dec4036511c828eddbe9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
		}
		if len(dots[y]) < width {
			dots[y] = append(dots[y], make([]bool, width-len(dots[y])+1)...)
		}
		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++ {
		dots[n-i] = append(dots[n-i], make([]bool, width-len(dots[n-i])+1)...)
		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 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("---")
		break
	}
	score := 0
	for y := 0; y < height; y++ {
		w := min(width, len(dots[y]))
		for x := 0; x < w; x++ {
			if dots[y][x] {
				score++
			}
		}
	}
	fmt.Println(score)
}