Coding

Advent of Code 2020 – Day 3

Day 3 was the first that really presented some problems, and I admit, I did a “cheaty trick” to solve it. Part 1 and 2 use the same code as well, because Part 2 was a simple variation of Part 1.

For Day 3, you get a “map” of a forest with Trees. You have to count how many trees will need to be avoided for a particular path through the forest. The path is a straight line along a particular slope, (over right, down some). here is the sample forest:

..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#

So, the problem I ran into, is that the hash (#) in Python, creates comments. I am sure there is a way, but I could not figure out how to escape out the hash to compare and count it in an ‘if’ statement. So what I did instead of was replace all the #s in my map code, with ‘T’s. I can look for ‘T’s all day.

With that, here is a my solution:

def split_str(s):
  return [ch for ch in s]

with open('day3datab.txt') as f:
    lines = [line.rstrip() for line in f]

trees = 0
slopex = 1
slopey = 2
posx=0
posy=0
line_loop=len(lines[1])
distance=len(lines)-2
#print distance

while (posy<distance):
  #print trees
  posx=posx+slopex
  posy=posy+slopey
  #print str(posy)+"/"+str(distance)
  if(posx>=line_loop):
    posx=posx-line_loop
  #print str(posx)+","+str(posy)
  print str(lines[posy][posx])
  whatis=str(lines[posy][posx])
  if (whatis == "T"):
    trees+=1
  print trees

The way the problem works is, the forest repeats to the right infinitely. So step one was getting the width of the forest repeat sequence, so I could loop my position back around as needed. Otherwise it’s a simple matter of updating the position of ‘row’ and ‘col’, checking if it needs to loop (is row position greater than the loop, if so subtract the loop value), then check for a T(ree).

Part 2 was the same problem, except you check several slopes and multiply the results together. To solve this I just adjusted my slope variables and then used a calculator to multiply the results.

I could have changed the code to ask for the slope each run, or even have it loop through several slopes until you tell it to end, when it would multiply the results itself, but frankly, I don’t have that kind of time. This is a fun side project to do while watching TV at night.

Advent of Code 2020 – Day 2

Day two is a little more complicated than Day 1 was. Today’s challenge is to take a blob of passwords, and verify if they are acceptable or not, per the “company standard” at the time of the creation of each password.

For example:

1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc

For part 1, on line 1, the letter a should appear at least 1 time and at most 3 times. For line 2, the letter b should appear at least 1 time and at most 3 times. For line 3, the letter c should appear at least 2 times and at most 9 times.

My solution for Day 2, Part 1 is below:

number_valid = 0
toolow = 0
toohigh = 0
number_of_entries = 0

with open('day2data.txt') as f:
    lines = [line.rstrip() for line in f]

for x in lines:
  dashloc = x.find("-")
  spaceloc = x.find(" ")
  colloc = x.find(":")
  mincount=int(x[0:dashloc])
  maxcount=int(x[int(dashloc)+1:spaceloc])
  limitchar=x[int(spaceloc+1):int(colloc)]
  password = x[int(colloc+2):]
  checkvalue = int(password.count(limitchar))
  if(mincount <= checkvalue <= maxcount):
    number_valid=number_valid+1
  if(checkvalue<mincount):
    toolow+=1
  if(checkvalue>maxcount):
    toohigh+=1
  if(mincount>=maxcount):
    print "Problem?"

print number_valid
#print toolow
#print toohigh
#print number_valid+toolow+toohigh

On a note, I’ve got some debugging bits still in there, that are commented out. After reading the file in, I’ve initialized some variables I’ll be using in the code. Then I start looping through the data.

This is where my janky code really gets to shine, as I am sure there is a better way to handle this. First step is to locate the special characters separating the data, from the exampe, “1-3 a:abcde”, these are a dash, a space and a colon. With these located, I’ve extracted the min and max values, the letter I’m tracking, and the password itself.

After getting these core values, it’s simple enough to count the occurrences of the letter, then verify it against the min/max values and count how many are valid.

I had a bit of trouble at first because I had used “password = x[int(colloc+2):-1]” instead of “password = x[int(colloc+2):]”. I’m not a master of a lot of programming languages, but I am familiar with enough of them that I get the syntax confused a lot (I keep forgetting the colon on ifs and loops in Python). I forget which language uses -1 for “go to tot he end of a range”, but I am pretty sure there is one, because I did this a lot at first.

Part 2 changes things up a bit. Instead of the first numbers representing a range of how many, it demands that the special letter appears at one of those two positions, but not both.

My solution for part 2 is below:

number_valid = 0
number_of_entries = 0

with open('day2data.txt') as f:
    lines = [line.rstrip() for line in f]

for x in lines:
  first=0
  second=0
  dashloc = x.find("-")
  spaceloc = x.find(" ")
  colloc = x.find(":")
  mincount=int(x[0:dashloc])-1
  maxcount=int(x[int(dashloc)+1:spaceloc])-1
  limitchar=x[int(spaceloc+1):int(colloc)]
  password = x[int(colloc+2):]
  if(password[mincount]==limitchar):
    first=1
  if(password[maxcount]==limitchar):
    second=1
  if ((first==1) or (second==1)):
    if(first !=second):
      number_valid+=1

print number_valid
#print toolow
#print toohigh
#print number_valid+toolow+toohigh

More sloppy code, i didn’t even change the variable names. Now, instead of counting and comparing, I’m checking for the special character in each position, and toggling a variable if it’s there or not, then making sure it’s nor present in both. This is where the new variables “first” and “second” come in.

This biggest challenge on Day 2 was that it required more manipulation of the input data.

Github Repository of my Solutions

Advent of Code 2020 – Day 1

So, I want to say up front, I don’t know if I will finish this, but I plan to try. Also, while I intend to publish these posts on the respective day of each challenge, I may not actually DO the challenge day of. That is to say, some of these, probably most of these, will be back dated.

Advent of Code is a little 25 day advent calendar of code based challenges. I heard it mentioned on TWIT by Leo Laporte. It can be done in any language or system. There are people who solve these using game engines and such. As this is my first go, and I am not a “professional programmer”, I am doing in in sloppy Python. I’ll be posting my solutions in a Github Repo, which means this challenge serves a second purpose of helping me have an excuse to learn how to better use Github.

The first day’s challenge is pretty simple. Given a list of numbers, figure out which ones add up to 2020 and then multiply them together. Each day has two challenges, based on the same base data set. The data set for each person seems to be different. Here is my solution for Day 1 Part 1:

with open('day1data.txt') as f:
    lines = [line.rstrip() for line in f]

for x in lines:
  for y in lines:
    if((int(x)+int(y))==2020):
      print int(x)*int(y)

Each day of this puzzle (so far) involves reading in a data file, and working with it. My solution involves looping each number, and multiplying it by each other number, and checking for if they sum 2020, then posting the result of multiplying the two numbers.

Part two is essentially the same except it involves three numbers instead of two. This pretty much just meant adding another layer of loop to my loops.

with open('day1data.txt') as f:
    lines = [line.rstrip() for line in f]

for x in lines:
  for y in lines:
    for z in lines:
      if((int(x)+int(y)+int(z))==2020):
        print int(x)*int(y)*int(z)

This first days’ challenge was pretty simple. The rest of the days, not so much.

Code Project: My Home Dashboard

So, this isn’t going to really have any code. I might, sometime int he distant future, publish some code, but this whole thing is very much a “add things as I go, ongoing project. The base code itself isn’t particularly complicated though. It’s a pretty simple HTML/PHP/CSS layout that wraps around various modules I’ve been building. I keep mentioning the Dashboard though when talking about the various projects, so I figure I should do a quick run down on what the Dashboard involves.

I’m actually building a more complex iteration of this project at work as well, to be used internally by my work group. The work one is considerably more complex, for example, it has a much more robust Admin area that is growing with features to manage locations, manage users, manage user teams, etc. The base layout framework is shared between the two dashboards, but the work one has a lot more actual functionality. Because I am the only one using the home version, I generally just code everything in, so it’s less modular. I also have to translate any code I write for one or the other version between the different Database back ends. I use MySQL at home, I use MS SQL at work.

At it’s base, it’s just a webpage on my project webserver that displays information. Some of that information is useful, some is just there to fill space and to practice coding something up. i mentioned above, it’s essentially a Header, sidebar, and Footer that wrap around a variable content box. On the home page, the content box contains what I have been calling “Quick Cards” with bits of information, that sometimes link to larger chunks of data. This is what it looks like, at the moment, on the home page.

I’ll dive in a bit on some of the menus and content but I am going to start with the Quick Card boxes, in order.

The Weather box seemed like an obvious choice for at a glance information. I want to make it link to a sub page with more forecast data, but for now, it just displays the current weather conditions for my location. Unfortunately it’s built on the Dark Sky API, which very recently announced is closing down, so I’ll have to find a new API to use.

Next is the COVID-19 stats widget. This is the other side of the COVID-19 Tracking Python Script I posted recent. It just pulls and displays the most recent information that the script has pulled. I may update this to link to a page with some timeline graphs on it, once I figure out how to put a data graph in a webpage.

Network devices is the most robust of all of the modules I’ve built so far. The Quick Card just shows the current number of active devices on the home network. Clicking it opens the Network Device page I talk about here.

Social Accounts is just link list to the various Twitter Accounts I have. I want to change this to be a modifiable list eventually, but for now it’s just a list. It does do a database pull to build the URLs, but I have not added a configuration page yet.

The next box displays how many unread posts are on each of my TT-RSS accounts. After Google killed Google Reader, I set up TinyTiny-RSS on my webserver and started using it for my feeds. I became overwhelmed so I broke all of my feeds into themed sub accounts. I would link to each sub account, but it’s all the same link, just with a different log in, so the links would be useless. Normally, I just use container tags to keep the different log in instances open.

Lastly is a tracker for Reddit Karma for several Reddit Accounts I have. Like my RSS feeds, I have broken my Reddit subs out into seperate themed accounts. i don’t really care that much about Reddit Karma, but I wanted to play around with APIs and JSON, so I figured this would be an easy project. I will probably post the script used in the future, but it’s essentially identical to the recently posted COVID-19 script. In fact the COVID-19 script was adapted from the Reddit Karma script.

Along the top navigation bar are some drop downs with useful links that don’t really have “At a glance” data. The first two, “My Websites” and “My Hosted Apps” are just drop downs with links to the Blogs I manage and my Webhosted apps for Email and TT-RSS.

The next drop down is similar in nature, in that it’s a list of links, but this one has an admin page so I can maintain the list as it changes. It also hasn’t quite found a home yet. I had it in the sidebar for a while, then I had it in a Quick Card, now it’s int he Navigation menu. It’s a list of links to web services on my internal home network. It’s linkes to Routers, Raspberry Pis, IP Cameras, my NAS, and to various things I have set up on my Project Server.

Next to that is the Gas Tracker, which is very much a WIP page. When I bought my car back in 2014, I decided I wanted to track my Gas consumption for the life of the car. Currently this lives in an Excel Spread Sheet on One Drive. I wanted to see about translating it into my own webpage and using SQL as the back end. Currently it just displays a table of data that I imported from Excel. There isn’t any way to add new data yet and it doesn’t calculate the Price/Gallon or total money paid or anything like that.

Lastly is an old project I did called Tweeter that I got up and running again and embedded into the Dashboard. Tweeter itself is fairly self contained and I will probably do a couple of detailed write ups on it in the future and post the code at that time. I also want to update it to use SQL as the back end so I’ll do a second write up when that happens.

A while back, I was looking for a way to automate posting Tweets, mostly so I could share links to articles, but have them space out over the course of a day. I couldn’t find a decent free service, and thus, Tweeter was born. Tweeter is a two part solution, it’s a PHP page that writes a text file, and some scripts (python and bash) that runs on a schedule and posts the contents of that text file to Twitter, one line at a time, on whatever cron schedule is set. I’m not going to go into anymore here, but I promise to post about it int he future. It’s also a little ugly and probably insecure as hell, but it works well.

The main fun of integrating Tweeter, the box is 140 characters wide, the same as a tweet. So I had to modify my core code framework to have a toggle for pages that don’t display the sidebar. It wasn’t anything complicated, but I hadn’t considered that need, and so I fixed it. That’s kind of part of the fun and point of doing these sort of code projects.

Tracking Covid-19 into a database using Python

At some point I need to do a little write up on my Home Dashboard Project, it’s inspired quite a few minor projects such as this one to make little web widgets. The dashboard is the simple part, it’s just dumping a database query into a table. Honestly, the script was easy too, because I adapted it from another script I built recently.

With COVID-19 all over the news, I wanted to add some stats to my dashboard for my state. Not so much because there aren’t already 1000 other places to get the numbers, but more to see if I could do it. The hardest part was finding a feed to stats. Then I found CovidTracking.com. Which has a nice little API. I then set to work adapting another script to pull from this API to dump stats for Illinois into the database. I am only interested in Illinois, but the script is built so the user can put a list of states into an array, and then it will loop through and add them all to the database.

The script is below, but this also requires some set up in SQL. Nothing complicated, mostly INT fields. an id as an int and primary key, negative_cases, positive_cases, and deaths, all as INT, state as a varchar with a length of 2, though technically the length is optional, then finally date_stamp as a DATETIME field with a default value of the current timestamp. The DATETIME isn’t directly touched here, but it makes it easier to manipulate the data later.

The code also requires you enter your database credentials. I’ve nammed my table “il_covid_stats, but you can change that to whatever you want down below in the “SQL = “INSERT….” line. I’ll leave it up to you what to do with the data, I pull mine into a PHP page.

Anyway, here is the python code:

# Python Covid Star Tracking to SQL
# use of json package
# Sample URL: https://covidtracking.com/api/states?state=IL

import json
import requests
import time
import MySQLdb

mydb = MySQLdb.connect(
  host="localhost",
  user="YOUR_DB_USERNAME",
  passwd="YOUR_DB_PASSWORD",
  database="YOUR_DB_NAME"
)
mycursor = mydb.cursor()
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'

#States to check as an Array, two letter abbreviations
states = ['IL']

def data_getter(statename):
  ####when reading from remote URL
  url = 'https://covidtracking.com/api/states?state='+statename

  user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
  headers = {'User-Agent': user_agent}
  response = requests.get(url,headers=headers)
  html = response.content
  statedata = json.loads(html)

  pos_cases = (statedata['positive'])
  neg_cases = (statedata['negative'])
  deaths = (statedata['death'])

  vals = (pos_cases,neg_cases,deaths,statename)

  mysqlinsert(vals)

def mysqlinsert(vals):
  ## This database name and columns can be changed but should be pre made in your database
  SQL = "INSERT INTO il_covid_stats (positive_cases, negative_cases, deaths, state) VALUES (%s, %s, %s, %s)"
  mycursor.execute(SQL, vals)
  mydb.commit()

# Loop through URLs for each state
for i in states:
  data_getter(i)