-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
165 lines (144 loc) · 3.02 KB
/
main.go
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
lib "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader, rounds int) {
scanner := bufio.NewScanner(input)
var points []*Point
for scanner.Scan() {
point := toPoint(scanner.Text())
points = append(points, &point)
}
f, err := ioutil.TempFile(os.TempDir(), "prefix-")
if err != nil {
log.Fatal("Cannot create temporary file", err)
}
fmt.Println(f.Name())
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
for round := 0; round < rounds; round++ {
for _, point := range points {
point.move()
}
cols := make(map[int]int)
for _, point := range points {
cols[point.position.col]++
}
countBars := 0
for _, v := range cols {
if v >= 8 {
countBars++
}
}
if countBars >= 3 {
//printBoard(toBoard(points))
writeBoard(f, round, toBoard(points))
}
}
}
func writeBoard(f *os.File, round int, board [][]bool) {
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("round %d:\n", round))
for _, row := range board {
for _, v := range row {
if v {
sb.WriteRune('#')
} else {
sb.WriteRune('.')
}
}
sb.WriteRune('\n')
}
sb.WriteRune('\n')
sb.WriteRune('\n')
_, err := f.WriteString(sb.String())
if err != nil {
panic(err)
}
}
func printBoard(board [][]bool) {
for _, row := range board {
for _, v := range row {
if v {
fmt.Print("#")
} else {
fmt.Print(".")
}
}
fmt.Println()
}
fmt.Println()
}
func toBoard(points []*Point) [][]bool {
minerRow := lib.NewMiner()
minerCol := lib.NewMiner()
maxerRow := lib.NewMaxer()
maxerCol := lib.NewMaxer()
for _, p := range points {
minerRow.Add(p.position.row)
maxerRow.Add(p.position.row)
minerCol.Add(p.position.col)
maxerCol.Add(p.position.col)
}
minRow := minerRow.Get()
minCol := minerCol.Get()
maxRow := maxerRow.Get()
maxCol := maxerCol.Get()
grid := make([][]bool, maxRow-minRow+1)
for row := minRow; row <= maxRow; row++ {
grid[row-minRow] = make([]bool, maxCol-minCol+1)
}
for _, point := range points {
grid[point.position.row-minRow][point.position.col-minCol] = true
}
return grid
}
type Point struct {
position Coord
velocity Coord
}
func (p *Point) move() {
p.position.row += p.velocity.row
p.position.col += p.velocity.col
}
func toPoint(s string) Point {
start := lib.IndexAll(s, "<")
end := lib.IndexAll(s, ">")
comma := lib.IndexAll(s, ",")
positionCol := lib.StringToInt(strings.Trim(s[start[0]+1:comma[0]], " "))
positionRow := lib.StringToInt(strings.Trim(s[comma[0]+2:end[0]], " "))
velocityCol := lib.StringToInt(strings.Trim(s[start[1]+1:comma[1]], " "))
velocityRow := lib.StringToInt(strings.Trim(s[comma[1]+2:end[1]], " "))
return Point{
position: Coord{
row: positionRow,
col: positionCol,
},
velocity: Coord{
row: velocityRow,
col: velocityCol,
},
}
}
type Coord struct {
row int
col int
}
func fs2(input io.Reader) int {
scanner := bufio.NewScanner(input)
for scanner.Scan() {
line := scanner.Text()
_ = line
}
return 42
}