aoc

commit d82bd019310d76f2996ad196dabca769f822bf4b

Author: Honza Pokorny <me@honza.ca>

moar

 001/input | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 001/main.py | 41 ++++++++++++++++++++++
 002/main.py | 75 +++++++++++++++++++++++++++++++++++++++++
 003/main.py | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++


diff --git a/001/input b/001/input
new file mode 100644
index 0000000000000000000000000000000000000000..154bf896f12156606867230657600df38d9b46b6
--- /dev/null
+++ b/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/001/main.py b/001/main.py
new file mode 100644
index 0000000000000000000000000000000000000000..458b0dd0ffb051cadaa408ea2dc5fa2377250491
--- /dev/null
+++ b/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/002/main.py b/002/main.py
new file mode 100644
index 0000000000000000000000000000000000000000..9baaeb856e425b1c4169c85e1d21955816e825be
--- /dev/null
+++ b/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/003/main.py b/003/main.py
new file mode 100644
index 0000000000000000000000000000000000000000..ee5b5d115f8d0481adbc4367d83cee19b1b18975
--- /dev/null
+++ b/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()