From 700e40b5e39a1224d9107cf0ca8a8d1cf40ac9fa Mon Sep 17 00:00:00 2001 From: y-syo Date: Fri, 5 Dec 2025 17:28:17 +0100 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat(day5):=20i?= =?UTF-8?q?=20hate=20part2=20:smadge:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2025/05/part1.py | 27 +++++++++++++++++++++++++ 2025/05/part2.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 2025/05/part1.py create mode 100644 2025/05/part2.py diff --git a/2025/05/part1.py b/2025/05/part1.py new file mode 100644 index 0000000..a23d356 --- /dev/null +++ b/2025/05/part1.py @@ -0,0 +1,27 @@ +import os + +if (os.path.isfile("./input")): + input = open("./input", "r").read() +else: + input = "3-5\n10-14\n16-20\n12-18\n\n1\n5\n8\n11\n17\n32\n" + +input = input[:-1] + +isList = True +fresh = [] +c = 0 +for line in input.split('\n'): + if (line == ""): + isList = False + continue + + if (isList): + fresh.append([int(line.split('-')[0]), int(line.split('-')[1])]) + + if (not isList): + for f in fresh: + if (int(line) >= f[0] and int(line) <= f[1]): + c += 1 + break + +print(c) diff --git a/2025/05/part2.py b/2025/05/part2.py new file mode 100644 index 0000000..a4b6528 --- /dev/null +++ b/2025/05/part2.py @@ -0,0 +1,51 @@ +import os + +if (os.path.isfile("./input")): + input = open("./input", "r").read() +else: + input = "3-5\n10-14\n16-20\n12-18\n\n" + +#input = input[:-1] + +fresh = [] +c = 0 + +for line in input.split('\n'): + if (line == ""): + break + + s = int(line.split('-')[0]) + e = int(line.split('-')[1]) + + for f in fresh: + if (f[0] <= s <= f[1]): + s = f[1] + 1; + if (f[0] <= e <= f[1]): + e = f[0] - 1; + + if (s > e): + continue + fresh.append([s, e]) + fresh.sort() + c += (e - s) + 1 + +print(c) + +ranges = [] +for line in input.split("\n"): + if line == "": + break + ranges.append([int(line.split('-')[0]), int(line.split('-')[1])]) + +ranges.sort() + +merged = [] +for s, e in ranges: + if not merged or s > merged[-1][1] + 1: + merged.append([s, e]) + else: + merged[-1][1] = max(merged[-1][1], e) + +c = sum(e - s + 1 for s, e in merged) + +print(c)