advent2022

Advent of Code 2022 Solutions
git clone git://bsandro.tech/advent2022
Log | Files | Refs | README | LICENSE

commit f23ede1b9a424b85bf48bccb95ed15399a2cb27a
parent 6bf87ae41fc0411df5cf2016c74b5fe63efc90c5
Author: bsandro <email@bsandro.tech>
Date:   Wed, 14 Dec 2022 09:34:35 +0000

Day 14 part 1

Diffstat:
Mday14/main.go | 32++++++++++++++++++++++++++++++--
1 file changed, 30 insertions(+), 2 deletions(-)

diff --git a/day14/main.go b/day14/main.go @@ -65,7 +65,7 @@ func (grid Grid) String() string { case 1: s += "#" case 2: - s += "O" + s += "o" default: s += " " } @@ -148,11 +148,39 @@ func day14(input_file string) { height := shapes.GetMaxY() + 1 offsetX := shapes.GetMinX() - width := shapes.GetMaxX() - offsetX + 1 + width := shapes.GetMaxX() - offsetX + 2 var grid Grid grid.Init(width, height) for _, s := range shapes { grid.AddShape(s, offsetX) } fmt.Printf("%v\n", grid) + + i := 0 + for { + p := Pos{x: 500 - offsetX, y: 1} + for p.x > 0 && p.x < width-1 && p.y < height-1 { + if grid[p.y+1][p.x] == 0 { + p.y += 1 + } else if grid[p.y+1][p.x-1] == 0 { + p.y += 1 + p.x -= 1 + } else if grid[p.y+1][p.x+1] == 0 { + p.y += 1 + p.x += 1 + } else { + //log.Fatal("invalid point") + break + } + } + if p.x > 0 && p.x < width-1 && p.y < height-1 && grid[p.y][p.x] == 0 { + grid[p.y][p.x] = 2 + } else { + break + } + i++ + //fmt.Printf("%v\n", grid) + } + fmt.Printf("%v\n", grid) + fmt.Println("grains of sand:", i) }