diff options
| author | Gustav Sörnäs <gusso230@student.liu.se> | 2019-12-08 11:06:56 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gusso230@student.liu.se> | 2019-12-08 11:06:56 +0100 |
| commit | 29e33f8f6a31565f5a2671b1c459ff1b829630f7 (patch) | |
| tree | c051274a9e9a895049a077a6bdce9056dfb9afb1 | |
| parent | d16fc72a33fced8c8623bea6cf9cdd9cf8999024 (diff) | |
| download | aoc-29e33f8f6a31565f5a2671b1c459ff1b829630f7.tar.gz | |
Refactor and create main.py
| -rw-r--r-- | solutions/input/01 | 100 | ||||
| -rw-r--r-- | solutions/input/02 | 1 | ||||
| -rw-r--r-- | solutions/input/03 | 2 | ||||
| -rw-r--r-- | solutions/input/04 | 0 | ||||
| -rw-r--r-- | solutions/py/01-1.py | 11 | ||||
| -rw-r--r-- | solutions/py/01-2.py | 16 | ||||
| -rw-r--r-- | solutions/py/02-1.py | 19 | ||||
| -rw-r--r-- | solutions/py/02-2.py | 28 | ||||
| -rw-r--r-- | solutions/py/03-1.py | 37 | ||||
| -rw-r--r-- | solutions/py/03-2.py | 41 | ||||
| -rw-r--r-- | solutions/py/04-1.py | 30 | ||||
| -rw-r--r-- | solutions/py/04-2.py | 31 | ||||
| -rw-r--r-- | solutions/py/05.py | 44 | ||||
| -rw-r--r-- | solutions/py/06.py | 75 | ||||
| -rw-r--r-- | solutions/py/08.py | 28 | ||||
| -rw-r--r-- | solutions/py/d01.py | 19 | ||||
| -rw-r--r-- | solutions/py/d02.py | 41 | ||||
| -rw-r--r-- | solutions/py/d03.py | 75 | ||||
| -rw-r--r-- | solutions/py/d04.py | 45 | ||||
| -rw-r--r-- | solutions/py/d05.py | 26 | ||||
| -rw-r--r-- | solutions/py/d06.py | 78 | ||||
| -rw-r--r-- | solutions/py/d07.py (renamed from solutions/py/07.py) | 47 | ||||
| -rw-r--r-- | solutions/py/d08.py | 31 | ||||
| -rw-r--r-- | solutions/py/main.py | 15 |
24 files changed, 464 insertions, 376 deletions
diff --git a/solutions/input/01 b/solutions/input/01 new file mode 100644 index 0000000..bcf9d47 --- /dev/null +++ b/solutions/input/01 @@ -0,0 +1,100 @@ +137503 +60363 +103031 +141000 +101650 +76081 +139069 +63717 +135021 +66034 +53912 +83417 +125978 +73206 +77497 +108822 +133339 +113618 +91973 +88741 +109942 +96523 +95973 +56595 +118638 +63936 +101635 +149154 +85522 +140962 +108196 +105804 +148464 +68429 +146808 +82541 +85581 +117253 +117900 +83457 +103354 +123875 +88412 +108573 +140651 +103774 +95291 +91290 +98690 +87761 +122907 +91499 +141746 +127300 +114866 +75472 +65369 +50978 +119756 +144115 +92483 +146317 +100770 +124156 +109933 +138037 +101126 +58517 +83653 +135656 +111483 +82784 +107459 +106641 +138030 +53599 +123886 +74425 +96919 +65410 +63823 +148278 +133753 +106661 +51147 +120571 +77900 +131827 +107882 +149359 +127565 +67109 +131547 +114874 +130493 +94905 +138654 +58504 +79591 +133856 diff --git a/solutions/input/02 b/solutions/input/02 new file mode 100644 index 0000000..bcebbfa --- /dev/null +++ b/solutions/input/02 @@ -0,0 +1 @@ +1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,6,1,19,2,19,9,23,1,23,5,27,2,6,27,31,1,31,5,35,1,35,5,39,2,39,6,43,2,43,10,47,1,47,6,51,1,51,6,55,2,55,6,59,1,10,59,63,1,5,63,67,2,10,67,71,1,6,71,75,1,5,75,79,1,10,79,83,2,83,10,87,1,87,9,91,1,91,10,95,2,6,95,99,1,5,99,103,1,103,13,107,1,107,10,111,2,9,111,115,1,115,6,119,2,13,119,123,1,123,6,127,1,5,127,131,2,6,131,135,2,6,135,139,1,139,5,143,1,143,10,147,1,147,2,151,1,151,13,0,99,2,0,14,0 diff --git a/solutions/input/03 b/solutions/input/03 new file mode 100644 index 0000000..f9771e4 --- /dev/null +++ b/solutions/input/03 @@ -0,0 +1,2 @@ +R990,D362,L316,U101,R352,U798,L314,D564,R961,D634,L203,U601,R973,U227,R996,D639,L868,D135,L977,D201,R911,D486,R906,U719,L546,U324,R302,D200,L879,D206,L872,U681,R628,D272,R511,D827,L929,U915,L399,U696,R412,D640,R234,U487,R789,U354,L620,D914,L7,D228,L55,D591,L250,D228,R816,U935,R553,U98,L833,D418,R582,D793,R804,U283,R859,D206,L842,U663,L935,U495,L995,D181,R75,D33,R126,U489,L894,D675,R33,U239,L623,D931,L830,U63,R77,D576,L85,D415,R443,U603,R654,U495,L273,U583,R10,D648,L840,U904,R489,D655,R997,U559,L614,U917,R809,U540,L41,U519,R256,U111,R29,D603,L931,U518,R443,D51,L788,U483,L665,U890,L392,D701,R907,D125,L438,D107,L266,U766,R743,D343,R898,U293,L318,U417,L23,U44,L668,U614,R83,U31,R452,U823,R16,D418,R68,U823,L53,D638,L394,D714,R992,U196,R913,D526,L458,U428,L412,U901,R610,U348,L904,D815,R274,U439,R207,D81,L20,D507,L179,U249,L221,U603,L897,U490,R127,U99,L709,U925,L818,D777,R292,U935,R801,U331,R412,U759,L698,D53,L969,U492,L502,D137,R513,D999,L808,D618,L240,U378,L284,D726,L609,U530,R537,D36,L504,D26,R244,D692,L186,U767,L690,U182,R559,D926,R706,D132,L325,D846,R494,U238,L519,U655,R57,U658,L471,D717,L964,D346,L448,U286,L457,D504,R614,U652,R583,D780,R882,U417,R573,D297,L144,U347,L254,D589,L387,U309,L88,D510,R435,U636,L640,U801,R774,U678,R247,D846,L775,U527,L225,U798,R577,U897,R11,U153,L297,D748,L284,U806,R512,U906,L181,U39,R264,D47,L561,D441,L181,U210,L278,U998,R256,D278,R350,U466,L335,D310,L4,U298,L531,D423,R851,U285,L235,D139,R209,U882,R801,D36,L777,D153,L63 +L995,D598,R577,U346,L797,D375,R621,D709,R781,U55,R965,U327,L479,U148,L334,U93,R644,U632,L557,D136,L690,D548,R982,D703,L971,U399,R600,D785,L504,U984,R18,U190,L755,D737,L787,D921,R303,U513,L544,U954,L814,U239,R550,D458,R518,D538,R362,D350,L103,U17,L362,D480,L80,U639,L361,D75,L356,D849,R635,U633,R934,U351,L314,U960,R657,U802,L687,U385,L558,D984,L996,U765,L147,D366,R908,U981,R44,U336,R396,U85,R819,D582,L21,D920,L627,D103,R922,U195,L412,D385,L159,U446,L152,U400,L303,U549,R734,D709,R661,U430,R177,U857,L53,U555,R35,D919,L163,D630,L162,U259,R46,D89,R965,D410,R37,U39,R621,D606,L816,D659,L668,D418,L775,D911,R296,U488,L129,U869,L455,U663,L942,U813,L274,D677,R161,D338,R455,D580,R976,D984,L336,U742,R334,U130,L210,U523,R958,U177,R126,U469,L513,D14,L772,D423,L369,D661,R167,D449,L685,U871,L930,U630,L54,D581,L921,U839,R782,D844,L581,D995,R110,U365,L594,D595,R391,D298,R297,U469,L148,D34,R5,D609,L654,U172,R940,D858,L682,D92,R395,D683,R947,U311,L850,U151,R452,U641,L599,D640,R86,U584,L518,D597,L724,D282,L691,D957,L119,U30,L8,D514,R237,U599,R775,U413,R802,D132,R925,U133,L980,D981,R272,U632,R995,U427,R770,D722,L817,D609,R590,D799,L699,U923,L881,U893,R79,U327,L405,D669,L702,D612,R895,D132,R420,U958,L955,U993,L817,D492,R453,D342,L575,D253,R97,U54,R456,U748,L912,U661,L987,D182,L816,U218,R933,D797,L207,D71,R546,U578,L894,D536,L253,D525,L164,D673,R784,U915,L774,U586,R733,D80,L510,U449,L403,D915,L612,D325,L470,U179,L460,U405,R297,D803,R637,U893,R565,U952,R550,U936,R378,D932,L669 diff --git a/solutions/input/04 b/solutions/input/04 new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/solutions/input/04 diff --git a/solutions/py/01-1.py b/solutions/py/01-1.py deleted file mode 100644 index 598caf4..0000000 --- a/solutions/py/01-1.py +++ /dev/null @@ -1,11 +0,0 @@ -import math -import sys - -s = 0 -for line in sys.stdin: - mass = int(line) - if mass == 0: - break - fuel = math.floor(mass / 3) - 2 - s += fuel -print(s) diff --git a/solutions/py/01-2.py b/solutions/py/01-2.py deleted file mode 100644 index 0017a8b..0000000 --- a/solutions/py/01-2.py +++ /dev/null @@ -1,16 +0,0 @@ -import math -import sys - -def get_fuel(mass): - fuel = math.floor(mass / 3) - 2 - if fuel <= 0: - return 0 - return fuel + get_fuel(fuel) - -fuels = [] -for line in sys.stdin: - if line.rstrip() == "": - break - fuels.append(get_fuel(int(line))) - -print(sum(fuels)) diff --git a/solutions/py/02-1.py b/solutions/py/02-1.py deleted file mode 100644 index 47ab15f..0000000 --- a/solutions/py/02-1.py +++ /dev/null @@ -1,19 +0,0 @@ -import sys - -program = [int(x) for x in input().split(",")] - -memory = program.copy() -memory[1] = 12 -memory[2] = 2 - -pointer = 0 -while True: - if memory[pointer] == 99: - break - elif memory[pointer] == 1: - memory[memory[pointer+3]] = memory[memory[pointer+1]] + memory[memory[pointer+2]] - elif memory[pointer] == 2: - memory[memory[pointer+3]] = memory[memory[pointer+1]] * memory[memory[pointer+2]] - pointer += 4 -print(memory[0]) - diff --git a/solutions/py/02-2.py b/solutions/py/02-2.py deleted file mode 100644 index cc6402e..0000000 --- a/solutions/py/02-2.py +++ /dev/null @@ -1,28 +0,0 @@ -import sys -import time - -program = [int(x) for x in input().split(" ")] - -start = time.time() - -for n in range(100): - for v in range(100): - memory = program.copy() - memory[1] = n - memory[2] = v - - pointer = 0 - while True: - if memory[pointer] == 99: - break - elif memory[pointer] == 1: - memory[memory[pointer+3]] = memory[memory[pointer+1]] + memory[memory[pointer+2]] - elif memory[pointer] == 2: - memory[memory[pointer+3]] = memory[memory[pointer+1]] * memory[memory[pointer+2]] - pointer += 4 - # print(n, v, memory[0]) - if memory[0] == 19690720: - print(n, v) - print("took " + str(time.time() - start)) - sys.exit() - diff --git a/solutions/py/03-1.py b/solutions/py/03-1.py deleted file mode 100644 index 3f26a84..0000000 --- a/solutions/py/03-1.py +++ /dev/null @@ -1,37 +0,0 @@ -import math - -def intersection(l1, l2): - return [value for value in l2 if value in l1] - -def man_dist(point): - return abs(point[0]) + abs(point[1]) - -wire1 = [] -wire2 = [] -for wire in (wire1, wire2): - x = 0 - y = 0 - dx = 0 - dy = 0 - for move in input().split(","): - if move[0] == "D": - dx = 0 - dy = -1 - elif move[0] == "U": - dx = 0 - dy = 1 - elif move[0] == "R": - dx = 1 - dy = 0 - elif move[0] == "L": - dx = -1 - dy = 0 - for i in range(int(move[1:])): - x += dx - y += dy - wire.append((x, y)) -points = intersection(wire1, wire2) -dist = man_dist(points[0]) -for point in points[1:]: - dist = min(dist, man_dist(point)) -print(dist) diff --git a/solutions/py/03-2.py b/solutions/py/03-2.py deleted file mode 100644 index 0bb1efc..0000000 --- a/solutions/py/03-2.py +++ /dev/null @@ -1,41 +0,0 @@ -import math - -def intersection(l1, l2): - return [value for value in l2 if value in l1] - -def man_dist(point): - return abs(point[0]) + abs(point[1]) - -wire1 = {} -wire2 = {} -# wire {(x,y) : dist} -for wire in (wire1, wire2): - x = 0 - y = 0 - dx = 0 - dy = 0 - steps = 0 - for move in input().split(","): - if move[0] == "D": - dx = 0 - dy = -1 - elif move[0] == "U": - dx = 0 - dy = 1 - elif move[0] == "R": - dx = 1 - dy = 0 - elif move[0] == "L": - dx = -1 - dy = 0 - for i in range(int(move[1:])): - x += dx - y += dy - steps += 1 - wire[(x, y)] = steps - -points = intersection(list(wire1.keys()), list(wire2.keys())) -length = wire1[points[0]] + wire2[points[0]] -for point in points[1:]: - length = min(length, wire1[point] + wire2[point]) -print(length) diff --git a/solutions/py/04-1.py b/solutions/py/04-1.py deleted file mode 100644 index 23096ec..0000000 --- a/solutions/py/04-1.py +++ /dev/null @@ -1,30 +0,0 @@ -from collections import Counter - -def containsDouble(num): - s = str(num) - amounts = [] - for n in (0,1,2,3,4,5,6,7,8,9): - amounts.append(s.count(str(n))) - c = Counter(amounts) - return c[0] + c[1] < 10 - -def isIncreasing(num): - s = str(num) - n = int(s[0]) - for sp in s[1:]: - if int(sp) < n: - return False - n = int(sp) - return True - -if __name__ == "__main__": - amount = 0 - for n in range(357253, 892942 + 1): - if containsDouble(n): - if isIncreasing(n): - print(n) - amount += 1 - if n % 10000 == 0: - pass - # print(n) - print(amount) diff --git a/solutions/py/04-2.py b/solutions/py/04-2.py deleted file mode 100644 index 582df1a..0000000 --- a/solutions/py/04-2.py +++ /dev/null @@ -1,31 +0,0 @@ -from collections import Counter - -def containsDouble(num): - s = str(num) - amounts = [] - for n in (0,1,2,3,4,5,6,7,8,9): - amounts.append(s.count(str(n))) - c = Counter(amounts) - if c[0] + c[1] < 10: - return c[2] >= 1 - -def isIncreasing(num): - s = str(num) - n = int(s[0]) - for sp in s[1:]: - if int(sp) < n: - return False - n = int(sp) - return True - -if __name__ == "__main__": - amount = 0 - for n in range(357253, 892942 + 1): - if containsDouble(n): - if isIncreasing(n): - print(n) - amount += 1 - if n % 10000 == 0: - pass - # print(n) - print(amount) diff --git a/solutions/py/05.py b/solutions/py/05.py deleted file mode 100644 index e9eb6c1..0000000 --- a/solutions/py/05.py +++ /dev/null @@ -1,44 +0,0 @@ -import intcode -import itertools -import queue -import sys - -def pt1(program): - c = intcode.Computer(program) - c.input = 1 - output = [] - while c.memory[c.pointer] != 99: - c.step() - if c.output != None: - output.append(c.output) - c.output = None - return output - -def pt2(program): - c = intcode.Computer(program) - c.input = 5 - output = [] - while c.memory[c.pointer] != 99: - c.step() - if c.output != None: - output.append(c.output) - c.output = None - return output - -if __name__ == "__main__": - f = open("../input/05", "r") - program = [int(x) for x in f.readline().split(",")] - - import cProfile - import timeit - - print("PART 1") - print(timeit.timeit('pt1(program)', globals=globals(), number=1000), "ms") - cProfile.run("pt1(program)") - - print("PART 2") - print(timeit.timeit('pt2(program)', globals=globals(), number=1000), "ms") - cProfile.run("pt2(program)") - - print(1, pt1(program)) - print(2, pt2(program)) diff --git a/solutions/py/06.py b/solutions/py/06.py deleted file mode 100644 index 2ca5f26..0000000 --- a/solutions/py/06.py +++ /dev/null @@ -1,75 +0,0 @@ -from typing import Iterable -import sys - -class Tree(object): - # https://stackoverflow.com/a/28015122 - def __init__(self, name='root', children=None): - self.name = name - self.children = [] - self.parent = None - if children is not None: - for child in children: - self.add_child(child) - def add_child(self, node): - self.children.append(node) - node.parent = self - - def __eq__(self, other): - if isinstance(other, Tree): - return self.name == other.name - return False - def parents(self): - return [self.parent] + self.parent.parents() if self.parent != None\ - else [] - -lines = open("../input/06", "r").readlines() - -trees = {} -seen = set() # seen trees. contains only IDs -leafs = {} - -for orbit in lines: - orbit = orbit.rstrip().split(")") - #print(orbit) - if not orbit[0] in seen: - trees[orbit[0]] = Tree(orbit[0]) - seen.add(orbit[0]) - -# hook up each child to its parent (since every parent now exists) -for orbit in lines: - orbit = orbit.rstrip().split(")") - if orbit[1] in seen: - trees[orbit[0]].add_child(trees[orbit[1]]) - else: - leafs[orbit[1]] = Tree(orbit[1]) - trees[orbit[0]].add_child(leafs[orbit[1]]) -for k in trees: - pass - #print(trees[k], trees[k].children) - -trees.update(leafs) - -sum_depths = 0 -depth = 0 -depths = {} -def count_children(tree, depth): - global sum_depths # fusk-rekursion? ville bara att det skulle fungera - sum_depths += depth - depths[tree.name] = depth - for child in tree.children: - count_children(child, depth+1) -count_children(trees["COM"], 0) -print("1:", sum_depths) - -# find common parent and distance to it -src_parents = trees["YOU"].parent.parents() -tar_parents = trees["SAN"].parent.parents() -src_dist = 0 -for src_parent in src_parents: - src_dist += 1 - tar_dist = 0 - for tar_parent in tar_parents: - tar_dist += 1 - if src_parent == tar_parent: - print("2:", src_parent.name, src_dist, tar_dist, src_dist + tar_dist) - sys.exit() diff --git a/solutions/py/08.py b/solutions/py/08.py deleted file mode 100644 index c57cb4c..0000000 --- a/solutions/py/08.py +++ /dev/null @@ -1,28 +0,0 @@ -def count(layer, num): - return sum([row.count(num) for row in layer]) - -f = open("../input/08", "r").readline() -img = [] -least_zeroes = 150 # 25*6 -n = 0 - -for l in range(100): - #print("l", l) - layer = [[int(f[l*25*6 + y*25 + x]) for x in range(25)] for y in range(6)] - if count(layer, 0) < least_zeroes: - least_zeroes = count(layer, 0) - n = count(layer, 1) * count(layer, 2) - img.append(layer) -print(1, n) - -result = img[0] -for layer in img[1:]: - for x in range(25): - for y in range(6): - if result[y][x] == 2: - result[y][x] = layer[y][x] -print(2) -for row in result: - for c in row: - print(('\u2588' if c == 1 else ' '), end='') - print("") diff --git a/solutions/py/d01.py b/solutions/py/d01.py new file mode 100644 index 0000000..d5197ab --- /dev/null +++ b/solutions/py/d01.py @@ -0,0 +1,19 @@ +import math +import sys + +def get_fuel(mass): + fuel = math.floor(mass / 3) - 2 + return 0 if fuel <= 0 else fuel + get_fuel(fuel) + +def pt1(_in): + s = 0 + for line in _in: + mass = int(line) + if mass == 0: + break + fuel = math.floor(mass / 3) - 2 + s += fuel + return s + +def pt2(input): + return sum([get_fuel(int(line)) for line in input]) diff --git a/solutions/py/d02.py b/solutions/py/d02.py new file mode 100644 index 0000000..15f0fea --- /dev/null +++ b/solutions/py/d02.py @@ -0,0 +1,41 @@ +import sys + +def pt1(input): + program = [int(x) for x in input[0].split(",")] + + memory = program.copy() + memory[1] = 12 + memory[2] = 2 + + pointer = 0 + while True: + if memory[pointer] == 99: + break + elif memory[pointer] == 1: + memory[memory[pointer+3]] = memory[memory[pointer+1]] + memory[memory[pointer+2]] + elif memory[pointer] == 2: + memory[memory[pointer+3]] = memory[memory[pointer+1]] * memory[memory[pointer+2]] + pointer += 4 + return memory[0] + +def pt2(input): + program = [int(x) for x in input[0].split(",")] + + for n in range(100): + for v in range(100): + memory = program.copy() + memory[1] = n + memory[2] = v + + pointer = 0 + while True: + if memory[pointer] == 99: + break + elif memory[pointer] == 1: + memory[memory[pointer+3]] = memory[memory[pointer+1]] + memory[memory[pointer+2]] + elif memory[pointer] == 2: + memory[memory[pointer+3]] = memory[memory[pointer+1]] * memory[memory[pointer+2]] + pointer += 4 + if memory[0] == 19690720: + return (n, v) + diff --git a/solutions/py/d03.py b/solutions/py/d03.py new file mode 100644 index 0000000..aec6d4a --- /dev/null +++ b/solutions/py/d03.py @@ -0,0 +1,75 @@ +import math + +def intersection(l1, l2): + l1 = set(l1) + l2 = set(l2) + return [value for value in l2 if value in l1] + +def man_dist(point): + return abs(point[0]) + abs(point[1]) + +def pt1(input): + wire1 = [] + wire2 = [] + for wire, moves in zip((wire1, wire2), (input[0], input[1])): + x = 0 + y = 0 + dx = 0 + dy = 0 + for move in moves.split(","): + if move[0] == "D": + dx = 0 + dy = -1 + elif move[0] == "U": + dx = 0 + dy = 1 + elif move[0] == "R": + dx = 1 + dy = 0 + elif move[0] == "L": + dx = -1 + dy = 0 + for i in range(int(move[1:])): + x += dx + y += dy + wire.append((x, y)) + points = intersection(wire1, wire2) + dist = man_dist(points[0]) + for point in points[1:]: + dist = min(dist, man_dist(point)) + return dist + +def pt2(input): + wire1 = {} + wire2 = {} + # wire {(x,y) : dist} + for wire, moves in zip((wire1, wire2), (input[0], input[1])): + x = 0 + y = 0 + dx = 0 + dy = 0 + steps = 0 + for move in moves.split(","): + if move[0] == "D": + dx = 0 + dy = -1 + elif move[0] == "U": + dx = 0 + dy = 1 + elif move[0] == "R": + dx = 1 + dy = 0 + elif move[0] == "L": + dx = -1 + dy = 0 + for i in range(int(move[1:])): + x += dx + y += dy + steps += 1 + wire[(x, y)] = steps + + points = intersection(list(wire1.keys()), list(wire2.keys())) + length = wire1[points[0]] + wire2[points[0]] + for point in points[1:]: + length = min(length, wire1[point] + wire2[point]) + return length diff --git a/solutions/py/d04.py b/solutions/py/d04.py new file mode 100644 index 0000000..3115f2a --- /dev/null +++ b/solutions/py/d04.py @@ -0,0 +1,45 @@ +from collections import Counter + +def isIncreasing(num): + s = str(num) + n = int(s[0]) + for sp in s[1:]: + if int(sp) < n: + return False + n = int(sp) + return True + +def pt1(input): + return + def containsDouble(num): + s = str(num) + amounts = [] + for n in (0,1,2,3,4,5,6,7,8,9): + amounts.append(s.count(str(n))) + c = Counter(amounts) + return c[0] + c[1] < 10 + + amount = 0 + for n in range(357253, 892942 + 1): + if containsDouble(n): + if isIncreasing(n): + amount += 1 + return amount + +def pt2(input): + return + def containsDouble(num): + s = str(num) + amounts = [] + for n in (0,1,2,3,4,5,6,7,8,9): + amounts.append(s.count(str(n))) + c = Counter(amounts) + if c[0] + c[1] < 10: + return c[2] >= 1 + amount = 0 + for n in range(357253, 892942 + 1): + if containsDouble(n): + if isIncreasing(n): + amount += 1 + return amount + diff --git a/solutions/py/d05.py b/solutions/py/d05.py new file mode 100644 index 0000000..446bcd6 --- /dev/null +++ b/solutions/py/d05.py @@ -0,0 +1,26 @@ +import intcode + +def pt1(input): + program = [int(x) for x in input[0].split(",")] + c = intcode.Computer(program) + c.input = 1 + output = [] + while c.memory[c.pointer] != 99: + c.step() + if c.output != None: + output.append(c.output) + c.output = None + return output + +def pt2(input): + program = [int(x) for x in input[0].split(",")] + c = intcode.Computer(program) + c.input = 5 + output = [] + while c.memory[c.pointer] != 99: + c.step() + if c.output != None: + output.append(c.output) + c.output = None + return output + diff --git a/solutions/py/d06.py b/solutions/py/d06.py new file mode 100644 index 0000000..b6b9ef7 --- /dev/null +++ b/solutions/py/d06.py @@ -0,0 +1,78 @@ +from typing import Iterable +import sys + +class Tree(object): + # https://stackoverflow.com/a/28015122 + def __init__(self, name='root', children=None): + self.name = name + self.children = [] + self.parent = None + if children is not None: + for child in children: + self.add_child(child) + def add_child(self, node): + self.children.append(node) + node.parent = self + + def __eq__(self, other): + if isinstance(other, Tree): + return self.name == other.name + return False + def parents(self): + return [self.parent] + self.parent.parents() if self.parent != None\ + else [] + +def gen_tree(input): + trees = {} + seen = set() # seen trees. contains only IDs + leafs = {} + + for orbit in input: + orbit = orbit.split(")") + #print(orbit) + if not orbit[0] in seen: + trees[orbit[0]] = Tree(orbit[0]) + seen.add(orbit[0]) + + # hook up each child to its parent (since every parent now exists) + for orbit in input: + orbit = orbit.rstrip().split(")") + if orbit[1] in seen: + trees[orbit[0]].add_child(trees[orbit[1]]) + else: + leafs[orbit[1]] = Tree(orbit[1]) + trees[orbit[0]].add_child(leafs[orbit[1]]) + for k in trees: + pass + #print(trees[k], trees[k].children) + + trees.update(leafs) + return trees + +sum_depths = 0 +def pt1(input): + tree = gen_tree(input) + depth = 0 + depths = {} + def count_children(tree, depth): + global sum_depths # fusk-rekursion? ville bara att det skulle fungera + sum_depths += depth + depths[tree.name] = depth + for child in tree.children: + count_children(child, depth+1) + count_children(tree["COM"], 0) + return sum_depths + +def pt2(input): + # find common parent and distance to it + tree = gen_tree(input) + src_parents = tree["YOU"].parent.parents() + tar_parents = tree["SAN"].parent.parents() + src_dist = 0 + for src_parent in src_parents: + src_dist += 1 + tar_dist = 0 + for tar_parent in tar_parents: + tar_dist += 1 + if src_parent == tar_parent: + return (src_parent.name, src_dist, tar_dist, src_dist + tar_dist) diff --git a/solutions/py/07.py b/solutions/py/d07.py index 18c3b89..e12d7c9 100644 --- a/solutions/py/07.py +++ b/solutions/py/d07.py @@ -2,15 +2,41 @@ import intcode import itertools import queue -def pt2(program): +def pt1(input): + program = [int(x) for x in input[0].split(",")] highest_signal = 0 highest_sequence = None - for phase_seq in list(itertools.permutations(list(range(5,10)))): + for phase_seq in list(itertools.permutations(list(range(0,5)))): + q = queue.Queue(5) + for phase in phase_seq: + q.put(phase) + amps = [intcode.Computer(program) for _ in range(5)] + for amp in amps: + amp.input = q.get() signal = 0 + for amp in amps: + while True: + amp.step() + if amp.input is None: + amp.input = signal + if amp.output is not None: + signal = amp.output + amp.output = None + break + if signal > highest_signal: + highest_signal = signal + highest_sequence = phase_seq + return (highest_sequence, highest_signal) + +def pt2(input): + program = [int(x) for x in input[0].split(",")] + highest_signal = 0 + highest_sequence = None + for phase_seq in list(itertools.permutations(list(range(5,10)))): q = queue.Queue(5) for phase in phase_seq: q.put(phase) - amps = [intcode.Computer(program) for i in range(5)] + amps = [intcode.Computer(program) for _ in range(5)] for amp in amps: amp.input = q.get() @@ -19,7 +45,7 @@ def pt2(program): while True: amp = amps[current_amp] amp.step() - if amp.input == None: + if amp.input is None: if amp.phase_read == False: amp.phase_read = True amp.input = signal @@ -41,15 +67,4 @@ def pt2(program): if signal > highest_signal: highest_signal = signal highest_sequence = phase_seq - return highest_signal - -if __name__ == "__main__": - f = open("../input/07", "r") - program = [int(x) for x in f.readline().split(",")] - - import cProfile - import timeit - - print(2, pt2(program)) - print(timeit.timeit('pt2(program)', globals=globals(), number=1)*1000, "ms") - cProfile.run("pt2(program)") + return (highest_sequence, highest_signal) diff --git a/solutions/py/d08.py b/solutions/py/d08.py new file mode 100644 index 0000000..659bc3a --- /dev/null +++ b/solutions/py/d08.py @@ -0,0 +1,31 @@ +def count(layer, num): + return sum([row.count(num) for row in layer]) + + +def pt1(input): + img = [] + least_zeroes = 150 # max + n = 0 + for l in range(100): + #print("l", l) + layer = [[int(input[0][l*25*6 + y*25 + x]) for x in range(25)] for y in range(6)] + if count(layer, 0) < least_zeroes: + least_zeroes = count(layer, 0) + n = count(layer, 1) * count(layer, 2) + img.append(layer) + return n + +def pt2(input): + img = [[[int(input[0][l*25*6 + y*25 + x]) for x in range(25)] for y in range(6)] for l in range(100)] + result = img[0] + for layer in img[1:]: + for x in range(25): + for y in range(6): + if result[y][x] == 2: + result[y][x] = layer[y][x] + str_res = [] + for row in result: + for c in row: + str_res.append('\u2588' if c == 1 else ' ') + str_res.append("\n") + return "\n" + ("".join(str_res)) diff --git a/solutions/py/main.py b/solutions/py/main.py new file mode 100644 index 0000000..3456dd6 --- /dev/null +++ b/solutions/py/main.py @@ -0,0 +1,15 @@ +import d01 +import d02 +import d03 +import d04 +import d05 +import d06 +import d07 +import d08 + +mods = [d01, d02, d03, d04, d05, d06, d07, d08] + +for mod, day in zip(mods, range(8)): + print("Day", str(day+1).zfill(2)) + print("Part", 1, mod.pt1(open("../input/" + str(day+1).zfill(2), "r").readlines())) + print("Part", 2, mod.pt2(open("../input/" + str(day+1).zfill(2), "r").readlines())) |
