feat: add code for day 2
This commit is contained in:
parent
52895f7463
commit
7bfbe15b08
1
2/input
Normal file
1
2/input
Normal file
@ -0,0 +1 @@
|
|||||||
|
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,13,19,2,9,19,23,1,23,6,27,1,13,27,31,1,31,10,35,1,9,35,39,1,39,9,43,2,6,43,47,1,47,5,51,2,10,51,55,1,6,55,59,2,13,59,63,2,13,63,67,1,6,67,71,1,71,5,75,2,75,6,79,1,5,79,83,1,83,6,87,2,10,87,91,1,9,91,95,1,6,95,99,1,99,6,103,2,103,9,107,2,107,10,111,1,5,111,115,1,115,6,119,2,6,119,123,1,10,123,127,1,127,5,131,1,131,2,135,1,135,5,0,99,2,0,14,0
|
57
2/script.rb
Executable file
57
2/script.rb
Executable file
@ -0,0 +1,57 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
INPUT = __dir__ + '/input'
|
||||||
|
OUTPUT = 19690720
|
||||||
|
|
||||||
|
def part1
|
||||||
|
File.readlines(INPUT).map { |line| execute(correct(to_opcode(line))).join(',') }
|
||||||
|
end
|
||||||
|
|
||||||
|
def part2
|
||||||
|
File.readlines(INPUT).map do |line|
|
||||||
|
opcodes = to_opcode(line)
|
||||||
|
(0..99).each do |i|
|
||||||
|
(0..99).each do |j|
|
||||||
|
opcodes[1] = i
|
||||||
|
opcodes[2] = j
|
||||||
|
result = execute(opcodes.dup)
|
||||||
|
return 100 * i + j if result[0] == OUTPUT
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_opcode(line)
|
||||||
|
line.chomp.split(',').map(&:to_i)
|
||||||
|
end
|
||||||
|
|
||||||
|
def correct(opcodes)
|
||||||
|
opcodes[1] = 12
|
||||||
|
opcodes[2] = 2
|
||||||
|
opcodes
|
||||||
|
end
|
||||||
|
|
||||||
|
def execute(opcodes)
|
||||||
|
i = 0
|
||||||
|
while i < opcodes.size
|
||||||
|
opcode = opcodes[i]
|
||||||
|
|
||||||
|
if opcode == 1
|
||||||
|
opcodes[opcodes[i + 3]] = opcodes[opcodes[i + 1]] + opcodes[opcodes[i + 2]]
|
||||||
|
elsif opcode == 2
|
||||||
|
opcodes[opcodes[i + 3]] = opcodes[opcodes[i + 1]] * opcodes[opcodes[i + 2]]
|
||||||
|
else
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
i += 4
|
||||||
|
end
|
||||||
|
|
||||||
|
opcodes
|
||||||
|
end
|
||||||
|
|
||||||
|
puts "=== Part 1 ==="
|
||||||
|
puts part1
|
||||||
|
puts "=== Part 2 ==="
|
||||||
|
puts part2
|
Loading…
x
Reference in New Issue
Block a user