Skip to content

Commit fbfa52b

Browse files
committed
Day 16 part 2 method 2 (that does not work)
1 parent 41301e6 commit fbfa52b

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

day16/README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,18 @@ And bingo, "That's the right answer!" However it is much slower than I think to
111111

112112
## Without maths
113113

114-
As I said above, I had another idea when I was doing the expansion. No maths at all, just compute. In the worst case, we will do `L/2*100*8 = 2.6*10^9` additions. It is much doable.
114+
As I said above, I had another idea when I was doing the expansion. No maths at all, just compute. ~~In the worst case, we will do -`L/2*100*8 = 2.6*10^9`- additions~~. It is much doable.
115+
116+
It took me like 5 minutes to write this:
117+
```python
118+
input = list(map(int, (input*N_REPEAT)[offset:]))
119+
output = input.copy()
120+
for i in range(N_ITERATION):
121+
for j in range(len(input)):
122+
output[j] = sum(input[j:]) % 10
123+
input = output.copy()
124+
return ''.join(map(str, output))
125+
```
126+
And it takes forever trying to finish the first iteration. When I look at the code, the complexity analyse above is wrong, it actually needs `100*(L/2)^2 = 10^15` additions to finish.
127+
128+
Lesson learnt: *sometimes we can just let the computer do the computing, but sometimes maths is helpful to solve impossible computing problem*.

day16/d16.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,22 @@ def part2(input):
3333
return ''.join(map(str, output))
3434

3535

36-
#print('Part 1:', get_output(input, N_ITERATION)[0:8])
37-
print('Part 2:', part2(input))
36+
def part2_2(input):
37+
L = len(input)*N_REPEAT
38+
offset = int(input[0:7])
39+
if (offset < L/2):
40+
sys.exit('Oh my, I have no idea deal with that offset.')
41+
42+
input = list(map(int, (input*N_REPEAT)[offset:]))
43+
output = input.copy()
44+
for i in range(N_ITERATION):
45+
print('Iteration', i, len(input))
46+
for j in range(len(input)):
47+
output[j] = sum(input[j:]) % 10
48+
input = output.copy()
49+
return ''.join(map(str, output))
50+
51+
52+
print('Part 1:', get_output(input, N_ITERATION)[0:8])
53+
print('Part 2:', part2(input))
54+
#print('Part 2:', part2_2(input))

0 commit comments

Comments
 (0)