diff --git a/day10/dayTen.go b/day10/dayTen.go index c46e7be..bb5cbe7 100644 --- a/day10/dayTen.go +++ b/day10/dayTen.go @@ -41,6 +41,13 @@ func Run() int { // fmt.Println(fieldMap.String()) fmt.Println("beore marking closest Outer:") // now main loop is closed with regards to 'S' neighbors + + + // TODO Hardcode change S to correct Title + fixedBeast := fieldMap.Cells[fieldMap.BeastCoord] + fixedBeast.Tile = '7' + fieldMap.Cells[fieldMap.BeastCoord] = fixedBeast + fieldMap.countIntersectionsTopDown() // fieldMap.initialMarkOuter() fmt.Println("after marking closest Outer:") @@ -140,6 +147,7 @@ type Map struct { func (m *Map) String() string { result := "" + result += fmt.Sprintf("map of height %d and with %d\n", m.Height, m.Width) for y := 0; y < m.Height; y++ { for x := 0; x < m.Width; x++ { cell := m.Cells[Coord{x, y}] @@ -270,7 +278,9 @@ func Read(filename string) Map { if err != nil { panic(fmt.Sprint("cannot read file ", filename)) } - lines := strings.Split(string(bytes), "\n") + text := string(bytes) + text = strings.TrimSpace(text) + lines := strings.Split(text, "\n") result.Height = len(lines) result.Width = len(lines[0]) result.Cells = map[Coord]Cell{} @@ -351,30 +361,78 @@ func (c *Cell) OutDirections() []Direction { } func (m *Map)countIntersectionsTopDown() { - buffer := make([][]int, m.Height) + stacks := make([][]rune, m.Width) // stack for each X 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] - } + stack := stacks[x] + len := len(stack) 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 + if cell.IsOnMainPath { + if len == 0 { + stack = append(stack, cell.Tile) } else { - buffer[y][x] = 1 + top := stack[len-1] + if isOpposite(top, cell.Tile) { + stack = stack[:len-1] + } else if isScrunching(top, cell.Tile) { + stack = stack[:len-1] + stack = append(stack, '-') + stack, _ = popTwoHorizontals(stack) + } else if cell.Tile != '|' { + stack = append(stack, cell.Tile) + } + } + stacks[x] = stack + } else { + if len == 0 { + m.markOuter(cell.Coord) } } } } - fmt.Println(buffer) - + for x := 0; x < m.Width; x++ { + stack := stacks[x] + fmt.Println(string(stack)) + } + fmt.Print(stacks) +} + +func popTwoHorizontals(stack []rune) ([]rune, bool) { + len := len(stack) + if len >= 2 { + top := stack[len-1] + prev := stack[len-2] + if top == '-' && prev == '-' { + return stack[:len-2], true + } + } + + return stack, false +} + +func isScrunching(pipe, other rune) bool { + switch pipe { + case 'F': + return other == 'J' + case '7': + return other == 'L' + } + return false +} + +func isOpposite(pipe, other rune) bool { + switch pipe { + case '-': + return other == '-' + case 'L': + return other == 'F' + case 'J': + return other == '7' + case 'F': + return other == 'L' + case '7': + return other == 'J' + } + return false } diff --git a/day10/notes.org b/day10/notes.org index e1200fd..efa4d52 100644 --- a/day10/notes.org +++ b/day10/notes.org @@ -157,3 +157,12 @@ and some elements take off? ** ok, one more attempt? not counting, but maintaining a stack? and opposite angles take the element off the stack? + +so - takes off - +and any left braket takes off any right bracket + +and two bottom angles shouldn't be allowed to counteract one another, but top & bottom should be balanced anyway +** uh. so many more updates. +and i'll need to owerwrite S to it's value somehow + +i will change the S myself. by hardcode