Friday, November 11, 2016

Advent of Code 2015 Day 14 Solution

I'm getting in the Christmas spirit, so here's a Python 3 solution to Advent of Code Day 14's puzzle. I love this site, by the way. Looking forward to the 2016 edition!

import re
f = open("input.txt", "r")
fl = f.readlines()
f.close()
names = {}
rawdistances = {}
winner = {}
isresting = {}
##Setting up dicts to store information about each reindeer
for item in fl:
    matches = re.search("([A-Za-z]+).*?(\d+).*?(\d+).*?(\d+)", item)
    ##matches returns a set of groups: the name of the reindeer, its speed,
    ##the amount of time it can fly before resting, and the time it must rest
    name = matches.group(1)
    speed = int(matches.group(2))
    flytime = int(matches.group(3))
    resttime = int(matches.group(4))
    names[name] = [speed, flytime, resttime]
    ##Store the information of the reindeer in a list in a dict
    isresting[name] = [False, flytime]
    ##isresting stores a list with:
    ##A boolean (False if resting, True if flying)
    ##The amount of time that will elapse before starting the next segment
    rawdistances[name] = 0
    winner[name] = 0
for i in range(2503):
##Replace 2503 with 2504 to solve Part Two
##The reason is that second no. 2503 also needs to be counted for Part Two
    print(str(i))
    for reindeer in names:
        if isresting[reindeer][1] == 0 and isresting[reindeer][0] == False:
            ##Switch to resting at the beginning of the second, it rests for 1 second
            isresting[reindeer][0] = True
            isresting[reindeer][1] = names[reindeer][2]-1
            print(reindeer + ' started resting')
        elif isresting[reindeer][0] == False:
            ##It continues flying
            isresting[reindeer][1] -= 1
            rawdistances[reindeer] += names[reindeer][0]
            print(reindeer + ' flew ' + str(names[reindeer][0]))
            print(reindeer + " is at " + str(rawdistances[reindeer]))
        elif isresting[reindeer][1] == 0 and isresting[reindeer][0] == True:
            ##Switch to flying at the beginning of the second, it flies for 1 second
            isresting[reindeer][0] = False
            isresting[reindeer][1] = names[reindeer][1]-1
            rawdistances[reindeer] += names[reindeer][0]
            print(reindeer + ' stopped resting')
        else:
            ##It continues resting
            isresting[reindeer][1] -= 1
            print(reindeer + ' is resting for ' + str(isresting[reindeer][1]) + " more seconds")

    ##Uncomment the below to find solution to Part Two
    ##high = 0
    ##for item in rawdistances.values():
    ##    if high < item:
    ##        high = item
    ##currname = [k for k, v in rawdistances.items() if v == high]
    ##for item in currname:
    ##    winner[item] += 1
    ##Deindent here!
##high = 0
##for item in rawdistances.values():
##    if high < winner[list(rawdistances.keys()[[list(rawdistances.values()).index(item)]]:
##        high = winner[list(rawdistances.keys())[list(rawdistances.values()).index(item)]]
high = 0
for item in rawdistances.values():
    if high < item:
        high = item
##Print the reindeer in the lead for Part One, the reindeer with the most points for Part Two
print(high)

Wednesday, July 6, 2016

Making an HTML Website with Flask

It's very simple to make a (simple) HTML-based website using Flask to deliver webpages -- that is precisely what I did with my new site mckinleymath.com. Here's a quick guide.

You should have an HTML file before starting, but if you don't, make one using a text editor of your choice and use online resources such as w3schools or HTML Dog to write one. Call this whatever you want - the example uses index.html. If you want a mobile version, make a separate file for that - I called it mobile.html. Then find a web server that can run python files (such as Digital Ocean for $5+ per month, which is what I use). In that server, you can make a file called routes.py like this (vim on DigitalOcean is built in):
from flask import Flask, request, render_template, Blueprint
app = Flask(__name__)
app.config['BASIC_AUTH_FORCE'] = True
@app.route('/')
def site():
return render_template('index.html')##change 'index.html' as necessary
##This is if you have a mobile site
@app.route("/mobile")
def mobile():
return render_template('mobile.html') ##change 'mobile.html' as necessary
if __name__ == "__main__":
app.run(host="0.0.0.0" , port=80)

Use screen to start a new terminal session that persists after you logout. In that session, run python routes.py. Then press Ctrl and A and D simultaneously to exit the session (leaving it running). You can logout at this point using exit.

Then do this on your DNS settings page. Most providers include a "subdomain forward" option, so forward it to your servers IP address. If you have a mobile site, you can subdomain forward that in a similar way -- do m.website.com -> website.com/mobile. Then do this in resource records to enable users being able to use the site without index.html or mobile.html being part of the URL:

Name: www
Type: cname
TTL: 1h
Data: website.com.

That's it. Save your changes and wait a minute or two for the records to update and it should be functional.