Skip to content

Commit 6b491c5

Browse files
committed
chore: consistent docstrings
1 parent 9f10344 commit 6b491c5

File tree

3 files changed

+172
-144
lines changed

3 files changed

+172
-144
lines changed

_2024/solutions/day22.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
"""Day 22: Monkey Market
22
3-
This module provides the solution for Day 22 of Advent of Code 2024.
3+
This module provides the solution for Advent of Code 2024 - Day 22.
44
5-
It solves a puzzle about analyzing secret codes and price patterns
6-
in a monkey marketplace.
5+
It solves a puzzle about analyzing pseudorandom secret number sequences and
6+
price patterns in a monkey marketplace. Buyers use secret numbers to generate
7+
prices, and finding optimal price change sequences maximizes banana earnings.
8+
9+
The solution involves simulating pseudorandom number generation through bitwise
10+
operations (XOR and modulo), tracking price changes across multiple buyers, and
11+
identifying the most profitable sequence of four consecutive price changes.
12+
13+
The module contains a Solution class that inherits from SolutionBase and
14+
implements methods to transform secret numbers and analyze price patterns.
715
"""
816

917
from collections import defaultdict
@@ -13,49 +21,50 @@
1321

1422

1523
class Solution(SolutionBase):
16-
"""Solution for the Monkey Market puzzle.
24+
"""Analyze secret numbers and price patterns in the monkey marketplace.
25+
26+
This solution implements pseudorandom number analysis:
27+
- Part 1: Calculate sum of secret numbers after 2000 transformations
28+
- Part 2: Find optimal price change sequence across all buyers
1729
18-
Methods
19-
-------
20-
part1: Calculate the sum of transformed secret codes.
21-
part2: Find the highest value pattern in price sequences.
30+
The solution uses bitwise operations (XOR) and modulo arithmetic to generate
31+
pseudorandom sequences, then analyzes price patterns to maximize profits.
2232
"""
2333

2434
rounds: int = 2000
2535

2636
def transform_secret(self, secret: int) -> int:
27-
"""Transform a secret code through a series of bitwise operations.
37+
"""Transform a secret number through bitwise operations.
2838
29-
The transformation consists of three steps:
30-
1. Multiply the secret by 64, XOR with the original secret, and take modulo 16777216.
31-
2. Integer divide the result by 32, XOR with the previous result, and take modulo 16777216.
32-
3. Multiply the result by 2048, XOR with the previous result, and take modulo 16777216.
33-
34-
The modulo operation ensures that the secret code remains within the range [0, 16777215].
39+
Applies three sequential transformations using multiplication, division,
40+
XOR (mix), and modulo (prune) operations. Each step mixes a calculated
41+
value into the secret using XOR, then prunes the result using modulo
42+
16777216 to keep values in range.
3543
3644
Args:
37-
secret: The secret code to be transformed.
45+
secret (int): The secret number to transform
3846
3947
Returns
4048
-------
41-
The transformed secret code.
49+
Transformed secret number after applying all three steps
4250
"""
4351
secret = ((secret * 64) ^ secret) % 16777216
4452
secret = ((secret // 32) ^ secret) % 16777216
4553
return ((secret * 2048) ^ secret) % 16777216
4654

4755
def part1(self, data: list[str]) -> int:
48-
"""Calculate the sum of transformed secret codes.
56+
"""Calculate sum of secret numbers after 2000 transformations.
4957
50-
Processes each input value through the specified number of rounds using the
51-
`transform_secret` method.
58+
Processes each initial secret number through 2000 rounds of transformation,
59+
then sums all final secret values. This simulates the pseudorandom number
60+
generation for each buyer over a full day.
5261
5362
Args:
54-
data: List of strings containing initial secret codes.
63+
data (list[str]): List of strings containing initial secret numbers
5564
5665
Returns
5766
-------
58-
Sum of all transformed secret values.
67+
Sum of all secret numbers after 2000 transformation rounds
5968
"""
6069
secrets = []
6170
for secret in map(int, data):
@@ -67,20 +76,19 @@ def part1(self, data: list[str]) -> int:
6776
return sum(secrets)
6877

6978
def part2(self, data: list[str]) -> int:
70-
"""Find the highest value pattern in price sequences.
71-
72-
Generates price sequences by applying the `transform_secret` method to each input value
73-
and taking the modulo 10 of the result to get price digits.
79+
"""Find the price change sequence that maximizes total bananas.
7480
75-
Analyzes sequences of 4 consecutive price changes and finds the pattern that leads to
76-
the highest subsequent price.
81+
Generates price sequences for each buyer by taking the ones digit of
82+
transformed secret numbers. Analyzes all possible sequences of four
83+
consecutive price changes and identifies which sequence yields the
84+
highest total price across all buyers when they first encounter it.
7785
7886
Args:
79-
data: List of strings containing initial secret codes.
87+
data (list[str]): List of strings containing initial secret numbers
8088
8189
Returns
8290
-------
83-
Maximum price value associated with any 4-change pattern.
91+
Maximum total bananas obtainable from any four-change sequence
8492
"""
8593
prices = []
8694
for secret in map(int, data):

_2024/solutions/day23.py

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
"""Day 23: LAN Party
22
3-
Analyze player connections and find gaming groups in a network.
3+
This module provides the solution for Advent of Code 2024 - Day 23.
44
5-
This module solves a puzzle about identifying gaming configurations
6-
based on network connections and group composition rules.
5+
It solves a puzzle about identifying groups of interconnected computers at a
6+
LAN party. The puzzle involves finding sets of computers that are all directly
7+
connected to each other (cliques) and locating the Chief Historian based on
8+
computer naming patterns.
9+
10+
The solution uses graph theory to analyze network connections, finding both
11+
small gaming groups (trios) that include specific computers and the largest
12+
possible fully-connected group representing the actual LAN party location.
13+
14+
The module contains a Solution class that inherits from SolutionBase and
15+
implements methods using NetworkX to find cliques in the connection graph.
716
"""
817

918
from itertools import combinations
@@ -14,32 +23,30 @@
1423

1524

1625
class Solution(SolutionBase):
17-
"""Solution for Advent of Code 2024 - Day 23: LAN Party.
18-
19-
This class solves a puzzle about analyzing LAN party connections and finding
20-
gaming groups. Part 1 identifies valid gaming trios including teachers, while
21-
Part 2 finds the largest possible gaming group.
22-
23-
Input format:
24-
- List of connections, one per line
25-
- Each line contains two node IDs separated by a hyphen
26-
- Node IDs starting with 't' represent teachers
27-
- All other nodes represent students
28-
- Connections indicate which players can directly play together
26+
"""Analyze computer networks and find LAN party groups using graph theory.
27+
28+
This solution implements clique detection algorithms:
29+
- Part 1: Count trios of interconnected computers with Chief Historian hint
30+
- Part 2: Find the largest fully-connected group (maximum clique)
31+
32+
The solution uses NetworkX graph library to model the network and efficiently
33+
find all cliques, which represent groups of computers that can all directly
34+
communicate with each other.
2935
"""
3036

3137
def construct_graph(self, data: list[str]) -> nx.Graph:
32-
"""Construct a NetworkX graph from the input connection data.
38+
"""Construct an undirected graph from connection data.
3339
34-
Creates an undirected graph where nodes represent players and edges
35-
represent possible direct connections for gaming.
40+
Creates a NetworkX graph where nodes represent computers and edges
41+
represent direct network connections between them. Each connection
42+
is bidirectional.
3643
3744
Args:
38-
data: List of strings, each containing two node IDs separated by a hyphen
45+
data (list[str]): List of connection strings in format "id1-id2"
3946
4047
Returns
4148
-------
42-
NetworkX Graph object representing the connection network
49+
NetworkX Graph object representing the computer network
4350
"""
4451
graph: nx.Graph = nx.Graph()
4552
edges = []
@@ -50,20 +57,18 @@ def construct_graph(self, data: list[str]) -> nx.Graph:
5057
return graph
5158

5259
def part1(self, data: list[str]) -> int:
53-
"""Count valid gaming trios that include at least one teacher.
60+
"""Count sets of three interconnected computers including the Chief Historian.
5461
55-
Analyzes the connection graph to find all possible groups of three
56-
players that:
57-
1. Are fully connected (form a clique)
58-
2. Include at least one teacher (node starting with 't')
59-
3. Can play together based on direct connections
62+
Finds all groups of three computers that are fully connected (each computer
63+
connected to the other two) and include at least one computer whose name
64+
starts with 't' (indicating the Chief Historian's potential location).
6065
6166
Args:
62-
data: List of strings representing player connections
67+
data (list[str]): List of connection strings representing the network
6368
6469
Returns
6570
-------
66-
Number of unique valid gaming trios
71+
Number of unique trios containing at least one computer starting with 't'
6772
"""
6873
graph = self.construct_graph(data)
6974
teacher_cliques = [
@@ -82,19 +87,18 @@ def part1(self, data: list[str]) -> int:
8287
)
8388

8489
def part2(self, data: list[str]) -> str:
85-
"""Find the largest possible gaming group.
90+
"""Find the password by identifying the largest LAN party group.
8691
87-
Identifies the maximum clique in the connection graph, representing
88-
the largest group of players that can all play together directly.
89-
Returns the players in alphabetical order as a comma-separated string.
92+
Locates the maximum clique in the network graph, representing the largest
93+
set of computers where every computer is directly connected to every other
94+
computer. Returns the sorted computer names as a comma-separated password.
9095
9196
Args:
92-
data: List of strings representing player connections
97+
data (list[str]): List of connection strings representing the network
9398
9499
Returns
95100
-------
96-
Comma-separated string of player IDs in the largest gaming group,
97-
sorted alphabetically
101+
Password string of alphabetically sorted computer IDs joined by commas
98102
"""
99103
graph = self.construct_graph(data)
100104
largest_clique = max(nx.find_cliques(graph), key=len)

0 commit comments

Comments
 (0)