-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinglenook.py
123 lines (89 loc) · 3.61 KB
/
inglenook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""Generate a starting and ending order for a 3-2-2 or 5-3-3 inglenook shunting
puzzle."""
from random import sample, choice, shuffle, randint
import argparse
def get_args():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subparser_name')
parser_322 = subparsers.add_parser('322',
help='Generate for a 322 inglenook')
parser_322.add_argument('wagons', nargs=6)
parser_322.set_defaults(func=get_starting_setup_322)
parser_533 = subparsers.add_parser('533',
help='Generate for a 533 inglenook')
parser_533.add_argument('wagons', nargs=8)
parser_533.set_defaults(func=get_starting_setup_533)
return parser.parse_args()
def get_starting_setup_322(args):
"""Generate the starting positions of the wagons."""
# Copy the wagons list for convenience
wagons = args.wagons
# The long siding can contain either 2 or 3 wagons.
num_long = randint(2, 3)
if num_long == 2:
num_short_1 = 2
num_short_2 = 2
if num_long == 3:
fill_mid = choice([True, False])
if fill_mid:
num_short_1 = 2
num_short_2 = 1
if not fill_mid:
num_short_1 = 1
num_short_2 = 2
# Randomise the order of the wagons
shuffle(wagons)
# Slice the shuffled list to get the wagons to go in each siding
long_siding = wagons[0:num_long]
short_1 = wagons[num_long:num_long+num_short_1]
short_2 = wagons[num_long+num_short_1:]
print("The starting setup is:")
print("Long siding: " + str(long_siding))
print("First short siding: " + str(short_1))
print("Second short siding: " + str(short_2))
def get_starting_setup_533(args):
"""Generate the starting positions of the wagons."""
wagons = args.wagons
# First decide where the wagons will go
# The longest siding must start with between 2 and 5 wagons
num_long = randint(2, 5)
# If there are two in the long siding, both the short sidings are full
if num_long == 2:
num_short_1 = 3
num_short_2 = 3
# If there are three or more wagons in the long siding, then the total
# number in the short sidings is 8 - num_long
if num_long >= 3:
# If there are five wagons in the long siding, mid can have 0 to
# 3 wagons: minimum of 8 - (num_long + 3)
# If there are four in the long siding, mid must have at least one
# wagon in: 8 - (num_long + 3)
# If there are three in the long siding, mid must have at least two in:
# 8 - (num_long + 3)
min_short = 8 - (num_long + 3)
num_short_1 = randint(min_short, 3)
num_short_2 = 8 - num_long - num_short_1
# Randomise the order of the wagons
shuffle(wagons)
# Slice the shuffled list to get the wagons to go in each siding
long_siding = wagons[0: num_long]
short_1 = wagons[num_long: num_long+num_short_1]
short_2 = wagons[num_long+num_short_1: ]
print("The starting setup is:")
print("Long siding: " + str(long_siding))
print("First short siding: " + str(short_1))
print("Second short siding: " + str(short_2))
def get_target(wagons, inglenook_type):
"""Set the target train to be assembled. Length depends on the type of
inglenook"""
if inglenook_type == '322':
target = sample(wagons, 4)
else:
target = sample(wagons, 5)
print("The target order is: " + str(target) + '\n')
def main():
args = get_args()
get_target(args.wagons, args.subparser_name)
args.func(args)
if __name__ == "__main__":
main()