This article discusses and contains the Python code solution for the American Computer Science League (ACSL) Intermediate Division 2014-2015 Contest 1 Problem Navigating ACSLland.

Navigating ACSLland problem
The Navigating ACSLland problem was the programming problem in ACSL Intermediate Division 2014-2015 Contest 1.
ACSL Intermediate 2014-2015 Navigating ACSLland problem
The first thing you do is read the Navigating ACSLland problem.
Make sure you understand the problem description. Read it many times, if necessary. Then, write your algorithm on paper before coding anything. For your algorithm, you can write either pseudocode or a flowchart.
Understanding the problem
From the problem description, you are asked to calculate the distance, travel time, and total trip cost between two cities. You are given the type of road with speed limit, kind of car and miles per gallon for that car.
YOu are given a linear highway connecting a series of cities, labeled A through G, with fixed distances between adjacent cities. For each line of input, you are given these:
- Starting and ending cities
- Vehicle type. C for Compact, M for Mid-size, F for Full-size, V for Van/SUV. This value determines your miles per gallon according to a lookup table in the problem description.
- Road type. I for Interstate, H for Highway, M for Main Road, S for Side Road. This value determines your average speed in miles per hour.
- Gas price. This is a float value that has the cost of gas per gallon.
Find Total Distance between cities
We will first calculate the distance between the two given cities. We can do this in multiple ways.
Method 1: Dictionary of tuple of cities : distance
This is one effient way of calculating distance between any two cities. We create this data type.
distances = {
('A', 'B'): 450,
('B', 'C'): 140,
('C', 'D'): 120,
('D', 'E'): 320,
('E', 'F'): 250,
('F', 'G'): 80
}
and this tuple to keep track of city name / index:
points = tuple('ABCDEFG')
To find the distance from A to C:
distances = {
('A', 'B'): 450,
('B', 'C'): 140,
('C', 'D'): 120,
('D', 'E'): 320,
('E', 'F'): 250,
('F', 'G'): 80
}
points = tuple('ABCDEFG')
total_distance = 0
starting_point_indx = points.index(starting_point)
ending_point_indx = points.index(ending_point)
for i in range(starting_point_indx, ending_point_indx):
total_distance += (distances[(points[i], points[i+1])])
Method 2: Cumulative Milestones
A second way is by accumulting the miles, using each city as checkpoint. This is more simple, but requires you to calculate mathematically the accumulated distance at each checkpoint. For example, to find the distance from A to C:
milestones = {
'A': 0,
'B': 450,
'C': 590,
'D': 710,
'E': 1030,
'F': 1280,
'G': 1360,
}
total_distance = milestones['C'] - milestones['A']
Find Travel Time
You get the travel time by dividing the total distance by the speed associated with the given road type (time = distance / speed). It's easier to find the minutes. Then, find the number of hours and leftover minutes and format it as HH:MM.
Find Gas cost
You get the fuel cost by dividing the total distance by the miles per gallon associated with the vehicle type (gallons = distance * gas price / mileage). This has to be formatted as $x.yy rounded to two decimal places.
Sample Input
The first sample input contains one line with this format:
A, C, M, H, 3.79
Ais the starting cityCis the destination cityMis the car kind, you can get the mileage (miles per gallon) hereHis the road type, you can get the speed (miles per hour) here3.79is the gas price, cost of gas per gallon
Save this sample input as a file navigating_acslland_1.in
A, C, M, H, 3.79
E, F, C, S, 3.69
B, E, F, M, 3.59
F, G, V, H, 3.89
A, G, C, I, 3.63
Sample Output
Save this sample output as a file navigating_acslland_1.out
590, 09:50, $89.44
250, 05:33, $32.95
580, 10:33, $94.65
80, 01:20, $15.56
1360, 20:55, $176.31
My Python solution
This is my working Python solution. Save it as a file navigating_acslland.py.
'''
ACSL Intermediate Division 2014-2015 Contest 1 - NAVIGATING ACSLland
Blog Post: https://aruljohn.com/blog/acsl-intermediate-navigating-acslland/
'''
'''
A to B 450 miles
B to C 140 miles
C to D 120 miles
D to E 320 miles
E to F 250 miles
F to G 80 miles
'''
# Vars
distances = {
('A', 'B'): 450,
('B', 'C'): 140,
('C', 'D'): 120,
('D', 'E'): 320,
('E', 'F'): 250,
('F', 'G'): 80
}
points = tuple('ABCDEFG')
mileages = { 'C': 28, 'M': 25, 'F': 22, 'V': 20 }
speed_limits = { 'I': 65, 'H': 60, 'M': 55, 'S': 45 }
# Return answer in the format of "distance, hh:mm, $cost"
def acslland(inline):
answer = ''
items = inline.split(', ')
if len(items) < 5:
# if invalid input
return answer
starting_point = items[0]
ending_point = items[1]
car_kind = items[2]
road_type = items[3]
gas_price = float(items[4])
# Find distance
total_distance = 0
starting_point_indx = points.index(starting_point)
ending_point_indx = points.index(ending_point)
for i in range(starting_point_indx, ending_point_indx):
total_distance += (distances[(points[i], points[i+1])])
answer = str(total_distance)
# Find time traveled
minutes = total_distance * 60 / speed_limits[road_type]
hhmm = f'{(str(int(minutes // 60))).zfill(2)}:{(str(int(round(minutes % 60)))).zfill(2)}'
answer += f', {hhmm}'
# Find gas cost
gas_price = total_distance / mileages[car_kind] * gas_price
answer += f', ${round(gas_price, 2)}'
return answer
# Main function here
if __name__ == '__main__':
line = input()
print(acslland(line))
'''
INPUT
A, C, M, H, 3.79
E, F, C, S, 3.69
B, E, F, M, 3.59
F, G, V, H, 3.89
A, G, C, I, 3.63
EXPECTED OUTPUT
590, 09:50, $89.44
250, 05:33, $32.95
580, 10:33, $94.65
80, 01:20, $15.56
1360, 20:55, $176.31
'''
Method 1: How to run the program with diff and cat
If you are running this program on Linux or macOS, you can use regular Unix/Linux commands.
$ diff <(cat navigating_acslland_1.in | python navigating_acslland.py) <(cat navigating_acslland_1.out)
Method 2: How to run the program the regular way
The regular way to run this program is:
python navigating_acslland.py
Copy/paste the input lines and press ENTER or return.
$ python navigating_acslland.py A, C, M, H, 3.79 590, 09:50, $89.44
Screenshot
$ python fibonacci_clock.py
R W G B G
W B B G R
W G B R B
G G B B B
W R G G G
04:50
08:30
10:40
11:00
01:50
Method 3: How to run the program using cat on the input file
On my Mac, I personally prefer running cat on the input file and pipe the output to the Python program.
cat navigating_acslland_1.in | python navigating_acslland.py
Screenshot
$ cat navigating_acslland_1.in | python navigating_acslland.py
04:50
08:30
10:40
11:00
01:50
Compare this output with the contents of navigating_acslland.out. They should be identical.
Use the Python shell
Feel free to test all your Python snippets in the Python shell.
Combine common code and DRY
Wherever possible, see if you can combine common code that repeats itself. DRY stands for don't repeat yourself. You can either use repeating code in a function with appropriate parameters. If you don't have time, or it's too late to make a clean fix, just leave it as it is, as long as it is working properly.
Conclusion
If there is anything you would like me to add to this article, feel free to comment below or contact me. Thanks.
Related Posts
If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.