Skip to content

Instantly share code, notes, and snippets.

@feugy
Created April 23, 2017 11:52
Show Gist options
  • Select an option

  • Save feugy/5c149f6864e144ede0c7f7f57ee9a9eb to your computer and use it in GitHub Desktop.

Select an option

Save feugy/5c149f6864e144ede0c7f7f57ee9a9eb to your computer and use it in GitHub Desktop.
[WIP] Solution for coding game "Ticket to Ride" (https://www.codingame.com/ide/puzzle/ticket-to-ride-europe)
const [trainCars, numTickets, numRoutes] = readline().split(' ').map(n => +n)
const colors = ['red', 'yellow', 'green', 'blue', 'white', 'black', 'orange', 'pink', 'engine']
// Player's hand: for each color type, a number of cards
const hand = {}
readline().split(' ').forEach((num, i) => {
hand[colors[i]] = +num
})
// Player's tickets: two cities and a number of earned points
const tickets = Array.from({length: numTickets}, () => {
const [p, from, to] = readline().split(' ')
return {points: +p, from, to}
}).sort((a, b) => b.points - a.points)
// Possible routes: two cities, a number of required cards and engines
const routes = Array.from({length: numRoutes}, () => {
const [l, e, c, from, to] = readline().split(' ')
return {length: +l, requiredEngines: +e, color: c.toLowerCase(), from, to}
})
// Graph of cities, by name. Contains an array of linked cities (route index)
const mapGraph = {}
routes.forEach(({from, to}, i) => {
if (!(from in mapGraph)) {
mapGraph[from] = []
}
mapGraph[from].push(i)
if (!(to in mapGraph)) {
mapGraph[to] = []
}
mapGraph[to].push(i)
})
/*printErr(JSON.stringify(hand, null, 2))
printErr(JSON.stringify(tickets, null, 2))
printErr(JSON.stringify(routes, null, 2))
printErr(JSON.stringify(mapGraph, null, 2))*/
const computeRouteScore = route => {
switch(route.length) {
case 1: return 1
case 2: return 2
case 3: return 4
case 4: return 7
case 6: return 15
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment