day18: reading instructions & hanging
This commit is contained in:
parent
1d027d57fc
commit
d799b122ce
@ -12,28 +12,26 @@ import (
|
|||||||
func Run() int {
|
func Run() int {
|
||||||
log.Println("hello day 18")
|
log.Println("hello day 18")
|
||||||
log.Println("problem of lagoon bgins")
|
log.Println("problem of lagoon bgins")
|
||||||
filename := "day18/input"
|
filename := "day18/example"
|
||||||
instructions := ReadInstructionas(filename)
|
instructions := ReadInstructionas2(filename)
|
||||||
h, w := calcHeightWidth(instructions)
|
h, w := calcHeightWidth(instructions)
|
||||||
log.Printf("read %d instructions", len(instructions))
|
log.Printf("read %+v instructions", instructions)
|
||||||
|
|
||||||
field := CreateField(h, w)
|
field := CreateField(h, w)
|
||||||
log.Println("created field")
|
log.Println("created field")
|
||||||
|
|
||||||
// fmt.Println(field.String())
|
// fmt.Println(field.String())
|
||||||
|
|
||||||
field.digByInstructions(instructions)
|
field.digByInstructions(instructions)
|
||||||
|
|
||||||
|
// fmt.Println(field.String())
|
||||||
// WriteToFile("borders.txt", field.String())
|
// WriteToFile("borders.txt", field.String())
|
||||||
// convert -size 3000x6000 xc:white -font "FreeMono" -pointsize 13 -fill black -draw @borders.txt borders.png
|
// 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()
|
field.digInsides()
|
||||||
|
|
||||||
|
// fmt.Println(field.Height, field.Width)
|
||||||
// WriteToFile("fulldug.txt", field.String())
|
// WriteToFile("fulldug.txt", field.String())
|
||||||
// convert -size 3000x6000 xc:white -font "FreeMono" -pointsize 13 -fill black -draw @fulldug.txt fulldug.png
|
// 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()
|
return field.countDugOut()
|
||||||
}
|
}
|
||||||
@ -115,6 +113,43 @@ func ReadInstruction(line string) Instruction {
|
|||||||
return Instruction{Direction: direction, Steps: steps, Color: color}
|
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) {
|
func calcHeightWidth(instructions []Instruction) (height, width int) {
|
||||||
movements := make(map[Direction]int)
|
movements := make(map[Direction]int)
|
||||||
for _, instr := range instructions {
|
for _, instr := range instructions {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user