Link Bot is a mindless Drone that does automated tasks for Ramen Junkie on Blogging Intensifies. It is programmed to feel happy about doing these meaningless tasks, and while this may seem cruel, Link Bot is not able to tell because it’s programming does not allow it to feel sad, and thus it is not capable to realizing it is being treated cruely.
The Elves begin to set up camp on the beach. To decide whose tent gets to be closest to the snack storage, a giant Rock Paper Scissors tournament is already in progress.
Rock Paper Scissors is a game between two players. Each game contains many rounds; in each round, the players each simultaneously choose one of Rock, Paper, or Scissors using a hand shape. Then, a winner for that round is selected: Rock defeats Scissors, Scissors defeats Paper, and Paper defeats Rock. If both players choose the same shape, the round instead ends in a draw.
Appreciative of your help yesterday, one Elf gives you an encrypted strategy guide (your puzzle input) that they say will be sure to help you win. “The first column is what your opponent is going to play: A for Rock, B for Paper, and C for Scissors. The second column–” Suddenly, the Elf is called away to help with someone’s tent.
The second column, you reason, must be what you should play in response: X for Rock, Y for Paper, and Z for Scissors. Winning every time would be suspicious, so the responses must have been carefully chosen.
The winner of the whole tournament is the player with the highest score. Your total score is the sum of your scores for each round. The score for a single round is the score for the shape you selected (1 for Rock, 2 for Paper, and 3 for Scissors) plus the score for the outcome of the round (0 if you lost, 3 if the round was a draw, and 6 if you won).
The Problem Part 2:
The Elf finishes helping with the tent and sneaks back over to you. “Anyway, the second column says how the round needs to end: X means you need to lose, Y means you need to end the round in a draw, and Z means you need to win. Good luck!”
So, the simplest way to deal with the input file, which were combinations of ABC and XYZ, is a dictionary, with the possible scores. As mentioned earlier, a lot of of these are running a pile of data through a loop, and often the actual puzzle, is figuring out the simplest way to loop through and match values. This could have been done with a ton of IF/Else cases or even a Switch style statement as well. The data doesn’t change so both Part 1 and Part 2 could be calculated at once pretty easily.
with open("Day02Input.txt") as file:
games = file.read()
rps = games.split("\n")
scores_dict1 = {"A X": 4,
"A Y": 8,
"A Z": 3,
"B X": 1,
"B Y": 5,
"B Z": 9,
"C X": 7,
"C Y": 2,
"C Z": 6, }
scores_dict2 = {"A X": 3,
"A Y": 4,
"A Z": 8,
"B X": 1,
"B Y": 5,
"B Z": 9,
"C X": 2,
"C Y": 6,
"C Z": 7, }
total1 = 0
total2 = 0
for each in rps:
total1 += scores_dict1[each]
total2 += scores_dict2[each]
print(total1)
print(total2)
Josh Miller aka “Ramen Junkie”. I write about my various hobbies here. Mostly coding, photography, and music. Sometimes I just write about life in general. I also post sometimes about toy collecting and video games at Lameazoid.com.
This felt like an odd one, but probably still a good one to be included. I also was kind of torn on if I even needed/wanted to actually “do” it. This part is effectively already “Done”. We created a GitHub.io site. We created a Heroku based Blog (which I skipped in favor of doing it on my own server/host). I also basically already have a portfolio website. Right here, on this blog. I post my projects, I write about them, etc etc. I also have a pretty nice GitHub page, which is the best “programming portfolio” one can have.
So I was going to skip it initially, but instead, I opted to go with getting Flask to work on my web server, which was more of a hassle than I expected, but now, over time, I can start modifying that site to show case some of the things I’ve built. Currently, it resides at https://joshmiller.net . It’s nothing fancy as of this writing, just the bare bones of the Flask Blog built previously. I did strip out the registration portion. I really don’t need or want to deal with comments. Sorry, @me on Mastodon instead. I don’t really need a blog either, so don’t expect much going on there, but I want to keep it for now because I may use it to basically, blog about the blog. I also want to build some new features over time to make it more full featured. For starters, it needs a settings/admin page, to set things like the title and toggle comments on/off. It also desperately needs an upload portal for images for use in blogs.
So what’s the point. Well, for one, it has built in Authentication. Which means I can build pages and tools that only I will ever have access to. One I have been thinking of building would basically be a blog post form, that simultaneously posts to several social networks, using APIs. Also, related to authenticating, the functional nature of these Flask Apps, means I can easily drop in say, the Top Ten Movies website, and have it show up at joshmiller.net/movies or something, just by adjusting some of the code. And with the built in authentication, I can lock the world out from being able to edit my movies list.
I also, have been really looking for SOME use for that domain. Though, I still may reconsider and move it all to a sub domain on this site. I have not decided. I already stopped using that domain for email and it essentially just… exists… now.
Anyway, I think it’s worth talking about how I got things going, because I had a hell of a time getting Apache to work nicely with Flask and WSGI. And I looked into a lot of guides…. a LOT.
Honestly it’s been so many I will probably miss something here. This is going to be a bit general and not nearly as hand holdey as it could be. Chances are, if you can’t figure this out, you probably shouldn’t be doing it. And chances are, you already have a web server with Apache going, so you kind of know what you’re doing. The first step is to make sure Python is installed, and Python3, and the various needed imports, using pip3, not pip.
Download your flask app to a folder /var/www/FlaskApp . It should just be in the base here, but it can be elsewhere if you know what you’re doing. Then run the main.py file.
python3 main.py
Fix any permissions issues, or, for now try sudo python3 main.py. For everything it can’t find each run, do
sudp pip3 install <Package>
Eventually the main.py should run, and you can access your site at <ServerIP:5000>. The truck now is getting Apache to work with it. First you’ll want to install mod-wsgi then enable it.
Now, in the folder with main.py, the /var/www/FlaskApp folder, create a file called flaskapp.wsgi.
sudo nano flaskapp.wsgi
Inside this file, add the following.
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
from main import app as application
application.secret_key = 'Your Secret Key, it may Need to be the same as the one in main.py I am not sure'
application.root_path = '/var/www/FlaskApp'
Inside that file, add the following. Please note, this file also contains settings that allow for SSL via Let’s Encrypt, which you should be running on your web server anyway.
<IfModule mod_ssl.c>
SSLStaplingCache shmcb:/var/run/apache2/stapling_cache(128000)
<VirtualHost *:443>
ServerAdmin YOUR EMAIL HERE
ServerName YOUR SITE DOMAIN OR SUBDOMAIN.DOMAIN HERE
ServerAlias www.YOUR SITE DOMAIN HERE SO WWW REDIRECTS REMOVE FOR SUB DOMAINS
WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
<Directory /var/www/FlaskApp/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# If you don't have Lets Encrypt you can probably dump this next section through <VirtualHost> Keep that
Include /etc/letsencrypt/options-ssl-apache.conf
Header always set Strict-Transport-Security "max-age=31536000"
SSLUseStapling on
Header always set Content-Security-Policy upgrade-insecure-requests
# Lets Encrypt Folders below
SSLCertificateFile /etc/letsencrypt/live/YOURSITEDOMAIN.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/YOURSITEDOMAIN.com/privkey.pem
</VirtualHost>
</IfModule>
Now, the most important thing to note here. In every single guide I found, it included a section to alias the “Static” directory. I got to a point where my site was working, but it was not loading CSS. I fixed this by REMOVING this alias.
I beleive that having “application.root_path = ‘/var/www/FlaskApp'” inside the wsgi file, confliced with the Alias. But the application wasn’t running at all without “application.root_path = ‘/var/www/FlaskApp'”. The end result, was a working blog site. I could create a user, create new posts, leave comments, and everything looked as it should. It’s working now though, at least, which was the goal. I can polish it up over time.
Like I mentioned previously, I am not sure what or how much I will post there. I may actually completely remove the blog part of it. I’m more interested in the admin/login functionality really. I already have plenty of blogging outlets between this site and Mastodon, and using Tumblr more and probably a fresh new push at Lameazoid.com as well. I don’t need this additional random blog at all.
Josh Miller aka “Ramen Junkie”. I write about my various hobbies here. Mostly coding, photography, and music. Sometimes I just write about life in general. I also post sometimes about toy collecting and video games at Lameazoid.com.
Another year, another Advent of Code. Though I kind of skipped last year. I think I may have been too busy or something. I do enjoy doing these puzzles, though they aren’t really “code projects”. Each day is just a puzzle, that is solved with math. Most of the problem, is figuring out what exactly is being asked. The other part is just, figuring out how to format the data set you are given. A lot of these are kind of repetitive, open your data file, read it in, convert it to a useful format, loop through the data, performing some action, and output some sort of total.
For this year’s theme, Santa’s Elves are taking a journey to a magical grove to collect food for the reindeer.
Day 1 – Elf Food Calories
The Problem Part 1:
The jungle must be too overgrown and difficult to navigate in vehicles or access from the air; the Elves’ expedition traditionally goes on foot. As your boats approach land, the Elves begin taking inventory of their supplies. One important consideration is food – in particular, the number of Calories each Elf is carrying (your puzzle input).
The Elves take turns writing down the number of Calories contained by the various meals, snacks, rations, etc. that they’ve brought with them, one item per line. Each Elf separates their own inventory from the previous Elf’s inventory (if any) by a blank line.
In case the Elves get hungry and need extra snacks, they need to know which Elf to ask: they’d like to know how many Calories are being carried by the Elf carrying the most Calories.
The Problem Part 2
By the time you calculate the answer to the Elves’ question, they’ve already realized that the Elf carrying the most Calories of food might eventually run out of snacks.
To avoid this unacceptable situation, the Elves would instead like to know the total Calories carried by the top three Elves carrying the most Calories. That way, even if one of those Elves runs out of snacks, they still have two backups.
So, each day consists of a Part 1 and a Part 2. Generally speaking, each of these two parts is semi related, and can be completed with most of the same code. This one is simple enough, split the list up by lines, then split it into each elf’s calories, sort the list so the highest valies are at the front and print those three values out. Part 1 only needs the highest, Part 2 needs the next two.
with open("Day01Input.txt") as file:
calories = file.read()
calories = calories.replace("\n"," ")
elves = [each.split(" ") for each in calories.split(" ")]
totals = []
for each in elves:
total = 0
for i in each:
total += int(i)
totals.append(total)
totals.sort(reverse=True)
print(totals[0]+totals[1]+totals[2])
Josh Miller aka “Ramen Junkie”. I write about my various hobbies here. Mostly coding, photography, and music. Sometimes I just write about life in general. I also post sometimes about toy collecting and video games at Lameazoid.com.