aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2025/05/a.py15
-rw-r--r--2025/05/b.py26
-rw-r--r--2025/README.md8
-rw-r--r--README.md2
4 files changed, 48 insertions, 3 deletions
diff --git a/2025/05/a.py b/2025/05/a.py
index 5fdb11b..b2699ba 100644
--- a/2025/05/a.py
+++ b/2025/05/a.py
@@ -1,6 +1,17 @@
1import fileinput 1import fileinput
2 2
3rang = True
4ranges = []
5s = 0
3with fileinput.input() as lines: 6with fileinput.input() as lines:
4 for line in lines: 7 for line in lines:
5 ... 8 if line == '\n':
6 9 rang = False
10 elif rang:
11 i = line.find('-')
12 ranges.append((int(line[:i]), int(line[i+1:-1])))
13 else:
14 x = int(line[:-1])
15 if any(x >= r[0] and x <= r[1] for r in ranges):
16 s += 1
17print(s)
diff --git a/2025/05/b.py b/2025/05/b.py
new file mode 100644
index 0000000..09bc5e4
--- /dev/null
+++ b/2025/05/b.py
@@ -0,0 +1,26 @@
1import fileinput
2
3def overlap(r1, r2):
4 return min(r1[1], r2[1]) >= max(r1[0], r2[0])
5
6def fuse(r1, r2):
7 return (min(r1[0], r2[0]), max(r1[1], r2[1]))
8
9ranges = []
10with fileinput.input() as lines:
11 for line in lines:
12 if line == '\n':
13 break
14 i = line.find('-')
15 ranges.append((int(line[:i]), int(line[i+1:-1])))
16
17s = set()
18for r in ranges:
19 c = r
20 for rr in list(s):
21 if overlap(c, rr):
22 c = fuse(c, rr)
23 s.remove(rr)
24 s.add(c)
25
26print(sum(r[1] - r[0] + 1 for r in s))
diff --git a/2025/README.md b/2025/README.md
index 8fccef4..a77c168 100644
--- a/2025/README.md
+++ b/2025/README.md
@@ -11,6 +11,7 @@ Example
11 11
12``` 12```
13Day -Part 1- -Part 2- 13Day -Part 1- -Part 2-
14 5 00:04:39 00:22:19
14 4 00:25:36 00:27:49 15 4 00:25:36 00:27:49
15 3 00:05:02 00:16:09 16 3 00:05:02 00:16:09
16 2 00:10:36 00:11:55 17 2 00:10:36 00:11:55
@@ -61,3 +62,10 @@ out of bounds.
61 62
62For part 2 I decided to quickly code the dumb "repeat part 1 until no 63For part 2 I decided to quickly code the dumb "repeat part 1 until no
63rolls are removed" strategy and it worked. 64rolls are removed" strategy and it worked.
65
66### Day 5: Cafeteria
67
68Part 2 required a little bit of thinking to handle overlaps correctly
69- at first I wrote a solution that did not handle overlaps, then one
70that can only handle single overlaps, and finally one that works in
71every case. My final solution is quite straightforward.
diff --git a/README.md b/README.md
index c38052e..33cafd8 100644
--- a/README.md
+++ b/README.md
@@ -14,4 +14,4 @@ See `year/README.md` for instructions on how to run my code.
14|2022| 50 | Rust | Done in 2025 to learn Rust | 14|2022| 50 | Rust | Done in 2025 to learn Rust |
15|2023| 50 | C | All solved by December 25, 2023 | 15|2023| 50 | C | All solved by December 25, 2023 |
16|2024| 50 | C++ | Each solved within 24h | 16|2024| 50 | C++ | Each solved within 24h |
17|2025| 8 | Python | Work in progress... | 17|2025| 10 | Python | Work in progress... |

Generated with cgit - Back to sebastiano.tronto.net