Skip to content

Instantly share code, notes, and snippets.

@neoatlantis
Last active November 8, 2018 21:08
Show Gist options
  • Save neoatlantis/8741f6493449b08b11fa761f2b5dd2f8 to your computer and use it in GitHub Desktop.
Save neoatlantis/8741f6493449b08b11fa761f2b5dd2f8 to your computer and use it in GitHub Desktop.
Ants on a rod walking...
22 48
----------------------------------------------------------------
0 -1---2---3------4----5-----
1 1---2---3------4----5------
2 ---2---3------4----5-------
3 --2---3------4----5--------
4 -2---3------4----5---------
5 2---3------4----5----------
6 ---3------4----5-----------
7 --3------4----5------------
8 -3------4----5-------------
9 3------4----5--------------
10 ------4----5---------------
11 -----4----5----------------
12 ----4----5-----------------
13 ---4----5------------------
14 --4----5-------------------
15 -4----5--------------------
16 4----5---------------------
17 ----5----------------------
18 ---5-----------------------
19 --5------------------------
20 -5-------------------------
21 5--------------------------
22 ---------------------------
22
----------------------------------------------------------------
0 ---1-2---3------4----5-----
1 ----2---3------4----5------
2 ---1-2-3------4----5-------
3 --1---3------4----5--------
4 -1---2-3----4----5---------
5 1---2---3--4----5----------
6 ---2-----34----5-----------
7 --2-----3--4--5------------
8 -2-----3----45-------------
9 2-----3----4--5------------
10 -----3----4----5-----------
11 ----3----4------5----------
12 ---3----4--------5---------
13 --3----4----------5--------
14 -3----4------------5-------
15 3----4--------------5------
16 ----4----------------5-----
17 ---4------------------5----
18 --4--------------------5---
19 -4----------------------5--
20 4------------------------5-
21 --------------------------5
22 -------------------------5-
23 ------------------------5--
24 -----------------------5---
25 ----------------------5----
26 ---------------------5-----
27 --------------------5------
28 -------------------5-------
29 ------------------5--------
30 -----------------5---------
31 ----------------5----------
32 ---------------5-----------
33 --------------5------------
34 -------------5-------------
35 ------------5--------------
36 -----------5---------------
37 ----------5----------------
38 ---------5-----------------
39 --------5------------------
40 -------5-------------------
41 ------5--------------------
42 -----5---------------------
43 ----5----------------------
44 ---5-----------------------
45 --5------------------------
46 -5-------------------------
47 5--------------------------
48 ---------------------------
48
#!/usr/bin/env python3
MAXPOS = 27
class Ant:
def __init__(self, index, initPos, direction=True):
assert 0 <= initPos <= 27
self.index = index
self.currentPos = initPos
self.direction = bool(direction)
def reverse(self):
self.direction = not self.direction
def walk(self):
self.currentPos += 1 if self.direction else -1
if self.currentPos == MAXPOS: self.reverse()
return self.currentPos
def simulate(directions=0, details=False):
rod = [
Ant(1, 3, directions & (1 << 0)),
Ant(2, 7, directions & (1 << 1)),
Ant(3, 11, directions & (1 << 2)),
Ant(4, 18, directions & (1 << 3)),
Ant(5, 23, directions & (1 << 4))
]
for i in range(0, 100):
# check for collision
for j in range(0, len(rod) - 1):
ant1, ant2 = rod[j], rod[j+1]
if abs(ant1.currentPos - ant2.currentPos) <= 1 and ant1.direction != ant2.direction:
ant1.reverse()
ant2.reverse()
# check for leaving ants
leaving = [ant for ant in rod if ant.walk() <= 0]
for leftAnt in leaving: rod.remove(leftAnt)
# print rod status
if details: print("%2d" % i, status(rod))
# exit if all done
if len(rod) < 1: return i
raise Exception("Time too long.")
def status(rod):
global MAXPOS
output = ["-"] * MAXPOS
for ant in rod:
output[ant.currentPos-1] = str(ant.index)
return "".join(output)
tmin, tmax = 9999999, 0
tmin_i, tmax_i = None, None
for i in range(0, 2**5):
r = simulate(i)
if r < tmin:
tmin, tmin_i = r, i
if r > tmax:
tmax, tmax_i = r, i
print(tmin, tmax)
print("-" * 64)
print(simulate(tmin_i, True))
print("-" * 64)
print(simulate(tmax_i, True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment