Skip to content

Commit 7fb2c94

Browse files
committed
2024 Day 22 Part 2 Complete
1 parent 0a8641e commit 7fb2c94

2 files changed

Lines changed: 55 additions & 4 deletions

File tree

2024/22/daily.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def parse(input_text):
99

1010
return codes
1111

12-
12+
sum_sequence = {}
1313
def gen_code(secret_number, generation):
1414
def mix(s, code):
1515
return s ^ code
@@ -18,7 +18,10 @@ def prune(code):
1818
return code & 16777215 # 2**24 - 1
1919

2020
code = secret_number
21+
last_digit = code % 10
22+
last_diff = [None, None, None, None]
2123

24+
gen_sequence = {}
2225
for i in range(generation):
2326
to_mix = code << 6 # multiply by 64
2427
code = mix(code, to_mix)
@@ -30,6 +33,27 @@ def prune(code):
3033
code = mix(code, to_mix)
3134
code = prune(code)
3235

36+
new_last_digit = code % 10
37+
diff = new_last_digit - last_digit
38+
last_diff = last_diff[1:]
39+
last_diff.append(diff)
40+
41+
# We want the first sequence for this generation
42+
sequence_key = tuple(last_diff)
43+
if i > 2 and sequence_key not in gen_sequence:
44+
# if sequence_key == (-2,1,-1,3):
45+
# print(f"{secret_number} - Sequence: {sequence_key} - G:{i} - C: {code} NLD: {new_last_digit} - LastDigit: {last_digit}")
46+
gen_sequence[tuple(last_diff)] = new_last_digit
47+
48+
last_digit = new_last_digit
49+
50+
for k, v in gen_sequence.items():
51+
if k not in sum_sequence:
52+
sum_sequence[k] = 0
53+
# if k == (-2,1,-1,3):
54+
# print(f"\tAdding {v} to {sum_sequence[k]}")
55+
sum_sequence[k] += v
56+
3357
return code
3458

3559
def part1(input_text):
@@ -40,7 +64,23 @@ def part1(input_text):
4064
return sum
4165

4266
def part2(input_text):
43-
return 0
67+
global sum_sequence
68+
sum_sequence = {}
69+
codes = parse(input_text)
70+
for code in codes:
71+
gen_code(code, 2000)
72+
73+
max_v = 0
74+
max_s = None
75+
print(f"Sum Sequence: {sum_sequence[(-2,1,-1,3)]}")
76+
for k, v in sum_sequence.items():
77+
if v > max_v:
78+
max_v = v
79+
max_s = k
80+
81+
return max_s, max_v
82+
83+
# 3, 0, -5, -3 - not the right answer
4484

4585
if __name__ == "__main__":
4686
with open(__file__.rsplit('/', 1)[0] + "/input.txt", 'r') as file:

2024/22/daily_test.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ def sample_data():
1010
2024
1111
"""
1212

13+
@pytest.fixture
14+
def sample_data_2():
15+
return """
16+
1
17+
2
18+
3
19+
2024
20+
"""
21+
1322
@pytest.fixture
1423
def parsed_data(sample_data):
1524
return parse(sample_data)
@@ -42,5 +51,7 @@ def test_gen_code(secret_number, generation, expected):
4251
def test_part1(sample_data):
4352
assert part1(sample_data) == 37327623
4453

45-
# def test_part2(sample_data):
46-
# assert part2(sample_data) == 154115708116294
54+
def test_part2(sample_data_2):
55+
sequence, bananas = part2(sample_data_2)
56+
assert sequence == (-2,1,-1,3)
57+
assert bananas == 23

0 commit comments

Comments
 (0)