advent2022

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

commit 7732ec3c80ee414dad6b88aa7e8a14bb64b4b106
parent f23ede1b9a424b85bf48bccb95ed15399a2cb27a
Author: bsandro <email@bsandro.tech>
Date:   Wed, 14 Dec 2022 11:57:09 +0000

Day 14 part 2

Diffstat:
Mday14/main.go | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
1 file changed, 70 insertions(+), 20 deletions(-)

diff --git a/day14/main.go b/day14/main.go @@ -16,30 +16,27 @@ type Pos struct { type Shape []Pos type Shapes []Shape -func (ss Shapes) GetMinX() int { - x := -1 +func (ss Shapes) GetLeftmost() Pos { + p := Pos{x: -1} for _, s := range ss { for _, e := range s { - if x == -1 { - x = e.x - } - if e.x < x { - x = e.x + if p.x == -1 || e.x < p.x { + p = e } } } - return x + return p } -func (ss Shapes) GetMaxX() int { - x := 0 +func (ss Shapes) GetRightmost() Pos { + var p Pos for _, s := range ss { for _, e := range s { - if e.x > x { - x = e.x + if e.x > p.x { + p = e } } } - return x + return p } func (ss Shapes) GetMaxY() int { y := 0 @@ -61,7 +58,7 @@ func (grid Grid) String() string { for x := 0; x < len(grid[y]); x++ { switch grid[y][x] { case 0: - s += "." + s += " " case 1: s += "#" case 2: @@ -147,14 +144,14 @@ func day14(input_file string) { } height := shapes.GetMaxY() + 1 - offsetX := shapes.GetMinX() - width := shapes.GetMaxX() - offsetX + 2 + offsetX := shapes.GetLeftmost().x + width := shapes.GetRightmost().x - offsetX + 2 var grid Grid grid.Init(width, height) for _, s := range shapes { grid.AddShape(s, offsetX) } - fmt.Printf("%v\n", grid) + //fmt.Printf("%v\n", grid) i := 0 for { @@ -169,7 +166,6 @@ func day14(input_file string) { p.y += 1 p.x += 1 } else { - //log.Fatal("invalid point") break } } @@ -181,6 +177,60 @@ func day14(input_file string) { i++ //fmt.Printf("%v\n", grid) } - fmt.Printf("%v\n", grid) - fmt.Println("grains of sand:", i) + //fmt.Printf("%v\n", grid) + fmt.Println("grains of sand 1:", i) + + var grid2 Grid + height += 2 + leftmost := shapes.GetLeftmost() + rightmost := shapes.GetRightmost() + minx := leftmost.x - (height - leftmost.y) + maxx := rightmost.x + (height - rightmost.y) + if minx > 500-height { + minx = 500 - height + } + if maxx < 500+height { + maxx = 500 + height + } + offsetX = minx + width = maxx - minx + 2 + ///fmt.Printf("minx:%d, maxx:%d,offsetx:%d,width:%d,height:%d\n",minx,maxx,offsetX,width,height) + if offsetX < 0 { + offsetX = 0 + } + grid2.Init(width, height) + for _, s := range shapes { + grid2.AddShape(s, offsetX) + } + floor := Shape{} + floor = append(floor, Pos{x: 0, y: height - 1}) + floor = append(floor, Pos{x: width - 1, y: height - 1}) + grid2.AddShape(floor, 0) + //fmt.Printf("%v\n", grid2) + i = 0 + for { + p := Pos{x: 500 - offsetX, y: 0} + for p.x > 0 && p.x < width-1 && p.y < height-1 { + if grid2[p.y+1][p.x] == 0 { + p.y += 1 + } else if grid2[p.y+1][p.x-1] == 0 { + p.y += 1 + p.x -= 1 + } else if grid2[p.y+1][p.x+1] == 0 { + p.y += 1 + p.x += 1 + } else { + break + } + } + if p.y < height-1 && grid2[p.y][p.x] == 0 { + grid2[p.y][p.x] = 2 + } else { + break + } + i++ + //fmt.Printf("%v\n", grid2) + } + //fmt.Printf("%v\n", grid2) + fmt.Println("grains of sand 2:", i) }