@@ -20,16 +20,24 @@ type Point struct {
2020func main () {
2121 ps := parse (readInput ())
2222 //fmt.Println(solve1(ps))
23- northMostVal (ps )
23+ NORTHMOST = northMostVal (ps )
2424 fmt .Println (solve2 (ps ))
2525}
2626
2727func area (p1 , p2 Point ) int {
2828
29- Lf := float64 ((p1 .row - p2 .row ) + 1 )
30- Hf := float64 (p1 .col - p2 .col ) + 1
31- H := int (math .Abs (Lf ))
32- L := int (math .Abs (Hf ))
29+ Lf := float64 ((p1 .row - p2 .row ))
30+ Hf := float64 (p1 .col - p2 .col )
31+ H := int (math .Abs (Lf )) + 1
32+ L := int (math .Abs (Hf )) + 1
33+
34+ if p1 .row == p2 .row {
35+ return int (float64 (math .Abs (Hf )))
36+ }
37+
38+ if p1 .col == p2 .col {
39+ return int (float64 (math .Abs (Lf )))
40+ }
3341
3442 return H * L
3543}
@@ -59,28 +67,27 @@ func drawLine(p1, p2 Point) []Point {
5967 if p1 .row == p2 .row {
6068 // do we move up or down?
6169 if p1 .col <= p2 .col {
62- for i := p1 .col ; i < p2 .col ; i ++ {
70+ for i := p1 .col ; i <= p2 .col ; i ++ {
6371 out = append (out , Point {p1 .row , i })
6472 }
6573 }
6674 if p1 .col > p2 .col {
67- for i := p1 .col ; i > p2 .col ; i -- {
75+ for i := p1 .col ; i >= p2 .col ; i -- {
6876 out = append (out , Point {p1 .row , i })
6977 }
7078 }
7179 } else if p1 .col == p2 .col {
7280 if p1 .row <= p2 .row {
73- for i := p1 .row ; i < p2 .row ; i ++ {
81+ for i := p1 .row ; i <= p2 .row ; i ++ {
7482 out = append (out , Point {i , p1 .col })
7583 }
7684 }
7785 if p1 .row > p2 .row {
78- for i := p1 .row ; i > p2 .row ; i -- {
86+ for i := p1 .row ; i >= p2 .row ; i -- {
7987 out = append (out , Point {i , p1 .col })
8088 }
8189 }
8290 } else {
83- fmt .Printf ("%v %v\n " , p1 , p2 )
8491 panic ("This should not happen yo" )
8592 }
8693 return out
@@ -107,7 +114,33 @@ func northMostVal(ps []Point) int {
107114 return row
108115}
109116
110- func inBoundary (p Point , m map [Point ]bool ) bool {
117+ func inBoundary (p Point , m map [Point ]bool , edges []Point ) bool {
118+ if m [p ] {
119+ return true
120+ }
121+
122+ hits := 0
123+ for i := 0 ; i < len (edges )- 1 ; i ++ {
124+ a , b := edges [i ], edges [i + 1 ]
125+ if a .col == b .col {
126+ continue
127+ }
128+ if a .row != b .row {
129+ continue
130+ }
131+ if (p .col >= a .col && p .col < b .col ) || (p .col >= b .col && p .col < a .col ) {
132+ if p .row > a .row {
133+ hits ++
134+ }
135+ }
136+ }
137+ if hits == 0 {
138+ return false
139+ }
140+ return hits % 2 == 1
141+ }
142+
143+ func inBoundary2 (p Point , m map [Point ]bool ) bool {
111144 WINDOW := 10000
112145 // it's in the figure if in each direction it only intersects _ONE_ boundary
113146
@@ -140,14 +173,13 @@ func inBoundary(p Point, m map[Point]bool) bool {
140173 }
141174 }
142175 if intersectsNorth == 0 || intersectsNorth % 2 == 0 {
143- for _ , enc := range encountered {
144- memo [enc ] = false
145- }
146176 return false
147177 }
148- for _ , enc := range encountered {
149- memo [enc ] = true
150- }
178+ /*
179+ for _, enc := range encountered {
180+ memo[enc] = true
181+ }
182+ */
151183 encountered = nil
152184 return true
153185}
@@ -170,11 +202,6 @@ func sortedPairs(points []Point) []Pair {
170202 return pairs
171203}
172204
173- // floodFill the entire rectangle so it's easier to do a 'contain' check
174- func floodFill (ps []Point ) map [Point ]bool {
175- return nil
176- }
177-
178205func solve2 (points []Point ) int {
179206 // draw a polygon essentially
180207 // then 'solve1' with points that are enclosed within this shape..
@@ -189,12 +216,11 @@ func solve2(points []Point) int {
189216 newPoints := drawLine (current , next )
190217 border = append (border , newPoints ... )
191218 }
192- //draw(border)
193219
194220 borderMap := toMap (border )
195221
196222 containsAll := func (p1 , p2 , p3 , p4 Point ) bool {
197- return inBoundary (p1 , borderMap ) && inBoundary (p2 , borderMap ) && inBoundary (p3 , borderMap ) && inBoundary (p4 , borderMap )
223+ return inBoundary (p1 , borderMap , points ) && inBoundary (p2 , borderMap , points ) && inBoundary (p3 , borderMap , points ) && inBoundary (p4 , borderMap , points )
198224 }
199225
200226 // now we need to select 2 points that do not paint outside this figure
@@ -211,11 +237,11 @@ outer:
211237 continue
212238 }
213239 // the points need to be on a diagonal
214- if point . row == other . row || point . col == other . col {
215- continue
216- }
217-
218- // we need to create 2 points to close the loop
240+ /*
241+ if point.row == other.row || point.col == other.col {
242+ continue
243+ }
244+ */
219245
220246 // figure out the correct bounding box
221247
@@ -238,14 +264,15 @@ outer:
238264 a := area (point , other )
239265 valid := true
240266 for _ , l := range rect {
241- if ! inBoundary (l , borderMap ) {
267+ if ! inBoundary (l , borderMap , points ) {
242268 valid = false
243269 continue outer
244270 }
245271 }
246272
247273 if valid {
248274 // return the first valid area
275+ fmt .Printf ("area %v for points %v %v\n " , a , point , other )
249276 return a
250277 }
251278 }
@@ -291,8 +318,8 @@ func draw(ps []Point) {
291318 m [p ] = true
292319 }
293320
294- for row := 0 ; row < 13 ; row ++ {
295- for col := 0 ; col < 13 ; col ++ {
321+ for row := 0 ; row < 1000000 ; row ++ {
322+ for col := 0 ; col < 1000000 ; col ++ {
296323 if _ , ok := m [Point {row , col }]; ok {
297324 fmt .Print ("#" )
298325 } else {
0 commit comments