**[FlipFlop Codes 2025][]** *[Code golf style][]* **[2025][]** ![](static/logo.png) Welcome to my solutions page of the [FlipFlop Codes Event][]! This is a fun event where you can solve puzzles and learn new things. Each year features an event with a set of puzzles. Puzzles unlock during the days, you can solve them to earn points or you can just solve them for fun! You can solve the puzzles in any language you like, and all puzzles have a quick solution for any machine. The event is open and free for everyone, and you can participate by logging in with your GitHub or Google account. It doesn't matter if you don't have a computer science or software engineering background— with a little bit of coding knowledge and some problem-solving skills, you can get pretty far! > "Next Event: July 2026. Stay tuned! > -- [Sebastiaan Heins][]" [FlipFlop Codes 2025]: https://flipflop.slome.org/2025 [Code golf style]: https://en.wikipedia.org/wiki/Code_golf [Sebastiaan Heins]: https://slome.org [FlipFlop Codes Event]: https://flipflop.slome.org [2025]: /flipflop/doc/ckout/2025/ # Code (🏌️) ## [Puzzle demo](https://flipflop.slome.org/2025/demo) ```py p, c = open("test.0" if __debug__ else "input.0").read(), __import__("collections") p1 = sum(int(i) for i in p.split()) p2 = round(p1 / len(p.split())) p3 = int(c.Counter(p.split()).most_common()[0][0] + c.Counter(p).most_common()[-1][0]) print(p1, p2, p3, sep="\n") assert (p1, p2, p3) == (474, 43, 690), (p1, p2, p3) ``` ## [Puzzle 1](https://flipflop.slome.org/2025/1) ```py p = open("test.1" if __debug__ else "input.1").read() p1 = (banana := lambda x: x.count("ba") + x.count("na") + x.count("ne"))(p) p2 = sum(x for i in p.split() if (x := banana(i)) % 2 == 0) p3 = sum(banana(i) for i in p.split() if "ne" not in i) print(p1, p2, p3, sep="\n") assert (p1, p2, p3) == (24, 16, 19), (p1, p2, p3) ``` ## [Puzzle 2](https://flipflop.slome.org/2025/2) ```py p, o = open("test.2" if __debug__ else "input.2").read(), "" fib = lambda n: n if n <= 1 else fib(n - 1) + fib(n - 2) # noqa: E731 c1 = p1 = s = c2 = p2 = c3 = p3 = 0 for i in p: if i == o: s += 1 else: c3 += fib(s) if o == "^" else -fib(s) p3, s, o = max(p3, c3), 1, i c1, c2 = c1 + (height := lambda x: x if i == "^" else -x)(1), c2 + height(s) p1, p2 = max(p1, c1), max(p2, c2) print(p1, p2, p3, sep="\n") assert (p1, p2, p3) == (6, 15, 4), (p1, p2, p3) ``` ## [Puzzle 3](https://flipflop.slome.org/2025/3) ```py p, c = open("test.3" if __debug__ else "input.3").read(), __import__("collections") special = lambda x: len(set(x)) < 3 # noqa: E731 color = lambda x: x.index(max(x)) # noqa: E731 p1 = c.Counter(p.split()).most_common()[0][0] sf = [i.split(",") for i in p.split()] p2 = sum(color(x) == 1 for x in sf if not special(x)) p3 = sum(special(x) * 10 or [5, 2, 4][color(x)] for x in sf) print(p1, p2, p3, sep="\n") assert (p1, p2, p3) == ("10,20,30", 0, 37), (p1, p2, p3) ``` ## [Puzzle 4](https://flipflop.slome.org/2025/4) ```py p, f3 = open("test.4" if __debug__ else "input.4"), [] p1 = p2 = p3 = x0 = y0 = 0 for line in p: x, y = map(int, line.split(",")) f3.append((x, y)) p1 += abs(x - x0) + abs(y - y0) p2 += max(abs(x - x0), abs(y - y0)) x0, y0 = x, y x0 = y0 = 0 for x, y in sorted(f3, key=lambda t: t[0] + t[1]): p3 += max(abs(x - x0), abs(y - y0)) x0, y0 = x, y print(p1, p2, p3, sep="\n") assert (p1, p2, p3) == (24, 12, 9), (p1, p2, p3) ``` ## [Puzzle 5](https://flipflop.slome.org/2025/5) ```py p = open("test.5" if __debug__ else "input.5").read().strip() x = y = p1 = p3 = 0 t = dict.fromkeys(p) while x < len(p): t.pop(p[x], None) y = z if (z := p.find(p[x], x + 1)) >= 0 else p.find(p[x]) p1 += (d := abs(y - x)) p3 += -d if p[x].isupper() else d x = y + 1 p2 = "".join(t.keys()) print(p1, p2, p3, sep="\n") assert (p1, p2, p3) == (38, "Bc", -6), (p1, p2, p3) ``` ## [Puzzle 6](https://flipflop.slome.org/2025/6) ```py p, n = (open("test.6"), 8) if __debug__ else (open("input.6"), 1000) s, a, b = [tuple(map(int, i.split(","))) for i in p], n // 4, n * 3 // 4 p1 = sum((g := lambda j: a <= j % n < b)(x * 100) and g(y * 100) for x, y in s) p2 = sum(g(x * 3600 * z) and g(y * 3600 * z) for x, y in s for z in range(1000)) p3 = sum(g(x * 31556926 * z) and g(y * 31556926 * z) for x, y in s for z in range(1000)) print(p1, p2, p3, sep="\n") assert (p1, p2, p3) == (4, 0, 1500), (p1, p2, p3) ``` ## [Puzzle 7](https://flipflop.slome.org/2025/7) ```py p = [tuple(map(int, i.split())) for i in open("test.7" if __debug__ else "input.7")] f = __import__("math").factorial p1 = sum(f(m + n - 2) // f(m - 1) // f(n - 1) for m, n in p) p2 = sum(f(2 * m + n - 3) // f(m - 1) ** 2 // f(n - 1) for m, n in p) p3 = sum(f((n - 1) * m) // f(n - 1) ** m for m, n in p) print(p1, p2, p3, sep="\n") assert (p1, p2, p3) == (11, 108, 98), (p1, p2, p3) ``` # Stats (⛳) ## LOCs File|blank|comment|code :-------|-------:|-------:|-------: p4.py|0|0|14 p2.py|0|0|13 p5.py|0|0|12 p3.py|0|0|9 p6.py|0|0|7 p7.py|0|0|7 _demo.py|0|0|6 p1.py|0|0|6 --------|--------|--------|-------- SUM:|0|0|74 ## Perf ``` 568341 430 4043 0:00.02 0.01usr 0.00sys 95%CPU 60524kB (0) pypy -O _demo.py 10446 5350 2911 0:00.02 0.00usr 0.01sys 95%CPU 55668kB (0) pypy -O p1.py 148 1533 45388 0:00.04 0.03usr 0.01sys 100%CPU 60200kB (0) pypy -O p2.py 21,87,85 815 10635 0:00.03 0.02usr 0.01sys 100%CPU 61932kB (0) pypy -O p3.py 7076 4951 2302 0:00.01 0.01usr 0.00sys 94%CPU 53448kB (0) pypy -O p4.py 1654 xkfWIHGzovOlth 60 0:00.02 0.00usr 0.01sys 100%CPU 53064kB (0) pypy -O p5.py 230 130400 244772 0:00.06 0.04usr 0.01sys 100%CPU 61148kB (0) pypy -O p6.py 206 13283072 2376030546190224 0:00.01 0.00usr 0.00sys 94%CPU 59812kB (0) pypy -O p7.py Total: 0.27s ``` !!!BWL: THE BEER-WARE LICENSE (Revision 42) Carlo~~@~~Miron~~.IT~~ wrote this file. As long as you retain this notice you can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return. —㎝