Skip to content

Instantly share code, notes, and snippets.

@pezholio
Created December 9, 2019 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pezholio/1d9260130ab58c0369e668ec225d109a to your computer and use it in GitHub Desktop.
Save pezholio/1d9260130ab58c0369e668ec225d109a to your computer and use it in GitHub Desktop.
Advent of Code 2019 - Day 2
file = File.read('input.txt')
memory = file.split(",").map(&:to_i)
finished = false
i = 0
while finished == false do
num = memory[i]
if [1,2].include?(num)
index = i+3
line = memory[i..index]
solution = if line[0] == 1
memory[line[1]] + memory[line[2]]
elsif line[0] == 2
memory[line[1]] * memory[line[2]]
end
position = line[3]
memory[position] = solution
i = index
elsif num == 99
finished = true
end
i = i+1
end
puts memory[0]
file = File.read('input.txt')
memory = file.split(",").map(&:to_i)
attempts = 0
nouns = (0..99).to_a
verbs = (0..99).to_a
answer = 0
while answer != 19690720
finished = false
i = 0
noun = nouns.sample.to_i
verb = verbs.sample.to_i
memory[1] = noun
memory[2] = verb
puts "trying #{memory[1]} and #{memory[2]} - Attempt #{attempts}"
while finished == false do
num = memory[i]
if [1,2].include?(num)
index = i+3
line = memory[i..index]
solution = if line[0] == 1
memory[line[1]] + memory[line[2]]
elsif line[0] == 2
memory[line[1]] * memory[line[2]]
end
position = line[3]
memory[position] = solution
i = index
elsif num == 99
finished = true
end
i = i+1
end
answer = memory[0]
memory = file.split(",").map(&:to_i)
attempts = attempts + 1
end
answer = 100 * noun + verb
puts "Answer = #{answer}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment