Last active
June 8, 2022 05:03
-
-
Save zeyadkhaled/f23c7bb5d9fc920de6d3d5090f699a44 to your computer and use it in GitHub Desktop.
Advent of Code 2021 Day 13 - Written for: https://blog.zefhub.io/solving-advent-of-code-using-zefops/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from zef.ops import * # don't forget to import ZefOps module from zef | |
str_points: str = """...""" | |
str_folds: str = """...""" | |
points = (str_points | |
| split["\n"] # ["897,393", "...] | |
| map[split[","] | map[int]] # [[897,393], [...] | |
| collect) | |
foldings = (str_folds | |
| split["\n"] # ["fold along y=6", ...] | |
| map[split[" "] | last | split["="]] # [["y", "6"], [...] | |
| map[lambda p: (int(p[0] == 'y'), int(p[1]))] # [(1, 6), ..] | |
| collect) | |
def fold(points: set, fold_info: tuple) -> set: | |
axis, val = fold_info | |
new_coordinate = lambda p: ( (p[0], val - (p[axis] - val)) if axis == 1 else (val - (p[axis] - val), p[1]) ) | |
dispatch = lambda p: new_coordinate(p) if p[axis] > val else tuple(p) | |
return set( | |
points | |
| map[dispatch] | |
| collect | |
) | |
def output(points): | |
for y in range((points | map[second] | max | collect) + 1): | |
for x in range((points | map[first] | max | collect) + 1): | |
if (x,y) in points: print("🟨🟨", end="") | |
else: print("⬜️⬜️", end="") | |
# Part 1 - Result | |
fold(points, foldings[0]) | length | run[print] | |
# Part 2 - Result | |
foldings | reduce[fold][points] | run[output] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment