day10, reading & printing

This commit is contained in:
efim 2023-12-10 09:00:19 +00:00
parent 3919a70d09
commit c2091b49fd
6 changed files with 166 additions and 3 deletions

143
day10/dayTen.go Normal file
View File

@ -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{}
}
}

5
day10/example1 Normal file
View File

@ -0,0 +1,5 @@
.....
.S-7.
.|.|.
.L-J.
.....

5
day10/example2noisy Normal file
View File

@ -0,0 +1,5 @@
-L|F7
7S-7|
L|7||
-L-J|
L|-JF

5
day10/example3 Normal file
View File

@ -0,0 +1,5 @@
..F7.
.FJ|.
SJ.L7
|F--J
LJ...

5
day10/example4noisy Normal file
View File

@ -0,0 +1,5 @@
7-F7-
.FJ|7
SJLL7
|F--J
LJ.LJ

View File

@ -3,12 +3,12 @@ package main
import ( import (
"log" "log"
"sunshine.industries/aoc2023/day9" "sunshine.industries/aoc2023/day10"
) )
func main() { func main() {
log.Print("> starting run:") log.Print("> starting run:")
result := day9.Run2() result := day10.Run()
log.Printf("day9 result: %d\n****\n", result) log.Printf("day10 result: %d\n****\n", result)
} }