diff options
| author | An0nSaiko <porfeas12@gmail.com> | 2022-12-17 19:54:36 +0200 |
|---|---|---|
| committer | An0nSaiko <porfeas12@gmail.com> | 2022-12-17 19:54:36 +0200 |
| commit | 0b30ee96367dcf41a854e0f7b8989ec641cd384e (patch) | |
| tree | d7f60639d7b8e21ad9ca11ffe083baaa567c6b89 | |
| parent | Day 16 (part1 & part2). Part2 is too slow and only works for the example (diff) | |
| download | aoc22-0b30ee96367dcf41a854e0f7b8989ec641cd384e.tar.gz aoc22-0b30ee96367dcf41a854e0f7b8989ec641cd384e.zip | |
Day 17
| -rw-r--r-- | day17/example.txt | 1 | ||||
| -rw-r--r-- | day17/input.txt | 1 | ||||
| -rw-r--r-- | day17/solution.nim | 143 |
3 files changed, 145 insertions, 0 deletions
diff --git a/day17/example.txt b/day17/example.txt new file mode 100644 index 0000000..97a1aa1 --- /dev/null +++ b/day17/example.txt | |||
| @@ -0,0 +1 @@ | |||
| >>><<><>><<<>><>>><<<>>><<<><<<>><>><<>> | |||
diff --git a/day17/input.txt b/day17/input.txt new file mode 100644 index 0000000..f5f3155 --- /dev/null +++ b/day17/input.txt | |||
| @@ -0,0 +1 @@ | |||
| >>>><>>>><>>><<<<>>><<<>>>><>>>><<<<>>><<>>>><<<><><<<>>>><<>><>>>><<><<>><<>><><<>><>>><>>>><>>><>>>><<><><>>><<<><<<<>>><<<><<>>>><<>>><>>><<<>><<>>><>><<>><<<<><<<<>>><<>>><<<<><>>>><<>>>><>>><<>>><<>>><><<<<>><<><<<<>>><>>>><<<><<<<><<<>>><>>>><<>>>><>>>><><<<<>><<>>><<<>><<<<>><<<><<<<>><>>><<>>><<<<>>>><<<<>>>><<<<>><<>>><<<><<<>><>>>><<>><<<>>><<<<>>>><<<<>>><<<<>>><<<<><<>><>><<<<>>><>>>><<<<>><<<>><<<><<<<><<>>>><<<<>>>><<<>>>><<<<>><<>>><>><<<<>>><>>>><<<>>>><<<>><<<>><><<<><<>>><<<<>><<<<><<>>>><>>>><<<><><<>>>><<<>><<<>>>><<<>>><<<<><<><<>>><<<<><<<>><<<<>>>><<<<><<<<>><<>>>><<>>>><<<<>><<>><<<<>>>><<<><>><<<>>>><<<<><<<>><<<<>><<<>><<<<>>>><<<>><>>><<<<>>><>>>><<<>>>><>><<<>>><>>>><<<>>><>>><><<>>>><>>><<<<><<<<>><<>>>><<<<>>><<<>>>><>><<<><<<<>>>><<<<>>>><<<>>>><<>>><>>>><<<><<><<<<>>><<<<>>>><>>><<<<>><<><><<<><<>><<<<>>><<><<>><<><<<>>><<<>><<<<>><<<<><<<>>><>><>><<<<>>><<><<<<>>><>>>><>>>><<<<>>><<>><>>><<><>>><<<<>>><<<<><<<>>><<>><>>><<<<><<>>><<>><<<<><<>><<>>>><>>>><<>><<>>>><<<><><<<<><<<<>>><<<>>><<<>>><><<<<>><<<>>>><<<><<><<<<>>><<>>>><<><<>>>><<<>>>><<<><<<<>><<<<>>>><<<>>>><<<<><>><<<>>>><>><<<>>><<<<><<<<>>><<<<>><<>>><<<<><<<>>><<>><<<>>><<>><<>>>><<<>><>>><<<<>>><<<>>>><<<<>>>><<>>>><<<<><>>><<<>>><<<><<>><>>>><<><<><<<<><<>><<>><>>>><<<>><<<<>>>><<<>>>><<<><><<><<<<><<><<<<>>>><<<<><>>>><<>>><<<<>>>><<>><<>><<>>>><<<<>><<<>><><<<<>>><>>>><<<>><<<<><<<<><<<><>><<<>><<<<><<>>>><<<<>>><>>><<<<>>><<>>>><<<<>>>><<<<>><<<<>>><<<>>>><<<<>>><<<>><<<>>><<>>>><<<<><<>><<<><>><<<<>>><<<<><<<>><<<>>>><<<<>>><<<<><>><>>>><<<<>>><<<<>><><<>><><<<><<<<><<>><>><<>>><<<>><<>>>><<>>><<<<>><<<<>>>><<<>><<>><><<<<><<<<><<<>>>><<>><<>>><<<>>><<><<><<<>><<><<<<>><>>><<<><<>>>><<<<>><<>><<<>>>><<<<>>>><<><<>>><<>><<>>>><>><<<>><<>>>><<<><><<><<<<><<<><>>><><<><><<>><>><<>>>><<>>>><>>>><<<<>>><>>>><>>><<><>>>><<<<><<<<>><<><<<>>>><>><><>>><<<>>><>>><>><<>>><<<><<<>>><<<<>><<<>>><<>><<<>><<>>>><<<<>><<<><>>>><<<<>>>><<>>><<<<>>>><<<><<>>>><<>>><<<>>><<<>>><>>><>><>>><<<<>>><<>>><<>><<>>>><<<<>>><><<<>>>><><<<<><><<<<><<><<<>>>><<<>>><>><<<>>>><<<<>>><<<<>><<<<>>><>>>><<><>>>><<<<><>>><>>><<<<>>>><<<><>><<><<<>><<<<>><<<<><<<>>>><<>>><<><<<<>><>><>>><<<>>><<<><>>>><><<>>><<>>><<>>>><>><><<<<>><<>><<<<>><<<<>>><>><>><<<><<<<><<<>><<>>><<<<>>><<>>><<<<>>>><>>>><<<<><<<<>>>><<>>><><<><<>>>><<<<>>><<<<>>><<<>>>><<<>><<<<>>><>>>><>><<<>>>><>><<>><<><<>>><>>><<<>>>><><<<>>>><<<><>>>><<<>><<<<>>><<<><<>>><<<>><<<><>>>><<<>><<<>>>><<<<>>>><>><<<>>><<>>><<<>><<<<><>><<>>>><<>><<>><>><<<>>><>><<<<>>><<<<>>>><>><>>><<<>>>><<<<>><><<<><<<<>>><<>>>><<<><<<><<<>>>><<<><<<>><<<<>>><<<<><<<<>><<<<><<<>><>>>><<<>><>><<<><><<<>><<<>>><<<<>>>><<<>><<><>>>><><<<><><>><<>>><>><<<<>>>><<><>><<<<><<>>>><<<>>>><>>><<<>>><<<<><<<<>>><>><<<<>><><>>>><><><>>>><>><<>><<<<>>>><<<><>>><<>>>><>><<<<>>>><<<<><<>>>><>>>><<<<><<<>>><>>>><<>><<<<>><<<<>><><<><<<<><<><<<>><<>><>>><><<>><<<<>><<<<>>><<<>>><<<>>><<><<>><<<<>>>><<<>>><<<<>>>><<><<<<>>><<<<>>><<>>><<>>><<<>>><<<<>>>><>>><<<>>>><<<><><><<>><<>><<<<>>><>>><<<>><>>><<<>>><<<>>><<<<>><<<<>>>><<<>>><<<><<>>><<<>>>><<>><>><<>>>><<<<>>><<>><<><<<>>>><>><<<>>>><<<><<<<>>>><<<<>>><>>>><<<<>>><<>>><>>>><<>>>><<<><<>>>><<><<<><<>>><<<>><<<><<>><<<<>>>><<<<>>><>>>><<<>>><<<<>>><>>><<<<>><<<>>>><<<<><<>>>><<<>>><>><<<>>>><<<<>>>><<<><>><<<>><<<>><><<<><<>><<<>><<<>><>>><<<><>>>><<<>><>><>>><<<<>><<>>><<>>>><><<<<>>><<>><<<>>>><<>><<<<><>>>><<<><<><<><<<>>>><>>><>><<>>>><<>>><<><><<<<>><<>><<<<>>><>>><<<><<<<><>>>><>>>><<>>><<<><<<>>><<<<>><<<>>>><><><<<<><<>>>><>><<<<><<<<>><<>>>><<<>>><<>>>><<<<>>><>><>><<<<>><<<>>><<<<>>>><<<>>>><><<<>>><><>>>><<<<>>><<<><<><<<<>>>><<><<<><<<>>><<<<>>><<<<>>><>>>><<<<><>><<>>><<<>>>><<<><<><<<>>><><<<>>><<<<><><>>>><>><>><<<>><<<><>>>><>>><<<><>><<>>><<><<<<>>>><<<>><<<>><<<>>><<<>><>>>><<>><>>>><<>><>>>><<>>><<>>>><<>><>><>>><<<<>><<>>>><<<<>>><>>><>>><<<<>><<>>><<<<>>><<<<>>><<>>><<<><<<><><<<<>>><<<>>>><><<<><<<>><>>>><<<<>>>><<<>>><<><<>>>><<<><<>>><<<><<<>>><>><<<<><<<>><>><<>>><<>><<<><>>><<>>>><<>>><<<<><<<<>><<<>><>>><<<<><>><<<<>><<<<><<>><>>>><<<<><<<>>><<>>>><<<>><<<><<>>>><<<<>>><<<<>>><<<>>><>><<<<>>><<>>>><>><<>>><<><<<>>>><<<>>>><<>>><<<>>>><<<>>><<<>><<<><<>>><<<>><><<>>><<<><<><<<><<<>><>>>><<>><<><<<><<<<>>>><>>><<<><<>><<>>><<<><<<<>>>><<>>><>>>><<<<><<<<><<>><<>>><<<<>>><><><<<><<<>><<<>>><<>>>><<>>><<>>><<<>>><<><<<<>>><<<>>><><<<<>><<>>>><<<<>>><<>><<>>>><><<<>><>>><<<>><<<><<<>>><<<><<<>>><<<<>>>><<>><<<><>>>><<<<>>>><<<<>><>>>><<><<<>>>><<<>>><<>>><<<<><<<<>>><>>><<<>>><><<<><<>><<>><<<>>><<<>>>><<<>>>><<>>>><<>>><>><<<<>>>><<>><<<>>><<<>>><>>><<<<><<>>><>>>><<<<>>><>><<<<><<<<>>><<<>>>><>><<<<>><<<>>>><<<>>>><>>><<<>><<<>>>><<>>><<<>>>><<<<><<<>><<<><<>>><<<<>>><<<<><>>>><<>>><<><>>>><<>>>><<>>>><>>>><>>>><>>><<<<><<<>><<<><<<<>>>><<<<>><>><<>><<>><<<>>>><<><>>><<<<>>>><<>>><<>>><>>>><>><<<<>><<<<>><<>>><<><<<>>><<<<>><<<<><<>>><<<<><<><<>>><<>>><>>>><<>>>><<<<><>>>><<<<>>>><<<<>>>><<>>>><<<<>>><<>><<<>>><<<>>>><<<><<<<>>><>>><<>>>><><<<<>>><<<>><<<>>><<>>>><<>>><>><<<<>>>><<<<>>><>>><<<>><<>>>><<<>><<<><<<>><<<<>>><<<>><<<>><<<><><<<<>><<>>>><>>><<<>>>><<<>>><<>>><>><<<>>><>><<>>><<<<>><<<>>>><<<<><<<<>><>><<><<<>><<>>><>>><<<><<<<><<<<>>>><<>>>><<<<>>><><<<>>>><>>><<<>>>><>>><<<>>><>>><<<><<<><<<>>>><<<<>>>><>>>><>>><<<>>><<<>><<<<><>>><<<<>><><<<<><<<>>><<<>>>><>>><<>>>><<<>>><>>>><<>>><<<<>>>><>>>><<>>>><<>>><<<<>><<<<>>>><>>>><<<>>>><>>><<><<<><>>>><<<<><>>>><<<><<<><<<><>>><<<<>>>><<<<>>>><>>><<<>>><<>>><<<<>>><<<>><<><<<><><<<<>>>><<<<><<<>>>><<<<>><<<<>>>><<<><<<<>>><<><><<<<>>><<<>>><>>>><<<><<<>><<>>>><<<>><<<>>><<>><<>><<<>>><<><<>><>>><<<<>><><<<><<><><><<<<>>>><<<<><<<><<>>><><<>>>><>><<<<><<>>><>>>><<<<>>><><<>>>><<>>><<<<>>><<<<>>>><<<><<<<>>><<<>><<<<>>><<<><<<><<>><><<>><<<<>><>>>><<<<>><<<>><<<<>>><<<<>>><>><>><<<>><<<><<<<><>><<>><<<<>><<<<>>>><<><<><>>><<>>><<<<>>><<<>>><<><<>>>><<<>>><<<<>>>><<>><<<<>>>><<>><<<><<<>>><<>>><<<<>>><>>>><>>><>>><>>>><>>>><>>><<>><>>><<>><<><<<<>>><>>><<<>><<<<>>><>>>><<>><>><><<<><<<<>><<<>>><<<><<><<<<>><<<<><>>><<<>>><<>><>>><<><<<<>><<>><<<><<>><<>>>><<><<<>>>><<<<>><<<>>><><<>>><><<<<><<<>><<>><><<<<>>>><<>><<<>><<>>><<<<>>>><<<><<<<>>><<>><<>>>><<<<>><<>><<<<><<<>><>>><<<<>><<<<>>>><>>><>>>><<>>><>><>>>><><<<<>><<<<>><>>><<>>>><<<<>>>><>>>><<<><<<><<<<><<<<><<<<>>>><<>>><<>>>><<<<><>>>><>>><<<>>><<<<><<<<>>><<<><<<<>>>><<<<>>><><<>>><<>>><<>>>><<<><<<<>>>><<<>>><<<<>>><<<>>>><<>>>><<<><<<><<<><>><<<<>>>><<<<>>>><>><<<<><<<<><<<>>>><<<<><<>>><<<<>><>><<<><<<<>><<<><>>>><<<<>>><<<<>>>><<>>><<><<<>>>><<<<>>>><<>><<<<>>><<<<>>><<><<<<><<<<>><<<<>>><<<<>>><<<><<<>><<><<<<>>><><<<<><><<<<><<<>>>><<<<><<<<>><<<<>>>><<<<>>>><<>><><<<<>><<<<>>>><<<>><<<<><><<<<><>>>><<<>><>><<>><<<>>>><>>>><>>><<<>>>><><<<<>>>><<<>>><<><<<<>>><<<><<<<>>>><<<<>>>><>>><<><>>><>>><>><>>>><>>>><<<>>>><>>>><<><<>>><<<<><<<>><>>><<<>><<<>><<<>>>><<><<<>>><<<<>>>><<>>>><<<>>><<<<>><<>><>>>><<<><<<>>><>>><><<<<>>>><<<>>>><<>>>><<>><><<<<>>>><<><<<<>>>><<<>>>><><>>><<<<>>>><>>>><<>>><>><<<><<>>>><<><<<<><>>><<><<<>>>><<<>>><<<>><<<>><<>>>><<<<>>><<<<>>>><<>>>><<<<>><<<<>>><<<<><<<<>><<>><<>>>><<<<>><<>>><>>>><><<><>>><<><<<><>>><<><<>><<>>>><<<>><<>>><<<<>>><<<<><<<<><>>>><<>>>><<<<>>>><>>>><><<>><<<<>><<<>><<>><<<<><<>><<<><>><<<>>>><>>>><<<>>>><<<>>><><<<>>>><<<<>>>><<<>>>><<<>><<>>>><>><<<>><>><<<><<<<>>><<<>>>><<><>>>><>>><<>>><>>>><<<<>>>><<<>><<>><<<>><<<<>><<<><<>><<>>><<>>><<<><<>>>><<<>>><<>><><<<<>><<<<>><>>>><<>>>><><>><>>>><<<<>><<<<>><<<<>>>><<>>>><<>>>><<<><<>>><><<>><<<>><>>><<>>><>>><<><>>><<>><<<<>>><<<>><<<><<<>>><<<<>>><>><<<>>><<<><>>><<><<<<><<<<>><>>><>>>><>>>><>>>><<<>>><<<<>>>><>>><<>>><>>><<<<>><<<>><<<<>><<><<<>><<<>>><<<<>><>>>><>>><<<<>><<<<>><<<<>><>>>><>><<>>>><<>>><<<<>>><<<<><<<>>><<>>>><<>><<>><<<><<>>>><<<>>>><>>><><<>>>><<>>><>>><<>>><>>>><<<<>><<>>><<<>><<>><<>>><>><>><<<<>>>><<>><<<>>>><>><<<<><<<<>>><<<<>>>><<<>>><<>>><<<<>>><>><>>><<>>><>>><<<<><<><<><<><<<><<<>>>><><<<<>>>><<<<>>><>>>><>>><<<<>>><>><<<<><<<><>>>><<<>>><>>><<<<>>>><<><<<<><<><<>>><<>><<<<><<>>>><<<>>>><<<>>><><<>>>><<>>>><>>><<<>>><>>>><>><<<>>><><<<<>>><<>>><<<<>>><><>><<<>>><<><<<<>>><<>>>><>>><<<<>>>><<<<><<<>><<>>><<<>><<<<>><>>><<<>>><>>>><<<<><<>>><<>>><<<<>>>><<>>>><<>>>><<<<>><<<>>><><<<>>>><>>>><<<<><<<>>>><<<<>>><>><<>><<<<>><<<<><<>><>>>><<>><<<>>>><>>><<>>><<<<>><<<>>><<>><>>>><<<<>>><>><>>><<<<>>>><<<<><<<<>>>><<>>><<<<><>><<>>>><<<<><<<>><<<>><<<<><<<<><<><<>><>><<<><<><<>><>>><>>>><<<<>>><>>><>>>><<<>>>><<<<><<<<>>>><>>>><>>><<<<>>>><<<<>><<>><<<>>>><<<<><>><<<>>><<>><><<>>>><<>>>><>>>><><<<<>>>><<>><<<>><<<<>><<>>><<<><<<><>><><<><<<>><<>><<<>><>>>><>>><<>>><<<<>>>><<<<>>><<<<><>>>><<>>>><<><<<>>>><<<<>>><<<><<<>><>>>><<><<<<>>><<<>>><<<<>><>>>><<>>><<><>><<<>>>><<<><<<<><<<>>>><<<<>><<>>>><>>><<>>><<>>>><<<<><<>>>><<<<>>>><<<<><<>>>><<<<>>>><<>>><<<>><<>>><<<<>>><>>><<><<<<><<<<>><<<>>>><>><<<>>><<<>><<<><<<><>>><<<>>>><<<><<<><<>>><>><<<>>><<<>>>><<<>>>><>>>><<<>>><<<<>>>><<><<><<<<>><<<><>>>><<>>><<>><<<<>><<<><<<>><<<>>>><<<><>>><<<><<<<>>><<>>>><<><>><<>>>><<<<><<<><<<<><<<>><<><>><>><<>>>><>>>><<<><>>><<>><<<<>><><<<>>><<>>><<>>>><<<>><<>>>><><<<<>><><<<<>>><<>><>>><<>>>><<<>>><<<<>>><>>>><<<<><<<<>>><<<><<>>><><<<><<>><>>><>><<<<>>>><>>><>>>><<<<>>>><<<>><<<<>><>>><<<>>>><>>>><<>>><<>>>><<<<>>>><<<>>>><>>>><>><<<<>>>><>><<<<>>>><<<<>>><<<>><<<>>>><<<><<<<><<>>><<<>>>><<>><<>>>><<>>><<<>>>><<>>>><<>>>><>>>><><<<>><<><<><>>><><<<>>>><>>><><<>>><<<<>>><<>>>><<<>>><<<<><<<><>><<<<>><<<>><<<>><<>>><<<><<<>><<>><<><<><<<<>>><<>>><<<<>>><>>><<<>>><<>>><<<>><><<<<>>>><>><<<>><<<>>>><<>><<>>><>>><<<<>>>><<>>><><<<>>><>>><>><<<<>>>><>>><>>><<>>>><<>><><<>><<<<><<>>>><<<>>><>>>><<<<>><<<<><<<>><<>>>><>>><<>>>><<>><<>><<<>>><<<><<<<>>><>>>><<<>>>><<<<>>><<>>>><<>>>><<<<>><<<<>><<<><>>><<>><<>>>><>><<<<>><>>>><<<>>><<<>>><<>>>><<<>>>><>><<<><<<<>><>>>><>>><<<<>>><<<>>><<<><<<<>>>><<<<><<>>><>>>><>>><>><>><<<<>>><<>>>><<<><<<<><<<>><<<<>><>><<<>><<<<>>>><><<<><<<><<>>>><<<>><<>>>><><>>><>><<<<>>>><<>>>><<<<>>>><<>>><<<<>>>><<<>>><<<>><><<>><><<><>><<>>>><<<<><<<<>>><<<<>><<>><<<<><<< | |||
diff --git a/day17/solution.nim b/day17/solution.nim new file mode 100644 index 0000000..9296a35 --- /dev/null +++ b/day17/solution.nim | |||
| @@ -0,0 +1,143 @@ | |||
| 1 | import strutils | ||
| 2 | import sets | ||
| 3 | import strformat | ||
| 4 | import tables | ||
| 5 | |||
| 6 | type | ||
| 7 | Point = tuple | ||
| 8 | x: int64 | ||
| 9 | y: int64 | ||
| 10 | |||
| 11 | Block = seq[Point] | ||
| 12 | |||
| 13 | let blocks = [ | ||
| 14 | # @@@@ | ||
| 15 | @[(-1i64,0i64),(0i64,0i64),(1i64,0i64),(2i64,0i64)], | ||
| 16 | # .@. | ||
| 17 | # @@@ | ||
| 18 | # .@. | ||
| 19 | @[(-1i64,1i64),(0i64,0i64),(0i64,1i64),(0i64,2i64),(1i64,1i64)], | ||
| 20 | # ..@ | ||
| 21 | # ..@ | ||
| 22 | # @@@ | ||
| 23 | @[(-1i64,0i64),(0i64,0i64),(1i64,0i64),(1i64,1i64),(1i64,2i64)], | ||
| 24 | # @ | ||
| 25 | # @ | ||
| 26 | # @ | ||
| 27 | # @ | ||
| 28 | @[(-1i64,0i64),(-1i64,1i64),(-1i64,2i64),(-1i64,3i64)], | ||
| 29 | # @@ | ||
| 30 | # @@ | ||
| 31 | @[(-1i64,0i64),(0i64,0i64),(-1i64,1i64),(0i64,1i64)] | ||
| 32 | ] | ||
| 33 | |||
| 34 | let | ||
| 35 | minX = -3i64 | ||
| 36 | maxX = 3i64 | ||
| 37 | |||
| 38 | proc outOfBorders(a: Point): bool = | ||
| 39 | a.x < minX or a.x > maxX | ||
| 40 | |||
| 41 | proc `+`(a, b: Point): Point = | ||
| 42 | result = (a.x + b.x, a.y + b.y) | ||
| 43 | |||
| 44 | proc peak(b: Block): int64 = | ||
| 45 | for s in b: | ||
| 46 | result = max(result, s.y) | ||
| 47 | |||
| 48 | proc collides(b: Block, occupied: HashSet[Point]): bool = | ||
| 49 | for s in b: | ||
| 50 | if occupied.contains(s): | ||
| 51 | return true | ||
| 52 | return false | ||
| 53 | |||
| 54 | proc move(b: Block, offset: Point, occupied: HashSet[Point]): Block = | ||
| 55 | for s in b: | ||
| 56 | let toPush = s + offset | ||
| 57 | if toPush.outOfBorders(): | ||
| 58 | return b | ||
| 59 | result.add(toPush) | ||
| 60 | |||
| 61 | if result.collides(occupied): | ||
| 62 | return b | ||
| 63 | |||
| 64 | proc drop(input: string, index: var int64, peak: int64, b: Block, occupied: var HashSet[Point]): int64 = | ||
| 65 | var | ||
| 66 | curr = b.move((0i64 ,peak+4), occupied) | ||
| 67 | |||
| 68 | while true: | ||
| 69 | if index < input.len(): | ||
| 70 | case input[index]: | ||
| 71 | of '<': | ||
| 72 | curr = curr.move((-1i64, 0i64), occupied) | ||
| 73 | of '>': | ||
| 74 | curr = curr.move((1i64, 0i64), occupied) | ||
| 75 | else: | ||
| 76 | assert(false) | ||
| 77 | index = (index + 1) mod input.len() | ||
| 78 | let next = curr.move((0i64, -1i64), occupied) | ||
| 79 | # Collision happened | ||
| 80 | if curr == next: | ||
| 81 | break | ||
| 82 | curr = next | ||
| 83 | |||
| 84 | # Blocked collided | ||
| 85 | for s in curr: | ||
| 86 | occupied.incl(s) | ||
| 87 | |||
| 88 | max(curr.peak(), peak) | ||
| 89 | |||
| 90 | proc draw(occupied: HashSet[Point], peak: int64): void = | ||
| 91 | for y in countdown(peak, 0i64, 1i64): | ||
| 92 | var line = "" | ||
| 93 | for x in minX .. maxX: | ||
| 94 | line &= (if occupied.contains((x,y)): '#' else: ' ') | ||
| 95 | echo '|' & line & '|' | ||
| 96 | |||
| 97 | proc solve(input: string, cap: int64): int64 = | ||
| 98 | var | ||
| 99 | reduced = false | ||
| 100 | dp: Table[(int64, int64), (int64, int64, int64)] | ||
| 101 | occupied = toHashSet([(-3i64,0i64), (-2i64,0i64), (-1i64,0i64), (0i64,0i64), (3i64,0i64), (2i64,0i64), (1i64,0i64)]) | ||
| 102 | index = 0i64 | ||
| 103 | i = 0i64 | ||
| 104 | peak = 0i64 | ||
| 105 | while i < cap: | ||
| 106 | let currState = (i mod 5, index) | ||
| 107 | # echo fmt"currState = {currState} -> {(peak, i)}" | ||
| 108 | block InitState: | ||
| 109 | if not reduced and dp.hasKeyOrPut(currState, (peak, i, 0i64)): | ||
| 110 | let | ||
| 111 | (oldResult, oldI, oldIncrease) = dp[currState] | ||
| 112 | period = i - oldI | ||
| 113 | increase = peak - oldResult | ||
| 114 | distToCap = cap - i | ||
| 115 | repeats = distToCap div period | ||
| 116 | rest = distToCap mod period | ||
| 117 | |||
| 118 | # Initialise increments | ||
| 119 | if oldIncrease != increase: | ||
| 120 | dp[currState] = (peak, i, increase) | ||
| 121 | break InitState | ||
| 122 | |||
| 123 | assert(oldIncrease == increase) | ||
| 124 | echo fmt"currState = {currState}" | ||
| 125 | echo fmt"period = {period}, increase = {increase}, repeats = {repeats}" | ||
| 126 | result += repeats * increase | ||
| 127 | i = cap - rest | ||
| 128 | reduced = true | ||
| 129 | continue | ||
| 130 | |||
| 131 | # echo fmt"i = {i}" | ||
| 132 | let newPeak = drop(input, index, peak, blocks[i mod 5], occupied) | ||
| 133 | result += newPeak - peak | ||
| 134 | peak = newPeak | ||
| 135 | i += 1 | ||
| 136 | # echo peak | ||
| 137 | # occupied.draw(peak) | ||
| 138 | |||
| 139 | let content = readFile("./input.txt").strip() | ||
| 140 | |||
| 141 | echo solve(content, 2022i64) | ||
| 142 | # run solve with large enough cap to find replayPoint | ||
| 143 | echo solve(content, 1_000_000_000_000) | ||
