From 53da8720bd18c695342ea63a4feee1a8843bc725 Mon Sep 17 00:00:00 2001 From: RhiobeT Date: Wed, 14 Dec 2022 15:33:49 +0100 Subject: [PATCH] Day 14 --- day13/common/distress.go | 4 +- day14/common/sand.go | 143 +++++++++++++++++++++++++++++++++++++++ day14/ex1/main.go | 24 +++++++ day14/ex2/main.go | 26 +++++++ 4 files changed, 195 insertions(+), 2 deletions(-) create mode 100644 day14/common/sand.go create mode 100644 day14/ex1/main.go create mode 100644 day14/ex2/main.go diff --git a/day13/common/distress.go b/day13/common/distress.go index a9d54be..a8f3a42 100644 --- a/day13/common/distress.go +++ b/day13/common/distress.go @@ -1,8 +1,8 @@ package common import ( - "bufio" - "strconv" + "bufio" + "strconv" ) diff --git a/day14/common/sand.go b/day14/common/sand.go new file mode 100644 index 0000000..466aba0 --- /dev/null +++ b/day14/common/sand.go @@ -0,0 +1,143 @@ +package common + +import ( + "bufio" + "fmt" + "strconv" + "strings" +) + + +type Tile int + +const ( + Air Tile = iota + Rock + Sand +) + + +type Map [][]Tile + +func NewMap() *Map { + newMap := Map(make([][]Tile, 1000)) + for i := 0; i < 1000; i++ { + newMap[i] = make([]Tile, 1000) + for j := 0; j < 1000; j++ { + newMap[i][j] = Air + } + } + return &newMap +} + +func (tileMap *Map) AddRockStructure(structure RockStructure) { + var yStart int + var yEnd int + var xStart int + var xEnd int + + if structure.yEnd > structure.yStart { + yStart = structure.yStart + yEnd = structure.yEnd + } else { + yStart = structure.yEnd + yEnd = structure.yStart + } + if structure.xEnd > structure.xStart { + xStart = structure.xStart + xEnd = structure.xEnd + } else { + xStart = structure.xEnd + xEnd = structure.xStart + } + + for i := yStart; i <= yEnd; i++ { + for j := xStart; j <= xEnd; j++ { + (*tileMap)[i][j] = Rock + } + } +} + +func (tileMap *Map) DropSand() bool { + rest := false + j := 500 + for i := 0; i < len(*tileMap)-1 && !rest; i++ { + if (*tileMap)[i+1][j] != Air { + if (*tileMap)[i+1][j-1] == Air { + j-- + } else if (*tileMap)[i+1][j+1] == Air { + j++ + } else { + (*tileMap)[i][j] = Sand + rest = true + } + } + } + return rest +} + +func (tileMap *Map) AddFloor() { + currFloor := 0 + for i := 0; i < len(*tileMap); i++ { + for j := 0; j < len((*tileMap)[i]); j++ { + if (*tileMap)[i][j] == Rock && i > currFloor { + currFloor = i + } + } + } + currFloor += 2 + + for j := 0; j < len((*tileMap)[currFloor]); j++ { + (*tileMap)[currFloor][j] = Rock + } +} + +func (tileMap *Map) IsSourceBlocked() bool { + return (*tileMap)[0][500] != Air +} + +func (tileMap *Map) Print(xMin, xMax, yMin, yMax int) { + for i := yMin; i <= yMax; i++ { + for j := xMin; j <= xMax; j++ { + switch (*tileMap)[i][j] { + case Air: + fmt.Print(".") + case Rock: + fmt.Print("#") + case Sand: + fmt.Print("o") + } + } + fmt.Println() + } +} + +type RockStructure struct { + xStart int + xEnd int + yStart int + yEnd int +} + + +func Parse(scanner bufio.Scanner) []RockStructure { + rockStructures := []RockStructure{} + + for scanner.Scan() { + line := scanner.Text() + splittedLine := strings.Split(line, " -> ") + for i := 0; i < len(splittedLine) - 1; i++ { + splittedStructureStart := strings.Split(splittedLine[i], ",") + splittedStructureEnd := strings.Split(splittedLine[i+1], ",") + + xStart, _ := strconv.Atoi(splittedStructureStart[0]) + yStart, _ := strconv.Atoi(splittedStructureStart[1]) + xEnd, _ := strconv.Atoi(splittedStructureEnd[0]) + yEnd, _ := strconv.Atoi(splittedStructureEnd[1]) + + rockStructures = append(rockStructures, RockStructure{xStart, xEnd, yStart, yEnd}) + } + } + + return rockStructures +} diff --git a/day14/ex1/main.go b/day14/ex1/main.go new file mode 100644 index 0000000..961202b --- /dev/null +++ b/day14/ex1/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "aoc2022/day14/common" + "bufio" + "fmt" + "os" +) + +func main() { + rockStructures := common.Parse(*bufio.NewScanner(os.Stdin)) + tileMap := common.NewMap() + + for _, rockStructure := range rockStructures { + tileMap.AddRockStructure(rockStructure) + } + + units := 0 + for tileMap.DropSand() { + units++ + } + + fmt.Println(units) +} diff --git a/day14/ex2/main.go b/day14/ex2/main.go new file mode 100644 index 0000000..92b6219 --- /dev/null +++ b/day14/ex2/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "aoc2022/day14/common" + "bufio" + "fmt" + "os" +) + +func main() { + rockStructures := common.Parse(*bufio.NewScanner(os.Stdin)) + tileMap := common.NewMap() + + for _, rockStructure := range rockStructures { + tileMap.AddRockStructure(rockStructure) + } + tileMap.AddFloor() + + units := 0 + for !tileMap.IsSourceBlocked() { + tileMap.DropSand() + units++ + } + + fmt.Println(units) +}