Automating Advent of Code
Tags: python, automation, adventofcode, windows
Published: 2020-12-16 20:01:51

---
title: Automating Advent of Code
published: true
description: How I automate my files and folder creations for each days Advent of Code. Also automatically download of the input file.
tags: python, automation, adventofcode, windows
cover_image: https://blogs.sap.com/wp-content/uploads/2020/11/EkaoQQTXEAMA4BN.jpg
---

Last year, December became my favorite month of the year when I stumbled upon something called Advent of Code. It's a calendar that each day presents 2 new coding challenges for you to crack. The goal is to solve these using any programming language you want, in order to save christmas.

[Link to Advent of Code](https://adventofcode.com/)

This year I got tired of creating all the files, folders and download the input everyday, that I finally decided to do something about it.

So what does a lazy coder do? We automate!

I make 1 folder for each day containing 2 files for each part. I have a *test.py* where I test code snippets and I create a *testinput.txt* for any test input. Then I download the input file for the day and add it accordingly.

_Disclaimer: I've only tested this on Windows so your mileage may vary._

Prerequisites: Python 3, [Selenium](https://selenium-python.readthedocs.io/installation.html#downloading-python-bindings-for-selenium), [Chrome Driver](https://chromedriver.chromium.org/downloads) *(or your driver of choice)*

[Link to github](https://github.com/Zolor/adventofcode2020/blob/master/setup.py)

```python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os, sys

#Verify we're getting input
if len(sys.argv) != 2:
    print('Missing day number. Run e.g:')
    print('python setup.py 13')
    sys.exit(2)

day = str(sys.argv[1])
path = str(os.getcwd()) + "/day" + day
#Create folder from input
try:
    os.mkdir(path)
except OSError:
    print ("Creation of the directory %s failed" % path)
 
#Create 4 empty files
lista = ["/part2.py", "/test.py", "/testinput.txt"]
for i in lista:
    open(path + i, 'a').close()

#Create and Write to part1.py
part1 = path + "/part1.py"
f = open(part1, "a")
f.write('data = open("input.txt").read().split("\\n")')
f.close()

chrome_options = Options()
chrome_options.add_argument("--headless")

# Replace value with your session cookie for AoC
session_cookie = {"name": "session", "value": "<Session_cookie>"}
# initialize driver object and change the <path_to_chrome_driver> depending on your directory where your chromedriver should be
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path="<path_to_chrome_driver>")

# URLs we need
url = "https://adventofcode.com/2020/day/"
url_input = "https://adventofcode.com/2020/day/" + day + "/input"

# get request to target the site selenium is active on, add our cookies and go to input
driver.get(url)
driver.add_cookie(session_cookie)
driver.get(url_input)

# Find our text input on the input page, "pre" is our tag ID
content = driver.find_element_by_tag_name("pre").text

# Save content to file
path = path + "/input.txt"
f = open(path, "a")
for line in content:
    f.write(line)
f.close()
# Remember to quit the driver
driver.quit()

#Success?!
print(str(path) + " and adjacent files created!")
```

Feel free to use this and adjust it to your own liking. Make sure to edit the 2 fields in the code:
1. Session cookie
2. path_to_chrome_driver

Run it by executing it together with a number for the day you're planning to do, e.g:

```
python setup.py 25
```