jounile
: jounile
jounile |
This Hello World is written in Clojure.
Run: $ lein run
(ns hello-world.core) (defn -main "I don't do a whole lot." [] (println "Hello, World!"))
This solution is written in C++.
Build using command: $ g++ solution.cpp -o ./hello-world
Run using: $ ./hello-world
Unresolved directive in ../../../../../../day00/c++/jounile/README.adoc - include::hello-world.cpp[tags=helper]
This solution is written in Python.
You can execute it with "python3 solution.py"
#!/usr/bin/python
# example inputs
#[1,9,10,3,2,3,11,0,99,30,40,50]
#[1,0,0,0,99]
#[2,3,0,3,99]
#[2,4,4,5,99,0]
#[1,1,1,4,99,5,6,0,99]
# input
#[1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,9,19,1,19,5,23,2,6,23,27,1,6,27,31,2,31,9,35,1,35,6,39,1,10,39,43,2,9,43,47,1,5,47,51,2,51,6,55,1,5,55,59,2,13,59,63,1,63,5,67,2,67,13,71,1,71,9,75,1,75,6,79,2,79,6,83,1,83,5,87,2,87,9,91,2,9,91,95,1,5,95,99,2,99,13,103,1,103,5,107,1,2,107,111,1,111,5,0,99,2,14,0,0]
# restore the gravity assist program
#[1,12,2,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,9,19,1,19,5,23,2,6,23,27,1,6,27,31,2,31,9,35,1,35,6,39,1,10,39,43,2,9,43,47,1,5,47,51,2,51,6,55,1,5,55,59,2,13,59,63,1,63,5,67,2,67,13,71,1,71,9,75,1,75,6,79,2,79,6,83,1,83,5,87,2,87,9,91,2,9,91,95,1,5,95,99,2,99,13,103,1,103,5,107,1,2,107,111,1,111,5,0,99,2,14,0,0]
# Part 1
def set_value_in_position(sequence, idx, value):
sequence[idx] = value
return sequence
def get_value_in_position(sequence, idx):
if(len(sequence) > idx):
return sequence[idx]
def change_output(sequence, output, new_output):
sequence[output] = new_output
return sequence
def add(sequence, pos1, pos2):
return get_value_in_position(sequence, pos1) + get_value_in_position(sequence, pos2)
def multiply(sequence, pos1, pos2):
return get_value_in_position(sequence, pos1) * get_value_in_position(sequence, pos2)
def calculate(sequence, noun, verb):
pointer = 0
while 1:
opscode = get_value_in_position(sequence, pointer)
arg1 = get_value_in_position(sequence, pointer+1)
arg2 = get_value_in_position(sequence, pointer+2)
output = get_value_in_position(sequence, pointer+3)
if(opscode == 1):
new_output = add(sequence, arg1, arg2)
elif(opscode == 2):
new_output = multiply(sequence, arg1, arg2)
elif(opscode == 99):
#print("halt" , opscode)
break
else:
print('Invalid opscode.', opscode)
if(new_output):
sequence = change_output(sequence, output, new_output)
pointer += 4
return sequence
for noun in range(100):
for verb in range(100):
#sequence = [1,1,1,4,99,5,6,0,99]
sequence = [1,12,2,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,9,19,1,19,5,23,2,6,23,27,1,6,27,31,2,31,9,35,1,35,6,39,1,10,39,43,2,9,43,47,1,5,47,51,2,51,6,55,1,5,55,59,2,13,59,63,1,63,5,67,2,67,13,71,1,71,9,75,1,75,6,79,2,79,6,83,1,83,5,87,2,87,9,91,2,9,91,95,1,5,95,99,2,99,13,103,1,103,5,107,1,2,107,111,1,111,5,0,99,2,14,0,0]
sequence = set_value_in_position(sequence, 1, noun)
sequence = set_value_in_position(sequence, 2, verb)
sequence = calculate(sequence, noun, verb)
#print("sequence", sequence)
if sequence[0] == 19690720:
print("Noun", noun)
print("Verb", verb)
print("Result: ", noun*100+verb)
# What value is left at position 0 after the program halts?
print("Value of position 0 =", sequence[0])
# Part 1 answer 3166704
# Part 2 answer
This solution is written in Python.
You can execute it with "python3 solution.py"
#!/usr/bin/python
def get_index_value(password, idx):
return int(str(password)[idx])
def previous_is_same(password, idx):
return get_index_value(password, idx) == get_index_value(password, idx-1)
def next_is_same(password, idx):
return get_index_value(password, idx) == get_index_value(password, idx+1)
def previous_and_next_not_same(password, idx):
return get_index_value(password, idx-1) != get_index_value(password, idx+1)
def has_double_digit(password):
for idx in range(0, 5):
if(next_is_same(password, idx)):
if (idx+2 > 5 or get_index_value(password, idx+2) != get_index_value(password, idx)):
if (idx-1 < 0 or get_index_value(password, idx-1) != get_index_value(password, idx)):
return True
return False
def is_next_value_larger(password):
index_value = get_index_value(password, 0)
next_index_value1 = get_index_value(password, 1)
next_index_value2 = get_index_value(password, 2)
next_index_value3 = get_index_value(password, 3)
next_index_value4 = get_index_value(password, 4)
next_index_value5 = get_index_value(password, 5)
if (next_index_value1 >= index_value
and next_index_value2 >= next_index_value1
and next_index_value3 >= next_index_value2
and next_index_value4 >= next_index_value3
and next_index_value5 >= next_index_value4):
return True
return False
def passes_criteria(password):
if is_next_value_larger(password) and has_double_digit(password):
return True
return False
def check_passwords(start, end):
passwords = []
for password in range(start, end):
if passes_criteria(password):
passwords.append(password)
return passwords
start = 347312
end = 805915
print(len(check_passwords(start, end)))
# 364
You can test it with "python3 test.py"
import unittest
import solution
# Part 1
example_1 = 111111 # meets these criteria (double 11, never decreases).
example_2 = 223450 # does not meet these criteria (decreasing pair of digits 50).
example_3 = 123789 # does not meet these criteria (no double).
# Part 2
example_4 = 112233 # meets these criteria because the digits never decrease and all repeated digits are exactly two digits long.
example_5 = 123444 # no longer meets the criteria (the repeated 44 is part of a larger group of 444).
example_6 = 111122 # meets the criteria (even though 1 is repeated more than twice, it still contains a double 22).
class TestSum(unittest.TestCase):
def test_example_1(self):
self.assertEqual(solution.passes_criteria(example_1), True, "111111 passes")
def test_example_2(self):
self.assertEqual(solution.passes_criteria(example_2), False, "223450 does not pass")
def test_example_3(self):
self.assertEqual(solution.passes_criteria(example_3), False, "123789 does not pass")
def test_example_4(self):
self.assertEqual(solution.passes_criteria(example_4), True, "112233 passes")
def test_example_5(self):
self.assertEqual(solution.passes_criteria(example_5), False, "123444 does not pass")
def test_example_6(self):
self.assertEqual(solution.passes_criteria(example_6), True, "111122 passes")
if __name__ == '__main__':
unittest.main()