day18, part1
This commit is contained in:
@@ -10,22 +10,30 @@ import (
|
||||
)
|
||||
|
||||
func Run() int {
|
||||
fmt.Println("hello day 18")
|
||||
log.Println("hello day 18")
|
||||
log.Println("problem of lagoon bgins")
|
||||
filename := "day18/example"
|
||||
filename := "day18/input"
|
||||
instructions := ReadInstructionas(filename)
|
||||
h, w := calcHeightWidth(instructions)
|
||||
field := CreateField(h, w)
|
||||
log.Printf("read %d instructions", len(instructions))
|
||||
|
||||
fmt.Println(field.String())
|
||||
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.String())
|
||||
// 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()
|
||||
}
|
||||
@@ -150,6 +158,7 @@ func (c Coord)applyDirection(d Direction) Coord {
|
||||
|
||||
type Cell struct {
|
||||
IsDug bool
|
||||
ToBeDug bool
|
||||
Walls map[Direction]string
|
||||
Coord Coord
|
||||
}
|
||||
@@ -187,6 +196,7 @@ func (f *Field)digByInstructions(instructions []Instruction) {
|
||||
f.Cells[row][col].IsDug = true
|
||||
|
||||
for _, instruction := range instructions {
|
||||
log.Printf("starting instruction %+v", instruction)
|
||||
for i := 0; i < instruction.Steps; i++ {
|
||||
runnerCoord = runnerCoord.applyDirection(instruction.Direction)
|
||||
row, col := f.coordToIndices(runnerCoord)
|
||||
@@ -197,17 +207,65 @@ func (f *Field)digByInstructions(instructions []Instruction) {
|
||||
}
|
||||
|
||||
func (f *Field)String() string {
|
||||
s := "\n"
|
||||
for _, row := range f.Cells {
|
||||
s := "text 15,15 \""
|
||||
firstNonemptyRow, lastNonemptyRow := 0,0
|
||||
seenInRows := false
|
||||
firstNonemptyCol, lastNonemptyCol := 0,0
|
||||
seenInCols := false
|
||||
|
||||
for i, row := range f.Cells {
|
||||
seenInRow := false
|
||||
for _, cell := range row {
|
||||
if cell.IsDug {
|
||||
s += "#"
|
||||
seenInRow = true
|
||||
} else {
|
||||
s += "."
|
||||
}
|
||||
}
|
||||
|
||||
seenInRows = seenInRows || seenInRow
|
||||
if seenInRow {
|
||||
lastNonemptyRow = i
|
||||
}
|
||||
if !seenInRows {
|
||||
firstNonemptyRow = i
|
||||
}
|
||||
}
|
||||
|
||||
for col := 0; col < f.Width; col++ {
|
||||
seenInCol := false
|
||||
for row := 0; row < f.Height; row++ {
|
||||
cell := f.Cells[row][col]
|
||||
if cell.IsDug {
|
||||
seenInCol = true
|
||||
}
|
||||
}
|
||||
seenInCols = seenInCols || seenInCol
|
||||
if seenInCol {
|
||||
lastNonemptyCol = col
|
||||
}
|
||||
if !seenInCols {
|
||||
firstNonemptyCol = col
|
||||
}
|
||||
}
|
||||
|
||||
rowLen := lastNonemptyCol - firstNonemptyCol + 1
|
||||
for i := firstNonemptyRow; i <= lastNonemptyRow; i++ {
|
||||
rowChars := make([]rune, rowLen)
|
||||
for col := firstNonemptyCol; col <= lastNonemptyCol; col++ {
|
||||
cell := f.Cells[i][col]
|
||||
if cell.IsDug {
|
||||
rowChars[col-firstNonemptyCol] = '#'
|
||||
} else if cell.ToBeDug {
|
||||
rowChars[col-firstNonemptyCol] = '@'
|
||||
} else {
|
||||
rowChars[col-firstNonemptyCol] = '.'
|
||||
}
|
||||
}
|
||||
|
||||
s += string(rowChars)
|
||||
s += "\n"
|
||||
}
|
||||
s += "\""
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -230,7 +288,7 @@ func (f *Field)digInsides() {
|
||||
} else {
|
||||
// not a dug out cell, maybe inside and needs to be dug out
|
||||
if isInside {
|
||||
cellPtr.IsDug = true
|
||||
cellPtr.ToBeDug = true
|
||||
}
|
||||
if rightCell.IsDug {
|
||||
nextUpCell := f.Cells[row-1][col+1]
|
||||
@@ -247,10 +305,24 @@ func (f *Field)digInsides() {
|
||||
func (f *Field)countDugOut() (result int) {
|
||||
for _, row := range f.Cells {
|
||||
for _, cell := range row {
|
||||
if cell.IsDug {
|
||||
if cell.IsDug || cell.ToBeDug {
|
||||
result += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func WriteToFile(filename string, content string) {
|
||||
fileBorder, err := os.Create(filename)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer func(){
|
||||
if err := fileBorder.Close(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
|
||||
fileBorder.WriteString(content)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user