Skip to content

Instantly share code, notes, and snippets.

@synio-wesley
synio-wesley / aoc2018_day1.py
Last active December 3, 2018 14:53
AOC 2018 - Day 1 - Python
# Part 1
print(sum([int(num) for num in open('day1.txt')]))
# Part 2
diffs = [int(num) for num in open('day1.txt')]
freq = 0
freqs = {}
i = 0
@synio-wesley
synio-wesley / aoc2018_day2.py
Last active December 3, 2018 15:29
AOC 2018 - Day 2 - Python
# Part 1
from collections import Counter
lines = [line.strip() for line in open('day2.txt')]
twos = 0
threes = 0
for line in lines:
counts = {l: c for l, c in Counter([l for l in line]).items()}
twos += 1 if 2 in counts.values() else 0
threes += 1 if 3 in counts.values() else 0
@synio-wesley
synio-wesley / aoc2018_day3.py
Last active December 3, 2018 17:25
AOC 2018 - Day 3 - Python
# Input for both parts
lines = [line.strip() for line in open('day3.txt')]
data = []
for line in lines:
parts = line.split(' ')
id = parts[0]
x, y = [int(x) for x in parts[2].rstrip(':').split(',')]
w, h = [int(x) for x in parts[3].split('x')]
data.append([id, x, y, w, h])
@synio-wesley
synio-wesley / aoc2018_day4.py
Created December 4, 2018 11:24
AOC 2018 - Day 4 - Python
# Prepare and sort data
data = [line.strip() for line in open('day4.txt')]
data.sort(key=lambda x: x.split(']')[0])
# Part 1
sleepingGuards = {}
currentGuard = None
asleepFrom = None
@synio-wesley
synio-wesley / aoc2018_day5.py
Created December 5, 2018 09:23
AOC 2018 - Day 5 - Python
# Input
polymer = [unit for unit in ''.join([line.strip() for line in open('day5.txt')])]
# Part 1 (+ function also used for part 2)
def react(polymer):
i = 0
while i + 1 < len(polymer):
unit1 = polymer[i]
@synio-wesley
synio-wesley / aoc2018_day6.py
Last active December 6, 2018 07:44
AOC 2018 - Day 6 - Python
# Input and shared code between parts
coords = [tuple(int(c) for c in line.strip().split(',')) for line in open('day6.txt')]
xMax = max(coords, key=lambda c: c[0])[0]
yMax = max(coords, key=lambda c: c[1])[1]
def manhattanDistance(p, q):
return abs(p[0] - q[0]) + abs(p[1] - q[1]);
@synio-wesley
synio-wesley / example_domains_config.json
Created April 9, 2019 08:41
Example JSON config for DNS records
# Example JSON
{
"templates": {
"empty": {
"records": []
},
"example": {
"records": [
{
@synio-wesley
synio-wesley / sync_cf_dns.py
Last active April 9, 2023 22:34
Sync CloudFlare DNS based on JSON file
"""
This is a simple Python 3 script to sync all CloudFlare DNS records based on a JSON file.
Basically:
- It adds new domains if they aren't in CloudFlare yet
- For every domain: new DNS records will be added, existing records will be updated and records not existing in the JSON will be deleted
By default the script only does a dry run (simulation). Run the script with parameter "execute" to execute it.
If you only want to add/update some domains, fill in the "only" key in the JSON file with an array of domains you want to add/update.