day10, marking the path
This commit is contained in:
parent
76be5c45c3
commit
0b6c521b5b
|
@ -14,8 +14,9 @@ func Run() int {
|
|||
// filename := "day10/example2noisy"
|
||||
filename := "day10/input"
|
||||
fieldMap := Read(filename)
|
||||
fmt.Println(fieldMap.String())
|
||||
fmt.Printf("%+v\n", fieldMap.Cells)
|
||||
fmt.Println(fieldMap.BeastCoord)
|
||||
// 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 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)
|
||||
}
|
||||
|
||||
|
@ -54,8 +61,15 @@ type Cell struct {
|
|||
Coord Coord
|
||||
Tile rune
|
||||
Neighbords []Coord
|
||||
IsOnMainPath bool
|
||||
}
|
||||
func (c *Cell)String() string {
|
||||
if c.Tile == 'S' {
|
||||
return "S"
|
||||
}
|
||||
if !c.IsOnMainPath {
|
||||
return " "
|
||||
}
|
||||
switch c.Tile {
|
||||
case '7': return "⌝"
|
||||
case 'J': return "⌟"
|
||||
|
@ -111,19 +125,40 @@ func (m *Map)String() string {
|
|||
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.
|
||||
// will run the path until it loops back at best, or terminates
|
||||
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
|
||||
previous := m.Cells[m.BeastCoord]
|
||||
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' {
|
||||
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
|
||||
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 {
|
||||
return
|
||||
}
|
||||
|
@ -131,8 +166,13 @@ func (m *Map)checkDirectionFromBeast(through Coord) (isCycle bool, len int) {
|
|||
currentCell, found = m.Cells[nextCoord]
|
||||
}
|
||||
if currentCell.Tile == 'S' {
|
||||
log.Printf("found cycle, len is %d\n", len)
|
||||
// log.Printf("found cycle, len is %d\n", len)
|
||||
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
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue