advent2022

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

commit 3ed41539edcb675ec3561bb81a95e96b130ebe75
parent 8848f59b3305813f306fb234d3d3b0a3b13c136d
Author: bsandro <email@bsandro.tech>
Date:   Sun, 18 Dec 2022 14:26:45 +0200

Day 17 slight cleanup

Diffstat:
Mday17/main.go | 132+++++++++++++++++++++++--------------------------------------------------------
1 file changed, 38 insertions(+), 94 deletions(-)

diff --git a/day17/main.go b/day17/main.go @@ -31,28 +31,26 @@ func day17(input_file string) { CreateBox(), } - dsum:=0 - for _,d:=range dirs{ - if d=='<' { + dsum := 0 + for _, d := range dirs { + if d == '<' { dsum-- - } else if d=='>' { + } else if d == '>' { dsum++ } } - //fmt.Println("dsum:",dsum) field := Field{width: 7, points: make(map[Pos]bool)} var di int64 = -1 - var si int64= -1 - var oy int64=0 // old max y + var si int64 = -1 + var oy int64 = 0 // old max y var i int64 = 0 - //fmt.Println("dirs:", len(dirs)-1, dirs[len(dirs)-1]) cache := make(map[string]Pos) getCacheSum := func() Pos { var ret Pos - for _,v:=range cache{ - ret.x +=v.x - ret.y+=v.y + for _, v := range cache { + ret.x += v.x + ret.y += v.y } return ret } @@ -69,49 +67,33 @@ func day17(input_file string) { if si >= int64(len(shapes)) { si %= int64(len(shapes)) } - odi:=di - - if i > int64(len(dirs)*2) && i > 20220{ - if cachesum.x==0||cachesum.y==0{ - cachesum=getCacheSum() - } - var mul int64 = (1000000000000-i) / int64(len(cache)) - //fmt.Println("mul:",mul) - //fmt.Println("cache:",len(cache)) - var ii int64 = i + int64(len(cache))*mul + 1 - //var dii int64 = cachesum.y*mul - if mul > 0 { - //fmt.Printf("i: %d -> %d\n", i, ii) - //fmt.Printf("di: %d -> %d (%d)\n", di, dii, dii%int64(len(dirs)-1)) - //fmt.Printf("maxy: %d -> %d\n", field.maxY, field.maxY+cachesum.x*mul) - field.maxY += cachesum.x*mul - i=ii - cpos1:=fmt.Sprintf("%d-%d",si,odi) - di= cache[cpos1].y //% int64(len(dirs)-1) - field.maxY +=cache[cpos1].x - //si++ - //if si>=int64(len(shapes)){ - // si%=int64(len(shapes)) - //} - //di++ - //if di>=int64(len(dirs)-1) { - // di%=int64(len(dirs)-1) - //} - + odi := di + + if i > int64(len(dirs)*2) && i > 20220 { + if cachesum.x == 0 || cachesum.y == 0 { + cachesum = getCacheSum() + } + var mul int64 = (1000000000000 - i) / int64(len(cache)) + var ii int64 = i + int64(len(cache))*mul + 1 + if mul > 0 { + field.maxY += cachesum.x * mul + i = ii + cpos1 := fmt.Sprintf("%d-%d", si, odi) + di = cache[cpos1].y + field.maxY += cache[cpos1].x + continue + } else { + cpos1 := fmt.Sprintf("%d-%d", si, odi) + if c1, e1 := cache[cpos1]; e1 { + field.maxY += c1.x + di = c1.y + i++ continue } else { - //fmt.Println("i, di:",i,di) - cpos1:=fmt.Sprintf("%d-%d",si,odi) - if c1,e1:=cache[cpos1];e1{ - field.maxY += c1.x - di=c1.y - i++ - continue - }else{ - log.Fatal("missing cache:",cpos1) - } + log.Fatal("missing cache:", cpos1) } } + } // spawning new block p := Pos{x: 2, y: field.maxY + 3} @@ -119,11 +101,9 @@ func day17(input_file string) { // wind di++ if di >= int64(len(dirs)-1) { - di %= int64(len(dirs)-1) + di %= int64(len(dirs) - 1) } d := dirs[di] - //fmt.Printf("%d(%v)\n", d,d) - //fmt.Printf("(%d)dir:%c %v\n",len(dirs),d,d) if d == '<' && p.x > 0 { p1 := Pos{x: p.x - 1, y: p.y} occ := shapes[si].GetOccupied(p1) @@ -136,11 +116,8 @@ func day17(input_file string) { if !field.Has(occ) { p.x++ } - } else { - //log.Fatal("wind direction is invalid:", d) - //fmt.Printf("can't move x:%d,mx:%d\n", p.x,p.x+shapes[si].width) } - + // try moving down p1 := Pos{x: p.x, y: p.y - 1} occ := shapes[si].GetOccupied(p1) @@ -151,32 +128,17 @@ func day17(input_file string) { if my > field.maxY { field.maxY = my } - if i>int64(len(dirs)) { - //fmt.Println("real increase:", field.maxY-oy,si,odi) - tp:=fmt.Sprintf("%d-%d",si,odi) - /*if ca,exists:=cache[tp];exists{ - //fmt.Printf("%d %d %d\n", i, field.maxY-ca, ca) - //log.Fatal("kek") - if ca!=field.maxY-oy { - fmt.Printf("[%d] %d!=%d\n",i, ca, field.maxY-oy) - cache[tp]=field.maxY-oy - } - } else {*/ - cache[tp] = Pos{x:field.maxY-oy,y:di} - //cache[tp].dd=di - //} + if i > int64(len(dirs)) { + tp := fmt.Sprintf("%d-%d", si, odi) + cache[tp] = Pos{x: field.maxY - oy, y: di} } - oy=field.maxY - //fmt.Println("max y:", field.maxY) + oy = field.maxY break } else { p = p1 - //field.Render(occ) } } i++ - - //field.Render(nil) } } @@ -214,24 +176,6 @@ func (f Field) Add(pts Points) { f.points[p] = true } } -func (f Field) Render(occ Points) { - for y := f.maxY + 10; y >= 0; y-- { - fmt.Print("|") - var x int64 - for x = 0; x < f.width; x++ { - p := Pos{x: x, y: y} - if f.points[p] { - fmt.Print("#") - } else if occ.Has(p) { - fmt.Print("@") - } else { - fmt.Print(".") - } - } - fmt.Print("|\n") - } - fmt.Println("`-------'") -} type Shape struct { width, height int64