diff --git a/day18/lagoon.go b/day18/lagoon.go index f1bab05..347af1f 100644 --- a/day18/lagoon.go +++ b/day18/lagoon.go @@ -12,28 +12,26 @@ import ( func Run() int { log.Println("hello day 18") log.Println("problem of lagoon bgins") - filename := "day18/input" - instructions := ReadInstructionas(filename) + filename := "day18/example" + instructions := ReadInstructionas2(filename) h, w := calcHeightWidth(instructions) - log.Printf("read %d instructions", len(instructions)) + log.Printf("read %+v instructions", instructions) field := CreateField(h, w) log.Println("created field") // fmt.Println(field.String()) - field.digByInstructions(instructions) + // fmt.Println(field.String()) // WriteToFile("borders.txt", field.String()) // convert -size 3000x6000 xc:white -font "FreeMono" -pointsize 13 -fill black -draw @borders.txt borders.png - // fmt.Println(field.String()) - // i'll start at (0,0), let's first just dig out the thing and check result field.digInsides() + // fmt.Println(field.Height, field.Width) // WriteToFile("fulldug.txt", field.String()) // convert -size 3000x6000 xc:white -font "FreeMono" -pointsize 13 -fill black -draw @fulldug.txt fulldug.png - // fmt.Println(field.Height, field.Width) return field.countDugOut() } @@ -115,6 +113,43 @@ func ReadInstruction(line string) Instruction { return Instruction{Direction: direction, Steps: steps, Color: color} } +func ReadInstructionas2(filename string) (result []Instruction) { + bytes, err := os.ReadFile(filename) + if err != nil { + panic(fmt.Sprint("error reading file: ", filename)) + } + text := strings.TrimSpace(string(bytes)) + for _, line := range strings.Split(text, "\n") { + result = append(result, ReadInstruction2(line)) + } + + return +} + +func ReadInstruction2(line string) Instruction { + fields := strings.Fields(line) + + hexDist := fields[2][2 : len(fields[2])-2] + hexDirection := fields[2][len(fields[2])-2:len(fields[2])-1] + var direction Direction + switch hexDirection { + case "0": direction = Rightward + case "1": direction = Downward + case "2": direction = Leftward + case "3": direction = Upward + } + + dist, err := strconv.ParseUint(hexDist, 16, 64) + if err != nil { + panic(err) + } + + return Instruction{ + Steps: int(dist), + Direction: direction, + } +} + func calcHeightWidth(instructions []Instruction) (height, width int) { movements := make(map[Direction]int) for _, instr := range instructions {