thomg

431746?v=4

thomg
: thomg

About me

Nothing here yet. Update your profile at /profiles/thomg.adoc

Day 01: python

this documentation is autogenerated. Add a README.adoc to your solution to take over the control of this :-)

python

calc_fuel.py
with open("input") as file:
    massList = file.readlines()

def calcModuleFuel(fuel, mass):
    addedFuel = int(mass/3)-2
    if addedFuel <=0:
        return fuel
    else:
        return calcModuleFuel(fuel+addedFuel, addedFuel)

def calcFuelSum(massList):
    fuelSum = 0
    for m in massList:
        m = float(m)
        fuelSum  += calcModuleFuel(0, m)
    return fuelSum

print calcFuelSum(massList)
input
85364
97431
135519
119130
137800
85946
146593
141318
103590
138858
92329
94292
132098
144266
72908
112896
87046
133058
141121
74681
83458
107417
121426
66005
106094
96458
113316
142676
79186
55480
147821
116419
70532
105344
116797
126387
139600
136382
121330
123485
134336
141201
131556
91346
117939
58373
129325
102237
60644
96712
126342
98939
66305
111403
143257
58721
55552
139078
74263
125989
90904
91058
92130
53176
81369
100856
110597
111141
129749
123822
75321
80963
102625
70161
107069
117982
86443
95627
147801
149508
101470
81879
133396
82276
144803
67049
127735
121064
122975
69435
139132
141284
70798
117921
108942
85662
75438
122699
116654
126797
README
DISCLAIMER: I'm not a professional developer and only doing this for fun. Solutions will be messy. I wrote the second solution over the first one so if you run the script now you can only get the answer for the second star.

Day 02: python

this documentation is autogenerated. Add a README.adoc to your solution to take over the control of this :-)

python

intcomp_loop.py
with open("program") as file:
    initialProgram = file.readlines()[0]
initialProgram = initialProgram.split(",")

def executeProgram(program):
    pointer = 0
    result = 0
    while 1:
        arg1 = program[pointer+1]
        arg2 = program[pointer+2]

        if program[pointer] == 1:
            result = program[arg1]+program[arg2]
        elif program[pointer] == 2:
            result = program[arg1]*program[arg2]
        elif program[pointer] == 99:
            break
        else:
            print("ERROR: bad opcode ("+str(program[pointer])+") at position: "+str(pointer))

        program[program[pointer+3]] = result
        pointer += 4
    return program[0]

for noun in range(99):
    for verb in range(99):
        program=list(map(int, initialProgram))
        program[1]=noun
        program[2]=verb
        if executeProgram(program) == 19690720:
            print(noun*100+verb)
            break
intcomp.py
with open("program") as file:
    program = file.readlines()[0]

program = program.split(",")
program = list(map(int, program))

initialProgram = program
program[1]=12
program[2]=2

def executeProgram(program):
    pointer = 0
    result = 0
    while 1:
        arg1 = program[pointer+1]
        arg2 = program[pointer+2]

        if program[pointer] == 1:
            result = program[arg1]+program[arg2]
        elif program[pointer] == 2:
            result = program[arg1]*program[arg2]
        elif program[pointer] == 99:
            break
        else:
            print("ERROR: bad opcode at position: "+str(pointer))

        program[program[pointer+3]] = result
        pointer += 4
    print(program[0])

executeProgram(program)
README
DISCLAIMER: I'm not a professional developer and only doing this for fun. Solutions will be messy. I should probably have written a class or two to prepare for the rest of the mission but I don't care right now.
program
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,10,1,19,1,19,9,23,1,23,6,27,2,27,13,31,1,10,31,35,1,10,35,39,2,39,6,43,1,43,5,47,2,10,47,51,1,5,51,55,1,55,13,59,1,59,9,63,2,9,63,67,1,6,67,71,1,71,13,75,1,75,10,79,1,5,79,83,1,10,83,87,1,5,87,91,1,91,9,95,2,13,95,99,1,5,99,103,2,103,9,107,1,5,107,111,2,111,9,115,1,115,6,119,2,13,119,123,1,123,5,127,1,127,9,131,1,131,10,135,1,13,135,139,2,9,139,143,1,5,143,147,1,13,147,151,1,151,2,155,1,10,155,0,99,2,14,0,0

Day 03: python

DISCLAIMER

I’m not a professional developer and only doing this for fun. Solutions will be messy. I ended up brute forcing this one because it got late in the evening.

Day 04: python

Disclaimer

I’m not a professional developer and only doing this for fun. The solutions might me messy.

Day 04

I ended up simply looping through the entire range and applying a regex to each one and loop over every single digit to determine if they are not descending.

Day 05: python

Disclaimer

I’m not a professional developer and only doing this for fun. The solutions might me messy.

Day 05

Adding the new opcodes and param modes to my intcomp from day 2 wasn’t too difficult, and that’s all that was needed for day 5. I just hope there will be enough time for me to refactor the thing before the next extension :)

Day 06: python

Disclaimer

I’m not a professional developer and only doing this for fun. The solutions might me messy.

Day 06

I was stuck for hours on star 1, even though I had the solution in my head within minutes. I wanted to model a Tree, so I wrote a Node class and gave it a list of Nodes (the Node’s children) as a member. Turns out it’s a bad idea to give a python class a list with objects of its own type as a member, because apparently any instance will somehow link to itself (or at least the memory where itself resides) and you end up in recursion hell. Live and learn.

Star 2 made me realize my life would have been a lot easier and I wouldn’t have needed a tree structure if I just stored the relations the other way around. But I got so distracted by the very tree-like example map that I couldn’t think of anything else at first.

Day 07: python

Disclaimer

I’m not a professional developer and only doing this for fun. The solutions might be messy.

Day 07

Gave the intcomp its own file. For star 1, I used quinary (digits from 0 to 4) to go through all possible phase settings in a single for loop.

For star 2, I added return values to the execution of a program: the current state of the program as a list of ints and the pointer to the instruction where the program halted.

Day 08: python

Disclaimer

I’m not a professional developer and only doing this for fun. The solutions might be messy.

Day 08

Not much to say about this one. I dumped the string from star 2 into an image<>binary converter on some website to get the image.

Day 12: python

Disclaimer

I’m not a professional developer and only doing this for fun. The solutions might be messy.

Day 12

Star 1 was straightforward. For star 2, the "trick" (at least for me) was not to make the simulation more efficient. Instead, it was to realize that the axes are independent, so it is enough to simulate only until a period in each axis has been found. The period for the entire system then has to be the least common multiple of the individual axes' periods.