diff --git a/day10/dayTen.go b/day10/dayTen.go index 4c35b4c..c46e7be 100644 --- a/day10/dayTen.go +++ b/day10/dayTen.go @@ -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 -`, } diff --git a/day10/example6 b/day10/example6 new file mode 100644 index 0000000..8f950ae --- /dev/null +++ b/day10/example6 @@ -0,0 +1,10 @@ +FF7FSF7F7F7F7F7F---7 +L|LJ||||||||||||F--J +FL-7LJLJ||||||LJL-77 +F--JF--7||LJLJ7F7FJ- +L---JF-JLJ.||-FJLJJ7 +|F|F-JF---7F7-L7L|7| +|FFJF7L7F-JF7|JL---7 +7-L-JL7||F7|L7F-7F7| +L.L7LFJ|||||FJL7||LJ +L7JLJL-JLJLJL--JLJ.L diff --git a/day10/notes.org b/day10/notes.org index ca94063..e1200fd 100644 --- a/day10/notes.org +++ b/day10/notes.org @@ -109,3 +109,51 @@ A⌜. A.B i guess i could just code that. +** it also doesn't work. pipes to into place where whole 3x3 is path pipes and info on 'outer' is lost +** will it work to cound intersections between a point and Top \ Left direction? +** hypothesis - from the point, count up, down, left & right - intersections with the golden path. +if into U&D or L&R both are odd - then inside. +short circuit - if into 1 direction number is 0 - definitely outside + +and that's square? could kube? well, there shouldn't be too many points without the pipe already + +maybe i can somehow count for the whole map? + +if element on path - increase by 1 value from top +*** also, what if i could consturct couter example before i start coding, you know +with the point that is outside, but has odd in two sides? + +yes, i think there's a counter example with a point which has 3 on all sides because of path going directly to that side +but maybe we don't count that as 'crossing'? only if we what? cross non-parallel line? + +that could be it +** ok, last idea, yes, count intersections +one additonal [][]int +from top to bottom. take value from top, if it's pipe (and doesn't have down movemnt) increase by 1 +if it's not on path and has Even number, already count as Outer +** oh, come on . + +7 +| +L is 1 + +but +7 +| +J is 0 + +how to account for that? +monitor the left and right somehow. +have another map, store left or right in previous cell if we are in top thingy? + +if we're on path, and previous is empty - store left or right? +so, copy previous + +and if we're counting up - store nothing. + +or just monitor angles? +maybe have stack? of seen path elements? +and some elements take off? +** ok, one more attempt? +not counting, but maintaining a stack? +and opposite angles take the element off the stack?