Author: Honza Pokorny <me@honza.ca>
Move to 2019
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
diff --git a/001/input b/001/input deleted file mode 100644 index 154bf896f12156606867230657600df38d9b46b6..0000000000000000000000000000000000000000 --- a/001/input +++ /dev/null @@ -1,100 +0,0 @@ -81157 -80969 -113477 -81295 -70537 -90130 -123804 -94276 -139327 -123719 -107814 -122142 -61204 -135309 -62810 -85750 -132568 -76450 -122948 -124649 -102644 -80055 -60517 -125884 -125708 -99051 -137158 -100450 -55239 -66758 -123848 -88711 -113047 -125528 -59285 -103978 -93047 -98038 -143019 -92031 -54353 -115597 -105629 -80411 -134966 -135473 -77357 -65776 -71096 -66926 -97853 -80349 -141914 -127221 -102492 -143587 -111493 -84711 -59826 -135652 -103334 -138211 -65088 -82244 -95011 -78760 -56691 -62070 -146134 -81650 -76904 -98838 -89629 -59950 -50390 -78616 -99731 -53831 -81273 -103980 -58485 -137684 -142457 -111050 -141916 -55567 -141945 -100794 -136425 -77911 -137114 -77450 -132048 -143066 -136805 -114135 -61565 -67286 -85512 -137493 diff --git a/001/main.py b/001/main.py deleted file mode 100644 index 458b0dd0ffb051cadaa408ea2dc5fa2377250491..0000000000000000000000000000000000000000 --- a/001/main.py +++ /dev/null @@ -1,41 +0,0 @@ -def round_down(value): - integer = int(value) - - if integer == value: - return value - - return integer - - -def calculate(value): - if value <= 0: - return 0 - - v = round_down(value / 3.0) - 2 - - if v <= 0: - return 0 - - return v + calculate(v) - - -def main(): - print(calculate(1969)) - print('---') - assert calculate(1969) == 966 - print(calculate(100756)) - assert calculate(100756) == 50346 - - - data = open('input').read().splitlines() - total = 0 - - for line in data: - required = calculate(int(line)) - total += required - - print(total) - - -if __name__ == '__main__': - main() diff --git a/002/main.py b/002/main.py deleted file mode 100644 index 9baaeb856e425b1c4169c85e1d21955816e825be..0000000000000000000000000000000000000000 --- a/002/main.py +++ /dev/null @@ -1,75 +0,0 @@ -import copy - - -OPS = [1, 2, 99] - - -class NonHaltException(Exception): - pass - - -def add(a, b): - return a + b - - -def mult(a, b): - return a * b - - -def intcode(input_list, a, b): - # fix it - input_list[1] = a - input_list[2] = b - - cur = 0 - - while True: - op = input_list[cur] - - if op not in OPS: - raise NonHaltException - - if op == 99: - break - - f = None - - if op == 1: - f = add - - if op == 2: - f = mult - - if not f: - continue - - a, b = input_list[cur + 1], input_list[cur + 2] - result_pos = input_list[cur + 3] - input_list[result_pos] = f(input_list[a], input_list[b]) - cur += 4 - - return input_list[0] - - -def main(): - - input_list = [1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,2,6,19,23,1,23,5,27,1,27,13,31,2,6,31,35,1,5,35,39,1,39,10,43,2,6,43,47,1,47,5,51,1,51,9,55,2,55,6,59,1,59,10,63,2,63,9,67,1,67,5,71,1,71,5,75,2,75,6,79,1,5,79,83,1,10,83,87,2,13,87,91,1,10,91,95,2,13,95,99,1,99,9,103,1,5,103,107,1,107,10,111,1,111,5,115,1,115,6,119,1,119,10,123,1,123,10,127,2,127,13,131,1,13,131,135,1,135,10,139,2,139,6,143,1,143,9,147,2,147,6,151,1,5,151,155,1,9,155,159,2,159,6,163,1,163,2,167,1,10,167,0,99,2,14,0,0] - - expected = 19690720 - - for a in range(0, 99): - for b in range(0, 99): - il = copy.deepcopy(input_list) - - try: - result = intcode(il, a, b) - except NonHaltException: - continue - - if result == expected: - print('Found the solution:', a, b) - return - - -if __name__ == '__main__': - main() diff --git a/003/main.py b/003/main.py deleted file mode 100644 index ee5b5d115f8d0481adbc4367d83cee19b1b18975..0000000000000000000000000000000000000000 --- a/003/main.py +++ /dev/null @@ -1,98 +0,0 @@ -example = [ - "R8,U5,L5,D3", - "U7,R6,D4,L4" -] - - -A = """R992,U284,L447,D597,R888,D327,R949,U520,R27,U555,L144,D284,R538,U249,R323,U297,R136,U838,L704,D621,R488,U856,R301,U539,L701,U363,R611,D94,L734,D560,L414,U890,R236,D699,L384,D452,R702,D637,L164,U410,R649,U901,L910,D595,R339,D346,R959,U777,R218,D667,R534,D762,R484,D914,L25,U959,R984,D922,R612,U999,L169,D599,L604,D357,L217,D327,L730,D949,L565,D332,L114,D512,R460,D495,L187,D697,R313,U319,L8,D915,L518,D513,R738,U9,R137,U542,L188,U440,R576,D307,R734,U58,R285,D401,R166,U156,L859,U132,L10,U753,L933,U915,R459,D50,R231,D166,L253,U844,R585,D871,L799,U53,R785,U336,R622,D108,R555,D918,L217,D668,L220,U738,L997,D998,R964,D456,L54,U930,R985,D244,L613,D116,L994,D20,R949,D245,L704,D564,L210,D13,R998,U951,L482,U579,L793,U680,L285,U770,L975,D54,R79,U613,L907,U467,L256,D783,R883,U810,R409,D508,L898,D286,L40,U741,L759,D549,R210,U411,R638,D643,L784,U538,L739,U771,L773,U491,L303,D425,L891,U182,R412,U951,L381,U501,R482,D625,R870,D320,L464,U555,R566,D781,L540,D754,L211,U73,L321,D869,R994,D177,R496,U383,R911,U819,L651,D774,L591,U666,L883,U767,R232,U822,L499,U44,L45,U873,L98,D487,L47,U803,R855,U256,R567,D88,R138,D678,L37,U38,R783,U569,L646,D261,L597,U275,L527,U48,R433,D324,L631,D160,L145,D128,R894,U223,R664,U510,R756,D700,R297,D361,R837,U996,L769,U813,L477,U420,L172,U482,R891,D379,L329,U55,R284,U155,L816,U659,L671,U996,R997,U252,R514,D718,L661,D625,R910,D960,L39,U610,R853,U859,R174,U215,L603,U745,L587,D736,R365,U78,R306,U158,L813,U885,R558,U631,L110,D232,L519,D366,R909,D10,R294""" - -B = """L1001,D833,L855,D123,R36,U295,L319,D700,L164,U576,L68,D757,R192,D738,L640,D660,R940,D778,R888,U772,R771,U900,L188,D464,L572,U184,R889,D991,L961,U751,R560,D490,L887,D748,R37,U910,L424,D401,L385,U415,L929,U193,R710,D855,L596,D323,L966,D505,L422,D139,L108,D135,R737,U176,R538,D173,R21,D951,R949,D61,L343,U704,R127,U468,L240,D834,L858,D127,R328,D863,R329,U477,R131,U864,R997,D38,R418,U611,R28,U705,R148,D414,R786,U264,L785,D650,R201,D250,R528,D910,R670,U309,L658,U190,R704,U21,R288,D7,R930,U62,R782,U621,R328,D725,R305,U700,R494,D137,R969,U142,L867,U577,R300,U162,L13,D698,R333,U865,R941,U796,L60,U902,L784,U832,R78,D578,R196,D390,R728,D922,R858,D994,L457,U547,R238,D345,R329,D498,R873,D212,R501,U474,L657,U910,L335,U133,R213,U417,R698,U829,L2,U704,L273,D83,R231,D247,R675,D23,L692,D472,L325,D659,L408,U746,L715,U395,L596,U296,R52,D849,L713,U815,R684,D551,L319,U768,R176,D182,R557,U731,R314,D543,L9,D256,R38,D809,L567,D332,R375,D572,R81,D479,L71,U968,L831,D247,R989,U390,R463,D576,R740,D539,R488,U367,L596,U375,L763,D824,R70,U448,R979,D977,L744,D379,R488,D671,L516,D334,L542,U517,L488,D390,L713,D932,L28,U924,L448,D229,L488,D501,R19,D910,L979,D411,R711,D824,L973,U291,R794,D485,R208,U370,R655,U450,L40,D804,L374,D671,R962,D829,L209,U111,L84,D876,L832,D747,L733,D560,L702,D972,R188,U817,L111,U26,L492,U485,L71,D59,L269,D870,L152,U539,R65,D918,L932,D260,L485,U77,L699,U254,R924,U643,L264,U96,R395,D917,R360,U354,R101,D682,R854,U450,L376,D378,R872,D311,L881,U630,R77,D766,R672""" - -example = [ - "R8,U5,L5,D3", - "U7,R6,D4,L4" -] -example = [ - "R75,D30,R83,U83,L12,D49,R71,U7,L72", - "U62,R66,U55,R34,D71,R55,D58,R83" -] - -example = [ - "R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51", - "U98,R91,D20,R16,D67,R40,U7,R15,U6,R7" -] -example = [A, B] - - -def get_points(w): - ps = w.split(',') - start = (0, 0) # x, y - - result = [] - - pos = (0, 0) - - for p in ps: - d, s = p[0], int(p[1:]) - - if d == 'L': - for _ in range(0, s): - n = (pos[0] - 1, pos[1]) - result.append(n) - pos = n - elif d == 'R': - for _ in range(0, s): - n = (pos[0] + 1, pos[1]) - result.append(n) - pos = n - elif d == 'U': - for _ in range(0, s): - n = (pos[0], pos[1] + 1) - result.append(n) - pos = n - elif d == 'D': - for _ in range(0, s): - n = (pos[0], pos[1] - 1) - result.append(n) - pos = n - else: - raise Exception('unknown direction') - - return result - - -def main(): - w1, w2 = example - - p1 = get_points(w1) - p2 = get_points(w2) - - crosses = set(p1).intersection(set(p2)) - - steps = 0 - - for c in crosses: - cs = 0 - - for s in p1: - cs += 1 - if s == c: - break - - for s in p2: - cs += 1 - if s == c: - break - - if steps == 0: - steps = cs - continue - - if cs < steps: - steps = cs - - print(steps) - - -if __name__ == '__main__': - main() diff --git a/004/main.py b/004/main.py deleted file mode 100644 index f98201e499710e15f0671e534c54a9bfc43b6e44..0000000000000000000000000000000000000000 --- a/004/main.py +++ /dev/null @@ -1,53 +0,0 @@ -def has_double(n): - s = str(n) - o = {} - - for d in s: - if d not in o: - o[d] = 1 - else: - o[d] += 1 - - for v in o.values(): - if v == 2: - return True - - return False - - -def does_increase(n): - s = str(n) - p = 0 - - for d in s: - dd = int(d) - - if dd < p: - return False - - p = dd - - return True - - -def matches(n): - return has_double(n) and does_increase(n) - - -def main(): - assert matches(112233) - assert not matches(123444) - assert matches(111122) - - # return - - c = 0 - for n in range(172930, 683082): - if matches(n): - c += 1 - - print(c) - - -if __name__ == '__main__': - main() diff --git a/005/main.py b/005/main.py deleted file mode 100644 index d7fe7f4b4084243fb1116dcf24a0c31087fe4717..0000000000000000000000000000000000000000 --- a/005/main.py +++ /dev/null @@ -1,221 +0,0 @@ -import re -import copy - -opcode = re.compile(r'(\d{2})') - -OPS = ['01', '02', '03', '04', '99'] - - -def rev(s): - r = [] - for l in s: - r.append(l) - - r.reverse() - return ''.join(r) - - - -class NonHaltException(Exception): - pass - - -def add(a, b): - return a + b - - -def mult(a, b): - return a * b - - -def intcode(input_list, input_code): - cur = 0 - if input_list[0] == 3: - input_list[input_list[1]] = input_code - cur += 2 - - while True: - op_value = input_list[cur] - op_value_string = '000000' + str(op_value) - - op = op_value_string[-2:] - - front = op_value_string[0:-2] - front = rev(front) - - if op == '99': - break - - if op == '01': - # 0 po, like before - # 1 im - a_mode = front[0] - b_mode = front[1] - - if a_mode == '0': - a = input_list[input_list[cur + 1]] - else: - a = input_list[cur + 1] - - if b_mode == '0': - b = input_list[input_list[cur + 2]] - else: - b = input_list[cur + 2] - - result_pos = input_list[cur + 3] - input_list[result_pos] = add(a, b) - cur += 4 - continue - - if op == '02': - # 0 po, like before - # 1 im - a_mode = front[0] - b_mode = front[1] - - if a_mode == '0': - a = input_list[input_list[cur + 1]] - else: - a = input_list[cur + 1] - - if b_mode == '0': - b = input_list[input_list[cur + 2]] - else: - b = input_list[cur + 2] - - result_pos = input_list[cur + 3] - input_list[result_pos] = mult(a, b) - cur += 4 - continue - - if op == '04': - mode = front[0] - if mode == '0': - print(input_list[input_list[cur + 1]]) - else: - print(input_list[cur + 1]) - cur += 2 - continue - - if op == '05': - mode = front[0] - if mode == '0': - v = input_list[input_list[cur + 1]] - else: - v = input_list[cur + 1] - - if v is not 0: - m2 = front[1] - if m2 == '0': - cur = input_list[input_list[cur + 2]] - else: - cur = input_list[cur + 2] - else: - cur += 3 - - if op == '06': - mode = front[0] - if mode == '0': - v = input_list[input_list[cur + 1]] - else: - v = input_list[cur + 1] - - if v == 0: - m2 = front[1] - if m2 == '0': - cur = input_list[input_list[cur + 2]] - else: - cur = input_list[cur + 2] - else: - cur += 3 - - if op == '07': - # 0 po, like before - # 1 im - a_mode = front[0] - b_mode = front[1] - - if a_mode == '0': - a = input_list[input_list[cur + 1]] - else: - a = input_list[cur + 1] - - if b_mode == '0': - b = input_list[input_list[cur + 2]] - else: - b = input_list[cur + 2] - - result_pos = input_list[cur + 3] - - if a < b: - input_list[result_pos] = 1 - else: - input_list[result_pos] = 0 - - cur += 4 - continue - - if op == '08': - # 0 po, like before - # 1 im - a_mode = front[0] - b_mode = front[1] - - if a_mode == '0': - a = input_list[input_list[cur + 1]] - else: - a = input_list[cur + 1] - - if b_mode == '0': - b = input_list[input_list[cur + 2]] - else: - b = input_list[cur + 2] - - result_pos = input_list[cur + 3] - - if a == b: - input_list[result_pos] = 1 - else: - input_list[result_pos] = 0 - - cur += 4 - continue - - -def main(): - input_list = [ - - 3,225,1,225,6,6,1100,1,238,225,104,0,1102,45,16,225,2,65,191,224,1001, - 224,-3172,224,4,224,102,8,223,223,1001,224,5,224,1,223,224,223,1102, - 90,55,225,101,77,143,224,101,-127,224,224,4,224,102,8,223,223,1001,224, - 7,224,1,223,224,223,1102,52,6,225,1101,65,90,225,1102,75,58,225,1102, - 53,17,224,1001,224,-901,224,4,224,1002,223,8,223,1001,224,3,224,1,224, - 223,223,1002,69,79,224,1001,224,-5135,224,4,224,1002,223,8,223,1001, - 224,5,224,1,224,223,223,102,48,40,224,1001,224,-2640,224,4,224, - 102,8,223,223,1001,224,1,224,1,224,223,223,1101,50,22,225,1001,218,29, - 224,101,-119,224,224,4,224,102,8,223,223,1001,224,2,224,1,223,224,223, - - - 1101,48,19,224,1001,224,-67,224,4,224,102,8,223,223,1001,224,6,224,1, - - 223,224,223,1101,61,77,225,1,13,74,224,1001,224,-103,224,4,224,1002,223,8,223,101,3,224,224,1,224,223,223,1102,28,90,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1, - 99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999, - 1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,7,226,677,224,102,2,223,223,1005,224,329,1001,223,1,223,8,226,677,224,1002,223,2,223,1005,224,344,101,1,223,223,8,226,226,224, - 1002,223,2,223,1006,224,359,101,1,223,223,1008,677,226,224,1002,223,2,223,1005,224,374,1001,223,1,223,108,677,677,224,1002,223,2,223,1005,224,389,1001,223,1,223,1107,226,677,224,1002,223,2, - - 223,1006,224,404,101,1,223,223,1008,226,226,224,102,2,223,223,1006,224,419,1001,223,1,223,7,677,226,224,1002,223,2,223,1005,224,434,101,1,223,223,1108,226,226,224,1002,223,2,223,1005,224,449, - 101,1,223,223,7,226,226,224,102,2,223,223,1005,224,464,101,1,223,223,108,677,226,224,102,2,223,223,1005,224,479,1001,223,1,223,1007,677,226,224,1002,223,2,223,1006,224,494,1001,223,1,223,1007, - 677,677,224,1002,223,2,223,1006,224,509,1001,223,1,223,107,677,677,224,1002,223,2,223,1005,224,524,101,1,223,223,1108,226,677,224,102,2,223,223,1006,224,539,1001,223,1,223,8,677,226,224,102,2, - 223,223,1005,224,554,101,1,223,223,1007,226,226,224,102,2,223,223,1006,224,569,1001,223,1,223,107,677,226,224,102,2,223,223,1005,224,584,1001,223,1,223,108,226,226,224,102,2,223,223,1006,224,599, - 1001,223,1,223,107,226,226,224,1002,223,2,223,1006,224,614,1001,223,1,223,1108,677,226,224,1002,223,2,223,1005,224,629,1001,223,1,223,1107,677,677,224,102,2,223,223,1005,224,644,1001,223,1,223, - 1008,677,677,224,102,2,223,223,1005,224,659,101,1,223,223,1107,677,226,224,1002,223,2,223,1006,224,674,101,1,223,223,4,223,99,226 - - - ] - - intcode(input_list, 5) - - - -if __name__ == '__main__': - main() diff --git a/006/input b/006/input deleted file mode 100644 index 43a2bac211778a4e33de455ae9a7b4e2095c53a5..0000000000000000000000000000000000000000 --- a/006/input +++ /dev/null @@ -1,1272 +0,0 @@ -6CF)4J7 -RC4)H87 -LMS)RL4 -KGS)1QW -H8N)KW5 -JJM)YPS -P1W)ZKT -BSY)FMR -9T5)JK4 -2X2)NQ5 -H6R)FLY -8N9)H7L -5LR)TYT -VM7)V5V -GVF)Q8P -PQV)9NB -4W3)V84 -H59)B3G -RGW)82C -BTQ)VRF -KQW)TNC -RT3)JHN -ZLY)15X -YJJ)55L -8Q2)6S7 -963)PNL -8F3)WPH -H2Z)LMN -MFK)DMJ -JGN)H4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P)KFB -GZ3)76P -CHJ)3GM -DX1)NVB -7LG)8ZY -QF1)HQ3 -85T)7DX -6T3)K7Q -49V)4BW -Y3R)DJF -4HL)641 -FYC)PGC \ No newline at end of file diff --git a/006/main.py b/006/main.py deleted file mode 100644 index 256ed6a72847d35ef926893b4c6391141c5fc568..0000000000000000000000000000000000000000 --- a/006/main.py +++ /dev/null @@ -1,105 +0,0 @@ -from functools import reduce - - -data = """COM)B -B)C -C)D -D)E -E)F -B)G -G)H -D)I -E)J -J)K -K)L""" - -data = """COM)B -B)C -C)D -D)E -E)F -B)G -G)H -D)I -E)J -J)K -K)L -K)YOU -I)SAN""" - - -def add(a, b): - return a + b - - -def assert_root(lines): - orbitors = [] - orbitees = [] - - for line in lines: - a, b = line.split(')') - orbitors.append(b) - orbitees.append(a) - - orbitors = set(orbitors) - orbitees = set(orbitees) - - root = orbitees.difference(orbitors) - assert len(root) == 1 - assert 'COM' in root - - -def count(x, k, n=0): - c = 0 - c += n - - for item in x.get(k, []): - c += count(x, item, n + 1) - - return c - - -def path(x, k): - if k == 'COM': - return [] - return [x[k]] + path(x, x[k]) - - -def main(): - x = {} - - data = open('input').read() - - lines = data.splitlines() - - assert_root(lines) - - for line in lines: - a, b = line.split(')') - x[b] = a - - you_path = path(x, 'YOU') - san_path = path(x, 'SAN') - - you_path.reverse() - san_path.reverse() - - common = [] - - for y, s in zip(you_path, san_path): - if y == s: - common.append(y) - else: - break - - print(you_path) - print(san_path) - print(common) - - res = (len(you_path) - len(common)) + (len(san_path) - len(common)) - print(res) - - - -if __name__ == '__main__': - main() diff --git a/007/main.py b/007/main.py deleted file mode 100644 index 8372b7d4ea77ed2c0a6ed13fc0a04c6ba7596020..0000000000000000000000000000000000000000 --- a/007/main.py +++ /dev/null @@ -1,300 +0,0 @@ -import re -import copy -import itertools - -opcode = re.compile(r'(\d{2})') - -OPS = ['01', '02', '03', '04', '99'] -NEEDS_INPUT = 'NI' -HALT = 'H' - - -def rev(s): - r = [] - for l in s: - r.append(l) - - r.reverse() - return ''.join(r) - - - -class NonHaltException(Exception): - pass - - -def add(a, b): - return a + b - - -def mult(a, b): - return a * b - - -def intcode(input_list, *input_codes, cur=0): - input_codes = list(input_codes) - input_codes.reverse() - - outputs = [] - - # if input_list[0] == 3: - # input_list[input_list[1]] = input_codes.pop() - # cur += 2 - - while True: - op_value = input_list[cur] - op_value_string = '000000' + str(op_value) - - op = op_value_string[-2:] - - front = op_value_string[0:-2] - front = rev(front) - - if op == '99': - break - - if op == '01': - # 0 po, like before - # 1 im - a_mode = front[0] - b_mode = front[1] - - if a_mode == '0': - a = input_list[input_list[cur + 1]] - else: - a = input_list[cur + 1] - - if b_mode == '0': - b = input_list[input_list[cur + 2]] - else: - b = input_list[cur + 2] - - result_pos = input_list[cur + 3] - input_list[result_pos] = add(a, b) - cur += 4 - continue - - if op == '02': - # 0 po, like before - # 1 im - a_mode = front[0] - b_mode = front[1] - - if a_mode == '0': - a = input_list[input_list[cur + 1]] - else: - a = input_list[cur + 1] - - if b_mode == '0': - b = input_list[input_list[cur + 2]] - else: - b = input_list[cur + 2] - - result_pos = input_list[cur + 3] - input_list[result_pos] = mult(a, b) - cur += 4 - continue - - if op == '03': - - if not input_codes: - return { - 'output': outputs, - 'state': NEEDS_INPUT, - 'memory': input_list, - 'cur': cur, - } - - input_list[input_list[cur + 1]] = input_codes.pop() - cur += 2 - continue - - if op == '04': - mode = front[0] - if mode == '0': - o = input_list[input_list[cur + 1]] - else: - o = input_list[cur + 1] - outputs.append(o) - cur += 2 - continue - - if op == '05': - mode = front[0] - if mode == '0': - v = input_list[input_list[cur + 1]] - else: - v = input_list[cur + 1] - - if v is not 0: - m2 = front[1] - if m2 == '0': - cur = input_list[input_list[cur + 2]] - else: - cur = input_list[cur + 2] - else: - cur += 3 - - if op == '06': - mode = front[0] - if mode == '0': - v = input_list[input_list[cur + 1]] - else: - v = input_list[cur + 1] - - if v == 0: - m2 = front[1] - if m2 == '0': - cur = input_list[input_list[cur + 2]] - else: - cur = input_list[cur + 2] - else: - cur += 3 - - if op == '07': - # 0 po, like before - # 1 im - a_mode = front[0] - b_mode = front[1] - - if a_mode == '0': - a = input_list[input_list[cur + 1]] - else: - a = input_list[cur + 1] - - if b_mode == '0': - b = input_list[input_list[cur + 2]] - else: - b = input_list[cur + 2] - - result_pos = input_list[cur + 3] - - if a < b: - input_list[result_pos] = 1 - else: - input_list[result_pos] = 0 - - cur += 4 - continue - - if op == '08': - # 0 po, like before - # 1 im - a_mode = front[0] - b_mode = front[1] - - if a_mode == '0': - a = input_list[input_list[cur + 1]] - else: - a = input_list[cur + 1] - - if b_mode == '0': - b = input_list[input_list[cur + 2]] - else: - b = input_list[cur + 2] - - result_pos = input_list[cur + 3] - - if a == b: - input_list[result_pos] = 1 - else: - input_list[result_pos] = 0 - - cur += 4 - continue - - return { - 'output': outputs, - 'state': HALT, - 'memory': input_list, - 'cur': None - } - - -def run_phases(input_list, phases): - prog_input = 0 - cur = 0 - - state = {} - - while True: - for p in itertools.repeat(phases): - for i in phases: - amplifier_state = state.get(i, { - 'memory': copy.deepcopy(input_list), - 'cur': 0, - 'state': None - }) - - if not amplifier_state['state']: - inputs = [i, prog_input] - else: - inputs = [prog_input] - - out = intcode(amplifier_state['memory'], - *inputs, - cur=amplifier_state['cur']) - - if out['state'] == HALT: - if i == phases[-1]: - return out['output'][0] - - state[i] = out - prog_input = out['output'][0] - - if prog_input > 1000000000000: - raise Exception('too much') - - -def find_solution(input_list): - max_v = 0 - max_perm = None - - for v in itertools.permutations('56789'): - phases = list(map(int, list(v))) - prog_out = run_phases(copy.deepcopy(input_list), phases) - - if prog_out > max_v: - max_v = prog_out - max_perm = v - - if not max_perm: - max_perm = [] - - return list(map(int, max_perm)), max_v - - - -def main(): - inputs = [ - ( - [3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26, 27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5], - [9,8,7,6,5], - 139629729 - ), - - ( - [ - 3,52,1001,52,-5,52,3,53,1,52,56,54,1007,54,5,55,1005,55,26,1001,54, - -5,54,1105,1,12,1,53,54,53,1008,54,0,55,1001,55,1,55,2,53,55,53,4, - 53,1001,56,-1,56,1005,56,6,99,0,0,0,0,10 - ], - [9,7,8,5,6], - 18216 - ), - - ] - - puzzle_input = [ - 3,8,1001,8,10,8,105,1,0,0,21,42,59,76,85,106,187,268,349,430,99999,3,9,102,3,9,9,1001,9,2,9,1002,9,3,9,1001,9,3,9,4,9,99,3,9,102,3,9,9,101,3,9,9,1002,9,2,9,4,9,99,3,9,102,3,9,9,1001,9,4,9,1002,9,5,9,4,9,99,3,9,102,2,9,9,4,9,99,3,9,101,3,9,9,1002,9,2,9,1001,9,4,9,1002,9,2,9,4,9,99,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,99 - ] - - # for input_list, expected_phase, expected_value in inputs: - # actual_phase, actual_value = find_solution(input_list) - # print(actual_phase, expected_phase) - # print(actual_value, expected_value) - # print('') - print(find_solution(puzzle_input)) - -if __name__ == '__main__': - main() diff --git a/008/input b/008/input deleted file mode 100644 index f0b44c6f724048a59e5749e8cae54d966fe2e3d2..0000000000000000000000000000000000000000 --- a/008/input +++ /dev/null @@ -1 +0,0 @@ -212222222222222222022222222022202022222222220220222022022012122222222221222202222222022222122201222022220202222221222220222222122222222222022222222222212222222222222222222222222122222122222222221222222022122112022222222222222202222222022220222201222012221222222220222222222222222222222221122222222222212222222222222222022222222022202222221222220222222122122022022222222222222202222222122220022212222012220202202222222221222222222222222222122222222222202222222222220222022222222222222022220222222222222222122202022222222220222202222222222020222211222112222222222222222220222222022222222221122222222222202222222222221222122222222222212122220222220222222122222222122122222221222222222222122020222200222102221202212222222222222122122222222220022222222222222222222222221222022222222122212022222222220221222022022202220222222221222222222222122120222212222012222222202202222221222022022222222220122222222222212222222222222222122222222022202222220222221220222022022122120022222222222202222222122021122220222222220222222210222220222122022222222220222222222222212222222222222222222222222122212122220222222220222022222102021222222221222222222222222020122201222102222222222211222222222022122222222221122222222222222222222222210222222222222222222122201222220222222122022102222122222222222212222222022121022211222022220202202221222221222222122222222222122222222222212222222222212222022222222222202222222222221221222202022202221122222221222212222222022020222201222112222222212212222221222022222222222220122222222222222222222212210222222222222022212122220222222220222222122122021222222222222222222222122120022221222112222222212221222220222022122222222221102222222212212222222212220222122222222222222022221222220220222012122112222222222221222212222222022020122201222202220212222221222221222022022222222222212222222212212222222212221222122222222122222122211222220222222122222112122222222222222212222222122222122212222222220212212202222221222022222222222220212222222222212222222212222222022222222122212122201222220221222222122102020122222222222222222222022021022200222112221202212211222221222122122222222222112222222202222222222202201222022222222222202122200222222221222222022002122022222222222202222222122221222201222112221222222202222222222022122222222220112222222222222222220222211222122222222022212122210222221220222222022222220021222222222202222222222022222222222022220222222221222021222022022222222220202222222222202222221222221222222222222022202222212222221220222002222222221220222221222202222222022122022211222012220202222210222220222122222222222220012222222222202222222222201222222222222022212222212222221221222112122102021122222220222212222222122220122221222122222212212222222221222222022222222221002222222222222222202222211222022222222022202222222222222220222102122122220222222220222212222222022120122222222212220202212201222122222122122222222222212222222212222222202212210222222222222022212022211222222221222022122102221222222220222222222222222021222221222122221202202210222022222122122222222221102222222202202222222202201222122222222222212222200222220222222222022202221120222221222212222222222021222201222202220212222012222022222122122222222221102222222202202222211202200222122222222022222022211222220222222112122102221122202221222202222222222220022201221202222212202110222021222222222222222221002222222202202222211212211222122222222122212222220222220221222222222210022020202221222222222222022122122212220112221202212222222222222022122222222220012222222222212222201202202222222222222122202122212222221222222222222121021220022022222212222222022020122212221002222202202121222220222022222222222221102222222202221222221202210222022222220122222022202222220220222012122220021220122122222202222222022021022210222122220222202102222021222022022222222221022222222222211222201212222222122222220022212122210222220222222022122211122122002121222212222222022221022222220212221202202020222121222022022222222221122222222212212222210222221222222222220122202022200222221222222122222101221121222120222212222222120021022201220222221212202100222221222222122222222220002222222212220222201222201222122222222222212222200222221222222012022201120221022122122212222222222121122221220112221222212010222022222122022222222222102222222202211222222212222222122222222222212122202222220222222002022211221022212022122222222222022020222220221102222212202201222220222222222222222220112222222212200222201212210222222222222022212122200222221221222102122220220220112220022212222222222022122201220112220222222001222221222022222222222222022222222222200222211200212222222222222122222222220222220222222012022010020221012222222202222222220221122222222012222202201211222221222122022222222220102222222212211222212221210222122222221122200122201222221221222102022102120122112021222222222222121022022202221212220202222100222222222122222222222221112222222212202222210200202222022221222022222122221222221221222012022122122021122222122202222222021120122200220202221202210110222121222022222222222222122222220212200222220221210222122222221222221222220222220221222222022212120020022221122222222222120022112200221202221202200102222221222022022222222221222222221212211222202120210222222221221022220022210222222220122022022211220122112122122222222222222020102221221122222212212112222021222122022222222221202222221202221222212211201222122221220022222022211222220221022202022212122020002121022202222222021222112210222202222222210102222222222022022222222220222222220212200222212012212222122220222122200222201222221220222102222220221220002021022202222222022220212210221202221222001212222221222222222222222220002222221202202222222222200222222221222022100222220222222221221122222101121020212022222212222222021120112220220022220202011002212122222122122222222220122202222222222222202202202222222222220222210222200220220221122122022002222202202022122222222222120222202201220112221212021121202120222122122222222222102212222212200222220210222222022221222122211122200221221220120002122121022121022021022222222222022222002212220012222212121112212022222022022222222220002222222212220222220011222222222221221222112222212221222221121112222120120001112021222212222222221121112211221202222222100102222122222022222222222222022212220212211222222120222022222220220112220222202221222222122102222110221110222020022222222222010120102201221112222222212120202020222122122222222220212202220212211222200122202222122222221102111022221220221222220202122102120200222020022202222222202122012212021122222202211202222220222022022222222220222222222202202222201202220022122221221202200122220221220222220022122111021212012021122212222222210222122220222222221212010112222021222122022222222221002202221212211222202211212222122220220022212122201220222220222212122001020102212120122222202222221021012222220222222212102200212221222022022222222221202212220202221222200112212022122222220022122122200222221222220022122121021122112120022212202212101220222212021002220222011222212221222122022222222222202222221012200222200210220222022222222012100022202221222222022002222200020010012021122222202222010221012222222202222202122220202221222022122222222220022202220002201222212120202012122222222002212222201220220220121212221220222122122120022202212202110122022222022122222212101111212121222122122222222221112202221202210222002022202112122221221012122022220222222220122122120012020102202222022202202222201222022211121202220212012110202120222122122222222222212212220002210222120021222012222221221012102222221222222222222122220100220212202022122222222212112121202222222102221222110000222120222122222222222220112212220202201222200111200122122222221022221022210220222220122022021120121112112022222222222212112020202202020002220202211000222021222122122222222220112202222122221222012201210112122221221212012022220222220222220112021222121102022222022222220212022222112210120222221222212210212221222222122222222222112202221002210221121202201112022222222012221222220221222220221112221211022121122120022202220222100121002202221112222222102211202222222022122222222221012212221112221220111000211022122220221022121222202220222220220002020002221202122222022202202212110121102200021202222222002200222022222222222222222222222212221112211222201111220012222220222122211222202220222221121222120022220220002222122222201202100020202220021202222212100002222120222222222222222221102212222102210222120110220002222220220012101022202222222222021112021012021201012020222210212222002020122222221122221222222101222221222122022222222220021202221122212220110111222212022221220212100222200220020221121022022221122011112022022211210202202120122200221112210202002100002220222122012222222221221212222112201222000200211122222220220022211122212221122221022212022012221010012221022201220222112121222211120222202212020222112222222122212222222221021202220202202220212110212112222220222022102022221220020222222212120010120122102020022220211202001121222220122222201222020022212020222222112222222221210222221112222222002122220202222222220012212122202220121220121222220102122000112020022211222212221122012222020112221222221100212220222222202222222220011222222022212221001101200102222221222022121022201221121221121212222101021122102021122201221222220222022202021212211222210201012120222122022222222220101202220002212220020200202012222220222012002112222220122220122222122211121111202021022212212222211021112212021222222212000001112222222022202222222221202222220000220221022010221112022221221202101022222221221221020012220202120021222121122221201222010221202201221202210202220121112222222222112222222222120202220221201222201120202212122222221202200212220221221222121122022112122000112022222210221222022021022201120002212202112201102022222222202222222221212212220001202221200001221212022222220212112002201220121220121012121201220221002221122212202202100122002202022022200222220002122022222122102222222221101222222021212221112020201012022220220122220202221220022220221002020201221201202121222202212222010120112221122012210212021212212022222022012222222221122212221102211222212020202022122220221112222122220221021201220122220012120102012122022201212202102220202200222202210222022120022120222222222222222222122202222111200221112122210122022220220212201202202222122201121102121001220202002222122200222202010220012222122122222202021211002120222122002222222221101202222222222221220212222112021222220212101212221221222220121102121100022221102222022202022222010222212202222222221222121001022020222022102222222222211222221102212220012022222122221221220022001222202221120201021122221021022022022022022211102202210122112210220022212202220002222222222222002222222220102201220011212220211001220222221222221202101112201220221221022012220221121012222120222210220202101120022202222012201222011102122221122022002222222212221012220110221221221112202112000220221202220202210221222220120212220101121021112122122221221212000220022210020212200212022022112222022122122222222212201120222120211222121121220012022220220212012202200222222202220100021022220220002220122222211222221221002101222212211202001120112020122022012222222201012220222221210221010021222022211220222022122122202220220200021110020112020101002020222202020202112222002200020022220222101112202020122222202220222201110122221100221220010201222222112220222112021002200021220200222210122021122011102222222202102012012121012222221002221202000021022022122022202220222200221011222220222221121101220102202222221202102022211120120222221211221112221121112222122212221202211220122001001112210212122212222020022002002222222202212111221100222222012112212022110222221012001012212222122212021122021011221010022222122200100020211021022111222222221212012201112121102102012220222212002011221011222221201220221002000222220002101112200020122202221120222010220212212021022200120200120222102011200102210212212201202020102102002222222220211121220111211222222000200202122220222002022002222120122210120112222111221011022022222212000120021021102221021112202212002120202120202012122221222211122100221112201220010222211022112222222022101022212120221220121201020200020000222120022201201021100020111022200022200202102101022020202122002220222201122111212021200220201022201022201220222012112102220222220201221202022200120121022120222210222000102021010200120102221202110210212022222012002221222211112101202011202221000210211102222222221012212122220120021210221212222020221012102022122201212212211121020022120122200212200112202120122212022221222212001221210212221221001222220202001221222022222112212222122202122220022000222221002120022210022120000021200011102222201222120011102021012122112220222222212202220202222220200020210002011220022012121202222120222212121222020102020120212220022102020201202022210200020222212212221010002022102002202220222221121211211101212222102201210012121220221002220222220222122221221111221010020200022021222101210110021122020001120202220222210012022020012012212222222220211220201102210220001002221222012220022102211112210221120222101120022000220222102022222121011220011020020211120122200212022012212021002122212221222201201200212000222021200021222002221220022002202002201122220221112110222221021221022120122212101110112220122121111222202222111101022121022122202222222201122201221120212120212110220122100220221220212212221121221200101212222021022012012021222220100220121112211210100012210212122102122122202212002220222202212011210101220222222222212022110222222022220022212222021200222102020011121122222022022220200120221211220002212022200012202110212222022212202222222211201000202120212121020211221112000220122010011222211220022211200112020121122121112021222111211111020121101020110022220002210012112022102212110222222220101121220021212022012020210002102220120112012102200121022212012121122011120000012122122212100200022211111102210022210002211202112121202102001220222201020012200111201121022202200012021222020220120002201122022201002102002221220211222220022001021012020221000010110202211122121022112022122112110222222210101020221220202021102110221202002222020002011222222121221211011112020020221002012020122021200120222021110002022202220112000200022122022102222220222202100210212202200022000122220202100222120000122212210121211212021221212001220000022222122221011002012212222011212222222212222012122120212012201222220201102001211010220221022020222012212221022021121022210122200110221002220112121212002022022111210022010201200202000110212122202011102022212002102222220200122212222200222220011211222122222200222210112122210022121112000222012111220121222220022121012222021022000220022201221202000022212221012202102220222210000122222121221122121210211122021212021220100102211122001210020100211101120100102221122102210120122222200110010021201022020000202020102212001220222200122110222101221120100000201122210221221222111002221121111201012000022012121222112022222020101111010100212121120202202102001112022020202012011222222222210002222001220020011210210202020201220001122222212020002002012012222122222000222020222202211100222021120021001002212002210102122021202002101201221200100011220110002102021120000201221012112111020101012011021212010122212010201221021011010111120101202121021120000002012211001001110110210100021010100002200112002220 \ No newline at end of file diff --git a/008/main.py b/008/main.py deleted file mode 100644 index 4392f01fbf853bd3150e4c2da539b24cfb39e143..0000000000000000000000000000000000000000 --- a/008/main.py +++ /dev/null @@ -1,57 +0,0 @@ -import itertools - - -W = 25 -H = 6 - - -def get_layers(data): - layers = [] - - while len(data): - layers.append(data[:W * H]) - data = data[W * H:] - - return layers - - -def main(): - data = open('input').read() - layers = get_layers(data) - - top = layers[0] - result = [] - - for i, d in enumerate(top): - if d in ['1', '0']: - result.append(d) - else: - o = None - c = 1 - while True: - l = layers[c] - candidate = l[i] - if candidate == '2': - c += 1 - continue - else: - result.append(candidate) - break - - a = [] - - for r in result: - if r == '1': - a.append('😀') - else: - a.append('🖤') - - for x in itertools.count(step=W): - if x >= len(result): - break - row = a[x:x+W] - print(''.join(row)) - - -if __name__ == '__main__': - main() diff --git a/009/input b/009/input deleted file mode 100644 index 544a9b45695a847d43eeaade6a9598d9a1d22fb4..0000000000000000000000000000000000000000 --- a/009/input +++ /dev/null @@ -1 +0,0 @@ -1102,34463338,34463338,63,1007,63,34463338,63,1005,63,53,1101,0,3,1000,109,988,209,12,9,1000,209,6,209,3,203,0,1008,1000,1,63,1005,63,65,1008,1000,2,63,1005,63,904,1008,1000,0,63,1005,63,58,4,25,104,0,99,4,0,104,0,99,4,17,104,0,99,0,0,1101,0,30,1016,1101,37,0,1005,1101,362,0,1023,1101,0,20,1014,1101,39,0,1013,1102,34,1,1007,1101,682,0,1027,1102,664,1,1025,1102,1,655,1028,1101,0,26,1002,1102,1,38,1015,1101,669,0,1024,1101,0,28,1017,1102,1,21,1000,1101,0,27,1012,1102,1,29,1008,1102,1,23,1019,1101,0,24,1011,1101,685,0,1026,1102,646,1,1029,1102,1,369,1022,1101,0,31,1003,1102,1,36,1001,1101,0,0,1020,1102,1,35,1009,1101,32,0,1010,1101,0,1,1021,1102,33,1,1004,1101,22,0,1006,1102,1,25,1018,109,14,1205,6,197,1001,64,1,64,1105,1,199,4,187,1002,64,2,64,109,-4,21107,40,39,9,1005,1019,219,1001,64,1,64,1105,1,221,4,205,1002,64,2,64,109,9,1206,1,239,4,227,1001,64,1,64,1106,0,239,1002,64,2,64,109,-9,2101,0,-8,63,1008,63,26,63,1005,63,261,4,245,1106,0,265,1001,64,1,64,1002,64,2,64,109,-6,2108,37,1,63,1005,63,287,4,271,1001,64,1,64,1105,1,287,1002,64,2,64,109,15,21108,41,44,-2,1005,1017,307,1001,64,1,64,1106,0,309,4,293,1002,64,2,64,109,-16,1207,1,34,63,1005,63,327,4,315,1105,1,331,1001,64,1,64,1002,64,2,64,109,8,1208,-9,29,63,1005,63,347,1106,0,353,4,337,1001,64,1,64,1002,64,2,64,109,4,2105,1,8,1001,64,1,64,1105,1,371,4,359,1002,64,2,64,109,-22,1201,9,0,63,1008,63,27,63,1005,63,391,1106,0,397,4,377,1001,64,1,64,1002,64,2,64,109,18,21107,42,43,5,1005,1016,415,4,403,1106,0,419,1001,64,1,64,1002,64,2,64,109,-8,1201,2,0,63,1008,63,37,63,1005,63,441,4,425,1105,1,445,1001,64,1,64,1002,64,2,64,109,27,1205,-9,463,4,451,1001,64,1,64,1106,0,463,1002,64,2,64,109,-1,1206,-8,475,1105,1,481,4,469,1001,64,1,64,1002,64,2,64,109,-6,21101,43,0,-8,1008,1015,43,63,1005,63,507,4,487,1001,64,1,64,1106,0,507,1002,64,2,64,109,-15,2101,0,-3,63,1008,63,35,63,1005,63,531,1001,64,1,64,1106,0,533,4,513,1002,64,2,64,109,-2,2102,1,-6,63,1008,63,18,63,1005,63,553,1105,1,559,4,539,1001,64,1,64,1002,64,2,64,109,7,21102,44,1,3,1008,1016,44,63,1005,63,581,4,565,1105,1,585,1001,64,1,64,1002,64,2,64,109,-11,1202,7,1,63,1008,63,34,63,1005,63,609,1001,64,1,64,1105,1,611,4,591,1002,64,2,64,109,6,1202,1,1,63,1008,63,35,63,1005,63,637,4,617,1001,64,1,64,1106,0,637,1002,64,2,64,109,16,2106,0,4,4,643,1001,64,1,64,1106,0,655,1002,64,2,64,109,-1,2105,1,1,4,661,1106,0,673,1001,64,1,64,1002,64,2,64,109,5,2106,0,-1,1105,1,691,4,679,1001,64,1,64,1002,64,2,64,109,-24,1208,-2,26,63,1005,63,709,4,697,1105,1,713,1001,64,1,64,1002,64,2,64,109,-10,2102,1,6,63,1008,63,21,63,1005,63,735,4,719,1105,1,739,1001,64,1,64,1002,64,2,64,109,25,21108,45,45,-9,1005,1010,757,4,745,1106,0,761,1001,64,1,64,1002,64,2,64,109,-12,1207,-7,20,63,1005,63,777,1106,0,783,4,767,1001,64,1,64,1002,64,2,64,109,-13,2108,22,6,63,1005,63,799,1106,0,805,4,789,1001,64,1,64,1002,64,2,64,109,17,21102,46,1,0,1008,1011,45,63,1005,63,825,1105,1,831,4,811,1001,64,1,64,1002,64,2,64,109,-6,2107,21,1,63,1005,63,849,4,837,1105,1,853,1001,64,1,64,1002,64,2,64,109,-3,2107,27,0,63,1005,63,873,1001,64,1,64,1105,1,875,4,859,1002,64,2,64,109,12,21101,47,0,0,1008,1014,48,63,1005,63,899,1001,64,1,64,1105,1,901,4,881,4,64,99,21102,27,1,1,21101,0,915,0,1105,1,922,21201,1,42931,1,204,1,99,109,3,1207,-2,3,63,1005,63,964,21201,-2,-1,1,21101,942,0,0,1106,0,922,21202,1,1,-1,21201,-2,-3,1,21102,1,957,0,1106,0,922,22201,1,-1,-2,1106,0,968,22101,0,-2,-2,109,-3,2106,0,0 \ No newline at end of file diff --git a/009/main.py b/009/main.py deleted file mode 100644 index 90f4b62e718ffe807e8ed01e1920854f35dfa86c..0000000000000000000000000000000000000000 --- a/009/main.py +++ /dev/null @@ -1,322 +0,0 @@ -import re -import copy -import itertools - -opcode = re.compile(r'(\d{2})') - -OPS = ['01', '02', '03', '04', '99'] -NEEDS_INPUT = 'NI' -HALT = 'H' - - -def rev(s): - r = [] - for l in s: - r.append(l) - - r.reverse() - return ''.join(r) - - - -class NonHaltException(Exception): - pass - - -def add(a, b): - return a + b - - -def mult(a, b): - return a * b - - -def intcode(input_list, *input_codes, cur=0): - input_list += [0 for _ in range(10_000)] - input_codes = list(input_codes) - input_codes.reverse() - - outputs = [] - - # if input_list[0] == 3: - # input_list[input_list[1]] = input_codes.pop() - # cur += 2 - - rel = 0 - while True: - op_value = input_list[cur] - op_value_string = '000000' + str(op_value) - - op = op_value_string[-2:] - - front = op_value_string[0:-2] - front = rev(front) - - if op == '99': - break - - if op == '01': - # 0 po, like before - # 1 im - a_mode = front[0] - b_mode = front[1] - c_mode = front[2] - - if a_mode == '0': - a = input_list[input_list[cur + 1]] - elif a_mode == '1': - a = input_list[cur + 1] - elif a_mode == '2': - a = input_list[rel + input_list[cur + 1]] - else: - # relative - raise NotImplementedError - - if b_mode == '0': - b = input_list[input_list[cur + 2]] - elif b_mode == '1': - b = input_list[cur + 2] - elif b_mode == '2': - b = input_list[rel + input_list[cur + 2]] - else: - raise NotImplementedError - - if c_mode == '2': - result_pos = rel + input_list[cur + 3] - else: - result_pos = input_list[cur + 3] - - input_list[result_pos] = add(a, b) - cur += 4 - continue - - if op == '02': - # 0 po, like before - # 1 im - a_mode = front[0] - b_mode = front[1] - c_mode = front[2] - - if a_mode == '0': - a = input_list[input_list[cur + 1]] - elif a_mode == '1': - a = input_list[cur + 1] - elif a_mode == '2': - a = input_list[rel + input_list[cur + 1]] - else: - raise NotImplementedError - - if b_mode == '0': - b = input_list[input_list[cur + 2]] - elif b_mode == '1': - b = input_list[cur + 2] - elif b_mode == '2': - b = input_list[rel + input_list[cur + 2]] - else: - raise NotImplementedError - - if c_mode == '2': - result_pos = rel + input_list[cur + 3] - else: - result_pos = input_list[cur + 3] - - input_list[result_pos] = mult(a, b) - cur += 4 - continue - - if op == '03': - - if not input_codes: - return { - 'output': outputs, - 'state': NEEDS_INPUT, - 'memory': input_list, - 'cur': cur, - } - - mode = front[0] - - if mode == '0': - input_list[input_list[cur + 1]] = input_codes.pop() - elif mode == '1': - input_list[cur + 1] = input_codes.pop() - elif mode == '2': - input_list[rel + input_list[cur + 1]] = input_codes.pop() - else: - raise NotImplementedError - - # input_list[input_list[cur + 1]] = input_codes.pop() - cur += 2 - continue - - if op == '04': - mode = front[0] - if mode == '0': - o = input_list[input_list[cur + 1]] - elif mode == '1': - o = input_list[cur + 1] - elif mode == '2': - o = input_list[rel + input_list[cur + 1]] - else: - raise NotImplementedError - - outputs.append(o) - cur += 2 - continue - - if op == '05': - mode = front[0] - if mode == '0': - v = input_list[input_list[cur + 1]] - elif mode == '1': - v = input_list[cur + 1] - elif mode == '2': - v = input_list[rel + input_list[cur + 1]] - else: - raise NotImplementedError - - if v is not 0: - m2 = front[1] - if m2 == '0': - cur = input_list[input_list[cur + 2]] - elif m2 == '1': - cur = input_list[cur + 2] - elif m2 == '2': - cur = input_list[rel + input_list[cur + 2]] - else: - raise NotImplementedError - else: - cur += 3 - - if op == '06': - mode = front[0] - if mode == '0': - v = input_list[input_list[cur + 1]] - elif mode == '1': - v = input_list[cur + 1] - elif mode == '2': - v = input_list[rel + input_list[cur + 1]] - else: - raise NotImplementedError - - if v == 0: - m2 = front[1] - if m2 == '0': - cur = input_list[input_list[cur + 2]] - elif m2 == '1': - cur = input_list[cur + 2] - elif m2 == '2': - cur = input_list[rel + input_list[cur + 2]] - else: - raise NotImplementedError - else: - cur += 3 - - if op == '07': - # 0 po, like before - # 1 im - a_mode = front[0] - b_mode = front[1] - c_mode = front[2] - - if a_mode == '0': - a = input_list[input_list[cur + 1]] - elif a_mode == '1': - a = input_list[cur + 1] - elif a_mode == '2': - a = input_list[rel + input_list[cur + 1]] - else: - raise NotImplementedError - - if b_mode == '0': - b = input_list[input_list[cur + 2]] - elif b_mode == '1': - b = input_list[cur + 2] - elif b_mode == '2': - b = input_list[rel + input_list[cur + 2]] - else: - raise NotImplementedError - - if c_mode == '2': - result_pos = rel + input_list[cur + 3] - else: - result_pos = input_list[cur + 3] - - if a < b: - input_list[result_pos] = 1 - else: - input_list[result_pos] = 0 - - cur += 4 - continue - - if op == '08': - # 0 po, like before - # 1 im - a_mode = front[0] - b_mode = front[1] - c_mode = front[2] - - if a_mode == '0': - a = input_list[input_list[cur + 1]] - elif a_mode == '1': - a = input_list[cur + 1] - elif a_mode == '2': - a = input_list[rel + input_list[cur + 1]] - else: - raise NotImplementedError - - if b_mode == '0': - b = input_list[input_list[cur + 2]] - elif b_mode == '1': - b = input_list[cur + 2] - elif b_mode == '2': - b = input_list[rel + input_list[cur + 2]] - else: - raise NotImplementedError - - if c_mode == '2': - result_pos = rel + input_list[cur + 3] - else: - result_pos = input_list[cur + 3] - - if a == b: - input_list[result_pos] = 1 - else: - input_list[result_pos] = 0 - - cur += 4 - continue - - if op == '09': - mode = front[0] - if mode == '0': - v = input_list[input_list[cur + 1]] - elif mode == '1': - v = input_list[cur + 1] - elif mode == '2': - v = input_list[rel + input_list[cur + 1]] - else: - raise NotImplementedError - - rel += v - cur += 2 - - return { - 'output': outputs, - 'state': HALT, - 'memory': input_list, - 'cur': None - } - - -def main(): - data = open('input').read().split(',') - data = list(map(int, data)) - - out = intcode(data, 2) - print(out['output']) - - -if __name__ == '__main__': - main() diff --git a/010/main.py b/010/main.py deleted file mode 100644 index 084bc2a1fbf1eb02c56dafd24fc78bd3705e6eec..0000000000000000000000000000000000000000 --- a/010/main.py +++ /dev/null @@ -1,166 +0,0 @@ -from copy import deepcopy -from math import gcd, atan2, pi -from itertools import groupby - - -PUZZLE = """.###..#######..####..##...# -########.#.###...###.#....# -###..#...#######...#..####. -.##.#.....#....##.#.#.....# -###.#######.###..##......#. -#..###..###.##.#.#####....# -#.##..###....#####...##.##. -####.##..#...#####.#..###.# -#..#....####.####.###.#.### -#..#..#....###...#####..#.. -##...####.######....#.####. -####.##...###.####..##....# -#.#..#.###.#.##.####..#...# -..##..##....#.#..##..#.#..# -##.##.#..######.#..#..####. -#.....#####.##........##### -###.#.#######..#.#.##..#..# -###...#..#.#..##.##..#####. -.##.#..#...#####.###.##.##. -...#.#.######.#####.#.####. -#..##..###...###.#.#..#.#.# -.#..#.#......#.###...###..# -#.##.#.#..#.#......#..#..## -.##.##.##.#...##.##.##.#..# -#.###.#.#...##..#####.###.# -#.####.#..#.#.##.######.#.. -.#.#####.##...#...#.##...#.""" - - -def get_distance_and_slope(ast1, ast2): - x1, y1 = ast1 - x2, y2 = ast2 - - x_, y_ = x2 - x1, y2 - y1 - x_, y_ = x1 - x2, y1 - y2 - - g = gcd(x_, y_) - - slope = int(x_ / g), int(y_ / g) - distance = float(x_ ** 2 + y_ ** 2) - return None, distance - - -def parse_map(data): - result = [] - - for line_i, line in enumerate(data.splitlines()): - for c_i, c in enumerate(line): - if c == '#': - result.append((c_i, line_i)) - - return result - - -def get_visible_asteroid_count(ast, asteroid_map): - data = [] - - for other_ast in asteroid_map: - if ast == other_ast: - continue - - slope, dist = get_distance_and_slope(ast, other_ast) - data.append({ - 'ast': ast, - 'distance': dist - }) - - data = sorted(data, key=lambda x: x['slope']) - grouped = groupby(data, lambda x: x['slope']) - visible = 0 - - for k, g in grouped: - visible += 1 - - return visible - - -def rem(self, rhs): - if rhs == 0: - raise Exception('zerooo') - - r = self % rhs - - if r < 0: - if rhs < 0: - return r - rhs - else: - return r + rhs - else: - return r - - -def get_angle(a, b): - y = b[0] - a[0] - x = a[1] - b[1] - angle = atan2(y, x) - - a = rem(angle, pi * 2.0) - prec = 100000 - - return int(a * prec) - - -def get_visible_asteroids_by_angle(ast, asteroid_map): - asteroids = [] - - for other_ast in asteroid_map: - if ast == other_ast: - continue - - _, dist = get_distance_and_slope(ast, other_ast) - asteroids.append({ - 'ast': other_ast, - # 'angle': get_angle(ast, other_ast), - 'distance': dist - }) - - asteroids = sorted(asteroids, key=lambda x: x['distance']) - sort_keys = {} - things = [] - - for i, asteroid in enumerate(asteroids): - angle = get_angle(ast, asteroid['ast']) - previous_asteroids = filter( - lambda x: angle == get_angle(ast, x['ast']), - asteroids[:i] - ) - rank = len(list(previous_asteroids)) - - s = "%s-%s" % asteroid['ast'] - sort_keys[s] = (rank, angle, asteroid['ast']) - things.append(s) - - things = sorted(things, key=lambda x: sort_keys[x]) - two_hundredth = things[199] - _, _, ast = sort_keys[two_hundredth] - print(ast[0] * 100 + ast[1]) - - -def find_best_asteroid(asteroid_map): - x = {} - for ast in asteroid_map: - num_visible_asts = get_visible_asteroid_count(ast, deepcopy(asteroid_map)) - x[num_visible_asts] = ast - - keys = list(x.keys()) - keys.sort() - keys.reverse() - return x[keys[0]], keys[0] - - -def main(): - asteroid_map = parse_map(PUZZLE) - # station, visible = find_best_asteroid(asteroid_map) - station = (17, 23) - - get_visible_asteroids_by_angle(station, asteroid_map) - - -if __name__ == '__main__': - main() diff --git a/011/input b/011/input deleted file mode 100644 index cdf4d55c3d83a12d74c00403f305d5efcb7bbd8d..0000000000000000000000000000000000000000 --- a/011/input +++ /dev/null @@ -1 +0,0 @@ -3,8,1005,8,321,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1002,8,1,29,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,1002,8,1,50,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1001,8,0,73,1,1105,16,10,2,1004,8,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1002,8,1,103,1006,0,18,1,105,14,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,102,1,8,131,1006,0,85,1,1008,0,10,1006,0,55,2,104,4,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1001,8,0,168,2,1101,1,10,1006,0,14,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,196,1006,0,87,1006,0,9,1,102,20,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,1001,8,0,228,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,1002,8,1,250,2,5,0,10,2,1009,9,10,2,107,17,10,1006,0,42,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,1001,8,0,287,2,102,8,10,1006,0,73,1006,0,88,1006,0,21,101,1,9,9,1007,9,925,10,1005,10,15,99,109,643,104,0,104,1,21102,1,387353256856,1,21101,0,338,0,1105,1,442,21101,936332866452,0,1,21101,349,0,0,1105,1,442,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,0,179357024347,1,21101,0,396,0,1105,1,442,21102,1,29166144659,1,21102,407,1,0,1105,1,442,3,10,104,0,104,0,3,10,104,0,104,0,21102,1,718170641252,1,21102,430,1,0,1106,0,442,21101,825012151040,0,1,21102,441,1,0,1106,0,442,99,109,2,21202,-1,1,1,21102,1,40,2,21102,1,473,3,21102,463,1,0,1105,1,506,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,468,469,484,4,0,1001,468,1,468,108,4,468,10,1006,10,500,1102,1,0,468,109,-2,2105,1,0,0,109,4,1202,-1,1,505,1207,-3,0,10,1006,10,523,21101,0,0,-3,22101,0,-3,1,21202,-2,1,2,21102,1,1,3,21102,1,542,0,1105,1,547,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,570,2207,-4,-2,10,1006,10,570,22102,1,-4,-4,1105,1,638,22102,1,-4,1,21201,-3,-1,2,21202,-2,2,3,21101,0,589,0,1106,0,547,22102,1,1,-4,21101,1,0,-1,2207,-4,-2,10,1006,10,608,21102,0,1,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,630,21202,-1,1,1,21102,630,1,0,105,1,505,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2106,0,0 \ No newline at end of file diff --git a/011/main.py b/011/main.py deleted file mode 100644 index 45adc445a2957ef1bb583b26706d7c517136a485..0000000000000000000000000000000000000000 --- a/011/main.py +++ /dev/null @@ -1,390 +0,0 @@ -import re -import copy -import itertools - -opcode = re.compile(r'(\d{2})') - -OPS = ['01', '02', '03', '04', '99'] -NEEDS_INPUT = 'NI' -HALT = 'H' - - -def rev(s): - r = [] - for l in s: - r.append(l) - - r.reverse() - return ''.join(r) - - - -class NonHaltException(Exception): - pass - - -def add(a, b): - return a + b - - -def mult(a, b): - return a * b - - -def intcode(input_list, *input_codes, cur=0): - input_list += [0 for _ in range(10_000)] - input_codes = list(input_codes) - input_codes.reverse() - - outputs = [] - - # if input_list[0] == 3: - # input_list[input_list[1]] = input_codes.pop() - # cur += 2 - - rel = 0 - while True: - op_value = input_list[cur] - op_value_string = '000000' + str(op_value) - - op = op_value_string[-2:] - - front = op_value_string[0:-2] - front = rev(front) - - if op == '99': - break - - if op == '01': - # 0 po, like before - # 1 im - a_mode = front[0] - b_mode = front[1] - c_mode = front[2] - - if a_mode == '0': - a = input_list[input_list[cur + 1]] - elif a_mode == '1': - a = input_list[cur + 1] - elif a_mode == '2': - a = input_list[rel + input_list[cur + 1]] - else: - # relative - raise NotImplementedError - - if b_mode == '0': - b = input_list[input_list[cur + 2]] - elif b_mode == '1': - b = input_list[cur + 2] - elif b_mode == '2': - b = input_list[rel + input_list[cur + 2]] - else: - raise NotImplementedError - - if c_mode == '2': - result_pos = rel + input_list[cur + 3] - else: - result_pos = input_list[cur + 3] - - input_list[result_pos] = add(a, b) - cur += 4 - continue - - if op == '02': - # 0 po, like before - # 1 im - a_mode = front[0] - b_mode = front[1] - c_mode = front[2] - - if a_mode == '0': - a = input_list[input_list[cur + 1]] - elif a_mode == '1': - a = input_list[cur + 1] - elif a_mode == '2': - a = input_list[rel + input_list[cur + 1]] - else: - raise NotImplementedError - - if b_mode == '0': - b = input_list[input_list[cur + 2]] - elif b_mode == '1': - b = input_list[cur + 2] - elif b_mode == '2': - b = input_list[rel + input_list[cur + 2]] - else: - raise NotImplementedError - - if c_mode == '2': - result_pos = rel + input_list[cur + 3] - else: - result_pos = input_list[cur + 3] - - input_list[result_pos] = mult(a, b) - cur += 4 - continue - - if op == '03': - - if not input_codes: - return { - 'output': outputs, - 'state': NEEDS_INPUT, - 'memory': input_list, - 'cur': cur, - } - - mode = front[0] - - if mode == '0': - input_list[input_list[cur + 1]] = input_codes.pop() - elif mode == '1': - input_list[cur + 1] = input_codes.pop() - elif mode == '2': - input_list[rel + input_list[cur + 1]] = input_codes.pop() - else: - raise NotImplementedError - - # input_list[input_list[cur + 1]] = input_codes.pop() - cur += 2 - continue - - if op == '04': - mode = front[0] - if mode == '0': - o = input_list[input_list[cur + 1]] - elif mode == '1': - o = input_list[cur + 1] - elif mode == '2': - o = input_list[rel + input_list[cur + 1]] - else: - raise NotImplementedError - - outputs.append(o) - cur += 2 - continue - - if op == '05': - mode = front[0] - if mode == '0': - v = input_list[input_list[cur + 1]] - elif mode == '1': - v = input_list[cur + 1] - elif mode == '2': - v = input_list[rel + input_list[cur + 1]] - else: - raise NotImplementedError - - if v is not 0: - m2 = front[1] - if m2 == '0': - cur = input_list[input_list[cur + 2]] - elif m2 == '1': - cur = input_list[cur + 2] - elif m2 == '2': - cur = input_list[rel + input_list[cur + 2]] - else: - raise NotImplementedError - else: - cur += 3 - - if op == '06': - mode = front[0] - if mode == '0': - v = input_list[input_list[cur + 1]] - elif mode == '1': - v = input_list[cur + 1] - elif mode == '2': - v = input_list[rel + input_list[cur + 1]] - else: - raise NotImplementedError - - if v == 0: - m2 = front[1] - if m2 == '0': - cur = input_list[input_list[cur + 2]] - elif m2 == '1': - cur = input_list[cur + 2] - elif m2 == '2': - cur = input_list[rel + input_list[cur + 2]] - else: - raise NotImplementedError - else: - cur += 3 - - if op == '07': - # 0 po, like before - # 1 im - a_mode = front[0] - b_mode = front[1] - c_mode = front[2] - - if a_mode == '0': - a = input_list[input_list[cur + 1]] - elif a_mode == '1': - a = input_list[cur + 1] - elif a_mode == '2': - a = input_list[rel + input_list[cur + 1]] - else: - raise NotImplementedError - - if b_mode == '0': - b = input_list[input_list[cur + 2]] - elif b_mode == '1': - b = input_list[cur + 2] - elif b_mode == '2': - b = input_list[rel + input_list[cur + 2]] - else: - raise NotImplementedError - - if c_mode == '2': - result_pos = rel + input_list[cur + 3] - else: - result_pos = input_list[cur + 3] - - if a < b: - input_list[result_pos] = 1 - else: - input_list[result_pos] = 0 - - cur += 4 - continue - - if op == '08': - # 0 po, like before - # 1 im - a_mode = front[0] - b_mode = front[1] - c_mode = front[2] - - if a_mode == '0': - a = input_list[input_list[cur + 1]] - elif a_mode == '1': - a = input_list[cur + 1] - elif a_mode == '2': - a = input_list[rel + input_list[cur + 1]] - else: - raise NotImplementedError - - if b_mode == '0': - b = input_list[input_list[cur + 2]] - elif b_mode == '1': - b = input_list[cur + 2] - elif b_mode == '2': - b = input_list[rel + input_list[cur + 2]] - else: - raise NotImplementedError - - if c_mode == '2': - result_pos = rel + input_list[cur + 3] - else: - result_pos = input_list[cur + 3] - - if a == b: - input_list[result_pos] = 1 - else: - input_list[result_pos] = 0 - - cur += 4 - continue - - if op == '09': - mode = front[0] - if mode == '0': - v = input_list[input_list[cur + 1]] - elif mode == '1': - v = input_list[cur + 1] - elif mode == '2': - v = input_list[rel + input_list[cur + 1]] - else: - raise NotImplementedError - - rel += v - cur += 2 - - return { - 'output': outputs, - 'state': HALT, - 'memory': input_list, - 'cur': None - } - - -def new_direction(current_direction, turn): - if turn == 0: # left - if current_direction == 0: - return 3 - else: - return current_direction - 1 - if turn == 1: - if current_direction == 3: - return 0 - else: - return current_direction + 1 - - -def new_position(current_position, direction): - x, y = current_position - - if direction == 0: - return (x, y + 1) - - if direction == 1: - return (x + 1, y) - - if direction == 2: - return (x, y - 1) - - if direction == 3: - return (x -1, y) - - raise Exception('wrong direction') - - -def main(): - data = open('input').read().split(',') - data = list(map(int, data)) - - current_position = (0, 0) - current_direction = 0 - memory = copy.deepcopy(data) - a = {} - cur = 0 - - # 0 up - # 1 right - # 2 down - # 3 left - # direction = 0 - - - - # read color - # say what to paint - # turn - # step - - while True: - print(current_position) - color = a.get(current_position, 0) - - out = intcode(memory, color, cur=cur) - - if out['state'] == HALT: - break - - color, turn = out['output'] - - a[current_position] = color - current_direction = new_direction(current_direction, turn) - current_position = new_position(current_position, current_direction) - memory = out['memory'] - cur = out['cur'] - - keys = set(list(a.keys())) - print(len(keys)) - - - -if __name__ == '__main__': - main() diff --git a/2019/001/input b/2019/001/input new file mode 100644 index 0000000000000000000000000000000000000000..154bf896f12156606867230657600df38d9b46b6 --- /dev/null +++ b/2019/001/input @@ -0,0 +1,100 @@ +81157 +80969 +113477 +81295 +70537 +90130 +123804 +94276 +139327 +123719 +107814 +122142 +61204 +135309 +62810 +85750 +132568 +76450 +122948 +124649 +102644 +80055 +60517 +125884 +125708 +99051 +137158 +100450 +55239 +66758 +123848 +88711 +113047 +125528 +59285 +103978 +93047 +98038 +143019 +92031 +54353 +115597 +105629 +80411 +134966 +135473 +77357 +65776 +71096 +66926 +97853 +80349 +141914 +127221 +102492 +143587 +111493 +84711 +59826 +135652 +103334 +138211 +65088 +82244 +95011 +78760 +56691 +62070 +146134 +81650 +76904 +98838 +89629 +59950 +50390 +78616 +99731 +53831 +81273 +103980 +58485 +137684 +142457 +111050 +141916 +55567 +141945 +100794 +136425 +77911 +137114 +77450 +132048 +143066 +136805 +114135 +61565 +67286 +85512 +137493 diff --git a/2019/001/main.py b/2019/001/main.py new file mode 100644 index 0000000000000000000000000000000000000000..458b0dd0ffb051cadaa408ea2dc5fa2377250491 --- /dev/null +++ b/2019/001/main.py @@ -0,0 +1,41 @@ +def round_down(value): + integer = int(value) + + if integer == value: + return value + + return integer + + +def calculate(value): + if value <= 0: + return 0 + + v = round_down(value / 3.0) - 2 + + if v <= 0: + return 0 + + return v + calculate(v) + + +def main(): + print(calculate(1969)) + print('---') + assert calculate(1969) == 966 + print(calculate(100756)) + assert calculate(100756) == 50346 + + + data = open('input').read().splitlines() + total = 0 + + for line in data: + required = calculate(int(line)) + total += required + + print(total) + + +if __name__ == '__main__': + main() diff --git a/2019/002/main.py b/2019/002/main.py new file mode 100644 index 0000000000000000000000000000000000000000..9baaeb856e425b1c4169c85e1d21955816e825be --- /dev/null +++ b/2019/002/main.py @@ -0,0 +1,75 @@ +import copy + + +OPS = [1, 2, 99] + + +class NonHaltException(Exception): + pass + + +def add(a, b): + return a + b + + +def mult(a, b): + return a * b + + +def intcode(input_list, a, b): + # fix it + input_list[1] = a + input_list[2] = b + + cur = 0 + + while True: + op = input_list[cur] + + if op not in OPS: + raise NonHaltException + + if op == 99: + break + + f = None + + if op == 1: + f = add + + if op == 2: + f = mult + + if not f: + continue + + a, b = input_list[cur + 1], input_list[cur + 2] + result_pos = input_list[cur + 3] + input_list[result_pos] = f(input_list[a], input_list[b]) + cur += 4 + + return input_list[0] + + +def main(): + + input_list = [1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,2,6,19,23,1,23,5,27,1,27,13,31,2,6,31,35,1,5,35,39,1,39,10,43,2,6,43,47,1,47,5,51,1,51,9,55,2,55,6,59,1,59,10,63,2,63,9,67,1,67,5,71,1,71,5,75,2,75,6,79,1,5,79,83,1,10,83,87,2,13,87,91,1,10,91,95,2,13,95,99,1,99,9,103,1,5,103,107,1,107,10,111,1,111,5,115,1,115,6,119,1,119,10,123,1,123,10,127,2,127,13,131,1,13,131,135,1,135,10,139,2,139,6,143,1,143,9,147,2,147,6,151,1,5,151,155,1,9,155,159,2,159,6,163,1,163,2,167,1,10,167,0,99,2,14,0,0] + + expected = 19690720 + + for a in range(0, 99): + for b in range(0, 99): + il = copy.deepcopy(input_list) + + try: + result = intcode(il, a, b) + except NonHaltException: + continue + + if result == expected: + print('Found the solution:', a, b) + return + + +if __name__ == '__main__': + main() diff --git a/2019/003/main.py b/2019/003/main.py new file mode 100644 index 0000000000000000000000000000000000000000..ee5b5d115f8d0481adbc4367d83cee19b1b18975 --- /dev/null +++ b/2019/003/main.py @@ -0,0 +1,98 @@ +example = [ + "R8,U5,L5,D3", + "U7,R6,D4,L4" +] + + +A = """R992,U284,L447,D597,R888,D327,R949,U520,R27,U555,L144,D284,R538,U249,R323,U297,R136,U838,L704,D621,R488,U856,R301,U539,L701,U363,R611,D94,L734,D560,L414,U890,R236,D699,L384,D452,R702,D637,L164,U410,R649,U901,L910,D595,R339,D346,R959,U777,R218,D667,R534,D762,R484,D914,L25,U959,R984,D922,R612,U999,L169,D599,L604,D357,L217,D327,L730,D949,L565,D332,L114,D512,R460,D495,L187,D697,R313,U319,L8,D915,L518,D513,R738,U9,R137,U542,L188,U440,R576,D307,R734,U58,R285,D401,R166,U156,L859,U132,L10,U753,L933,U915,R459,D50,R231,D166,L253,U844,R585,D871,L799,U53,R785,U336,R622,D108,R555,D918,L217,D668,L220,U738,L997,D998,R964,D456,L54,U930,R985,D244,L613,D116,L994,D20,R949,D245,L704,D564,L210,D13,R998,U951,L482,U579,L793,U680,L285,U770,L975,D54,R79,U613,L907,U467,L256,D783,R883,U810,R409,D508,L898,D286,L40,U741,L759,D549,R210,U411,R638,D643,L784,U538,L739,U771,L773,U491,L303,D425,L891,U182,R412,U951,L381,U501,R482,D625,R870,D320,L464,U555,R566,D781,L540,D754,L211,U73,L321,D869,R994,D177,R496,U383,R911,U819,L651,D774,L591,U666,L883,U767,R232,U822,L499,U44,L45,U873,L98,D487,L47,U803,R855,U256,R567,D88,R138,D678,L37,U38,R783,U569,L646,D261,L597,U275,L527,U48,R433,D324,L631,D160,L145,D128,R894,U223,R664,U510,R756,D700,R297,D361,R837,U996,L769,U813,L477,U420,L172,U482,R891,D379,L329,U55,R284,U155,L816,U659,L671,U996,R997,U252,R514,D718,L661,D625,R910,D960,L39,U610,R853,U859,R174,U215,L603,U745,L587,D736,R365,U78,R306,U158,L813,U885,R558,U631,L110,D232,L519,D366,R909,D10,R294""" + +B = """L1001,D833,L855,D123,R36,U295,L319,D700,L164,U576,L68,D757,R192,D738,L640,D660,R940,D778,R888,U772,R771,U900,L188,D464,L572,U184,R889,D991,L961,U751,R560,D490,L887,D748,R37,U910,L424,D401,L385,U415,L929,U193,R710,D855,L596,D323,L966,D505,L422,D139,L108,D135,R737,U176,R538,D173,R21,D951,R949,D61,L343,U704,R127,U468,L240,D834,L858,D127,R328,D863,R329,U477,R131,U864,R997,D38,R418,U611,R28,U705,R148,D414,R786,U264,L785,D650,R201,D250,R528,D910,R670,U309,L658,U190,R704,U21,R288,D7,R930,U62,R782,U621,R328,D725,R305,U700,R494,D137,R969,U142,L867,U577,R300,U162,L13,D698,R333,U865,R941,U796,L60,U902,L784,U832,R78,D578,R196,D390,R728,D922,R858,D994,L457,U547,R238,D345,R329,D498,R873,D212,R501,U474,L657,U910,L335,U133,R213,U417,R698,U829,L2,U704,L273,D83,R231,D247,R675,D23,L692,D472,L325,D659,L408,U746,L715,U395,L596,U296,R52,D849,L713,U815,R684,D551,L319,U768,R176,D182,R557,U731,R314,D543,L9,D256,R38,D809,L567,D332,R375,D572,R81,D479,L71,U968,L831,D247,R989,U390,R463,D576,R740,D539,R488,U367,L596,U375,L763,D824,R70,U448,R979,D977,L744,D379,R488,D671,L516,D334,L542,U517,L488,D390,L713,D932,L28,U924,L448,D229,L488,D501,R19,D910,L979,D411,R711,D824,L973,U291,R794,D485,R208,U370,R655,U450,L40,D804,L374,D671,R962,D829,L209,U111,L84,D876,L832,D747,L733,D560,L702,D972,R188,U817,L111,U26,L492,U485,L71,D59,L269,D870,L152,U539,R65,D918,L932,D260,L485,U77,L699,U254,R924,U643,L264,U96,R395,D917,R360,U354,R101,D682,R854,U450,L376,D378,R872,D311,L881,U630,R77,D766,R672""" + +example = [ + "R8,U5,L5,D3", + "U7,R6,D4,L4" +] +example = [ + "R75,D30,R83,U83,L12,D49,R71,U7,L72", + "U62,R66,U55,R34,D71,R55,D58,R83" +] + +example = [ + "R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51", + "U98,R91,D20,R16,D67,R40,U7,R15,U6,R7" +] +example = [A, B] + + +def get_points(w): + ps = w.split(',') + start = (0, 0) # x, y + + result = [] + + pos = (0, 0) + + for p in ps: + d, s = p[0], int(p[1:]) + + if d == 'L': + for _ in range(0, s): + n = (pos[0] - 1, pos[1]) + result.append(n) + pos = n + elif d == 'R': + for _ in range(0, s): + n = (pos[0] + 1, pos[1]) + result.append(n) + pos = n + elif d == 'U': + for _ in range(0, s): + n = (pos[0], pos[1] + 1) + result.append(n) + pos = n + elif d == 'D': + for _ in range(0, s): + n = (pos[0], pos[1] - 1) + result.append(n) + pos = n + else: + raise Exception('unknown direction') + + return result + + +def main(): + w1, w2 = example + + p1 = get_points(w1) + p2 = get_points(w2) + + crosses = set(p1).intersection(set(p2)) + + steps = 0 + + for c in crosses: + cs = 0 + + for s in p1: + cs += 1 + if s == c: + break + + for s in p2: + cs += 1 + if s == c: + break + + if steps == 0: + steps = cs + continue + + if cs < steps: + steps = cs + + print(steps) + + +if __name__ == '__main__': + main() diff --git a/2019/004/main.py b/2019/004/main.py new file mode 100644 index 0000000000000000000000000000000000000000..f98201e499710e15f0671e534c54a9bfc43b6e44 --- /dev/null +++ b/2019/004/main.py @@ -0,0 +1,53 @@ +def has_double(n): + s = str(n) + o = {} + + for d in s: + if d not in o: + o[d] = 1 + else: + o[d] += 1 + + for v in o.values(): + if v == 2: + return True + + return False + + +def does_increase(n): + s = str(n) + p = 0 + + for d in s: + dd = int(d) + + if dd < p: + return False + + p = dd + + return True + + +def matches(n): + return has_double(n) and does_increase(n) + + +def main(): + assert matches(112233) + assert not matches(123444) + assert matches(111122) + + # return + + c = 0 + for n in range(172930, 683082): + if matches(n): + c += 1 + + print(c) + + +if __name__ == '__main__': + main() diff --git a/2019/005/main.py b/2019/005/main.py new file mode 100644 index 0000000000000000000000000000000000000000..d7fe7f4b4084243fb1116dcf24a0c31087fe4717 --- /dev/null +++ b/2019/005/main.py @@ -0,0 +1,221 @@ +import re +import copy + +opcode = re.compile(r'(\d{2})') + +OPS = ['01', '02', '03', '04', '99'] + + +def rev(s): + r = [] + for l in s: + r.append(l) + + r.reverse() + return ''.join(r) + + + +class NonHaltException(Exception): + pass + + +def add(a, b): + return a + b + + +def mult(a, b): + return a * b + + +def intcode(input_list, input_code): + cur = 0 + if input_list[0] == 3: + input_list[input_list[1]] = input_code + cur += 2 + + while True: + op_value = input_list[cur] + op_value_string = '000000' + str(op_value) + + op = op_value_string[-2:] + + front = op_value_string[0:-2] + front = rev(front) + + if op == '99': + break + + if op == '01': + # 0 po, like before + # 1 im + a_mode = front[0] + b_mode = front[1] + + if a_mode == '0': + a = input_list[input_list[cur + 1]] + else: + a = input_list[cur + 1] + + if b_mode == '0': + b = input_list[input_list[cur + 2]] + else: + b = input_list[cur + 2] + + result_pos = input_list[cur + 3] + input_list[result_pos] = add(a, b) + cur += 4 + continue + + if op == '02': + # 0 po, like before + # 1 im + a_mode = front[0] + b_mode = front[1] + + if a_mode == '0': + a = input_list[input_list[cur + 1]] + else: + a = input_list[cur + 1] + + if b_mode == '0': + b = input_list[input_list[cur + 2]] + else: + b = input_list[cur + 2] + + result_pos = input_list[cur + 3] + input_list[result_pos] = mult(a, b) + cur += 4 + continue + + if op == '04': + mode = front[0] + if mode == '0': + print(input_list[input_list[cur + 1]]) + else: + print(input_list[cur + 1]) + cur += 2 + continue + + if op == '05': + mode = front[0] + if mode == '0': + v = input_list[input_list[cur + 1]] + else: + v = input_list[cur + 1] + + if v is not 0: + m2 = front[1] + if m2 == '0': + cur = input_list[input_list[cur + 2]] + else: + cur = input_list[cur + 2] + else: + cur += 3 + + if op == '06': + mode = front[0] + if mode == '0': + v = input_list[input_list[cur + 1]] + else: + v = input_list[cur + 1] + + if v == 0: + m2 = front[1] + if m2 == '0': + cur = input_list[input_list[cur + 2]] + else: + cur = input_list[cur + 2] + else: + cur += 3 + + if op == '07': + # 0 po, like before + # 1 im + a_mode = front[0] + b_mode = front[1] + + if a_mode == '0': + a = input_list[input_list[cur + 1]] + else: + a = input_list[cur + 1] + + if b_mode == '0': + b = input_list[input_list[cur + 2]] + else: + b = input_list[cur + 2] + + result_pos = input_list[cur + 3] + + if a < b: + input_list[result_pos] = 1 + else: + input_list[result_pos] = 0 + + cur += 4 + continue + + if op == '08': + # 0 po, like before + # 1 im + a_mode = front[0] + b_mode = front[1] + + if a_mode == '0': + a = input_list[input_list[cur + 1]] + else: + a = input_list[cur + 1] + + if b_mode == '0': + b = input_list[input_list[cur + 2]] + else: + b = input_list[cur + 2] + + result_pos = input_list[cur + 3] + + if a == b: + input_list[result_pos] = 1 + else: + input_list[result_pos] = 0 + + cur += 4 + continue + + +def main(): + input_list = [ + + 3,225,1,225,6,6,1100,1,238,225,104,0,1102,45,16,225,2,65,191,224,1001, + 224,-3172,224,4,224,102,8,223,223,1001,224,5,224,1,223,224,223,1102, + 90,55,225,101,77,143,224,101,-127,224,224,4,224,102,8,223,223,1001,224, + 7,224,1,223,224,223,1102,52,6,225,1101,65,90,225,1102,75,58,225,1102, + 53,17,224,1001,224,-901,224,4,224,1002,223,8,223,1001,224,3,224,1,224, + 223,223,1002,69,79,224,1001,224,-5135,224,4,224,1002,223,8,223,1001, + 224,5,224,1,224,223,223,102,48,40,224,1001,224,-2640,224,4,224, + 102,8,223,223,1001,224,1,224,1,224,223,223,1101,50,22,225,1001,218,29, + 224,101,-119,224,224,4,224,102,8,223,223,1001,224,2,224,1,223,224,223, + + + 1101,48,19,224,1001,224,-67,224,4,224,102,8,223,223,1001,224,6,224,1, + + 223,224,223,1101,61,77,225,1,13,74,224,1001,224,-103,224,4,224,1002,223,8,223,101,3,224,224,1,224,223,223,1102,28,90,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1, + 99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999, + 1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,7,226,677,224,102,2,223,223,1005,224,329,1001,223,1,223,8,226,677,224,1002,223,2,223,1005,224,344,101,1,223,223,8,226,226,224, + 1002,223,2,223,1006,224,359,101,1,223,223,1008,677,226,224,1002,223,2,223,1005,224,374,1001,223,1,223,108,677,677,224,1002,223,2,223,1005,224,389,1001,223,1,223,1107,226,677,224,1002,223,2, + + 223,1006,224,404,101,1,223,223,1008,226,226,224,102,2,223,223,1006,224,419,1001,223,1,223,7,677,226,224,1002,223,2,223,1005,224,434,101,1,223,223,1108,226,226,224,1002,223,2,223,1005,224,449, + 101,1,223,223,7,226,226,224,102,2,223,223,1005,224,464,101,1,223,223,108,677,226,224,102,2,223,223,1005,224,479,1001,223,1,223,1007,677,226,224,1002,223,2,223,1006,224,494,1001,223,1,223,1007, + 677,677,224,1002,223,2,223,1006,224,509,1001,223,1,223,107,677,677,224,1002,223,2,223,1005,224,524,101,1,223,223,1108,226,677,224,102,2,223,223,1006,224,539,1001,223,1,223,8,677,226,224,102,2, + 223,223,1005,224,554,101,1,223,223,1007,226,226,224,102,2,223,223,1006,224,569,1001,223,1,223,107,677,226,224,102,2,223,223,1005,224,584,1001,223,1,223,108,226,226,224,102,2,223,223,1006,224,599, + 1001,223,1,223,107,226,226,224,1002,223,2,223,1006,224,614,1001,223,1,223,1108,677,226,224,1002,223,2,223,1005,224,629,1001,223,1,223,1107,677,677,224,102,2,223,223,1005,224,644,1001,223,1,223, + 1008,677,677,224,102,2,223,223,1005,224,659,101,1,223,223,1107,677,226,224,1002,223,2,223,1006,224,674,101,1,223,223,4,223,99,226 + + + ] + + intcode(input_list, 5) + + + +if __name__ == '__main__': + main() diff --git a/2019/006/input b/2019/006/input new file mode 100644 index 0000000000000000000000000000000000000000..43a2bac211778a4e33de455ae9a7b4e2095c53a5 --- /dev/null +++ b/2019/006/input @@ -0,0 +1,1272 @@ +6CF)4J7 +RC4)H87 +LMS)RL4 +KGS)1QW +H8N)KW5 +JJM)YPS +P1W)ZKT +BSY)FMR +9T5)JK4 +2X2)NQ5 +H6R)FLY +8N9)H7L +5LR)TYT +VM7)V5V +GVF)Q8P +PQV)9NB +4W3)V84 +H59)B3G +RGW)82C +BTQ)VRF +KQW)TNC +RT3)JHN +ZLY)15X +YJJ)55L +8Q2)6S7 +963)PNL +8F3)WPH +H2Z)LMN +MFK)DMJ +JGN)H4X +P1C)Y9K +92D)8YS +2M2)PQ1 +YY6)PRG +3J8)LBX +2WD)TK3 +CNS)BJL +B53)JJ5 +NND)CBM +FHS)RP5 +RMS)6VH +FBB)QVC +SD2)WPN +BXL)76Q +V8H)LCW +MXC)2W2 +CCX)KC9 +JSQ)2T8 +7HM)D24 +XN9)N36 +J3X)PRZ +6VQ)8ZV +QR4)MTL +KVT)V47 +PXR)VFJ +VZN)P7W +WK6)72B +R46)J6N +5M4)LZB +4PL)V6R +WWR)BJ1 +TPX)NRM +BS7)X7M +JJ5)8N2 +V9N)D6W +7K2)2LZ +3JZ)FV1 +2DJ)6N1 +RC5)L5D +2RG)FK8 +V8M)CL6 +KLY)MBD +DMJ)KVD +6M6)6WX +HB1)1HD +KJW)G61 +HZF)B1Q +2N4)73R +H8G)8XL +ZGK)RSY +35Y)FSR +H34)GLB +9MV)WR9 +NBS)RV6 +6QM)KLM +HN3)K2T +LCB)GNZ +LDJ)D3T +5PC)512 +BXH)6MZ +XS7)W5B +LF3)6LJ +3Z8)ZTJ +3NT)9D9 +65F)JVH +V6V)C9B +KTQ)KG8 +6WP)92D +X4M)WGG +66R)B53 +B5C)2MP +FC4)6T3 +WWR)VRD +9DG)P94 +XFV)TQQ +4LK)V21 +2H8)HVS +JHN)QHZ +2GV)VSL +581)XRN +THC)ZGK +CB8)R2T +FN9)4Y9 +1KH)RHQ +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)BQZ +X34)QF1 +DQW)XJL +GPB)QM4 +C3V)CPM +4J7)66R +95S)37D +B57)ZKR +78D)61G +CYN)NC5 +CMP)FKN +HQ3)VCN +CZC)JSX +K8V)G8T +KT9)WBS +VZH)Y6J +9H6)PVN +CCP)3M8 +VP7)DB4 +KXM)TQG +5KN)Z2Y +QL2)LF3 +9NY)FXS +8VY)M8K +JBS)B4W +DZD)P4N +RQF)3Z5 +3Q6)WRS +GXX)5NR +63W)3QB +W8Z)J3X +6VH)VJ6 +4BW)YNF +MC3)TL6 +SLD)J48 +MYP)HQ7 +DT6)G8J +GPF)1PY +6PP)KFB +GZ3)76P +CHJ)3GM +DX1)NVB +7LG)8ZY +QF1)HQ3 +85T)7DX +6T3)K7Q +49V)4BW +Y3R)DJF +4HL)641 +FYC)PGC \ No newline at end of file diff --git a/2019/006/main.py b/2019/006/main.py new file mode 100644 index 0000000000000000000000000000000000000000..256ed6a72847d35ef926893b4c6391141c5fc568 --- /dev/null +++ b/2019/006/main.py @@ -0,0 +1,105 @@ +from functools import reduce + + +data = """COM)B +B)C +C)D +D)E +E)F +B)G +G)H +D)I +E)J +J)K +K)L""" + +data = """COM)B +B)C +C)D +D)E +E)F +B)G +G)H +D)I +E)J +J)K +K)L +K)YOU +I)SAN""" + + +def add(a, b): + return a + b + + +def assert_root(lines): + orbitors = [] + orbitees = [] + + for line in lines: + a, b = line.split(')') + orbitors.append(b) + orbitees.append(a) + + orbitors = set(orbitors) + orbitees = set(orbitees) + + root = orbitees.difference(orbitors) + assert len(root) == 1 + assert 'COM' in root + + +def count(x, k, n=0): + c = 0 + c += n + + for item in x.get(k, []): + c += count(x, item, n + 1) + + return c + + +def path(x, k): + if k == 'COM': + return [] + return [x[k]] + path(x, x[k]) + + +def main(): + x = {} + + data = open('input').read() + + lines = data.splitlines() + + assert_root(lines) + + for line in lines: + a, b = line.split(')') + x[b] = a + + you_path = path(x, 'YOU') + san_path = path(x, 'SAN') + + you_path.reverse() + san_path.reverse() + + common = [] + + for y, s in zip(you_path, san_path): + if y == s: + common.append(y) + else: + break + + print(you_path) + print(san_path) + print(common) + + res = (len(you_path) - len(common)) + (len(san_path) - len(common)) + print(res) + + + +if __name__ == '__main__': + main() diff --git a/2019/007/main.py b/2019/007/main.py new file mode 100644 index 0000000000000000000000000000000000000000..8372b7d4ea77ed2c0a6ed13fc0a04c6ba7596020 --- /dev/null +++ b/2019/007/main.py @@ -0,0 +1,300 @@ +import re +import copy +import itertools + +opcode = re.compile(r'(\d{2})') + +OPS = ['01', '02', '03', '04', '99'] +NEEDS_INPUT = 'NI' +HALT = 'H' + + +def rev(s): + r = [] + for l in s: + r.append(l) + + r.reverse() + return ''.join(r) + + + +class NonHaltException(Exception): + pass + + +def add(a, b): + return a + b + + +def mult(a, b): + return a * b + + +def intcode(input_list, *input_codes, cur=0): + input_codes = list(input_codes) + input_codes.reverse() + + outputs = [] + + # if input_list[0] == 3: + # input_list[input_list[1]] = input_codes.pop() + # cur += 2 + + while True: + op_value = input_list[cur] + op_value_string = '000000' + str(op_value) + + op = op_value_string[-2:] + + front = op_value_string[0:-2] + front = rev(front) + + if op == '99': + break + + if op == '01': + # 0 po, like before + # 1 im + a_mode = front[0] + b_mode = front[1] + + if a_mode == '0': + a = input_list[input_list[cur + 1]] + else: + a = input_list[cur + 1] + + if b_mode == '0': + b = input_list[input_list[cur + 2]] + else: + b = input_list[cur + 2] + + result_pos = input_list[cur + 3] + input_list[result_pos] = add(a, b) + cur += 4 + continue + + if op == '02': + # 0 po, like before + # 1 im + a_mode = front[0] + b_mode = front[1] + + if a_mode == '0': + a = input_list[input_list[cur + 1]] + else: + a = input_list[cur + 1] + + if b_mode == '0': + b = input_list[input_list[cur + 2]] + else: + b = input_list[cur + 2] + + result_pos = input_list[cur + 3] + input_list[result_pos] = mult(a, b) + cur += 4 + continue + + if op == '03': + + if not input_codes: + return { + 'output': outputs, + 'state': NEEDS_INPUT, + 'memory': input_list, + 'cur': cur, + } + + input_list[input_list[cur + 1]] = input_codes.pop() + cur += 2 + continue + + if op == '04': + mode = front[0] + if mode == '0': + o = input_list[input_list[cur + 1]] + else: + o = input_list[cur + 1] + outputs.append(o) + cur += 2 + continue + + if op == '05': + mode = front[0] + if mode == '0': + v = input_list[input_list[cur + 1]] + else: + v = input_list[cur + 1] + + if v is not 0: + m2 = front[1] + if m2 == '0': + cur = input_list[input_list[cur + 2]] + else: + cur = input_list[cur + 2] + else: + cur += 3 + + if op == '06': + mode = front[0] + if mode == '0': + v = input_list[input_list[cur + 1]] + else: + v = input_list[cur + 1] + + if v == 0: + m2 = front[1] + if m2 == '0': + cur = input_list[input_list[cur + 2]] + else: + cur = input_list[cur + 2] + else: + cur += 3 + + if op == '07': + # 0 po, like before + # 1 im + a_mode = front[0] + b_mode = front[1] + + if a_mode == '0': + a = input_list[input_list[cur + 1]] + else: + a = input_list[cur + 1] + + if b_mode == '0': + b = input_list[input_list[cur + 2]] + else: + b = input_list[cur + 2] + + result_pos = input_list[cur + 3] + + if a < b: + input_list[result_pos] = 1 + else: + input_list[result_pos] = 0 + + cur += 4 + continue + + if op == '08': + # 0 po, like before + # 1 im + a_mode = front[0] + b_mode = front[1] + + if a_mode == '0': + a = input_list[input_list[cur + 1]] + else: + a = input_list[cur + 1] + + if b_mode == '0': + b = input_list[input_list[cur + 2]] + else: + b = input_list[cur + 2] + + result_pos = input_list[cur + 3] + + if a == b: + input_list[result_pos] = 1 + else: + input_list[result_pos] = 0 + + cur += 4 + continue + + return { + 'output': outputs, + 'state': HALT, + 'memory': input_list, + 'cur': None + } + + +def run_phases(input_list, phases): + prog_input = 0 + cur = 0 + + state = {} + + while True: + for p in itertools.repeat(phases): + for i in phases: + amplifier_state = state.get(i, { + 'memory': copy.deepcopy(input_list), + 'cur': 0, + 'state': None + }) + + if not amplifier_state['state']: + inputs = [i, prog_input] + else: + inputs = [prog_input] + + out = intcode(amplifier_state['memory'], + *inputs, + cur=amplifier_state['cur']) + + if out['state'] == HALT: + if i == phases[-1]: + return out['output'][0] + + state[i] = out + prog_input = out['output'][0] + + if prog_input > 1000000000000: + raise Exception('too much') + + +def find_solution(input_list): + max_v = 0 + max_perm = None + + for v in itertools.permutations('56789'): + phases = list(map(int, list(v))) + prog_out = run_phases(copy.deepcopy(input_list), phases) + + if prog_out > max_v: + max_v = prog_out + max_perm = v + + if not max_perm: + max_perm = [] + + return list(map(int, max_perm)), max_v + + + +def main(): + inputs = [ + ( + [3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26, 27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5], + [9,8,7,6,5], + 139629729 + ), + + ( + [ + 3,52,1001,52,-5,52,3,53,1,52,56,54,1007,54,5,55,1005,55,26,1001,54, + -5,54,1105,1,12,1,53,54,53,1008,54,0,55,1001,55,1,55,2,53,55,53,4, + 53,1001,56,-1,56,1005,56,6,99,0,0,0,0,10 + ], + [9,7,8,5,6], + 18216 + ), + + ] + + puzzle_input = [ + 3,8,1001,8,10,8,105,1,0,0,21,42,59,76,85,106,187,268,349,430,99999,3,9,102,3,9,9,1001,9,2,9,1002,9,3,9,1001,9,3,9,4,9,99,3,9,102,3,9,9,101,3,9,9,1002,9,2,9,4,9,99,3,9,102,3,9,9,1001,9,4,9,1002,9,5,9,4,9,99,3,9,102,2,9,9,4,9,99,3,9,101,3,9,9,1002,9,2,9,1001,9,4,9,1002,9,2,9,4,9,99,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,99 + ] + + # for input_list, expected_phase, expected_value in inputs: + # actual_phase, actual_value = find_solution(input_list) + # print(actual_phase, expected_phase) + # print(actual_value, expected_value) + # print('') + print(find_solution(puzzle_input)) + +if __name__ == '__main__': + main() diff --git a/2019/008/input b/2019/008/input new file mode 100644 index 0000000000000000000000000000000000000000..f0b44c6f724048a59e5749e8cae54d966fe2e3d2 --- /dev/null +++ b/2019/008/input @@ -0,0 +1 @@ +212222222222222222022222222022202022222222220220222022022012122222222221222202222222022222122201222022220202222221222220222222122222222222022222222222212222222222222222222222222122222122222222221222222022122112022222222222222202222222022220222201222012221222222220222222222222222222222221122222222222212222222222222222022222222022202222221222220222222122122022022222222222222202222222122220022212222012220202202222222221222222222222222222122222222222202222222222220222022222222222222022220222222222222222122202022222222220222202222222222020222211222112222222222222222220222222022222222221122222222222202222222222221222122222222222212122220222220222222122222222122122222221222222222222122020222200222102221202212222222222222122122222222220022222222222222222222222221222022222222122212022222222220221222022022202220222222221222222222222122120222212222012222222202202222221222022022222222220122222222222212222222222222222122222222022202222220222221220222022022122120022222222222202222222122021122220222222220222222210222220222122022222222220222222222222212222222222222222222222222122212122220222222220222022222102021222222221222222222222222020122201222102222222222211222222222022122222222221122222222222222222222222210222222222222222222122201222220222222122022102222122222222222212222222022121022211222022220202202221222221222222122222222222122222222222212222222222212222022222222222202222222222221221222202022202221122222221222212222222022020222201222112222222212212222221222022222222222220122222222222222222222212210222222222222022212122220222222220222222122122021222222222222222222222122120022221222112222222212221222220222022122222222221102222222212212222222212220222122222222222222022221222220220222012122112222222222221222212222222022020122201222202220212222221222221222022022222222222212222222212212222222212221222122222222122222122211222220222222122222112122222222222222212222222122222122212222222220212212202222221222022222222222220212222222222212222222212222222022222222122212122201222220221222222122102020122222222222222222222022021022200222112221202212211222221222122122222222222112222222202222222222202201222022222222222202122200222222221222222022002122022222222222202222222122221222201222112221222222202222222222022122222222220112222222222222222220222211222122222222022212122210222221220222222022222220021222222222202222222222022222222222022220222222221222021222022022222222220202222222222202222221222221222222222222022202222212222221220222002222222221220222221222202222222022122022211222012220202222210222220222122222222222220012222222222202222222222201222222222222022212222212222221221222112122102021122222220222212222222122220122221222122222212212222222221222222022222222221002222222222222222202222211222022222222022202222222222222220222102122122220222222220222212222222022120122222222212220202212201222122222122122222222222212222222212222222202212210222222222222022212022211222222221222022122102221222222220222222222222222021222221222122221202202210222022222122122222222221102222222202202222222202201222122222222222212222200222220222222222022202221120222221222212222222222021222201222202220212222012222022222122122222222221102222222202202222211202200222122222222022222022211222220222222112122102221122202221222202222222222220022201221202222212202110222021222222222222222221002222222202202222211212211222122222222122212222220222220221222222222210022020202221222222222222022122122212220112221202212222222222222022122222222220012222222222212222201202202222222222222122202122212222221222222222222121021220022022222212222222022020122212221002222202202121222220222022222222222221102222222202221222221202210222022222220122222022202222220220222012122220021220122122222202222222022021022210222122220222202102222021222022022222222221022222222222211222201212222222122222220022212122210222220222222022122211122122002121222212222222022221022222220212221202202020222121222022022222222221122222222212212222210222221222222222220122202022200222221222222122222101221121222120222212222222120021022201220222221212202100222221222222122222222220002222222212220222201222201222122222222222212222200222221222222012022201120221022122122212222222222121122221220112221222212010222022222122022222222222102222222202211222222212222222122222222222212122202222220222222002022211221022212022122222222222022020222220221102222212202201222220222222222222222220112222222212200222201212210222222222222022212122200222221221222102122220220220112220022212222222222022122201220112220222222001222221222022222222222222022222222222200222211200212222222222222122222222220222220222222012022010020221012222222202222222220221122222222012222202201211222221222122022222222220102222222212211222212221210222122222221122200122201222221221222102022102120122112021222222222222121022022202221212220202222100222222222122222222222221112222222212202222210200202222022221222022222122221222221221222012022122122021122222122202222222021120122200220202221202210110222121222022222222222222122222220212200222220221210222122222221222221222220222220221222222022212120020022221122222222222120022112200221202221202200102222221222022022222222221222222221212211222202120210222222221221022220022210222222220122022022211220122112122122222222222222020102221221122222212212112222021222122022222222221202222221202221222212211201222122221220022222022211222220221022202022212122020002121022202222222021222112210222202222222210102222222222022022222222220222222220212200222212012212222122220222122200222201222221220222102222220221220002021022202222222022220212210221202221222001212222221222222222222222220002222221202202222222222200222222221222022100222220222222221221122222101121020212022222212222222021120112220220022220202011002212122222122122222222220122202222222222222202202202222222222220222210222200220220221122122022002222202202022122222222222120222202201220112221212021121202120222122122222222222102212222212200222220210222222022221222122211122200221221220120002122121022121022021022222222222022222002212220012222212121112212022222022022222222220002222222212220222220011222222222221221222112222212221222221121112222120120001112021222212222222221121112211221202222222100102222122222022222222222222022212220212211222222120222022222220220112220222202221222222122102222110221110222020022222222222010120102201221112222222212120202020222122122222222220212202220212211222200122202222122222221102111022221220221222220202122102120200222020022202222222202122012212021122222202211202222220222022022222222220222222222202202222201202220022122221221202200122220221220222220022122111021212012021122212222222210222122220222222221212010112222021222122022222222221002202221212211222202211212222122220220022212122201220222220222212122001020102212120122222202222221021012222220222222212102200212221222022022222222221202212220202221222200112212022122222220022122122200222221222220022122121021122112120022212202212101220222212021002220222011222212221222122022222222222202222221012200222200210220222022222222012100022202221222222022002222200020010012021122222202222010221012222222202222202122220202221222022122222222220022202220002201222212120202012122222222002212222201220220220121212221220222122122120022202212202110122022222022122222212101111212121222122122222222221112202221202210222002022202112122221221012122022220222222220122122120012020102202222022202202222201222022211121202220212012110202120222122122222222222212212220002210222120021222012222221221012102222221222222222222122220100220212202022122222222212112121202222222102221222110000222120222122222222222220112212220202201222200111200122122222221022221022210220222220122022021120121112112022222222222212112020202202020002220202211000222021222122122222222220112202222122221222012201210112122221221212012022220222220222220112021222121102022222022222220212022222112210120222221222212210212221222222122222222222112202221002210221121202201112022222222012221222220221222220221112221211022121122120022202220222100121002202221112222222102211202222222022122222222221012212221112221220111000211022122220221022121222202220222220220002020002221202122222022202202212110121102200021202222222002200222022222222222222222222222212221112211222201111220012222220222122211222202220222221121222120022220220002222122222201202100020202220021202222212100002222120222222222222222221102212222102210222120110220002222220220012101022202222222222021112021012021201012020222210212222002020122222221122221222222101222221222122022222222220021202221122212220110111222212022221220212100222200220020221121022022221122011112022022211210202202120122200221112210202002100002220222122012222222221221212222112201222000200211122222220220022211122212221122221022212022012221010012221022201220222112121222211120222202212020222112222222122212222222221021202220202202220212110212112222220222022102022221220020222222212120010120122102020022220211202001121222220122222201222020022212020222222112222222221210222221112222222002122220202222222220012212122202220121220121222220102122000112020022211222212221122012222020112221222221100212220222222202222222220011222222022212221001101200102222221222022121022201221121221121212222101021122102021122201221222220222022202021212211222210201012120222122022222222220101202220002212220020200202012222220222012002112222220122220122222122211121111202021022212212222211021112212021222222212000001112222222022202222222221202222220000220221022010221112022221221202101022222221221221020012220202120021222121122221201222010221202201221202210202220121112222222222112222222222120202220221201222201120202212122222221202200212220221221222121122022112122000112022222210221222022021022201120002212202112201102022222222202222222221212212220001202221200001221212022222220212112002201220121220121012121201220221002221122212202202100122002202022022200222220002122022222122102222222221101222222021212221112020201012022220220122220202221220022220221002020201221201202121222202212222010120112221122012210212021212212022222022012222222221122212221102211222212020202022122220221112222122220221021201220122220012120102012122022201212202102220202200222202210222022120022120222222222222222222122202222111200221112122210122022220220212201202202222122201121102121001220202002222122200222202010220012222122122222202021211002120222122002222222221101202222222222221220212222112021222220212101212221221222220121102121100022221102222022202022222010222212202222222221222121001022020222022102222222222211222221102212220012022222122221221220022001222202221120201021122221021022022022022022211102202210122112210220022212202220002222222222222002222222220102201220011212220211001220222221222221202101112201220221221022012220221121012222120222210220202101120022202222012201222011102122221122022002222222212221012220110221221221112202112000220221202220202210221222220120212220101121021112122122221221212000220022210020212200212022022112222022122122222222212201120222120211222121121220012022220220212012202200222222202220100021022220220002220122222211222221221002101222212211202001120112020122022012222222201012220222221210221010021222022211220222022122122202220220200021110020112020101002020222202020202112222002200020022220222101112202020122222202220222201110122221100221220010201222222112220222112021002200021220200222210122021122011102222222202102012012121012222221002221202000021022022122022202220222200221011222220222221121101220102202222221202102022211120120222221211221112221121112222122212221202211220122001001112210212122212222020022002002222222202212111221100222222012112212022110222221012001012212222122212021122021011221010022222122200100020211021022111222222221212012201112121102102012220222212002011221011222221201220221002000222220002101112200020122202221120222010220212212021022200120200120222102011200102210212212201202020102102002222222220211121220111211222222000200202122220222002022002222120122210120112222111221011022022222212000120021021102221021112202212002120202120202012122221222211122100221112201220010222211022112222222022101022212120221220121201020200020000222120022201201021100020111022200022200202102101022020202122002220222201122111212021200220201022201022201220222012112102220222220201221202022200120121022120222210222000102021010200120102221202110210212022222012002221222211112101202011202221000210211102222222221012212122220120021210221212222020221012102022122201212212211121020022120122200212200112202120122212022221222212001221210212221221001222220202001221222022222112212222122202122220022000222221002120022210022120000021200011102222201222120011102021012122112220222222212202220202222220200020210002011220022012121202222120222212121222020102020120212220022102020201202022210200020222212212221010002022102002202220222221121211211101212222102201210012121220221002220222220222122221221111221010020200022021222101210110021122020001120202220222210012022020012012212222222220211220201102210220001002221222012220022102211112210221120222101120022000220222102022222121011220011020020211120122200212022012212021002122212221222201201200212000222021200021222002221220022002202002201122220221112110222221021221022120122212101110112220122121111222202222111101022121022122202222222201122201221120212120212110220122100220221220212212221121221200101212222021022012012021222220100220121112211210100012210212122102122122202212002220222202212011210101220222222222212022110222222022220022212222021200222102020011121122222022022220200120221211220002212022200012202110212222022212202222222211201000202120212121020211221112000220122010011222211220022211200112020121122121112021222111211111020121101020110022220002210012112022102212110222222220101121220021212022012020210002102220120112012102200121022212012121122011120000012122122212100200022211111102210022210002211202112121202102001220222201020012200111201121022202200012021222020220120002201122022201002102002221220211222220022001021012020221000010110202211122121022112022122112110222222210101020221220202021102110221202002222020002011222222121221211011112020020221002012020122021200120222021110002022202220112000200022122022102222220222202100210212202200022000122220202100222120000122212210121211212021221212001220000022222122221011002012212222011212222222212222012122120212012201222220201102001211010220221022020222012212221022021121022210122200110221002220112121212002022022111210022010201200202000110212122202011102022212002102222220200122212222200222220011211222122222200222210112122210022121112000222012111220121222220022121012222021022000220022201221202000022212221012202102220222210000122222121221122121210211122021212021220100102211122001210020100211101120100102221122102210120122222200110010021201022020000202020102212001220222200122110222101221120100000201122210221221222111002221121111201012000022012121222112022222020101111010100212121120202202102001112022020202012011222222222210002222001220020011210210202020201220001122222212020002002012012222122222000222020222202211100222021120021001002212002210102122021202002101201221200100011220110002102021120000201221012112111020101012011021212010122212010201221021011010111120101202121021120000002012211001001110110210100021010100002200112002220 \ No newline at end of file diff --git a/2019/008/main.py b/2019/008/main.py new file mode 100644 index 0000000000000000000000000000000000000000..4392f01fbf853bd3150e4c2da539b24cfb39e143 --- /dev/null +++ b/2019/008/main.py @@ -0,0 +1,57 @@ +import itertools + + +W = 25 +H = 6 + + +def get_layers(data): + layers = [] + + while len(data): + layers.append(data[:W * H]) + data = data[W * H:] + + return layers + + +def main(): + data = open('input').read() + layers = get_layers(data) + + top = layers[0] + result = [] + + for i, d in enumerate(top): + if d in ['1', '0']: + result.append(d) + else: + o = None + c = 1 + while True: + l = layers[c] + candidate = l[i] + if candidate == '2': + c += 1 + continue + else: + result.append(candidate) + break + + a = [] + + for r in result: + if r == '1': + a.append('😀') + else: + a.append('🖤') + + for x in itertools.count(step=W): + if x >= len(result): + break + row = a[x:x+W] + print(''.join(row)) + + +if __name__ == '__main__': + main() diff --git a/2019/009/input b/2019/009/input new file mode 100644 index 0000000000000000000000000000000000000000..544a9b45695a847d43eeaade6a9598d9a1d22fb4 --- /dev/null +++ b/2019/009/input @@ -0,0 +1 @@ +1102,34463338,34463338,63,1007,63,34463338,63,1005,63,53,1101,0,3,1000,109,988,209,12,9,1000,209,6,209,3,203,0,1008,1000,1,63,1005,63,65,1008,1000,2,63,1005,63,904,1008,1000,0,63,1005,63,58,4,25,104,0,99,4,0,104,0,99,4,17,104,0,99,0,0,1101,0,30,1016,1101,37,0,1005,1101,362,0,1023,1101,0,20,1014,1101,39,0,1013,1102,34,1,1007,1101,682,0,1027,1102,664,1,1025,1102,1,655,1028,1101,0,26,1002,1102,1,38,1015,1101,669,0,1024,1101,0,28,1017,1102,1,21,1000,1101,0,27,1012,1102,1,29,1008,1102,1,23,1019,1101,0,24,1011,1101,685,0,1026,1102,646,1,1029,1102,1,369,1022,1101,0,31,1003,1102,1,36,1001,1101,0,0,1020,1102,1,35,1009,1101,32,0,1010,1101,0,1,1021,1102,33,1,1004,1101,22,0,1006,1102,1,25,1018,109,14,1205,6,197,1001,64,1,64,1105,1,199,4,187,1002,64,2,64,109,-4,21107,40,39,9,1005,1019,219,1001,64,1,64,1105,1,221,4,205,1002,64,2,64,109,9,1206,1,239,4,227,1001,64,1,64,1106,0,239,1002,64,2,64,109,-9,2101,0,-8,63,1008,63,26,63,1005,63,261,4,245,1106,0,265,1001,64,1,64,1002,64,2,64,109,-6,2108,37,1,63,1005,63,287,4,271,1001,64,1,64,1105,1,287,1002,64,2,64,109,15,21108,41,44,-2,1005,1017,307,1001,64,1,64,1106,0,309,4,293,1002,64,2,64,109,-16,1207,1,34,63,1005,63,327,4,315,1105,1,331,1001,64,1,64,1002,64,2,64,109,8,1208,-9,29,63,1005,63,347,1106,0,353,4,337,1001,64,1,64,1002,64,2,64,109,4,2105,1,8,1001,64,1,64,1105,1,371,4,359,1002,64,2,64,109,-22,1201,9,0,63,1008,63,27,63,1005,63,391,1106,0,397,4,377,1001,64,1,64,1002,64,2,64,109,18,21107,42,43,5,1005,1016,415,4,403,1106,0,419,1001,64,1,64,1002,64,2,64,109,-8,1201,2,0,63,1008,63,37,63,1005,63,441,4,425,1105,1,445,1001,64,1,64,1002,64,2,64,109,27,1205,-9,463,4,451,1001,64,1,64,1106,0,463,1002,64,2,64,109,-1,1206,-8,475,1105,1,481,4,469,1001,64,1,64,1002,64,2,64,109,-6,21101,43,0,-8,1008,1015,43,63,1005,63,507,4,487,1001,64,1,64,1106,0,507,1002,64,2,64,109,-15,2101,0,-3,63,1008,63,35,63,1005,63,531,1001,64,1,64,1106,0,533,4,513,1002,64,2,64,109,-2,2102,1,-6,63,1008,63,18,63,1005,63,553,1105,1,559,4,539,1001,64,1,64,1002,64,2,64,109,7,21102,44,1,3,1008,1016,44,63,1005,63,581,4,565,1105,1,585,1001,64,1,64,1002,64,2,64,109,-11,1202,7,1,63,1008,63,34,63,1005,63,609,1001,64,1,64,1105,1,611,4,591,1002,64,2,64,109,6,1202,1,1,63,1008,63,35,63,1005,63,637,4,617,1001,64,1,64,1106,0,637,1002,64,2,64,109,16,2106,0,4,4,643,1001,64,1,64,1106,0,655,1002,64,2,64,109,-1,2105,1,1,4,661,1106,0,673,1001,64,1,64,1002,64,2,64,109,5,2106,0,-1,1105,1,691,4,679,1001,64,1,64,1002,64,2,64,109,-24,1208,-2,26,63,1005,63,709,4,697,1105,1,713,1001,64,1,64,1002,64,2,64,109,-10,2102,1,6,63,1008,63,21,63,1005,63,735,4,719,1105,1,739,1001,64,1,64,1002,64,2,64,109,25,21108,45,45,-9,1005,1010,757,4,745,1106,0,761,1001,64,1,64,1002,64,2,64,109,-12,1207,-7,20,63,1005,63,777,1106,0,783,4,767,1001,64,1,64,1002,64,2,64,109,-13,2108,22,6,63,1005,63,799,1106,0,805,4,789,1001,64,1,64,1002,64,2,64,109,17,21102,46,1,0,1008,1011,45,63,1005,63,825,1105,1,831,4,811,1001,64,1,64,1002,64,2,64,109,-6,2107,21,1,63,1005,63,849,4,837,1105,1,853,1001,64,1,64,1002,64,2,64,109,-3,2107,27,0,63,1005,63,873,1001,64,1,64,1105,1,875,4,859,1002,64,2,64,109,12,21101,47,0,0,1008,1014,48,63,1005,63,899,1001,64,1,64,1105,1,901,4,881,4,64,99,21102,27,1,1,21101,0,915,0,1105,1,922,21201,1,42931,1,204,1,99,109,3,1207,-2,3,63,1005,63,964,21201,-2,-1,1,21101,942,0,0,1106,0,922,21202,1,1,-1,21201,-2,-3,1,21102,1,957,0,1106,0,922,22201,1,-1,-2,1106,0,968,22101,0,-2,-2,109,-3,2106,0,0 \ No newline at end of file diff --git a/2019/009/main.py b/2019/009/main.py new file mode 100644 index 0000000000000000000000000000000000000000..90f4b62e718ffe807e8ed01e1920854f35dfa86c --- /dev/null +++ b/2019/009/main.py @@ -0,0 +1,322 @@ +import re +import copy +import itertools + +opcode = re.compile(r'(\d{2})') + +OPS = ['01', '02', '03', '04', '99'] +NEEDS_INPUT = 'NI' +HALT = 'H' + + +def rev(s): + r = [] + for l in s: + r.append(l) + + r.reverse() + return ''.join(r) + + + +class NonHaltException(Exception): + pass + + +def add(a, b): + return a + b + + +def mult(a, b): + return a * b + + +def intcode(input_list, *input_codes, cur=0): + input_list += [0 for _ in range(10_000)] + input_codes = list(input_codes) + input_codes.reverse() + + outputs = [] + + # if input_list[0] == 3: + # input_list[input_list[1]] = input_codes.pop() + # cur += 2 + + rel = 0 + while True: + op_value = input_list[cur] + op_value_string = '000000' + str(op_value) + + op = op_value_string[-2:] + + front = op_value_string[0:-2] + front = rev(front) + + if op == '99': + break + + if op == '01': + # 0 po, like before + # 1 im + a_mode = front[0] + b_mode = front[1] + c_mode = front[2] + + if a_mode == '0': + a = input_list[input_list[cur + 1]] + elif a_mode == '1': + a = input_list[cur + 1] + elif a_mode == '2': + a = input_list[rel + input_list[cur + 1]] + else: + # relative + raise NotImplementedError + + if b_mode == '0': + b = input_list[input_list[cur + 2]] + elif b_mode == '1': + b = input_list[cur + 2] + elif b_mode == '2': + b = input_list[rel + input_list[cur + 2]] + else: + raise NotImplementedError + + if c_mode == '2': + result_pos = rel + input_list[cur + 3] + else: + result_pos = input_list[cur + 3] + + input_list[result_pos] = add(a, b) + cur += 4 + continue + + if op == '02': + # 0 po, like before + # 1 im + a_mode = front[0] + b_mode = front[1] + c_mode = front[2] + + if a_mode == '0': + a = input_list[input_list[cur + 1]] + elif a_mode == '1': + a = input_list[cur + 1] + elif a_mode == '2': + a = input_list[rel + input_list[cur + 1]] + else: + raise NotImplementedError + + if b_mode == '0': + b = input_list[input_list[cur + 2]] + elif b_mode == '1': + b = input_list[cur + 2] + elif b_mode == '2': + b = input_list[rel + input_list[cur + 2]] + else: + raise NotImplementedError + + if c_mode == '2': + result_pos = rel + input_list[cur + 3] + else: + result_pos = input_list[cur + 3] + + input_list[result_pos] = mult(a, b) + cur += 4 + continue + + if op == '03': + + if not input_codes: + return { + 'output': outputs, + 'state': NEEDS_INPUT, + 'memory': input_list, + 'cur': cur, + } + + mode = front[0] + + if mode == '0': + input_list[input_list[cur + 1]] = input_codes.pop() + elif mode == '1': + input_list[cur + 1] = input_codes.pop() + elif mode == '2': + input_list[rel + input_list[cur + 1]] = input_codes.pop() + else: + raise NotImplementedError + + # input_list[input_list[cur + 1]] = input_codes.pop() + cur += 2 + continue + + if op == '04': + mode = front[0] + if mode == '0': + o = input_list[input_list[cur + 1]] + elif mode == '1': + o = input_list[cur + 1] + elif mode == '2': + o = input_list[rel + input_list[cur + 1]] + else: + raise NotImplementedError + + outputs.append(o) + cur += 2 + continue + + if op == '05': + mode = front[0] + if mode == '0': + v = input_list[input_list[cur + 1]] + elif mode == '1': + v = input_list[cur + 1] + elif mode == '2': + v = input_list[rel + input_list[cur + 1]] + else: + raise NotImplementedError + + if v is not 0: + m2 = front[1] + if m2 == '0': + cur = input_list[input_list[cur + 2]] + elif m2 == '1': + cur = input_list[cur + 2] + elif m2 == '2': + cur = input_list[rel + input_list[cur + 2]] + else: + raise NotImplementedError + else: + cur += 3 + + if op == '06': + mode = front[0] + if mode == '0': + v = input_list[input_list[cur + 1]] + elif mode == '1': + v = input_list[cur + 1] + elif mode == '2': + v = input_list[rel + input_list[cur + 1]] + else: + raise NotImplementedError + + if v == 0: + m2 = front[1] + if m2 == '0': + cur = input_list[input_list[cur + 2]] + elif m2 == '1': + cur = input_list[cur + 2] + elif m2 == '2': + cur = input_list[rel + input_list[cur + 2]] + else: + raise NotImplementedError + else: + cur += 3 + + if op == '07': + # 0 po, like before + # 1 im + a_mode = front[0] + b_mode = front[1] + c_mode = front[2] + + if a_mode == '0': + a = input_list[input_list[cur + 1]] + elif a_mode == '1': + a = input_list[cur + 1] + elif a_mode == '2': + a = input_list[rel + input_list[cur + 1]] + else: + raise NotImplementedError + + if b_mode == '0': + b = input_list[input_list[cur + 2]] + elif b_mode == '1': + b = input_list[cur + 2] + elif b_mode == '2': + b = input_list[rel + input_list[cur + 2]] + else: + raise NotImplementedError + + if c_mode == '2': + result_pos = rel + input_list[cur + 3] + else: + result_pos = input_list[cur + 3] + + if a < b: + input_list[result_pos] = 1 + else: + input_list[result_pos] = 0 + + cur += 4 + continue + + if op == '08': + # 0 po, like before + # 1 im + a_mode = front[0] + b_mode = front[1] + c_mode = front[2] + + if a_mode == '0': + a = input_list[input_list[cur + 1]] + elif a_mode == '1': + a = input_list[cur + 1] + elif a_mode == '2': + a = input_list[rel + input_list[cur + 1]] + else: + raise NotImplementedError + + if b_mode == '0': + b = input_list[input_list[cur + 2]] + elif b_mode == '1': + b = input_list[cur + 2] + elif b_mode == '2': + b = input_list[rel + input_list[cur + 2]] + else: + raise NotImplementedError + + if c_mode == '2': + result_pos = rel + input_list[cur + 3] + else: + result_pos = input_list[cur + 3] + + if a == b: + input_list[result_pos] = 1 + else: + input_list[result_pos] = 0 + + cur += 4 + continue + + if op == '09': + mode = front[0] + if mode == '0': + v = input_list[input_list[cur + 1]] + elif mode == '1': + v = input_list[cur + 1] + elif mode == '2': + v = input_list[rel + input_list[cur + 1]] + else: + raise NotImplementedError + + rel += v + cur += 2 + + return { + 'output': outputs, + 'state': HALT, + 'memory': input_list, + 'cur': None + } + + +def main(): + data = open('input').read().split(',') + data = list(map(int, data)) + + out = intcode(data, 2) + print(out['output']) + + +if __name__ == '__main__': + main() diff --git a/2019/010/main.py b/2019/010/main.py new file mode 100644 index 0000000000000000000000000000000000000000..084bc2a1fbf1eb02c56dafd24fc78bd3705e6eec --- /dev/null +++ b/2019/010/main.py @@ -0,0 +1,166 @@ +from copy import deepcopy +from math import gcd, atan2, pi +from itertools import groupby + + +PUZZLE = """.###..#######..####..##...# +########.#.###...###.#....# +###..#...#######...#..####. +.##.#.....#....##.#.#.....# +###.#######.###..##......#. +#..###..###.##.#.#####....# +#.##..###....#####...##.##. +####.##..#...#####.#..###.# +#..#....####.####.###.#.### +#..#..#....###...#####..#.. +##...####.######....#.####. +####.##...###.####..##....# +#.#..#.###.#.##.####..#...# +..##..##....#.#..##..#.#..# +##.##.#..######.#..#..####. +#.....#####.##........##### +###.#.#######..#.#.##..#..# +###...#..#.#..##.##..#####. +.##.#..#...#####.###.##.##. +...#.#.######.#####.#.####. +#..##..###...###.#.#..#.#.# +.#..#.#......#.###...###..# +#.##.#.#..#.#......#..#..## +.##.##.##.#...##.##.##.#..# +#.###.#.#...##..#####.###.# +#.####.#..#.#.##.######.#.. +.#.#####.##...#...#.##...#.""" + + +def get_distance_and_slope(ast1, ast2): + x1, y1 = ast1 + x2, y2 = ast2 + + x_, y_ = x2 - x1, y2 - y1 + x_, y_ = x1 - x2, y1 - y2 + + g = gcd(x_, y_) + + slope = int(x_ / g), int(y_ / g) + distance = float(x_ ** 2 + y_ ** 2) + return None, distance + + +def parse_map(data): + result = [] + + for line_i, line in enumerate(data.splitlines()): + for c_i, c in enumerate(line): + if c == '#': + result.append((c_i, line_i)) + + return result + + +def get_visible_asteroid_count(ast, asteroid_map): + data = [] + + for other_ast in asteroid_map: + if ast == other_ast: + continue + + slope, dist = get_distance_and_slope(ast, other_ast) + data.append({ + 'ast': ast, + 'distance': dist + }) + + data = sorted(data, key=lambda x: x['slope']) + grouped = groupby(data, lambda x: x['slope']) + visible = 0 + + for k, g in grouped: + visible += 1 + + return visible + + +def rem(self, rhs): + if rhs == 0: + raise Exception('zerooo') + + r = self % rhs + + if r < 0: + if rhs < 0: + return r - rhs + else: + return r + rhs + else: + return r + + +def get_angle(a, b): + y = b[0] - a[0] + x = a[1] - b[1] + angle = atan2(y, x) + + a = rem(angle, pi * 2.0) + prec = 100000 + + return int(a * prec) + + +def get_visible_asteroids_by_angle(ast, asteroid_map): + asteroids = [] + + for other_ast in asteroid_map: + if ast == other_ast: + continue + + _, dist = get_distance_and_slope(ast, other_ast) + asteroids.append({ + 'ast': other_ast, + # 'angle': get_angle(ast, other_ast), + 'distance': dist + }) + + asteroids = sorted(asteroids, key=lambda x: x['distance']) + sort_keys = {} + things = [] + + for i, asteroid in enumerate(asteroids): + angle = get_angle(ast, asteroid['ast']) + previous_asteroids = filter( + lambda x: angle == get_angle(ast, x['ast']), + asteroids[:i] + ) + rank = len(list(previous_asteroids)) + + s = "%s-%s" % asteroid['ast'] + sort_keys[s] = (rank, angle, asteroid['ast']) + things.append(s) + + things = sorted(things, key=lambda x: sort_keys[x]) + two_hundredth = things[199] + _, _, ast = sort_keys[two_hundredth] + print(ast[0] * 100 + ast[1]) + + +def find_best_asteroid(asteroid_map): + x = {} + for ast in asteroid_map: + num_visible_asts = get_visible_asteroid_count(ast, deepcopy(asteroid_map)) + x[num_visible_asts] = ast + + keys = list(x.keys()) + keys.sort() + keys.reverse() + return x[keys[0]], keys[0] + + +def main(): + asteroid_map = parse_map(PUZZLE) + # station, visible = find_best_asteroid(asteroid_map) + station = (17, 23) + + get_visible_asteroids_by_angle(station, asteroid_map) + + +if __name__ == '__main__': + main() diff --git a/2019/011/input b/2019/011/input new file mode 100644 index 0000000000000000000000000000000000000000..cdf4d55c3d83a12d74c00403f305d5efcb7bbd8d --- /dev/null +++ b/2019/011/input @@ -0,0 +1 @@ +3,8,1005,8,321,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1002,8,1,29,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,1002,8,1,50,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1001,8,0,73,1,1105,16,10,2,1004,8,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1002,8,1,103,1006,0,18,1,105,14,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,102,1,8,131,1006,0,85,1,1008,0,10,1006,0,55,2,104,4,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1001,8,0,168,2,1101,1,10,1006,0,14,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,196,1006,0,87,1006,0,9,1,102,20,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,1001,8,0,228,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,1002,8,1,250,2,5,0,10,2,1009,9,10,2,107,17,10,1006,0,42,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,1001,8,0,287,2,102,8,10,1006,0,73,1006,0,88,1006,0,21,101,1,9,9,1007,9,925,10,1005,10,15,99,109,643,104,0,104,1,21102,1,387353256856,1,21101,0,338,0,1105,1,442,21101,936332866452,0,1,21101,349,0,0,1105,1,442,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,0,179357024347,1,21101,0,396,0,1105,1,442,21102,1,29166144659,1,21102,407,1,0,1105,1,442,3,10,104,0,104,0,3,10,104,0,104,0,21102,1,718170641252,1,21102,430,1,0,1106,0,442,21101,825012151040,0,1,21102,441,1,0,1106,0,442,99,109,2,21202,-1,1,1,21102,1,40,2,21102,1,473,3,21102,463,1,0,1105,1,506,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,468,469,484,4,0,1001,468,1,468,108,4,468,10,1006,10,500,1102,1,0,468,109,-2,2105,1,0,0,109,4,1202,-1,1,505,1207,-3,0,10,1006,10,523,21101,0,0,-3,22101,0,-3,1,21202,-2,1,2,21102,1,1,3,21102,1,542,0,1105,1,547,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,570,2207,-4,-2,10,1006,10,570,22102,1,-4,-4,1105,1,638,22102,1,-4,1,21201,-3,-1,2,21202,-2,2,3,21101,0,589,0,1106,0,547,22102,1,1,-4,21101,1,0,-1,2207,-4,-2,10,1006,10,608,21102,0,1,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,630,21202,-1,1,1,21102,630,1,0,105,1,505,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2106,0,0 \ No newline at end of file diff --git a/2019/011/main.py b/2019/011/main.py new file mode 100644 index 0000000000000000000000000000000000000000..45adc445a2957ef1bb583b26706d7c517136a485 --- /dev/null +++ b/2019/011/main.py @@ -0,0 +1,390 @@ +import re +import copy +import itertools + +opcode = re.compile(r'(\d{2})') + +OPS = ['01', '02', '03', '04', '99'] +NEEDS_INPUT = 'NI' +HALT = 'H' + + +def rev(s): + r = [] + for l in s: + r.append(l) + + r.reverse() + return ''.join(r) + + + +class NonHaltException(Exception): + pass + + +def add(a, b): + return a + b + + +def mult(a, b): + return a * b + + +def intcode(input_list, *input_codes, cur=0): + input_list += [0 for _ in range(10_000)] + input_codes = list(input_codes) + input_codes.reverse() + + outputs = [] + + # if input_list[0] == 3: + # input_list[input_list[1]] = input_codes.pop() + # cur += 2 + + rel = 0 + while True: + op_value = input_list[cur] + op_value_string = '000000' + str(op_value) + + op = op_value_string[-2:] + + front = op_value_string[0:-2] + front = rev(front) + + if op == '99': + break + + if op == '01': + # 0 po, like before + # 1 im + a_mode = front[0] + b_mode = front[1] + c_mode = front[2] + + if a_mode == '0': + a = input_list[input_list[cur + 1]] + elif a_mode == '1': + a = input_list[cur + 1] + elif a_mode == '2': + a = input_list[rel + input_list[cur + 1]] + else: + # relative + raise NotImplementedError + + if b_mode == '0': + b = input_list[input_list[cur + 2]] + elif b_mode == '1': + b = input_list[cur + 2] + elif b_mode == '2': + b = input_list[rel + input_list[cur + 2]] + else: + raise NotImplementedError + + if c_mode == '2': + result_pos = rel + input_list[cur + 3] + else: + result_pos = input_list[cur + 3] + + input_list[result_pos] = add(a, b) + cur += 4 + continue + + if op == '02': + # 0 po, like before + # 1 im + a_mode = front[0] + b_mode = front[1] + c_mode = front[2] + + if a_mode == '0': + a = input_list[input_list[cur + 1]] + elif a_mode == '1': + a = input_list[cur + 1] + elif a_mode == '2': + a = input_list[rel + input_list[cur + 1]] + else: + raise NotImplementedError + + if b_mode == '0': + b = input_list[input_list[cur + 2]] + elif b_mode == '1': + b = input_list[cur + 2] + elif b_mode == '2': + b = input_list[rel + input_list[cur + 2]] + else: + raise NotImplementedError + + if c_mode == '2': + result_pos = rel + input_list[cur + 3] + else: + result_pos = input_list[cur + 3] + + input_list[result_pos] = mult(a, b) + cur += 4 + continue + + if op == '03': + + if not input_codes: + return { + 'output': outputs, + 'state': NEEDS_INPUT, + 'memory': input_list, + 'cur': cur, + } + + mode = front[0] + + if mode == '0': + input_list[input_list[cur + 1]] = input_codes.pop() + elif mode == '1': + input_list[cur + 1] = input_codes.pop() + elif mode == '2': + input_list[rel + input_list[cur + 1]] = input_codes.pop() + else: + raise NotImplementedError + + # input_list[input_list[cur + 1]] = input_codes.pop() + cur += 2 + continue + + if op == '04': + mode = front[0] + if mode == '0': + o = input_list[input_list[cur + 1]] + elif mode == '1': + o = input_list[cur + 1] + elif mode == '2': + o = input_list[rel + input_list[cur + 1]] + else: + raise NotImplementedError + + outputs.append(o) + cur += 2 + continue + + if op == '05': + mode = front[0] + if mode == '0': + v = input_list[input_list[cur + 1]] + elif mode == '1': + v = input_list[cur + 1] + elif mode == '2': + v = input_list[rel + input_list[cur + 1]] + else: + raise NotImplementedError + + if v is not 0: + m2 = front[1] + if m2 == '0': + cur = input_list[input_list[cur + 2]] + elif m2 == '1': + cur = input_list[cur + 2] + elif m2 == '2': + cur = input_list[rel + input_list[cur + 2]] + else: + raise NotImplementedError + else: + cur += 3 + + if op == '06': + mode = front[0] + if mode == '0': + v = input_list[input_list[cur + 1]] + elif mode == '1': + v = input_list[cur + 1] + elif mode == '2': + v = input_list[rel + input_list[cur + 1]] + else: + raise NotImplementedError + + if v == 0: + m2 = front[1] + if m2 == '0': + cur = input_list[input_list[cur + 2]] + elif m2 == '1': + cur = input_list[cur + 2] + elif m2 == '2': + cur = input_list[rel + input_list[cur + 2]] + else: + raise NotImplementedError + else: + cur += 3 + + if op == '07': + # 0 po, like before + # 1 im + a_mode = front[0] + b_mode = front[1] + c_mode = front[2] + + if a_mode == '0': + a = input_list[input_list[cur + 1]] + elif a_mode == '1': + a = input_list[cur + 1] + elif a_mode == '2': + a = input_list[rel + input_list[cur + 1]] + else: + raise NotImplementedError + + if b_mode == '0': + b = input_list[input_list[cur + 2]] + elif b_mode == '1': + b = input_list[cur + 2] + elif b_mode == '2': + b = input_list[rel + input_list[cur + 2]] + else: + raise NotImplementedError + + if c_mode == '2': + result_pos = rel + input_list[cur + 3] + else: + result_pos = input_list[cur + 3] + + if a < b: + input_list[result_pos] = 1 + else: + input_list[result_pos] = 0 + + cur += 4 + continue + + if op == '08': + # 0 po, like before + # 1 im + a_mode = front[0] + b_mode = front[1] + c_mode = front[2] + + if a_mode == '0': + a = input_list[input_list[cur + 1]] + elif a_mode == '1': + a = input_list[cur + 1] + elif a_mode == '2': + a = input_list[rel + input_list[cur + 1]] + else: + raise NotImplementedError + + if b_mode == '0': + b = input_list[input_list[cur + 2]] + elif b_mode == '1': + b = input_list[cur + 2] + elif b_mode == '2': + b = input_list[rel + input_list[cur + 2]] + else: + raise NotImplementedError + + if c_mode == '2': + result_pos = rel + input_list[cur + 3] + else: + result_pos = input_list[cur + 3] + + if a == b: + input_list[result_pos] = 1 + else: + input_list[result_pos] = 0 + + cur += 4 + continue + + if op == '09': + mode = front[0] + if mode == '0': + v = input_list[input_list[cur + 1]] + elif mode == '1': + v = input_list[cur + 1] + elif mode == '2': + v = input_list[rel + input_list[cur + 1]] + else: + raise NotImplementedError + + rel += v + cur += 2 + + return { + 'output': outputs, + 'state': HALT, + 'memory': input_list, + 'cur': None + } + + +def new_direction(current_direction, turn): + if turn == 0: # left + if current_direction == 0: + return 3 + else: + return current_direction - 1 + if turn == 1: + if current_direction == 3: + return 0 + else: + return current_direction + 1 + + +def new_position(current_position, direction): + x, y = current_position + + if direction == 0: + return (x, y + 1) + + if direction == 1: + return (x + 1, y) + + if direction == 2: + return (x, y - 1) + + if direction == 3: + return (x -1, y) + + raise Exception('wrong direction') + + +def main(): + data = open('input').read().split(',') + data = list(map(int, data)) + + current_position = (0, 0) + current_direction = 0 + memory = copy.deepcopy(data) + a = {} + cur = 0 + + # 0 up + # 1 right + # 2 down + # 3 left + # direction = 0 + + + + # read color + # say what to paint + # turn + # step + + while True: + print(current_position) + color = a.get(current_position, 0) + + out = intcode(memory, color, cur=cur) + + if out['state'] == HALT: + break + + color, turn = out['output'] + + a[current_position] = color + current_direction = new_direction(current_direction, turn) + current_position = new_position(current_position, current_direction) + memory = out['memory'] + cur = out['cur'] + + keys = set(list(a.keys())) + print(len(keys)) + + + +if __name__ == '__main__': + main()