day10, marking the path

This commit is contained in:
efim 2023-12-10 12:31:47 +00:00
parent 76be5c45c3
commit 0b6c521b5b
2 changed files with 67 additions and 7 deletions

View File

@ -14,8 +14,9 @@ func Run() int {
// filename := "day10/example2noisy" // filename := "day10/example2noisy"
filename := "day10/input" filename := "day10/input"
fieldMap := Read(filename) fieldMap := Read(filename)
fmt.Println(fieldMap.String()) fmt.Println(fieldMap.BeastCoord)
fmt.Printf("%+v\n", fieldMap.Cells) // fmt.Println(fieldMap.String())
// fmt.Printf("%+v\n", fieldMap.Cells)
// log.Printf(">> does Equals work? {1,2} == {1,2} is %t\n", (Coord{1,2} == Coord{1,2})) // log.Printf(">> does Equals work? {1,2} == {1,2} is %t\n", (Coord{1,2} == Coord{1,2}))
// log.Printf(">> does Index work? {1,2} in [{2,2}, {1,2}] is %d \n", slices.Index([]Coord{{2,2}, {1,2}}, Coord{1,2})) // log.Printf(">> does Index work? {1,2} in [{2,2}, {1,2}] is %d \n", slices.Index([]Coord{{2,2}, {1,2}}, Coord{1,2}))
@ -34,6 +35,12 @@ func Run() int {
} }
} }
fmt.Println("beore marking:")
fieldMap.markMainLoop()
fmt.Println("after marking:")
fmt.Println(fieldMap.String())
// now main loop is closed with regards to 'S' neighbors
return (len / 2) + (len % 2) return (len / 2) + (len % 2)
} }
@ -54,8 +61,15 @@ type Cell struct {
Coord Coord Coord Coord
Tile rune Tile rune
Neighbords []Coord Neighbords []Coord
IsOnMainPath bool
} }
func (c *Cell)String() string { func (c *Cell)String() string {
if c.Tile == 'S' {
return "S"
}
if !c.IsOnMainPath {
return " "
}
switch c.Tile { switch c.Tile {
case '7': return "⌝" case '7': return "⌝"
case 'J': return "⌟" case 'J': return "⌟"
@ -111,19 +125,40 @@ func (m *Map)String() string {
return result return result
} }
func (m *Map)markMainLoop() {
start := m.Cells[m.BeastCoord]
start.IsOnMainPath = true
m.Cells[m.BeastCoord] = start
previous := start
currentCell := m.Cells[previous.Neighbords[0]]
// log.Printf("starting marking of main loop from %+v through %+v\n", start, currentCell)
for currentCell.Tile != 'S' {
currentCell.IsOnMainPath = true
m.Cells[currentCell.Coord] = currentCell
// log.Printf("marking loop on %+v (%s)\n", currentCell, currentCell.String())
nextCoord, err := currentCell.Next(previous.Coord)
// log.Printf("next coord will be %v %s\n", nextCoord, err)
if err != nil {
return
}
previous = currentCell
currentCell = m.Cells[nextCoord]
}
}
// call for each direction from beast. // call for each direction from beast.
// will run the path until it loops back at best, or terminates // will run the path until it loops back at best, or terminates
func (m *Map)checkDirectionFromBeast(through Coord) (isCycle bool, len int) { func (m *Map)checkDirectionFromBeast(through Coord) (isCycle bool, len int) {
defer log.Printf("about to return check from beast %v, isCycle : %t. len is %d", through, isCycle, len) // defer log.Printf("about to return check from beast %v, isCycle : %t. len is %d", through, isCycle, len)
len = 1 len = 1
previous := m.Cells[m.BeastCoord] previous := m.Cells[m.BeastCoord]
currentCell, found := m.Cells[through] currentCell, found := m.Cells[through]
log.Printf("check direction init for %+v\n", currentCell) // log.Printf("check direction init for %+v\n", currentCell)
for found && currentCell.Tile != 'S' { for found && currentCell.Tile != 'S' {
log.Printf("check direction loop for %+v (%s)\n", currentCell, currentCell.String()) // log.Printf("check direction loop for %+v (%s)\n", currentCell, currentCell.String())
len += 1 len += 1
nextCoord, err := currentCell.Next(previous.Coord) nextCoord, err := currentCell.Next(previous.Coord)
log.Printf("next coord will be %v %s\n", nextCoord, err) // log.Printf("next coord will be %v %s\n", nextCoord, err)
if err != nil { if err != nil {
return return
} }
@ -131,8 +166,13 @@ func (m *Map)checkDirectionFromBeast(through Coord) (isCycle bool, len int) {
currentCell, found = m.Cells[nextCoord] currentCell, found = m.Cells[nextCoord]
} }
if currentCell.Tile == 'S' { if currentCell.Tile == 'S' {
log.Printf("found cycle, len is %d\n", len) // log.Printf("found cycle, len is %d\n", len)
isCycle = true isCycle = true
// let's close the loop now.
beastCell := m.Cells[m.BeastCoord]
beastCell.Neighbords = []Coord{previous.Coord, through}
m.Cells[m.BeastCoord] = beastCell
// log.Printf("cells are not %+v", m.Cells)
} }
return return

20
day10/notes.org Normal file
View File

@ -0,0 +1,20 @@
#+title: Notes
* part 2.
how would i even try to check of 'in' or 'out' of the loop?
i guess i could mark things while i go through the loop?
or, i could count manually?
i could change neighbors of the beast point.
then do one more pass around, setting bool 'onMainPath' to true
and then change all else to .
i guess i could then do one more pass, and mark as 'I' and 'O' closest line of .
another idea i don't really want to pursue - 'color fill from some corner'
this is also painful due to pipes touching still need to let the 'O' filling in.
but yeah, if i make initial filling in of the I, then i could just fill in all . with I until nothing changes.
and count I
sounds like a plan