Add 2017 stuff and other docs

This commit is contained in:
Daniel Flanagan 2019-12-01 15:03:09 -06:00
parent 59c00b921f
commit c7edcd7ef0
11 changed files with 264 additions and 0 deletions

26
2017/day-1-part-1.py Executable file
View File

@ -0,0 +1,26 @@
#!/usr/bin/env python3
import sys
def digits(digitString):
index = 0
final = 0
while index < len(digitString):
ds1 = digitString[index]
ds2 = "-1"
if index == len(digitString) - 1:
ds2 = digitString[0]
else:
ds2 = digitString[index + 1]
if ds1 == ds2:
final += int(digitString[index])
print("DS[{}]: {}".format(index, digitString[index]))
index += 1
print("Answer: {}".format(final))
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Please provide an argument with your string of digits.")
else:
digits(sys.argv[1])

22
2017/day-1-part-2.py Executable file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env python3
import sys
def digits(digitString):
index = 0
final = 0
while index < len(digitString):
d1index = int(index)
d2index = int((int(index) + (len(digitString) / 2)) % len(digitString))
print(d1index, d2index)
if digitString[d1index] == digitString[d2index]:
final += int(digitString[d1index])
index += 1
print("Answer: {}".format(final))
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Please provide an argument with your string of digits.")
else:
digits(sys.argv[1])

35
2017/day-2-part-1.py Executable file
View File

@ -0,0 +1,35 @@
#!/usr/bin/env python3
# usage: cat inputs/day-2.txt | ./$SCRIPTNAME.py
import sys
def spreadsheet_line(line, sep="\t"):
highest = -sys.maxsize
lowest = sys.maxsize
numbers = map(int, line.strip().split(sep))
try:
for x in numbers:
if x < lowest:
lowest = x
if x > highest:
highest = x
except:
return
return highest - lowest
def checksum(spreadsheet_text, line_sep="\n"):
total = 0
lines = spreadsheet_text.split(line_sep)
for line in lines:
line_diff = spreadsheet_line(line)
try:
total += line_diff
except:
pass
return total
if __name__ == "__main__":
print(checksum(sys.stdin.read()).strip())

44
2017/day-2-part-2.py Executable file
View File

@ -0,0 +1,44 @@
#!/usr/bin/env python3
# usage: cat inputs/day-2.txt | ./$SCRIPTNAME.py
import sys
def try_to_int(val):
try:
return int(val)
except:
return
def spreadsheet_line(line, sep="\t"):
numbers = list(filter(lambda x: x != None, map(try_to_int, line.strip().split(sep))))
length = len(numbers)
print(numbers)
try:
for i in range(0, length):
x = numbers[i]
for j in range(i + 1, length):
y = numbers[j]
low = min(x, y)
high = max(x, y)
rem = high % low
if rem == 0:
print(high / low)
return high / low
except:
return
def checksum(spreadsheet_text, line_sep="\n"):
total = 0
lines = spreadsheet_text.split(line_sep)
for line in lines:
line_diff = spreadsheet_line(line)
try:
total += line_diff
except:
pass
return total
if __name__ == "__main__":
print(checksum(sys.stdin.read()).strip())

90
2017/day-3-part-1.py Executable file
View File

@ -0,0 +1,90 @@
#!/usr/bin/env python3
import sys
"""
this uses a edge detection method that I devised that I think
is particularly interesting - it basically works like this:
1. figure out the area of the spiral (especially the most recent corners
"spiraled")
2. determine the distance from the center of the current edge of the spiral
3. add that to half the length of the perpendicular edge to get the distance
this algorithm comes out to be less than O(n) - whatever the inverse of the
function for the area of a rectangle where the width and height alternately
increase O(1/rect(n)) or something of that sort.
"""
def rect_area(target):
# generator for nearly-square rectangles (alternating increasing width and
# height) up to a certain area (or width * height)
# generates tuples in the format [width, height, area (or width * height)]
# just passed a target area (or width * height)
w, h, a = 1, 1, 1
yield w, h, a
# effectively handles target = 1
while a < target:
# increase width by 1
w += 1
a = w * h
yield w, h, a
if a >= target:
break
# increase width by 1
h += 1
a = w * h
yield w, h, a
if a >= target:
break
def calculate_spiral_coordinates(slot):
# get a list of "spiral" rectangles
rects = list(rect_area(slot))
last = rects[-1]
w, h, idx = last
# this determines which edge the corner is on
# if we're on a right or top edge, we handle that differently (since it's
# always further from the center of the spiral)
# TODO: perhaps a floor() might do the trick?
cw = w
ch = h
if w % 2 == 1:
cw -= 1
if h % 2 == 1:
ch -= 1
# finally, we need to determine how close to the center of the current edge
# we are
dtc = 0
if len(rects) > 1:
next_last = rects[-2]
current_edge_center = None
if w == h: # slot is on the top or bottom edge
# since we're on the top or bottom edge, half the height plus our
# distance from the edge's center is our distance
# our distance from the edge's center is the absolute value of the
# current slot minus the previous corner's last index + half the
# modified width + 1
distance_from_center = abs(slot - (next_last[2] + (cw / 2) + 1))
return int((ch / 2) + distance_from_center)
else: # left or right edge
# same basic idea, only we just swap width and height
distance_from_center = abs(slot - (next_last[2] + (ch / 2) + 1))
return int((cw / 2) + distance_from_center)
dtc = abs(slot - current_edge_center)
return
else:
return int((cw / 2) + (ch / 2))
if __name__ == "__main__":
slot = None
try:
slot = int(sys.stdin.read().strip())
print("Slot:", slot)
spiral = calculate_spiral_coordinates(slot)
print("Distance:", spiral)
except ValueError as e: # invalid input
print("Input must be an integer.")

1
2017/inputs/day-1.txt Normal file
View File

@ -0,0 +1 @@
3294199471327195994824832197564859876682638188889768298894243832665654681412886862234525991553276578641265589959178414218389329361496673991614673626344552179413995562266818138372393213966143124914469397692587251112663217862879233226763533911128893354536353213847122251463857894159819828724827969576432191847787772732881266875469721189331882228146576832921314638221317393256471998598117289632684663355273845983933845721713497811766995367795857965222183668765517454263354111134841334631345111596131682726196574763165187889337599583345634413436165539744188866156771585647718555182529936669683581662398618765391487164715724849894563314426959348119286955144439452731762666568741612153254469131724137699832984728937865956711925592628456617133695259554548719328229938621332325125972547181236812263887375866231118312954369432937359357266467383318326239572877314765121844831126178173988799765218913178825966268816476559792947359956859989228917136267178571776316345292573489873792149646548747995389669692188457724414468727192819919448275922166321158141365237545222633688372891451842434458527698774342111482498999383831492577615154591278719656798277377363284379468757998373193231795767644654155432692988651312845433511879457921638934877557575241394363721667237778962455961493559848522582413748218971212486373232795878362964873855994697149692824917183375545192119453587398199912564474614219929345185468661129966379693813498542474732198176496694746111576925715493967296487258237854152382365579876894391815759815373319159213475555251488754279888245492373595471189191353244684697662848376529881512529221627313527441221459672786923145165989611223372241149929436247374818467481641931872972582295425936998535194423916544367799522276914445231582272368388831834437562752119325286474352863554693373718848649568451797751926315617575295381964426843625282819524747119726872193569785611959896776143539915299968276374712996485367853494734376257511273443736433464496287219615697341973131715166768916149828396454638596713572963686159214116763

16
2017/inputs/day-2.txt Normal file
View File

@ -0,0 +1,16 @@
116 1259 1045 679 1334 157 277 1217 218 641 1089 136 247 1195 239 834
269 1751 732 3016 260 6440 5773 4677 306 230 6928 7182 231 2942 2738 3617
644 128 89 361 530 97 35 604 535 297 599 121 567 106 114 480
105 408 120 363 430 102 137 283 123 258 19 101 181 477 463 279
873 116 840 105 285 238 540 22 117 125 699 953 920 106 113 259
3695 161 186 2188 3611 2802 157 2154 3394 145 2725 1327 3741 2493 3607 4041
140 1401 110 119 112 1586 125 937 1469 1015 879 1798 122 1151 100 926
2401 191 219 607 267 2362 932 2283 889 2567 2171 2409 1078 2247 2441 245
928 1142 957 1155 922 1039 452 285 467 305 506 221 281 59 667 232
3882 1698 170 5796 2557 173 1228 4630 174 3508 5629 4395 180 5100 2814 2247
396 311 223 227 340 313 355 469 229 162 107 76 363 132 453 161
627 1331 1143 1572 966 388 198 2068 201 239 176 1805 1506 1890 1980 1887
3390 5336 1730 4072 5342 216 3823 85 5408 5774 247 5308 232 256 5214 787
176 1694 1787 1586 3798 4243 157 4224 3603 2121 3733 851 2493 4136 148 153
2432 4030 3397 4032 3952 2727 157 3284 3450 3229 4169 3471 4255 155 127 186
919 615 335 816 138 97 881 790 855 89 451 789 423 108 95 116

1
2017/inputs/day-3.txt Normal file
View File

@ -0,0 +1 @@
265149

13
2017/readme.md Normal file
View File

@ -0,0 +1,13 @@
# Advent of Code 2017
> My personal solutions to the 2017 Advent of Code.
To learn about Advent of Code, check it out [here][adventofcode].
## To Do
Add `stdin` support to day 1.
[adventofcode]: https://adventofcode.com

8
2019/readme.md Normal file
View File

@ -0,0 +1,8 @@
# 2019 Advent of Code
This year, I decided to mess around more with moonscript and Lua.
See [times][times] for running speeds.
[times]: ./times.md

8
readme.md Normal file
View File

@ -0,0 +1,8 @@
# Advent of Code
This repository is meant to contain all my [Advent of Code][aoc] code throughout
the years I've participated (to whatever extent).
+ [2019](./2019)
+ [2018](./2018)
+ [2017](./2017)