# Memory Reallocation — Python — #adventofcode Day 6

Series

This post is part of the series Advent of Code 2017

- Reflections on #aoc2017
- The Halting Problem — Python — #adventofcode Day 25
- Electromagnetic Moat — Rust — #adventofcode Day 24
- Coprocessor Conflagration — Haskell — #adventofcode Day 23
- Sporifica Virus — Rust — #adventofcode Day 22
- Fractal Art — Python — #adventofcode Day 21
- Particle Swarm — Python — #adventofcode Day 20
- A Series of Tubes — Rust — #adventofcode Day 19
- Duet — Haskell — #adventofcode Day 18
- Spinlock — Rust/Python — #adventofcode Day 17
- Permutation Promenade — Julia — #adventofcode Day 16
- Dueling Generators — Rust — #adventofcode Day 15
- Disk Defragmentation — Haskell — #adventofcode Day 14
- Packet Scanners — Haskell — #adventofcode Day 13
- Digital Plumber — Python — #adventofcode Day 12
- Hex Ed — Python — #adventofcode Day 11
- Knot Hash — Haskell — #adventofcode Day 10
- Stream Processing — Haskell — #adventofcode Day 9
- I Heard You Like Registers — Python — #adventofcode Day 8
- Recursive Circus — Ruby — #adventofcode Day 7
- > Memory Reallocation — Python — #adventofcode Day 6 <
- A Maze of Twisty Trampolines — C++ — #adventofcode Day 5
- High Entropy Passphrases — Python — #adventofcode Day 4
- Spiral Memory — Go — #adventofcode Day 3
- Corruption Checksum — Python — #adventofcode Day 2
- Inverse Captcha — Coconut — #adventofcode Day 1
- Advent of Code 2017: introduction

**Series:**Advent of Code 2017

Today’s challenge asks us to follow a recipe for redistributing objects in memory that bears a striking resemblance to the rules of the African game Mancala.

!!! commentary When I was doing my MSci, one of our programming exercises was to write (in Haskell, IIRC) a program to play a Mancala variant called Oware, so this had a nice ring of nostalgia.

```
Back to Python today: it's already become clear that it's by far my most fluent language, which makes sense as it's the only one I've used consistently since my schooldays. I'm a bit behind on the blog posts, so you get this one without any explanation, for now at least!
```

```
import math
def reallocate(mem):
max_val = -math.inf
size = len(mem)
for i, x in enumerate(mem):
if x > max_val:
max_val = x
max_index = i
i = max_index
mem[i] = 0
remaining = max_val
while remaining > 0:
i = (i + 1) % size
mem[i] += 1
remaining -= 1
return mem
def detect_cycle(mem):
mem = list(mem)
steps = 0
prev_states = {}
while tuple(mem) not in prev_states:
prev_states[tuple(mem)] = steps
steps += 1
mem = reallocate(mem)
return (steps, steps - prev_states[tuple(mem)])
initial_state = map(int, input().split())
print("Initial state is ", initial_state)
steps, cycle = detect_cycle(initial_state)
print("Steps to cycle: ", steps)
print("Steps in cycle: ", cycle)
```

## Webmentions

You can respond to this post, "Memory Reallocation — Python — #adventofcode Day 6", by:
liking, boosting or replying to a tweet or toot that mentions it; or
sending a webmention from your own site to `https://erambler.co.uk/blog/day-06/`

## Comments

*Powered by Cactus Comments 🌵*