Browse Source

AoC 2015. Yes, the whole thing.

master
Terrana Ninetailed 2 years ago
parent
commit
1e70780b66
  1. 1
      2015/day01/part1.py
  2. 10
      2015/day01/part2.py
  3. 1000
      2015/day02/data.txt
  4. 12
      2015/day02/part1.py
  5. 12
      2015/day02/part2.py
  6. 1
      2015/day03/data.txt
  7. 23
      2015/day03/part1.py
  8. 31
      2015/day03/part2.py
  9. 28
      2015/day04/part1.py
  10. 16
      2015/day04/part2.py
  11. 1000
      2015/day05/data.txt
  12. 39
      2015/day05/part1.py
  13. 43
      2015/day05/part2.py
  14. 300
      2015/day06/data.txt
  15. 28
      2015/day06/part1.py
  16. 28
      2015/day06/part2.py
  17. 339
      2015/day07/data.txt
  18. 66
      2015/day07/part1.py
  19. 68
      2015/day07/part2.py
  20. 300
      2015/day08/data.txt
  21. 13
      2015/day08/part1.py
  22. 18
      2015/day08/part2.py
  23. 28
      2015/day09/data.txt
  24. 36
      2015/day09/part1.py
  25. 35
      2015/day09/part2.py
  26. 28
      2015/day10/bothparts.py
  27. 41
      2015/day11/bothparts.py
  28. 1
      2015/day12/data.json
  29. 6
      2015/day12/part1.py
  30. 24
      2015/day12/part2.py
  31. 47
      2015/day13/bothparts.py
  32. 56
      2015/day13/data.txt
  33. 9
      2015/day14/data.txt
  34. 15
      2015/day14/part1.py
  35. 60
      2015/day14/part2.py
  36. 4
      2015/day15/data.txt
  37. 61
      2015/day15/part1.py
  38. 61
      2015/day15/part2.py
  39. 500
      2015/day16/data.txt
  40. 40
      2015/day16/part1.py
  41. 53
      2015/day16/part2.py
  42. 20
      2015/day17/data.txt
  43. 13
      2015/day17/part1.py
  44. 20
      2015/day17/part2.py
  45. 100
      2015/day18/data.txt
  46. 57
      2015/day18/part1.py
  47. 41
      2015/day18/part2.py
  48. 45
      2015/day19/data.txt
  49. 35
      2015/day19/part1.py
  50. 86
      2015/day19/part2.py
  51. 23
      2015/day20/part1.py
  52. 24
      2015/day20/part2.py
  53. 3
      2015/day21/data.txt
  54. 123
      2015/day21/part1.py
  55. 99
      2015/day21/part2.py
  56. 102
      2015/day22/part1.py
  57. 102
      2015/day22/part2.py
  58. 48
      2015/day23/data.txt
  59. 56
      2015/day23/part1.py
  60. 56
      2015/day23/part2.py
  61. 29
      2015/day24/data.txt
  62. 45
      2015/day24/part1.py
  63. 45
      2015/day24/part2.py
  64. 25
      2015/day25/part1.py

1
2015/day01/part1.py

File diff suppressed because one or more lines are too long

10
2015/day01/part2.py

File diff suppressed because one or more lines are too long

1000
2015/day02/data.txt

File diff suppressed because it is too large

12
2015/day02/part1.py

@ -0,0 +1,12 @@
with open('data.txt') as data_in:
data = [line.strip() for line in data_in]
accumulator = 0
for present in data:
l, w, h = (int(d) for d in present.split('x'))
area = 2*l*w + 2*w*h + 2*h*l
extra = min(l*w, w*h, h*l)
accumulator += area + extra
print(accumulator)

12
2015/day02/part2.py

@ -0,0 +1,12 @@
with open('data.txt') as data_in:
data = [line.strip() for line in data_in]
accumulator = 0
for present in data:
l, w, h = (int(d) for d in present.split('x'))
length = 2 * min(l+w, w+h, h+l)
bow = l * w * h
accumulator += length + bow
print(accumulator)

1
2015/day03/data.txt

File diff suppressed because one or more lines are too long

23
2015/day03/part1.py

@ -0,0 +1,23 @@
with open('data.txt') as data_in:
data = data_in.read().strip()
instructions = {
'<': (-1, 0),
'>': ( 1, 0),
'v': ( 0,-1),
'^': ( 0, 1),
}
visited = {0: {0: 1}}
x = 0
y = 0
for instruction in data:
dx, dy = instructions.get(instruction, (0,0))
x += dx
y += dy
if y not in visited: visited[y] = {}
if x not in visited[y]: visited[y][x] = 0
visited[y][x] += 1
print(sum(len(y.keys()) for y in visited.values()))

31
2015/day03/part2.py

@ -0,0 +1,31 @@
with open('data.txt') as data_in:
data = data_in.read().strip()
instructions = {
'<': (-1, 0),
'>': ( 1, 0),
'v': ( 0,-1),
'^': ( 0, 1),
}
visited = {0: {0: 1}}
x = 0
y = 0
x2 = 0
y2 = 0
for i in range(len(data)):
dx, dy = instructions.get(data[i], (0,0))
if i & 1 == 0:
x += dx
y += dy
if y not in visited: visited[y] = {}
if x not in visited[y]: visited[y][x] = 0
visited[y][x] += 1
else:
x2 += dx
y2 += dy
if y2 not in visited: visited[y2] = {}
if x2 not in visited[y2]: visited[y2][x2] = 0
print(sum(len(y.keys()) for y in visited.values()))

28
2015/day04/part1.py

@ -0,0 +1,28 @@
import hashlib
import sys
def run(data):
suffix = 0
data = data.encode()
while True:
md5 = hashlib.md5()
md5.update(data)
md5.update(str(suffix).encode())
result = md5.hexdigest()
if result[:5] == '00000': return suffix
suffix += 1
test_data = (
('abcdef', 609043),
('pqrstuv', 1048970)
)
for data, expected in test_data:
result = run(data)
if result != expected:
print(f'[{data}]\nExpected: [{expected}]\nActual: [{result}]')
sys.exit()
real_data = 'ckczppom'
print(run(real_data))

16
2015/day04/part2.py

@ -0,0 +1,16 @@
import hashlib
def run(data):
suffix = 0
data = data.encode()
while True:
md5 = hashlib.md5()
md5.update(data)
md5.update(str(suffix).encode())
result = md5.hexdigest()
if result[:6] == '000000': return suffix
suffix += 1
real_data = 'ckczppom'
print(run(real_data))

1000
2015/day05/data.txt

File diff suppressed because it is too large

39
2015/day05/part1.py

@ -0,0 +1,39 @@
import re
import sys
vowels = ('a', 'e', 'i', 'o', 'u')
forbidden = ('ab', 'cd', 'pq', 'xy')
def is_nice(data):
vowel_count = 0
repeat = False
last = ''
for c in data:
if last + c in forbidden: return False
if c in vowels: vowel_count += 1
if not repeat and last == c: repeat = True
last = c
return repeat and vowel_count >= 3
test_data = (
('ugknbfddgicrmopn', True),
('aaa', True),
('jchzalrnumimnmhp', False),
('haegwjzuvuyypxyu', False),
('dvszwmarrgswjxmb', False)
)
for data, expected in test_data:
result = is_nice(data)
if result != expected:
print(f'[{data}]\nExpected: [{expected}]\nActual: [{result}]')
sys.exit()
nice = 0
with open('data.txt') as data_in:
for line in data_in:
if is_nice(line): nice += 1
print(nice)

43
2015/day05/part2.py

@ -0,0 +1,43 @@
import sys
def is_nice(data):
vowel_count = 0
sandwich = False
repeat_pair = False
last = None
last_but_one = None
pairs = []
for c in data:
if not repeat_pair and last and not (c == last and c == last_but_one):
pair = last + c
if pair in pairs:
repeat_pair = True
else:
pairs.append(pair)
if not sandwich and last_but_one == c and last != c: sandwich = True
if repeat_pair and sandwich: return True
last_but_one = last
last = c
return False
test_data = (
('qjhvhtzxzqqjkmpb', True),
('xxyxx', True),
('uurcxstgmygtbstg', False),
('ieodomkazucvgmuy', False)
)
for data, expected in test_data:
result = is_nice(data)
if result != expected:
print(f'[{data}]\nExpected: [{expected}]\nActual: [{result}]')
sys.exit()
nice = 0
with open('data.txt') as data_in:
for line in data_in:
if is_nice(line): nice += 1
print(nice)

300
2015/day06/data.txt

@ -0,0 +1,300 @@
turn off 660,55 through 986,197
turn off 341,304 through 638,850
turn off 199,133 through 461,193
toggle 322,558 through 977,958
toggle 537,781 through 687,941
turn on 226,196 through 599,390
turn on 240,129 through 703,297
turn on 317,329 through 451,798
turn on 957,736 through 977,890
turn on 263,530 through 559,664
turn on 158,270 through 243,802
toggle 223,39 through 454,511
toggle 544,218 through 979,872
turn on 313,306 through 363,621
toggle 173,401 through 496,407
toggle 333,60 through 748,159
turn off 87,577 through 484,608
turn on 809,648 through 826,999
toggle 352,432 through 628,550
turn off 197,408 through 579,569
turn off 1,629 through 802,633
turn off 61,44 through 567,111
toggle 880,25 through 903,973
turn on 347,123 through 864,746
toggle 728,877 through 996,975
turn on 121,895 through 349,906
turn on 888,547 through 931,628
toggle 398,782 through 834,882
turn on 966,850 through 989,953
turn off 891,543 through 914,991
toggle 908,77 through 916,117
turn on 576,900 through 943,934
turn off 580,170 through 963,206
turn on 184,638 through 192,944
toggle 940,147 through 978,730
turn off 854,56 through 965,591
toggle 717,172 through 947,995
toggle 426,987 through 705,998
turn on 987,157 through 992,278
toggle 995,774 through 997,784
turn off 796,96 through 845,182
turn off 451,87 through 711,655
turn off 380,93 through 968,676
turn on 263,468 through 343,534
turn on 917,936 through 928,959
toggle 478,7 through 573,148
turn off 428,339 through 603,624
turn off 400,880 through 914,953
toggle 679,428 through 752,779
turn off 697,981 through 709,986
toggle 482,566 through 505,725
turn off 956,368 through 993,516
toggle 735,823 through 783,883
turn off 48,487 through 892,496
turn off 116,680 through 564,819
turn on 633,865 through 729,930
turn off 314,618 through 571,922
toggle 138,166 through 936,266
turn on 444,732 through 664,960
turn off 109,337 through 972,497
turn off 51,432 through 77,996
turn off 259,297 through 366,744
toggle 801,130 through 917,544
toggle 767,982 through 847,996
turn on 216,507 through 863,885
turn off 61,441 through 465,731
turn on 849,970 through 944,987
toggle 845,76 through 852,951
toggle 732,615 through 851,936
toggle 251,128 through 454,778
turn on 324,429 through 352,539
toggle 52,450 through 932,863
turn off 449,379 through 789,490
turn on 317,319 through 936,449
toggle 887,670 through 957,838
toggle 671,613 through 856,664
turn off 186,648 through 985,991
turn off 471,689 through 731,717
toggle 91,331 through 750,758
toggle 201,73 through 956,524
toggle 82,614 through 520,686
toggle 84,287 through 467,734
turn off 132,367 through 208,838
toggle 558,684 through 663,920
turn on 237,952 through 265,997
turn on 694,713 through 714,754
turn on 632,523 through 862,827
turn on 918,780 through 948,916
turn on 349,586 through 663,976
toggle 231,29 through 257,589
toggle 886,428 through 902,993
turn on 106,353 through 236,374
turn on 734,577 through 759,684
turn off 347,843 through 696,912
turn on 286,699 through 964,883
turn on 605,875 through 960,987
turn off 328,286 through 869,461
turn off 472,569 through 980,848
toggle 673,573 through 702,884
turn off 398,284 through 738,332
turn on 158,50 through 284,411
turn off 390,284 through 585,663
turn on 156,579 through 646,581
turn on 875,493 through 989,980
toggle 486,391 through 924,539
turn on 236,722 through 272,964
toggle 228,282 through 470,581
toggle 584,389 through 750,761
turn off 899,516 through 900,925
turn on 105,229 through 822,846
turn off 253,77 through 371,877
turn on 826,987 through 906,992
turn off 13,152 through 615,931
turn on 835,320 through 942,399
turn on 463,504 through 536,720
toggle 746,942 through 786,998
turn off 867,333 through 965,403
turn on 591,477 through 743,692
turn off 403,437 through 508,908
turn on 26,723 through 368,814
turn on 409,485 through 799,809
turn on 115,630 through 704,705
turn off 228,183 through 317,220
toggle 300,649 through 382,842
turn off 495,365 through 745,562
turn on 698,346 through 744,873
turn on 822,932 through 951,934
toggle 805,30 through 925,421
toggle 441,152 through 653,274
toggle 160,81 through 257,587
turn off 350,781 through 532,917
toggle 40,583 through 348,636
turn on 280,306 through 483,395
toggle 392,936 through 880,955
toggle 496,591 through 851,934
turn off 780,887 through 946,994
turn off 205,735 through 281,863
toggle 100,876 through 937,915
turn on 392,393 through 702,878
turn on 956,374 through 976,636
toggle 478,262 through 894,775
turn off 279,65 through 451,677
turn on 397,541 through 809,847
turn on 444,291 through 451,586
toggle 721,408 through 861,598
turn on 275,365 through 609,382
turn on 736,24 through 839,72
turn off 86,492 through 582,712
turn on 676,676 through 709,703
turn off 105,710 through 374,817
toggle 328,748 through 845,757
toggle 335,79 through 394,326
toggle 193,157 through 633,885
turn on 227,48 through 769,743
toggle 148,333 through 614,568
toggle 22,30 through 436,263
toggle 547,447 through 688,969
toggle 576,621 through 987,740
turn on 711,334 through 799,515
turn on 541,448 through 654,951
toggle 792,199 through 798,990
turn on 89,956 through 609,960
toggle 724,433 through 929,630
toggle 144,895 through 201,916
toggle 226,730 through 632,871
turn off 760,819 through 828,974
toggle 887,180 through 940,310
toggle 222,327 through 805,590
turn off 630,824 through 885,963
turn on 940,740 through 954,946
turn on 193,373 through 779,515
toggle 304,955 through 469,975
turn off 405,480 through 546,960
turn on 662,123 through 690,669
turn off 615,238 through 750,714
turn on 423,220 through 930,353
turn on 329,769 through 358,970
toggle 590,151 through 704,722
turn off 884,539 through 894,671
toggle 449,241 through 984,549
toggle 449,260 through 496,464
turn off 306,448 through 602,924
turn on 286,805 through 555,901
toggle 722,177 through 922,298
toggle 491,554 through 723,753
turn on 80,849 through 174,996
turn off 296,561 through 530,856
toggle 653,10 through 972,284
toggle 529,236 through 672,614
toggle 791,598 through 989,695
turn on 19,45 through 575,757
toggle 111,55 through 880,871
turn off 197,897 through 943,982
turn on 912,336 through 977,605
toggle 101,221 through 537,450
turn on 101,104 through 969,447
toggle 71,527 through 587,717
toggle 336,445 through 593,889
toggle 214,179 through 575,699
turn on 86,313 through 96,674
toggle 566,427 through 906,888
turn off 641,597 through 850,845
turn on 606,524 through 883,704
turn on 835,775 through 867,887
toggle 547,301 through 897,515
toggle 289,930 through 413,979
turn on 361,122 through 457,226
turn on 162,187 through 374,746
turn on 348,461 through 454,675
turn off 966,532 through 985,537
turn on 172,354 through 630,606
turn off 501,880 through 680,993
turn off 8,70 through 566,592
toggle 433,73 through 690,651
toggle 840,798 through 902,971
toggle 822,204 through 893,760
turn off 453,496 through 649,795
turn off 969,549 through 990,942
turn off 789,28 through 930,267
toggle 880,98 through 932,434
toggle 568,674 through 669,753
turn on 686,228 through 903,271
turn on 263,995 through 478,999
toggle 534,675 through 687,955
turn off 342,434 through 592,986
toggle 404,768 through 677,867
toggle 126,723 through 978,987
toggle 749,675 through 978,959
turn off 445,330 through 446,885
turn off 463,205 through 924,815
turn off 417,430 through 915,472
turn on 544,990 through 912,999
turn off 201,255 through 834,789
turn off 261,142 through 537,862
turn off 562,934 through 832,984
turn off 459,978 through 691,980
turn off 73,911 through 971,972
turn on 560,448 through 723,810
turn on 204,630 through 217,854
turn off 91,259 through 611,607
turn on 877,32 through 978,815
turn off 950,438 through 974,746
toggle 426,30 through 609,917
toggle 696,37 through 859,201
toggle 242,417 through 682,572
turn off 388,401 through 979,528
turn off 79,345 through 848,685
turn off 98,91 through 800,434
toggle 650,700 through 972,843
turn off 530,450 through 538,926
turn on 428,559 through 962,909
turn on 78,138 through 92,940
toggle 194,117 through 867,157
toggle 785,355 through 860,617
turn off 379,441 through 935,708
turn off 605,133 through 644,911
toggle 10,963 through 484,975
turn off 359,988 through 525,991
turn off 509,138 through 787,411
toggle 556,467 through 562,773
turn on 119,486 through 246,900
turn on 445,561 through 794,673
turn off 598,681 through 978,921
turn off 974,230 through 995,641
turn off 760,75 through 800,275
toggle 441,215 through 528,680
turn off 701,636 through 928,877
turn on 165,753 through 202,780
toggle 501,412 through 998,516
toggle 161,105 through 657,395
turn on 113,340 through 472,972
toggle 384,994 through 663,999
turn on 969,994 through 983,997
turn on 519,600 through 750,615
turn off 363,899 through 948,935
turn on 271,845 through 454,882
turn off 376,528 through 779,640
toggle 767,98 through 854,853
toggle 107,322 through 378,688
turn off 235,899 through 818,932
turn on 445,611 through 532,705
toggle 629,387 through 814,577
toggle 112,414 through 387,421
toggle 319,184 through 382,203
turn on 627,796 through 973,940
toggle 602,45 through 763,151
turn off 441,375 through 974,545
toggle 871,952 through 989,998
turn on 717,272 through 850,817
toggle 475,711 through 921,882
toggle 66,191 through 757,481
turn off 50,197 through 733,656
toggle 83,575 through 915,728
turn on 777,812 through 837,912
turn on 20,984 through 571,994
turn off 446,432 through 458,648
turn on 715,871 through 722,890
toggle 424,675 through 740,862
toggle 580,592 through 671,900
toggle 296,687 through 906,775

28
2015/day06/part1.py

@ -0,0 +1,28 @@
import re
instruction_pattern = re.compile('([a-z ]+) ([0-9]+),([0-9]+) .+ ([0-9]+),([0-9]+)')
operations = {
'turn on': lambda state: True,
'turn off': lambda state: False,
'toggle': lambda state: not state
}
def execute(lights, instruction):
global operations
opcode, x1, y1, x2, y2 = instruction_pattern.search(instruction).groups()
op = operations[opcode];
for y in range(int(y1), int(y2)+1):
for x in range(int(x1), int(x2)+1):
lights[y][x] = op(lights[y][x])
lights = [];
for row in range(1000):
lights.append([False] * 1000)
with open('data.txt') as data:
for line in data:
execute(lights, line)
print(sum(sum(l == True for l in row) for row in lights))

28
2015/day06/part2.py

@ -0,0 +1,28 @@
import re
instruction_pattern = re.compile('([a-z ]+) ([0-9]+),([0-9]+) .+ ([0-9]+),([0-9]+)')
operations = {
'turn on': lambda state: state + 1,
'turn off': lambda state: max(0, state - 1),
'toggle': lambda state: state + 2
}
def execute(lights, instruction):
global operations
opcode, x1, y1, x2, y2 = instruction_pattern.search(instruction).groups()
op = operations[opcode];
for y in range(int(y1), int(y2)+1):
for x in range(int(x1), int(x2)+1):
lights[y][x] = op(lights[y][x])
lights = [];
for row in range(1000):
lights.append([0] * 1000)
with open('data.txt') as data:
for line in data:
execute(lights, line)
print(sum(sum(row) for row in lights))

339
2015/day07/data.txt

@ -0,0 +1,339 @@
bn RSHIFT 2 -> bo
lf RSHIFT 1 -> ly
fo RSHIFT 3 -> fq
cj OR cp -> cq
fo OR fz -> ga
t OR s -> u
lx -> a
NOT ax -> ay
he RSHIFT 2 -> hf
lf OR lq -> lr
lr AND lt -> lu
dy OR ej -> ek
1 AND cx -> cy
hb LSHIFT 1 -> hv
1 AND bh -> bi
ih AND ij -> ik
c LSHIFT 1 -> t
ea AND eb -> ed
km OR kn -> ko
NOT bw -> bx
ci OR ct -> cu
NOT p -> q
lw OR lv -> lx
NOT lo -> lp
fp OR fv -> fw
o AND q -> r
dh AND dj -> dk
ap LSHIFT 1 -> bj
bk LSHIFT 1 -> ce
NOT ii -> ij
gh OR gi -> gj
kk RSHIFT 1 -> ld
lc LSHIFT 1 -> lw
lb OR la -> lc
1 AND am -> an
gn AND gp -> gq
lf RSHIFT 3 -> lh
e OR f -> g
lg AND lm -> lo
ci RSHIFT 1 -> db
cf LSHIFT 1 -> cz
bn RSHIFT 1 -> cg
et AND fe -> fg
is OR it -> iu
kw AND ky -> kz
ck AND cl -> cn
bj OR bi -> bk
gj RSHIFT 1 -> hc
iu AND jf -> jh
NOT bs -> bt
kk OR kv -> kw
ks AND ku -> kv
hz OR ik -> il
b RSHIFT 1 -> v
iu RSHIFT 1 -> jn
fo RSHIFT 5 -> fr
be AND bg -> bh
ga AND gc -> gd
hf OR hl -> hm
ld OR le -> lf
as RSHIFT 5 -> av
fm OR fn -> fo
hm AND ho -> hp
lg OR lm -> ln
NOT kx -> ky
kk RSHIFT 3 -> km
ek AND em -> en
NOT ft -> fu
NOT jh -> ji
jn OR jo -> jp
gj AND gu -> gw
d AND j -> l
et RSHIFT 1 -> fm
jq OR jw -> jx
ep OR eo -> eq
lv LSHIFT 15 -> lz
NOT ey -> ez
jp RSHIFT 2 -> jq
eg AND ei -> ej
NOT dm -> dn
jp AND ka -> kc
as AND bd -> bf
fk OR fj -> fl
dw OR dx -> dy
lj AND ll -> lm
ec AND ee -> ef
fq AND fr -> ft
NOT kp -> kq
ki OR kj -> kk
cz OR cy -> da
as RSHIFT 3 -> au
an LSHIFT 15 -> ar
fj LSHIFT 15 -> fn
1 AND fi -> fj
he RSHIFT 1 -> hx
lf RSHIFT 2 -> lg
kf LSHIFT 15 -> kj
dz AND ef -> eh
ib OR ic -> id
lf RSHIFT 5 -> li
bp OR bq -> br
NOT gs -> gt
fo RSHIFT 1 -> gh
bz AND cb -> cc
ea OR eb -> ec
lf AND lq -> ls
NOT l -> m
hz RSHIFT 3 -> ib
NOT di -> dj
NOT lk -> ll
jp RSHIFT 3 -> jr
jp RSHIFT 5 -> js
NOT bf -> bg
s LSHIFT 15 -> w
eq LSHIFT 1 -> fk
jl OR jk -> jm
hz AND ik -> im
dz OR ef -> eg
1 AND gy -> gz
la LSHIFT 15 -> le
br AND bt -> bu
NOT cn -> co
v OR w -> x
d OR j -> k
1 AND gd -> ge
ia OR ig -> ih
NOT go -> gp
NOT ed -> ee
jq AND jw -> jy
et OR fe -> ff
aw AND ay -> az
ff AND fh -> fi
ir LSHIFT 1 -> jl
gg LSHIFT 1 -> ha
x RSHIFT 2 -> y
db OR dc -> dd
bl OR bm -> bn
ib AND ic -> ie
x RSHIFT 3 -> z
lh AND li -> lk
ce OR cd -> cf
NOT bb -> bc
hi AND hk -> hl
NOT gb -> gc
1 AND r -> s
fw AND fy -> fz
fb AND fd -> fe
1 AND en -> eo
z OR aa -> ab
bi LSHIFT 15 -> bm
hg OR hh -> hi
kh LSHIFT 1 -> lb
cg OR ch -> ci
1 AND kz -> la
gf OR ge -> gg
gj RSHIFT 2 -> gk
dd RSHIFT 2 -> de
NOT ls -> lt
lh OR li -> lj
jr OR js -> jt
au AND av -> ax
0 -> c
he AND hp -> hr
id AND if -> ig
et RSHIFT 5 -> ew
bp AND bq -> bs
e AND f -> h
ly OR lz -> ma
1 AND lu -> lv
NOT jd -> je
ha OR gz -> hb
dy RSHIFT 1 -> er
iu RSHIFT 2 -> iv
NOT hr -> hs
as RSHIFT 1 -> bl
kk RSHIFT 2 -> kl
b AND n -> p
ln AND lp -> lq
cj AND cp -> cr
dl AND dn -> do
ci RSHIFT 2 -> cj
as OR bd -> be
ge LSHIFT 15 -> gi
hz RSHIFT 5 -> ic
dv LSHIFT 1 -> ep
kl OR kr -> ks
gj OR gu -> gv
he RSHIFT 5 -> hh
NOT fg -> fh
hg AND hh -> hj
b OR n -> o
jk LSHIFT 15 -> jo
gz LSHIFT 15 -> hd
cy LSHIFT 15 -> dc
kk RSHIFT 5 -> kn
ci RSHIFT 3 -> ck
at OR az -> ba
iu RSHIFT 3 -> iw
ko AND kq -> kr
NOT eh -> ei
aq OR ar -> as
iy AND ja -> jb
dd RSHIFT 3 -> df
bn RSHIFT 3 -> bp
1 AND cc -> cd
at AND az -> bb
x OR ai -> aj
kk AND kv -> kx
ao OR an -> ap
dy RSHIFT 3 -> ea
x RSHIFT 1 -> aq
eu AND fa -> fc
kl AND kr -> kt
ia AND ig -> ii
df AND dg -> di
NOT fx -> fy
k AND m -> n
bn RSHIFT 5 -> bq
km AND kn -> kp
dt LSHIFT 15 -> dx
hz RSHIFT 2 -> ia
aj AND al -> am
cd LSHIFT 15 -> ch
hc OR hd -> he
he RSHIFT 3 -> hg
bn OR by -> bz
NOT kt -> ku
z AND aa -> ac
NOT ak -> al
cu AND cw -> cx
NOT ie -> if
dy RSHIFT 2 -> dz
ip LSHIFT 15 -> it
de OR dk -> dl
au OR av -> aw
jg AND ji -> jj
ci AND ct -> cv
dy RSHIFT 5 -> eb
hx OR hy -> hz
eu OR fa -> fb
gj RSHIFT 3 -> gl
fo AND fz -> gb
1 AND jj -> jk
jp OR ka -> kb
de AND dk -> dm
ex AND ez -> fa
df OR dg -> dh
iv OR jb -> jc
x RSHIFT 5 -> aa
NOT hj -> hk
NOT im -> in
fl LSHIFT 1 -> gf
hu LSHIFT 15 -> hy
iq OR ip -> ir
iu RSHIFT 5 -> ix
NOT fc -> fd
NOT el -> em
ck OR cl -> cm
et RSHIFT 3 -> ev
hw LSHIFT 1 -> iq
ci RSHIFT 5 -> cl
iv AND jb -> jd
dd RSHIFT 5 -> dg
as RSHIFT 2 -> at
NOT jy -> jz
af AND ah -> ai
1 AND ds -> dt
jx AND jz -> ka
da LSHIFT 1 -> du
fs AND fu -> fv
jp RSHIFT 1 -> ki
iw AND ix -> iz
iw OR ix -> iy
eo LSHIFT 15 -> es
ev AND ew -> ey
ba AND bc -> bd
fp AND fv -> fx
jc AND je -> jf
et RSHIFT 2 -> eu
kg OR kf -> kh
iu OR jf -> jg
er OR es -> et
fo RSHIFT 2 -> fp
NOT ca -> cb
bv AND bx -> by
u LSHIFT 1 -> ao
cm AND co -> cp
y OR ae -> af
bn AND by -> ca
1 AND ke -> kf
jt AND jv -> jw
fq OR fr -> fs
dy AND ej -> el
NOT kc -> kd
ev OR ew -> ex
dd OR do -> dp
NOT cv -> cw
gr AND gt -> gu
dd RSHIFT 1 -> dw
NOT gw -> gx
NOT iz -> ja
1 AND io -> ip
NOT ag -> ah
b RSHIFT 5 -> f
NOT cr -> cs
kb AND kd -> ke
jr AND js -> ju
cq AND cs -> ct
il AND in -> io
NOT ju -> jv
du OR dt -> dv
dd AND do -> dq
b RSHIFT 2 -> d
jm LSHIFT 1 -> kg
NOT dq -> dr
bo OR bu -> bv
gk OR gq -> gr
he OR hp -> hq
NOT h -> i
hf AND hl -> hn
gv AND gx -> gy
x AND ai -> ak
bo AND bu -> bw
hq AND hs -> ht
hz RSHIFT 1 -> is
gj RSHIFT 5 -> gm
g AND i -> j
gk AND gq -> gs
dp AND dr -> ds
b RSHIFT 3 -> e
gl AND gm -> go
gl OR gm -> gn
y AND ae -> ag
hv OR hu -> hw
1674 -> b
ab AND ad -> ae
NOT ac -> ad
1 AND ht -> hu
NOT hn -> ho

66
2015/day07/part1.py

@ -0,0 +1,66 @@
import re
import sys
assignment = '->'
constant = re.compile('[0-9]+')
reference = re.compile('[a-z]+')
operator = re.compile('[A-Z]+')
binary_operations = {
'AND': lambda op1, op2: op1 & op2,
'OR': lambda op1, op2: op1 | op2,
'LSHIFT': lambda op1, op2: (op1 << op2) & 0xffff,
'RSHIFT': lambda op1, op2: op1 >> op2
}
unary_operations = {
'NOT': lambda op1: ~op1 & 0xffff
}
cache = {}
wires = {}
def dereference(ref):
global wires
if constant.match(ref):
return int(ref)
elif reference.match(ref):
if ref not in cache:
cache[ref] = wires[ref]()
return cache[ref]
else:
raise Exception(f'Bad reference {ref}')
i = 0
def line_number():
global i
i = i + 1
if i > 10000: sys.exit()
return i - 1
def execute(instruction):
global operations, wires
tokens = instruction.split(' ')
if tokens[-2] != assignment:
raise Exception('No assignment -> in penultimate position\n ' + instruction)
if not reference.match(tokens[-1]):
raise Exception('No wire reference in final position\n ' + instruction)
output = tokens[-1]
operation = tokens[:-2]
if len(operation) == 1:
# Assignment
wires[output] = lambda: dereference(operation[0])
elif len(operation) == 2:
# Unary
wires[output] = lambda: unary_operations[operation[0]](dereference(operation[1]))
elif len(operation) == 3:
# Binary
wires[output] = lambda: binary_operations[operation[1]](dereference(operation[0]), dereference(operation[2]))
else:
raise Exception('Left side of statement too long\n ' + instruction)
with open('data.txt') as data:
for line in data:
execute(line.strip())
print(wires['a']())

68
2015/day07/part2.py

@ -0,0 +1,68 @@
import re
import sys
assignment = '->'
constant = re.compile('[0-9]+')
reference = re.compile('[a-z]+')
operator = re.compile('[A-Z]+')
binary_operations = {
'AND': lambda op1, op2: op1 & op2,
'OR': lambda op1, op2: op1 | op2,
'LSHIFT': lambda op1, op2: (op1 << op2) & 0xffff,
'RSHIFT': lambda op1, op2: op1 >> op2
}
unary_operations = {
'NOT': lambda op1: ~op1 & 0xffff
}
cache = {}
wires = {}
def dereference(ref):
global wires
if constant.match(ref):
return int(ref)
elif reference.match(ref):
if ref not in cache:
cache[ref] = wires[ref]()
return cache[ref]
else:
raise Exception(f'Bad reference {ref}')
i = 0
def line_number():
global i
i = i + 1
if i > 10000: sys.exit()
return i - 1
def execute(instruction):
global operations, wires
tokens = instruction.split(' ')
if tokens[-2] != assignment:
raise Exception('No assignment -> in penultimate position\n ' + instruction)
if not reference.match(tokens[-1]):
raise Exception('No wire reference in final position\n ' + instruction)
output = tokens[-1]
operation = tokens[:-2]
if len(operation) == 1:
# Assignment
wires[output] = lambda: dereference(operation[0])
elif len(operation) == 2:
# Unary
wires[output] = lambda: unary_operations[operation[0]](dereference(operation[1]))
elif len(operation) == 3:
# Binary
wires[output] = lambda: binary_operations[operation[1]](dereference(operation[0]), dereference(operation[2]))
else:
raise Exception('Left side of statement too long\n ' + instruction)
with open('data.txt') as data:
for line in data:
execute(line.strip())
execute(str(wires['a']()) + ' -> b')
cache.clear()
print(wires['a']())

300
2015/day08/data.txt

@ -0,0 +1,300 @@
"\xa8br\x8bjr\""
"nq"
"zjrfcpbktjmrzgsz\xcaqsc\x03n\"huqab"
"daz\\zyyxddpwk"
"draes\xa2n\\g\x27ek\"lj\"\\viqych"
"nnx\\krnrfomdnt\x2flbl\xd2xpo\"cp\"k"
"kwdaapalq"
"u\"ptk"
"ckhorczuiudfjmmcc\\u\"wozqxibsfjma"
"ydctdrxat\"pd\"lwi\"bjesevfw\xe8"
"v\"\xa8rrzep\"\"r"
"nbydghkfvmq\\\xe0\"lfsrsvlsj\"i\x61liif"
"jsas\"u"
"odipikxlo"
"\"rnubsgwltqkbsu\"pcpcs"
"eitk\\f\\mhcqqoym\\ji"
"vnedc"
"\"lhcaurdqzyjyu"
"haxzsa\"zcn\"y\"foclgtjfcnv\"m\x68krc"
"\"eoeggg\"tmiydvcay\"vfavc"
"snqvyqoncwxcvwbdktoywch"
"rnfgjsyr\xd5wacy"
"ik\"hebrpvsts"
"txw"
"\x15pxtdkogd\"urbm\"gevhh\"nxr\x3erxtk"
"cetqtcy"
"inleep\\mgl"
"uflwbxvww\x2cxzezqnaply\"yh\"qlllzk"
"eepak\"xqtedzt"
"na\x61qzfieafvyrsnwkssznohjmc"
"yceaonylz\xc1\\jrlbbkzwsidfi"
"ybqafngkcqpbp"
"\xaft"
"yidjpaobqydso"
"ju\\ldxig\\lrdrhjcmm\x77rc"
"tylacqeslnwj\x48ds\"tjxa"
"efbfm"
"\\fxkgoprgdcjgyajykg\\dtbrz"
"eujvva"
"h\x7acwfpikme\\vwthyvrqdnx\""
"rbpbrxm\\\"\"\"voxx"
"ykiw\"tkb\\lforu\"rsf\\tf\"x\"rqti"
"e\\wh\x77aqeugiq\\ihhfqfuaij"
"g\"t\\o"
"nxzo\"hf\\xp"
"dxiaqfo\xea"
"kali\\zczhiqkqzybjj\"fgdjnik"
"zdkgrqmdv"
"bimxim\xb6lrwsaj\"ui\"a"
"\"rrznitibgx\\olpsjmjqzctxaubdifsq"
"zb\"khzixaacmhuzmlymoformipdzml"
"qfwi"
"hjwsxfpphttjy\"\"zixais\xbblgnqfto"
"puj\\qmyu\"nqgaqfthbwjokbmrpbhpi"
"cyxdpkh\\\""
"q"
"m"
"tbxdzzllarlo"
"gbtys"
"gytilk\\vlqxvcuutjunrqc"
"uugkvcuzan\\eyhb"
"yaxr\"genlbgw\"\\uc"
"nrgecjeip\\sjdvgqaqxwsqactopu"
"pu\"r\"txpyrkfny\\zmwfneyvwmnkkdipv"
"jm\xa3bhwvq"
"qxojmnml\"w\x9airr"
"xbzsuihs\x4dcedy\xaclrhgii\\\""
"drgjirusrekrwmvxllwdm"
"\x28hfxnfpycmpnkku\"csuf\xaarxlqyg\"x"
"\"zvz\\rmg\"\\sxxoifffyqfyn\"iq\"ps"
"\"z"
"zbwkmk\"sgzos\x93gtc\""
"bvm\x28aa\\\\\"pywuhaniox\\z\\hbp\xd7mold"
"aszgvsyna"
"qf\"vdwuss"
"lnohni\"qwiacjsjegstlbfq\\kyjhyd"
"c\\naawulxlqplnacvytspry\xf5ytxxqq"
"razwqmsqgbaaxcd\\f"
"radggyrjrg\"zx"
"\"pu\x11t\\ajcjuieinlkvya"
"veggiskh"
"eglfhjxiet\"kouqfskwsy\"hpthsldel"
"mv\xc1b\"f\\shrssnjwcpmurepdxdlcj"
"dlayjd\"suvzotgdtc"
"\xa9pvxeopn"
"lpplsaxy\"oiwaq"
"hqwh\\lusv"
"hykykwlx\"\xa5atkgh\\d\x63dff"
"vfktanpjy\"xxetc"
"dnhwkgjnsmsswfuelvihvjl\"jtf"
"x\"dwvzra\"nbbsewftehczgbvfzd\"rau"
"csfi\"mzejnjqkqupwadrgti\"von"
"xckf\xf7xsm\\pgvlpetjndpyblais\\z"
"yecy\x6fuj\x58bwpgeuiw\"mdu"
"fgb"
"c\\lx\x3efthet\xfdelgvwvpem"
"kgyrmarvfwjinlowt"
"yzte"
"vc\"z"
"sxevqfzmmdwsuu\""
"fxbaercmcy\xb6md"
"f"
"m\x44gqbcppho\\b"
"gtafr\x57m\x11jy\"\"erwmmpiwjkbckuw"
"ufdjt\"kssprzxqixzxmq\x58q"
"yzbyo\"lfdbyaxexyfbnyv\\\xe8xmre"
"u\x43ntr\\\\byyfjr\"iveujvnwsqbnpuvrta"
"us\xf6bai"
"c\\edh"
"tzckolphexfq\\\x23\xfbdqv\\\"m"
"yjafhbvhhj\x1b\"bplb"
"\"o"
"rubahvmp\""
"qmkukrnrmqumh"
"wdpxyvyidhwjf\\nabbijwhr\xc5bksvy\"p"
"u\"prlpg\""
"nsvcquyxbwilsxxemf\xd9leq"
"y\xcetxuafl"
"it"
"kwdlysf\\xjpelae"
"viwh\x58wpjjlnvryuti\x2chngrx\\nhtkui"
"vhn\x9ehre\xc3ncsqbozms\"nl"
"ytc\xa3mgeeogjcqavmmmd"
"xzlexlitseozoxtpzzutfq"
"cish\x07lmovj"
"ekbflwqzaiivdr\"pq\\azrfbntqwkn"
"uc\"xdbegmlmhksofzohavtrnxf"
"xfdnrdqdrcjzbe"
"ndg\"ckgrpisib\"rg\"p\\lmpfzlssnvk"
"witfjwpbyyzlop"
"zonlww\"emrbcsgdtrg\"rjzy\x64zqntlw"
"dvgb\"zn\\vrbzema\"ckmd"
"\\vdlmxhlvldk\"pmzazeip"
"\"\"r"
"rsntinv"
"iy"
"lr\x20efh"
"csgexlb\"zqdavlxxhtdbh\"\"\x0fkpvhiphm"
"ouwhp\"ogbft"
"cm\\ckltng\"dw\x8brf\xf0eppgckd"
"zmnlsgalhpkejsizfsbtnfliu\"nhc"
"pnrkaayqvwpdjbhcrbb\"yfeq\"aq"
"ozh\\hoxow\x2csrtr\\r\""
"bqxabj\"u\\s"
"cpsjti\"gy"
"aa\"p\\nki\\ajijkqev"
"q\"\"lfdentjgd\\"
"bmokvpoebutfki"
"pielvcbne\xf6efvzxn"
"kx"
"zlgmqagcrbhrwtwtmmg"
"aiyhmntcqjbpv\xb5hhswxbryoedvos"
"tdpaxrb"
"fu\"\x7dttkyvhrlwko"
"oirc\\\"cqlnqffjqt\\k"
"edxlia\\tcyby"
"jpeybgwfayerfrfbvfog\"ol"
"ysr"
"bzwzilgwfugjk"
"tlcc\x75nukvwjgftetjcs\xaecwc"
"dsqssa\"vzrf\"sewbp\\ahhlmhbeihlh"
"qtgmjck\"n\"guki\"gmdivwqxismqj"
"\"f"
"wuorvlovucngbzdszqpikyk"
"dfrdsacoukmgvhbq\"\"iwto"
"\"ey\"ch\\wcgioe\\\"ouvligmsw"
"ciqlszzgs"
"\\tzyrkaoi\"sopjaq"
"lmtnv"
"ar\"fqoroigiertjjlm\"ymgi\\kkjewsxd"
"wehcimlvudpxtamdn\"rwy"
"hr\"zvrwthr\"vruzqfrldn\"b"
"sggekodkiwvym\"mhsco"
"ltlkfbrrdvk\\"
"uut\"sfjnz\"\\ef"
"hxilg\\"
"zsredsiwlzrpedibn"
"vtfi"
"\\h"
"qekfrc\xf6wduodbwrguqcng\\n"
"\"lljlfdrxftwidn\\pkv\xd9ij"
"mrvgqynpehkliuijlpp"
"gikjph"
"yoxcdrdt\"wbaurnyhoyxoihu"
"onmomwuxuammbzxe"
"rnrr\\twviz\x61gqaljr\x0dmtw"
"r\"vupaoi"
"l"
"sei"
"jwxtdtbkd\\kxd"
"\x22v\\"
"ahd"
"j\"bjqxs"
"\\i\x24gglxub\\nzsajokt"
"lviwpu\"uxdlh\\zuy\"xqy\"ytdzlx\"r"
"kptfmys"
"fwxzikfhczkjwyjszqdbkepaeellc"
"nlqpsvbrbd\\ns"
"qryuwkjiodw\"\"vaqyq\"dmyifm"
"tw\x15kdmaudjl\\zorhp\"alwh"
"aatrvczesykekkjfyb\"kb"
"usqcutbqbxxhucwxo\xc1ltb\"j\"bghjcvws"
"ilhsrnzxkz"
"bianqfdfdhvw"
"hqibqs\x7ax\"qoxqoaqtcsz"
"htxtoojbbauztwxuiq\\ngyfy\\obzc"
"rxn\\moxlj"
"mtus\x84erh\"dbe"
"asx\x50huvsitcxadt"
"\"bugggtnrc\"\"kl\"hmpu\x83hqrvhpo"
"ewisbp\"\"vuzf\\w\x5fvalszdhl"
"scusplpwxfnxu\x57\"zynpn\x99xerc\\ri"
"m\\kinmkke\x0cl"
"xhuzit\x7fd"
"kfbo\x04\x50ruqirn"
"t\"\"xpbdscmdoug"
"punvpsgnbgyxe\"sptmpz"
"bxukkazijr"
"nxyrcdaoo\"rjkk\"wntehcvcip\"vrd"
"rdpvqskmihqaw"
"p\\gwdhtqnpgthod"
"nwnuf\"\"yebycearom\"nqym\"\xd4sii\xccle"
"alda\"ptspo\"wkkv\"zoi\"hkb\"vnntyd"
"ixpgpfzbqv"
"znui\"\\fzn\x03qozabh\"rva\"pv\x67"
"e\"zswmwuk"
"hcccygwfa"
"ngmace\\rtyllolr\"\x68bw"
"\\c\"jyufbry\"ryo\"xpo\x26ecninfeckh\\s"
"hdnpngtuc\"dzbvvosn\x31fwtpzbrt"
"hesbpd\xd4"
"dsdbstuzrdfmrnyntufs\"dmv"
"d\xeeibcwhcvkt"
"fvzwrsfjdqdmy\"\"v"
"ns\"dqafz\\lkyoflnazv\"mn\x37\"o\"yj\"e"
"dypilgbwzccayxa\"bnmuernx"
"q\xa9ztqrhreb\"\"kxfeyodqb"
"iz\xa5qjxqulaawuwz\"rqmpcj\\yel"
"z\"\\pq\"\"y\x67zpjtn"
"ifxqvivp\"kiiftdoe"
"jxzebj\"\x35\"qr\"ecglcutuoyywqumcs\"kk"
"q"
"yob\x85qmpuwexptczbkrl"
"cjiavv\"uudpozvibyycnmxhxpxmpjoz"
"xro\\uiqyrcid"
"nod\\k"
"d\"neiec"
"tqyrqvwyvmz\\pzgzzcqsqsrgbqbtapoz"
"r\"xvocpeuxfxslgueb\x05kzyyie\"aoec"
"\"du\\uirlhcbgv\\cjqhfreqnvn"
"zp\x04\x15\"pbjwhrjtmiba"
"\\cv\""
"k\"rwnb\\hiu\"rqd\"rc\\nyakrhly"
"klrmafjzandiddodgz"
"xipzhqzhvlpykzcuppx"
"zdvrvn\xd0mtfvpylbn\\\\sxcznrzugwznl"
"ody\\pvm\"kpjiudzhxazirgxzvumeat\"o"
"kllvhdp\"prjikzrrc\"adgpegc\\\"gk"
"sqtpug\xbcaauxaamw"
"wegxxrrxdvpivrqievfeokmnojsk"
"\\bo"
"gijhz"
"ylowluvabwrigssdgtxdwsiorxev\xdd"
"\""
"ghnsrnsqtxpygikahkrl"
"\"rcfqkbjf\"sgxg\"vnd\\rotn"
"ap\"smgsuexjrbuqs\"mpbstogj\"x"
"koaunz\\sgt\"opv"
"yialiuzwix"
"yp\"ndxgwzml\"bt"
"lpcjxmggfsy\\szbxccarjkqzasqkb\xcfd\x0c"
"x"
"mgakc"
"vjieunoh\x73fjwx"
"erbvv\"qulsd"
"mimycrbfhqkarmz"
"tihfbgcszuej\"c\xfbvoqskkhbgpaddioo"
"mziavkwrmekriqghw"
"izk\\tnjd\\ed\\emokvjoc"
"c\"nhbqzndro\\g"
"usfngdo"
"aypljdftvptt"
"ym\"afvq\xbcc"
"zabi\"wjpvugwhl"
"ebvptcjqjhc\"n\"p\"dxrphegr\\"
"mzlqqxokhye\xd9\\rffhnzs"
"hnipqknwpsjakanuewe"
"rqgbfcjdrmiz\"h"
"kzzp\\z\\txmkwaouxictybwx"
"yzmspjkqrteiydswlvb"
"gjpxklgpzv\"txri\\hotpuiukzzzd"
"p\"rxergtbsxmjmkeeqwvoagnki\""
"santipvuiq"
"\"ihjqlhtwbuy\"hdkiv\"mtiqacnf\\"
"oliaggtqyyx"
"fwwnpmbb"
"yrtdrieazfxyyneo"
"nywbv\\"
"twc\\ehfqxhgomgrgwpxyzmnkioj"
"qludrkkvljljd\\xvdeum\x4e"

13
2015/day08/part1.py

@ -0,0 +1,13 @@
import re
with open('data.txt') as data_in:
strings = [line.strip()[1:-1] for line in data_in]
code_length = sum(len(s) for s in strings) + 2 * len(strings)
string_length = 0
for s in strings:
decoded = re.sub('\\\\(x(..)|[\\\\"])', '.', s)
string_length += len(decoded)
print(code_length, string_length)
print(code_length - string_length)

18
2015/day08/part2.py

@ -0,0 +1,18 @@
import re
with open('data.txt') as data_in:
strings = [line.strip()[1:-1] for line in data_in]
# strings = ['', 'abc', 'aaa\\"aaa', '\\x27']
code_length = sum(len(s) for s in strings) + 2 * len(strings)
string_length = 0
encoded_length = 0
for s in strings:
decoded = re.sub('\\\\(x(..)|[\\\\"])', '.', s)
string_length += len(decoded)
encoded = '"\\"' + re.sub('[\\\\"]', '\\\\\\g<0>', s) + '\\""'
encoded_length += len(encoded)
print(encoded_length, code_length, string_length)
print(encoded_length - code_length)

28
2015/day09/data.txt

@ -0,0 +1,28 @@
AlphaCentauri to Snowdin = 66
AlphaCentauri to Tambi = 28
AlphaCentauri to Faerun = 60
AlphaCentauri to Norrath = 34
AlphaCentauri to Straylight = 34
AlphaCentauri to Tristram = 3
AlphaCentauri to Arbre = 108
Snowdin to Tambi = 22
Snowdin to Faerun = 12
Snowdin to Norrath = 91
Snowdin to Straylight = 121
Snowdin to Tristram = 111
Snowdin to Arbre = 71
Tambi to Faerun = 39
Tambi to Norrath = 113
Tambi to Straylight = 130
Tambi to Tristram = 35
Tambi to Arbre = 40
Faerun to Norrath = 63
Faerun to Straylight = 21
Faerun to Tristram = 57
Faerun to Arbre = 83
Norrath to Straylight = 9
Norrath to Tristram = 50
Norrath to Arbre = 60
Straylight to Tristram = 27
Straylight to Arbre = 81
Tristram to Arbre = 90

36
2015/day09/part1.py

@ -0,0 +1,36 @@
import re
graph = {}
with open('data.txt') as data_in:
for line in data_in:
node1, node2, length = re.search('(.*) to (.*) = ([0-9]+)', line).groups()
if node1 not in graph: graph[node1] = {}
graph[node1][node2] = int(length)
if node2 not in graph: graph[node2] = {}
graph[node2][node1] = int(length)
# Oh yeah, brute forcing the Travelling Salesman Problem!
nodes = graph.keys()
def calculate_path_length(path):
global graph
length = 0
for i in range(len(path) - 1):
length += graph[path[i]][path[i+1]]
return length
def try_paths(path=[], best=(None, float('inf'))):
global nodes
for k in nodes:
if k not in path:
newpath = path + [k]
if len(newpath) < len(nodes):
result = try_paths(newpath)
else:
result = (newpath, calculate_path_length(newpath))
if result[1] < best[1]:
best = result
return best
print(try_paths())

35
2015/day09/part2.py

@ -0,0 +1,35 @@
import re
graph = {}
with open('data.txt') as data_in:
for line in data_in:
node1, node2, length = re.search('(.*) to (.*) = ([0-9]+)', line).groups()
if node1 not in graph: graph[node1] = {}
graph[node1][node2] = int(length)
if node2 not in graph: graph[node2] = {}
graph[node2][node1] = int(length)
nodes = graph.keys()
def calculate_path_length(path):
global graph
length = 0
for i in range(len(path) - 1):
length += graph[path[i]][path[i+1]]
return length
def try_paths(path=[], best=(None, float('-inf'))):
global nodes
for k in nodes:
if k not in path:
newpath = path + [k]
if len(newpath) < len(nodes):
result = try_paths(newpath)
else:
result = (newpath, calculate_path_length(newpath))
if result[1] > best[1]:
best = result
return best
print(try_paths())

28
2015/day10/bothparts.py

@ -0,0 +1,28 @@
data = '1113222113'
#data = '1'
# Run length encoding
def rle(data):
current = data[0]
count = 1
result = ''
for c in data[1:]:
if c == current:
count += 1
else:
result += str(count) + str(current)
current = c
count = 1
result += str(count) + str(current)
return result
for i in range(40):
data = rle(data)
print(len(data))
for i in range(10):
data = rle(data)
print(len(data))

41
2015/day11/bothparts.py

@ -0,0 +1,41 @@
def increment(data):
codes = [ord(c) for c in data]
codes[-1] += 1
while chr(codes[-1]) in 'ilo':
codes[-1] += 1
pos = len(codes) - 1
while True:
if codes[pos] > ord('z'):
codes[pos] -= 26
if pos > 0:
codes[pos-1] += 1
while chr(codes[pos-1]) in 'ilo':
codes[pos-1] += 1
pos -= 1
else:
break
else:
break
return ''.join(chr(c) for c in codes)
def is_valid(password):
# Reject forbidden characters
if any(f in password for f in 'ilo'): return False
# Find pairs and runs
pairs = set()
codes = [ord(c) for c in password]
run = False
for i in range(len(password) - 1):
if i + 2 < len(password) and codes[i] + 1 == codes[i+1] and codes[i] + 2 == codes[i+2]:
run = True
if password[i] == password[i+1]: pairs.add(password[i])
return run and len(pairs) >= 2
password = increment('cqjxjnds')
while not is_valid(password):
password = increment(password)
print(password)
password = increment(password)
while not is_valid(password):
password = increment(password)
print(password)

1
2015/day12/data.json

File diff suppressed because one or more lines are too long

6
2015/day12/part1.py

@ -0,0 +1,6 @@
import re
with open('data.json') as data_in:
data = data_in.read()
print(sum(int(i) for i in re.findall('-?[0-9]+', data)))

24
2015/day12/part2.py

@ -0,0 +1,24 @@
import json
def json_sum(data):
values = [data]
while True:
newvalues = []
for v in values:
if type(v) is int:
newvalues.append(v)
elif type(v) is dict and 'red' not in v.values():
for v2 in v.values():
newvalues.append(v2)
elif type(v) is list:
for v2 in v:
newvalues.append(v2)
if values == newvalues: break
values = newvalues
return sum(values)
with open('data.json') as data_in:
data = json.load(data_in)
print(json_sum(data))

47
2015/day13/bothparts.py

@ -0,0 +1,47 @@
import re
pattern = re.compile('(\\w+) would (gain|lose) (\\d+) happiness units by sitting next to (\\w+)\\.')
graph = {}
with open('data.txt') as data:
for line in data:
subject, gainlose, amount, neighbour = pattern.search(line).groups()
amount = int(amount)
if gainlose == 'lose': amount = -amount
graph.setdefault(subject, {})[neighbour] = amount
# Wait, this is just Travelling Salesman again. To the Day 9 Part 2 solution!
def calculate_path_length(graph, path):
# Add the edges between the first and last nodes
length = graph[path[0]][path[-1]] + graph[path[-1]][path[0]]
for i in range(len(path) - 1):
# And account for the edge weight in both directions
length += graph[path[i]][path[i+1]] + graph[path[i+1]][path[i]]
return length
def try_paths(graph, path=[], best=(None, float('-inf'))):
nodes = graph.keys()
for k in nodes:
if k not in path:
newpath = path + [k]
if len(newpath) < len(nodes):
result = try_paths(graph, newpath)
else:
result = (newpath, calculate_path_length(graph, newpath))
if result[1] > best[1]:
best = result
return best
print(try_paths(graph))
# For part to, add "Yourself" who doesn't really care
graph['Yourself'] = {}
for k in graph.keys():
graph['Yourself'][k] = 0
graph[k]['Yourself'] = 0
print(try_paths(graph))

56
2015/day13/data.txt

@ -0,0 +1,56 @@
Alice would lose 2 happiness units by sitting next to Bob.
Alice would lose 62 happiness units by sitting next to Carol.
Alice would gain 65 happiness units by sitting next to David.
Alice would gain 21 happiness units by sitting next to Eric.
Alice would lose 81 happiness units by sitting next to Frank.
Alice would lose 4 happiness units by sitting next to George.
Alice would lose 80 happiness units by sitting next to Mallory.
Bob would gain 93 happiness units by sitting next to Alice.
Bob would gain 19 happiness units by sitting next to Carol.
Bob would gain 5 happiness units by sitting next to David.
Bob would gain 49 happiness units by sitting next to Eric.
Bob would gain 68 happiness units by sitting next to Frank.
Bob would gain 23 happiness units by sitting next to George.
Bob would gain 29 happiness units by sitting next to Mallory.
Carol would lose 54 happiness units by sitting next to Alice.
Carol would lose 70 happiness units by sitting next to Bob.
Carol would lose 37 happiness units by sitting next to David.
Carol would lose 46 happiness units by sitting next to Eric.
Carol would gain 33 happiness units by sitting next to Frank.
Carol would lose 35 happiness units by sitting next to George.
Carol would gain 10 happiness units by sitting next to Mallory.
David would gain 43 happiness units by sitting next to Alice.
David would lose 96 happiness units by sitting next to Bob.
David would lose 53 happiness units by sitting next to Carol.
David would lose 30 happiness units by sitting next to Eric.
David would lose 12 happiness units by sitting next to Frank.
David would gain 75 happiness units by sitting next to George.
David would lose 20 happiness units by sitting next to Mallory.
Eric would gain 8 happiness units by sitting next to Alice.
Eric would lose 89 happiness units by sitting next to Bob.
Eric would lose 69 happiness units by sitting next to Carol.
Eric would lose 34 happiness units by sitting next to David.
Eric would gain 95 happiness units by sitting next to Frank.
Eric would gain 34 happiness units by sitting next to George.
Eric would lose 99 happiness units by sitting next to Mallory.
Frank would lose 97 happiness units by sitting next to Alice.
Frank would gain 6 happiness units by sitting next to Bob.
Frank would lose 9 happiness units by sitting next to Carol.
Frank would gain 56 happiness units by sitting next to David.
Frank would lose 17 happiness units by sitting next to Eric.
Frank would gain 18 happiness units by sitting next to George.
Frank would lose 56 happiness units by sitting next to Mallory.
George would gain 45 happiness units by sitting next to Alice.
George would gain 76 happiness units by sitting next to Bob.
George would gain 63 happiness units by sitting next to Carol.
George would gain 54 happiness units by sitting next to David.
George would gain 54 happiness units by sitting next to Eric.