-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd15.go
187 lines (161 loc) · 2.91 KB
/
d15.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
. "./intcode"
"fmt"
"os"
)
type Dir int
const (
North Dir = 1
South Dir = 2
West Dir = 3
East Dir = 4
None Dir = 5
)
func (d Dir) String() string {
return [...]string{"?", "N", "S", "W", "E"}[d]
}
func (d Dir) Rotate180() Dir {
return map[Dir]Dir{
North: South,
South: North,
East: West,
West: East,
}[d]
}
type Field int
const (
Wall Field = 0
Empty Field = 1
Tank Field = 2
Oxigen Field = 3
)
func (f Field) String() string {
return [...]string{"#", ".", "T", "O"}[f]
}
type Pair struct{ x, y int }
func (p Pair) Step(d Dir) Pair {
v := map[Dir]Pair{
North: Pair{0, -1},
South: Pair{0, 1},
West: Pair{-1, 0},
East: Pair{1, 0}}[d]
return Pair{p.x + v.x, p.y + v.y}
}
type Head struct {
pos Pair
f Field
from Dir
}
type Map map[Pair]Head
func show(m Map, p Pair, w, h int) {
fmt.Println()
for j := p.y - h/2; j < p.y+h/2; j++ {
for i := p.x - w/2; i < p.x+w/2; i++ {
if p.x == i && p.y == j {
fmt.Print("X")
continue
}
h, ok := m[Pair{i, j}]
if ok {
fmt.Print(h.f)
} else {
fmt.Print("?")
}
}
fmt.Println()
}
fmt.Println()
}
func main() {
prog := LoadIntProg(os.Args[1])
in := make(chan int)
out := make(chan int)
vm := &VM{prog.Clone(), Position(0), Position(0), Running, ChannelIO{in, out}}
go vm.Run()
root := Head{Pair{0, 0}, Empty, None}
m := make(Map)
m[root.pos] = root
cur := root
dir := None
steps := 0
r1 := 0
var tank Pair
for {
m, cur, dir, steps = step1(in, out, m, cur, steps)
if cur.f == Tank {
//fmt.Println(dir, cur, steps)
tank = cur.pos
r1 = steps
}
if dir == None {
break
}
}
//show(m, cur.pos, 60, 45)
fmt.Println("Result1:", r1)
t := 0
q := make([]Pair, 0)
q = append(q, tank)
m[cur.pos] = Head{tank, Oxigen, None}
grow := 0
for len(q) > 0 {
m, q, grow = step2(m, q)
if grow == 0 {
break
}
t += 1
}
fmt.Println("Result2:", t)
}
func step1(in chan int, out chan int, m Map, cur Head, steps int) (Map, Head, Dir, int) {
curpos := cur.pos
var d Dir
dirs := [4]Dir{North, East, South, West}
for _, d := range dirs {
nextpos := cur.pos.Step(d)
_, ok := m[nextpos]
if !ok {
in <- int(d)
f := Field(<-out)
m[nextpos] = Head{nextpos, f, d}
if f == Wall {
continue
}
cur = m[nextpos]
steps += 1
break
}
}
if curpos == cur.pos {
if cur.from == None {
d = None
} else {
d = cur.from.Rotate180()
in <- int(d)
f := Field(<-out)
steps -= 1
if f == Wall {
panic("ops")
}
cur = m[cur.pos.Step(d)]
}
}
return m, cur, d, steps
}
func step2(m Map, q []Pair) (Map, []Pair, int) {
newq := make([]Pair, 0)
cnt := 0
for i := 0; i < len(q); i++ {
dirs := [4]Dir{North, East, South, West}
for _, d := range dirs {
nextpos := q[i].Step(d)
if m[nextpos].f == Empty {
m[nextpos] = Head{nextpos, Oxigen, None}
newq = append(newq, nextpos)
cnt += 1
}
}
}
return m, newq, cnt
}