Skip to content

Instantly share code, notes, and snippets.

@avoronkin
Created September 19, 2019 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avoronkin/f59f07917b0ed61b5f7ddc656f3325cc to your computer and use it in GitHub Desktop.
Save avoronkin/f59f07917b0ed61b5f7ddc656f3325cc to your computer and use it in GitHub Desktop.
lotto
function getTickets(){
var tickets = []
$('.bingo_ticket').each(function () {
var $ticket = $(this)
var numbers = []
$ticket.find('.numbers td').each(function () {
var $td = $(this)
var tdText = $td.text()
if(tdText) {
numbers.push(parseInt(tdText, 10))
}
})
tickets.push({
id: $ticket.find('.ticket_id').text(),
numbers: numbers.sort(sortNumber)
})
})
return tickets
}
function sortNumber(a, b) {
return a - b;
}
function nearbyCount(arr) {
return arr.reduce((count, num, index) => {
var nextNum = arr[index + 1]
if(nextNum && (nextNum - num === 1)) {
++count
}
return count
}, 0)
}
function refresh () {
$('.refresh_btn').click()
}
function highlight(ticketId){
$el = $('.ticket_id:contains("' + ticketId + '")').closest('.bingo_ticket').css( "background-color", "red" )
}
function unhighlight(){
$('.bingo_ticket').css( "background-color", "white" )
}
function search(){
unhighlight()
var tickets = getTickets()
tickets.map(t => {
t.nearbyCount = nearbyCount(t.numbers)
return t
})
const count = tickets.reduce((count, ticket) => {
count[ticket.nearbyCount] = count[ticket.nearbyCount] || 0
++count[ticket.nearbyCount]
return count
}, {})
console.log(count)
const filtered = tickets.filter(t => t.nearbyCount < 3)
filtered.forEach(t => {
highlight(t.id)
})
if(filtered.length) {
console.log('FOUND!!!', filtered.map(t => `${t.id} ${t.nearbyCount}`).join(', '))
} else {
refresh()
setTimeout(function () {
search()
}, 5000)
}
}
search()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment