From 6aa70c6ff1fb8fe3d1a346ec25fd930e59a47946 Mon Sep 17 00:00:00 2001 From: Thibaud Date: Wed, 11 Dec 2024 09:28:02 +0100 Subject: [PATCH] 2024 day 11 --- 2024/day11/: | 37 +++++++++++++++++++++++++++++++++++++ 2024/day11/day11.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 2024/day11/: create mode 100644 2024/day11/day11.py diff --git a/2024/day11/: b/2024/day11/: new file mode 100644 index 0000000..eb69526 --- /dev/null +++ b/2024/day11/: @@ -0,0 +1,37 @@ +from collections import Counter + + +def blink(data, steps): + counter = Counter(data) + for i in range(steps): + new_counter = Counter() + #data = tuple(x for y in (blink_stone(s) for s in data) for x in y) + for stone, count in counter.items(): + s = str(stone) + if stone == 0: + new_counter[1] += count + elif len(s) % 2 == 0: + first, second = int(s[:len(s)//2]), int(s[len(s)//2:]) + new_counter[first] += count + new_counter[second] += count + else: + new_counter[2024*stone] += count + counter = new_counter + print(len(counter.items())) + return counter.total() + + +if __name__ == "__main__": + res = blink((125, 17), 25) + assert res == 55312, f"expected 55312, but was {res}" + + import sys + if len(sys.argv) > 1: + infile = sys.argv[1] + with open(infile) as f: + data = tuple(map(int, f.read().rstrip().split())) + part1 = blink(data, 25) + print("Part 1: ", part1) + part2 = blink(data, 75) + print("Part 2: ", part2) + diff --git a/2024/day11/day11.py b/2024/day11/day11.py new file mode 100644 index 0000000..5d89fca --- /dev/null +++ b/2024/day11/day11.py @@ -0,0 +1,42 @@ +from collections import Counter + +def main(data): + part1 = run(data, 25) + print("Part 1: ", part1) + part2 = run(data, 75) + print("Part 2: ", part2) + + +def run(data, steps): + data = Counter(data) + for _ in range(steps): + data = blink(data) + return data.total() + + +def blink(data): + new_counter = Counter() + for stone, count in data.items(): + s = str(stone) + if stone == 0: + new_counter[1] += count + elif len(s) % 2 == 0: + first, second = int(s[:len(s)//2]), int(s[len(s)//2:]) + new_counter[first] += count + new_counter[second] += count + else: + new_counter[2024*stone] += count + return new_counter + + +if __name__ == "__main__": + res = run((125, 17), 25) + assert res == 55312, f"expected 55312, but was {res}" + + import sys + if len(sys.argv) > 1: + infile = sys.argv[1] + with open(infile) as f: + data = tuple(map(int, f.read().rstrip().split())) + main(data) +