Completed days 1 and 2

This commit is contained in:
Andrew R. M 2019-12-02 21:32:16 -05:00
commit cf706d01c4
6 changed files with 223 additions and 0 deletions

100
01/input Normal file
View File

@ -0,0 +1,100 @@
110756
132543
57911
58262
119938
58581
52446
127591
132449
82732
51388
115723
67376
61402
71379
99264
54697
120877
130457
89519
92846
121983
145752
57606
136613
74147
142443
91993
66409
71590
74057
126005
103231
104401
105004
100771
60204
125178
132927
97615
116662
91806
74435
69993
77268
124654
116862
79505
132479
104118
59975
133267
71379
136031
64325
85017
149922
148287
62061
92790
81205
74146
116381
78975
66557
74568
77797
60262
111913
53703
139663
65642
90693
105015
147887
139533
97861
68607
146757
97707
148185
87966
115839
118377
71123
82938
63957
76062
144141
138096
132460
67338
142338
76347
128877
104797
104637
107605
66506
127296

14
01/solution1.rb Executable file
View File

@ -0,0 +1,14 @@
#!/usr/bin/env ruby
def calculate_fuel(mass)
return (mass / 3).floor - 2
end
fuel_requirements = 0
ARGF.each do |line|
mass = line.to_i
fuel_requirements += calculate_fuel(mass)
end
puts "Fuel Requirements: #{fuel_requirements}"

22
01/solution2.rb Executable file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env ruby
def calculate_fuel(mass)
return (mass / 3).floor - 2
end
def calculate_fuel_with_fuels_fuel(mass)
fuel_required = calculate_fuel(mass)
if calculate_fuel(fuel_required) > 0
fuel_required += calculate_fuel_with_fuels_fuel(fuel_required)
end
return fuel_required
end
fuel_requirements = 0
ARGF.each do |line|
mass = line.to_i
fuel_requirements += calculate_fuel_with_fuels_fuel(mass)
end
puts "Fuel Requirements: #{fuel_requirements}"

1
02/input Normal file
View File

@ -0,0 +1 @@
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,19,5,23,1,6,23,27,1,27,5,31,2,31,10,35,2,35,6,39,1,39,5,43,2,43,9,47,1,47,6,51,1,13,51,55,2,9,55,59,1,59,13,63,1,6,63,67,2,67,10,71,1,9,71,75,2,75,6,79,1,79,5,83,1,83,5,87,2,9,87,91,2,9,91,95,1,95,10,99,1,9,99,103,2,103,6,107,2,9,107,111,1,111,5,115,2,6,115,119,1,5,119,123,1,123,2,127,1,127,9,0,99,2,0,14,0

35
02/solution1.rb Executable file
View File

@ -0,0 +1,35 @@
#!/usr/bin/env ruby
def process_opcodes(list)
index = 0
while index < (list.length - 1)
opcode = list[index]
case opcode
when 1
input1 = list[list[index+1]]
input2 = list[list[index+2]]
list[list[index+3]] = input1 + input2
when 2
input1 = list[list[index+1]]
input2 = list[list[index+2]]
list[list[index+3]] = input1 * input2
when 99
break
end
index += 4
end
return list
end
ARGF.each do |line|
# Eat the opcode list
list = line.split(",").map(&:to_i)
# Fix the program as per the exercise
list[1] = 12
list[2] = 2
# Print the post processed list for inspection to get the 0th element
puts process_opcodes(list).inspect
end

51
02/solution2.rb Executable file
View File

@ -0,0 +1,51 @@
#!/usr/bin/env ruby
def process_opcodes(list)
index = 0
while index < (list.length - 1)
opcode = list[index]
case opcode
when 1
input1 = list[list[index+1]]
input2 = list[list[index+2]]
list[list[index+3]] = input1 + input2
when 2
input1 = list[list[index+1]]
input2 = list[list[index+2]]
list[list[index+3]] = input1 * input2
when 99
break
end
index += 4
end
return list
end
ARGF.each do |line|
# Eat the opcode list
list = line.split(",").map(&:to_i)
noun = 0
verb = 0
target_number = 19690720
program_output = [0]
while program_output[0] != target_number
temp_list = list.dup
temp_list[1] = noun
temp_list[2] = verb
# Enumarate through all possible nouns and verbs O(n^2)
if noun < 99
noun += 1
else
noun = 0
verb += 1
end
program_output = process_opcodes(temp_list)
end
# Print the post processed list for inspection to get the 0th element
puts program_output.inspect
end