day10, still not working

This commit is contained in:
efim
2023-12-10 17:03:40 +00:00
parent 742786af47
commit 71262cabe7
3 changed files with 87 additions and 59 deletions

View File

@@ -12,7 +12,7 @@ import (
func Run() int {
fmt.Println("hello day 10")
// filename := "day10/example2noisy"
filename := "day10/input"
filename := "day10/example6"
fieldMap := Read(filename)
fmt.Println(fieldMap.BeastCoord)
// fmt.Println(fieldMap.String())
@@ -41,7 +41,8 @@ func Run() int {
// fmt.Println(fieldMap.String())
fmt.Println("beore marking closest Outer:")
// now main loop is closed with regards to 'S' neighbors
fieldMap.initialMarkOuter()
fieldMap.countIntersectionsTopDown()
// fieldMap.initialMarkOuter()
fmt.Println("after marking closest Outer:")
fmt.Println(fieldMap.String())
return (len / 2) + (len % 2)
@@ -75,8 +76,8 @@ func (c *Cell) String() string {
if c.IsOuter {
return "O"
}
if !c.IsOnMainPath {
return " "
if !c.IsOnMainPath && !c.IsOuter {
return "I"
}
switch c.Tile {
case '7':
@@ -198,7 +199,7 @@ outer:
// outerRunner = m.markOuterAndMove(previous, outerRunner, exitingPreviousBy)
m.markOuterAroundPathElem(currentCell)
// m.markOuterAroundPathElem(currentCell)
var err error
nextCoord, _, err = currentCell.Next(previous.Coord)
@@ -213,6 +214,7 @@ outer:
func (m *Map) markOuter(outerPointerCoord Coord) {
if !m.isValidCoord(outerPointerCoord) {
log.Printf("non valid %+v to mark as Outer", outerPointerCoord)
return
}
outerPointer := m.Cells[outerPointerCoord]
@@ -256,7 +258,7 @@ func (m *Map) checkDirectionFromBeast(through Coord) (isCycle bool, len int) {
}
func (m *Map) isValidCoord(c Coord) bool {
if c.X < 0 || c.Y < 0 || c.X >= m.Height || c.Y >= m.Width {
if c.X < 0 || c.Y < 0 || c.X >= m.Width || c.Y >= m.Height {
return false
}
return true
@@ -348,63 +350,31 @@ func (c *Cell) OutDirections() []Direction {
}
}
func (m *Map)markOuterAroundPathElem(cell Cell) {
log.Printf("will mark outers around %+v\n", cell)
if !cell.IsOnMainPath {
panic("not on main path!?")
}
outerAreas := make(map[rune]any)
for i, mapLine := range strings.Split(outerSections[cell.Tile], "\n") {
for j, mapRune := range mapLine {
dy := 1 - i
dx := 1 - j
neighborCellCoords := Coord{cell.Coord.X + dx, cell.Coord.Y + dy}
if m.isValidCoord(neighborCellCoords) {
neighbor := m.Cells[neighborCellCoords]
if neighbor.IsOuter {
outerAreas[mapRune] = struct{}{}
func (m *Map)countIntersectionsTopDown() {
buffer := make([][]int, m.Height)
for y := 0; y < m.Height; y++ {
buffer[y] = make([]int, m.Width)
for x := 0; x < m.Width; x++ {
if y > 0 {
buffer[y][x] = buffer[y-1][x]
}
cell := m.Cells[Coord{x, y}]
if !cell.IsOnMainPath && buffer[y][x] % 2 == 0 {
log.Printf("not main path y=%d x=%d cell is even", y, x)
m.markOuter(cell.Coord)
}
pathEnds := !slices.Contains(cell.OutDirections(), DOWN)
if cell.IsOnMainPath && pathEnds {
log.Printf("iter y=%d x=%d increasing for %+v\n", y, x, cell)
if y > 0 {
buffer[y][x] = buffer[y-1][x] + 1
} else {
buffer[y][x] = 1
}
}
}
}
for i, mapLine := range strings.Split(outerSections[cell.Tile], "\n") {
for j, mapRune := range mapLine {
dy := -1 + i
dx := -1 + j
neighborCellCoords := Coord{cell.Coord.X + dx, cell.Coord.Y + dy}
// log.Printf("i=%d;j=%d ; checking map rune %s for %+v. areas to cover are %+v dx = %d ; dy = %d\n", i, j, string(mapRune), neighborCellCoords, outerAreas, dx, dy)
_, areaIsOuter := outerAreas[mapRune]
if areaIsOuter {
m.markOuter(neighborCellCoords)
}
}
}
}
fmt.Println(buffer)
var outerSections map[rune]string = map[rune]string{
'|': `A.B
A|B
A.B`,
'-': `AAA
.-.
BBB
`,
'L': `A.B
AL.
AAA
`,
'J': `B.A
.JA
AAA
`,
'F': `AAA
AF.
A.B
`,
'7': `AAA
.7A
B.A
`,
}