diff --git a/day10/dayTen.go b/day10/dayTen.go new file mode 100644 index 0000000..34cf6ef --- /dev/null +++ b/day10/dayTen.go @@ -0,0 +1,143 @@ +package day10 + +import ( + "errors" + "fmt" + "os" + "strings" +) + +func Run() int { + fmt.Println("hello day 10") + // filename := "day10/example2noisy" + filename := "day10/example1" + fieldMap := Read(filename) + fmt.Println(fieldMap.String()) + + return 0 +} + +// so do i work with just [][]rune ? +// func Next(from Coord, through Coord) (Coord, error) ? +// and here check that 'from' has exit into 'through' +// and check that 'through' has entrance from 'from' + +// so, i guess i could do 'exit direction' and 'entrance direction' +// then compare 'exit direction' with what's available on 'from' +// +// or i can just have function 'canExit(from, to Coord)' and canEnter(from, to Coord) + +// i suppose it would be nice to just create Cell(Coord, Type) and +// cell would map 'from' to 'to' + +type Cell struct { + Coord Coord + Tile rune +} +func (c *Cell)String() string { + switch c.Tile { + case '7': return "⌝" + case 'J': return "⌟" + case 'F': return "⌜" + case 'L': return "⌞" + case '.': return " " + default: return string(c.Tile) + } +} +type Coord struct { + X, Y int +} + +type Map struct { + Cells map[Coord]Cell + Height, Width int + BeastCoord Coord +} +func (m *Map)String() string { + result := "" + for y := 0; y < m.Height; y++ { + for x := 0; x < m.Width; x++ { + cell := m.Cells[Coord{x, y}] + result += cell.String() + } + result += "\n" + } + return result +} + +func Read(filename string) Map { + result := Map{} + bytes, err := os.ReadFile(filename) + if err != nil { + panic(fmt.Sprint("cannot read file ", filename)) + } + lines := strings.Split(string(bytes), "\n") + result.Height = len(lines) + result.Width = len(lines[0]) + result.Cells = map[Coord]Cell{} + + for y, line := range lines { + for x, symb := range line { + coord := Coord{X: x, Y: y} + if symb == 'S' { + result.BeastCoord = coord + } + result.Cells[coord] = Cell{ + Coord: coord, + Tile: symb, + } + } + } + + return result +} + +// doesn't check whether 'from' has exit into c +// only whether c can accept conntion from that direction +func (c *Cell) Next(from Coord) (to Coord, err error) { + nextCoord, found := c.Directions()[from] + if !found { + return Coord{}, errors.New(fmt.Sprintf("no direction from %+v to %+v through %+v\n", from, to, c)) + } + + return nextCoord, nil +} + +// x from left to right; y from top to bottom +func (c *Cell) Directions() map[Coord]Coord { + x, y := c.Coord.X, c.Coord.Y + switch c.Tile { + case '|': + return map[Coord]Coord{ + {x, y + 1}: {x, y - 1}, + {x, y - 1}: {x, y + 1}, + } + case '-': + return map[Coord]Coord{ + {x - 1, y}: {x + 1, y}, + {x + 1, y}: {x - 1, y}, + } + case 'L': + return map[Coord]Coord{ + {x, y + 1}: {x + 1, y}, + {x + 1, y}: {x, y + 1}, + } + case 'J': + return map[Coord]Coord{ + {x, y + 1}: {x - 1, y}, + {x - 1, y}: {x, y + 1}, + } + case 'F': + return map[Coord]Coord{ + {x + 1, y}: {x, y + 1}, + {x, y + 1}: {x + 1, y}, + } + case '7': + return map[Coord]Coord{ + {x - 1, y}: {x, y + 1}, + {x, y + 1}: {x - 1, y}, + } + default: + return map[Coord]Coord{} + } +} diff --git a/day10/example1 b/day10/example1 new file mode 100644 index 0000000..7650925 --- /dev/null +++ b/day10/example1 @@ -0,0 +1,5 @@ +..... +.S-7. +.|.|. +.L-J. +..... diff --git a/day10/example2noisy b/day10/example2noisy new file mode 100644 index 0000000..689fff6 --- /dev/null +++ b/day10/example2noisy @@ -0,0 +1,5 @@ +-L|F7 +7S-7| +L|7|| +-L-J| +L|-JF diff --git a/day10/example3 b/day10/example3 new file mode 100644 index 0000000..682499d --- /dev/null +++ b/day10/example3 @@ -0,0 +1,5 @@ +..F7. +.FJ|. +SJ.L7 +|F--J +LJ... diff --git a/day10/example4noisy b/day10/example4noisy new file mode 100644 index 0000000..3aea4dd --- /dev/null +++ b/day10/example4noisy @@ -0,0 +1,5 @@ +7-F7- +.FJ|7 +SJLL7 +|F--J +LJ.LJ diff --git a/main.go b/main.go index 86061a0..f320e06 100644 --- a/main.go +++ b/main.go @@ -3,12 +3,12 @@ package main import ( "log" - "sunshine.industries/aoc2023/day9" + "sunshine.industries/aoc2023/day10" ) func main() { log.Print("> starting run:") - result := day9.Run2() - log.Printf("day9 result: %d\n****\n", result) + result := day10.Run() + log.Printf("day10 result: %d\n****\n", result) }