advent2022

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

commit c7543d3b8a5e1fdadbb52d02c8eeee9a2c2e7e6e
parent 2453331dde52f6afed6cb4d9512de39b7b3196af
Author: bsandro <email@bsandro.tech>
Date:   Sun, 18 Dec 2022 10:13:49 +0200

Day 17 part 2 (dirty)

Diffstat:
Mday17/main.go | 124+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
Aday17/task.txt | 348+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 453 insertions(+), 19 deletions(-)

diff --git a/day17/main.go b/day17/main.go @@ -31,18 +31,96 @@ func day17(input_file string) { CreateBox(), } + dsum:=0 + for _,d:=range dirs{ + if d=='<' { + dsum-- + } else if d=='>' { + dsum++ + } + } + //fmt.Println("dsum:",dsum) + field := Field{width: 7, points: make(map[Pos]bool)} - di := 0 - si := 0 - i := 0 + var di int64 = -1 + 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 + } + return ret + } + var cachesum Pos for { if i == 2022 { + fmt.Println("Part 1:", field.maxY) + } + if i == 1000000000000 { + fmt.Println("Part 2:", field.maxY) break } + si++ + 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) + //} + + 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) + } + } + } + // spawning new block p := Pos{x: 2, y: field.maxY + 3} for { // wind + di++ + if 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) @@ -62,11 +140,7 @@ func day17(input_file string) { //log.Fatal("wind direction is invalid:", d) //fmt.Printf("can't move x:%d,mx:%d\n", p.x,p.x+shapes[si].width) } - di++ - if di >= len(dirs)-1 { // have to account for the newline at the end of the string - //fmt.Println("wrapping up wind direction") - di = 0 - } + // try moving down p1 := Pos{x: p.x, y: p.y - 1} occ := shapes[si].GetOccupied(p1) @@ -77,6 +151,22 @@ 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 + //} + } + oy=field.maxY //fmt.Println("max y:", field.maxY) break } else { @@ -85,18 +175,13 @@ func day17(input_file string) { } } i++ - si++ - if si >= len(shapes) { - si = 0 - } + //field.Render(nil) } - - fmt.Println("max y:", field.maxY) } type Pos struct { - x, y int + x, y int64 } type Points []Pos @@ -110,9 +195,9 @@ func (pts Points) Has(p Pos) bool { } type Field struct { - width int + width int64 points map[Pos]bool - maxY int + maxY int64 } func (f Field) Has(pts Points) bool { @@ -132,7 +217,8 @@ func (f Field) Add(pts Points) { func (f Field) Render(occ Points) { for y := f.maxY + 10; y >= 0; y-- { fmt.Print("|") - for x := 0; x < f.width; x++ { + var x int64 + for x = 0; x < f.width; x++ { p := Pos{x: x, y: y} if f.points[p] { fmt.Print("#") @@ -148,7 +234,7 @@ func (f Field) Render(occ Points) { } type Shape struct { - width, height int + width, height int64 occupied Points } diff --git a/day17/task.txt b/day17/task.txt @@ -0,0 +1,348 @@ +--- Day 17: Pyroclastic Flow --- + +Your handheld device has located an alternative exit from the cave for you and the elephants. The ground is rumbling almost continuously now, but the strange valves bought you some time. It's definitely getting warmer in here, though. + +The tunnels eventually open into a very tall, narrow chamber. Large, oddly-shaped rocks are falling into the chamber from above, presumably due to all the rumbling. If you can't work out where the rocks will fall next, you might be crushed! + +The five types of rocks have the following peculiar shapes, where # is rock and . is empty space: + +#### + +.#. +### +.#. + +..# +..# +### + +# +# +# +# + +## +## + +The rocks fall in the order shown above: first the - shape, then the + shape, and so on. Once the end of the list is reached, the same order repeats: the - shape falls first, sixth, 11th, 16th, etc. + +The rocks don't spin, but they do get pushed around by jets of hot gas coming out of the walls themselves. A quick scan reveals the effect the jets of hot gas will have on the rocks as they fall (your puzzle input). + +For example, suppose this was the jet pattern in your cave: + +>>><<><>><<<>><>>><<<>>><<<><<<>><>><<>> + +In jet patterns, < means a push to the left, while > means a push to the right. The pattern above means that the jets will push a falling rock right, then right, then right, then left, then left, then right, and so on. If the end of the list is reached, it repeats. + +The tall, vertical chamber is exactly seven units wide. Each rock appears so that its left edge is two units away from the left wall and its bottom edge is three units above the highest rock in the room (or the floor, if there isn't one). + +After a rock appears, it alternates between being pushed by a jet of hot gas one unit (in the direction indicated by the next symbol in the jet pattern) and then falling one unit down. If any movement would cause any part of the rock to move into the walls, floor, or a stopped rock, the movement instead does not occur. If a downward movement would have caused a falling rock to move into the floor or an already-fallen rock, the falling rock stops where it is (having landed on something) and a new rock immediately begins falling. + +Drawing falling rocks with @ and stopped rocks with #, the jet pattern in the example above manifests as follows: + +The first rock begins falling: +|..@@@@.| +|.......| +|.......| +|.......| ++-------+ + +Jet of gas pushes rock right: +|...@@@@| +|.......| +|.......| +|.......| ++-------+ + +Rock falls 1 unit: +|...@@@@| +|.......| +|.......| ++-------+ + +Jet of gas pushes rock right, but nothing happens: +|...@@@@| +|.......| +|.......| ++-------+ + +Rock falls 1 unit: +|...@@@@| +|.......| ++-------+ + +Jet of gas pushes rock right, but nothing happens: +|...@@@@| +|.......| ++-------+ + +Rock falls 1 unit: +|...@@@@| ++-------+ + +Jet of gas pushes rock left: +|..@@@@.| ++-------+ + +Rock falls 1 unit, causing it to come to rest: +|..####.| ++-------+ + +A new rock begins falling: +|...@...| +|..@@@..| +|...@...| +|.......| +|.......| +|.......| +|..####.| ++-------+ + +Jet of gas pushes rock left: +|..@....| +|.@@@...| +|..@....| +|.......| +|.......| +|.......| +|..####.| ++-------+ + +Rock falls 1 unit: +|..@....| +|.@@@...| +|..@....| +|.......| +|.......| +|..####.| ++-------+ + +Jet of gas pushes rock right: +|...@...| +|..@@@..| +|...@...| +|.......| +|.......| +|..####.| ++-------+ + +Rock falls 1 unit: +|...@...| +|..@@@..| +|...@...| +|.......| +|..####.| ++-------+ + +Jet of gas pushes rock left: +|..@....| +|.@@@...| +|..@....| +|.......| +|..####.| ++-------+ + +Rock falls 1 unit: +|..@....| +|.@@@...| +|..@....| +|..####.| ++-------+ + +Jet of gas pushes rock right: +|...@...| +|..@@@..| +|...@...| +|..####.| ++-------+ + +Rock falls 1 unit, causing it to come to rest: +|...#...| +|..###..| +|...#...| +|..####.| ++-------+ + +A new rock begins falling: +|....@..| +|....@..| +|..@@@..| +|.......| +|.......| +|.......| +|...#...| +|..###..| +|...#...| +|..####.| ++-------+ + +The moment each of the next few rocks begins falling, you would see this: + +|..@....| +|..@....| +|..@....| +|..@....| +|.......| +|.......| +|.......| +|..#....| +|..#....| +|####...| +|..###..| +|...#...| +|..####.| ++-------+ + +|..@@...| +|..@@...| +|.......| +|.......| +|.......| +|....#..| +|..#.#..| +|..#.#..| +|#####..| +|..###..| +|...#...| +|..####.| ++-------+ + +|..@@@@.| +|.......| +|.......| +|.......| +|....##.| +|....##.| +|....#..| +|..#.#..| +|..#.#..| +|#####..| +|..###..| +|...#...| +|..####.| ++-------+ + +|...@...| +|..@@@..| +|...@...| +|.......| +|.......| +|.......| +|.####..| +|....##.| +|....##.| +|....#..| +|..#.#..| +|..#.#..| +|#####..| +|..###..| +|...#...| +|..####.| ++-------+ + +|....@..| +|....@..| +|..@@@..| +|.......| +|.......| +|.......| +|..#....| +|.###...| +|..#....| +|.####..| +|....##.| +|....##.| +|....#..| +|..#.#..| +|..#.#..| +|#####..| +|..###..| +|...#...| +|..####.| ++-------+ + +|..@....| +|..@....| +|..@....| +|..@....| +|.......| +|.......| +|.......| +|.....#.| +|.....#.| +|..####.| +|.###...| +|..#....| +|.####..| +|....##.| +|....##.| +|....#..| +|..#.#..| +|..#.#..| +|#####..| +|..###..| +|...#...| +|..####.| ++-------+ + +|..@@...| +|..@@...| +|.......| +|.......| +|.......| +|....#..| +|....#..| +|....##.| +|....##.| +|..####.| +|.###...| +|..#....| +|.####..| +|....##.| +|....##.| +|....#..| +|..#.#..| +|..#.#..| +|#####..| +|..###..| +|...#...| +|..####.| ++-------+ + +|..@@@@.| +|.......| +|.......| +|.......| +|....#..| +|....#..| +|....##.| +|##..##.| +|######.| +|.###...| +|..#....| +|.####..| +|....##.| +|....##.| +|....#..| +|..#.#..| +|..#.#..| +|#####..| +|..###..| +|...#...| +|..####.| ++-------+ + +To prove to the elephants your simulation is accurate, they want to know how tall the tower will get after 2022 rocks have stopped (but before the 2023rd rock begins falling). In this example, the tower of rocks will be 3068 units tall. + +How many units tall will the tower of rocks be after 2022 rocks have stopped falling? + +Your puzzle answer was 3227. +--- Part Two --- + +The elephants are not impressed by your simulation. They demand to know how tall the tower will be after 1000000000000 rocks have stopped! Only then will they feel confident enough to proceed through the cave. + +In the example above, the tower would be 1514285714288 units tall! + +How tall will the tower be after 1000000000000 rocks have stopped? + +Your puzzle answer was 1597714285698.