File tree 1 file changed +40
-0
lines changed
1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ input = File . read ( "./02/input.txt" ) . chomp . split ( ',' ) . map ( &:to_i )
2
+
3
+ def cpu ( mem , pos )
4
+ case mem [ pos ]
5
+ when 1
6
+ # add
7
+ mem [ mem [ pos +3 ] ] = mem [ mem [ pos +1 ] ] + mem [ mem [ pos +2 ] ]
8
+ when 2
9
+ # mult
10
+ mem [ mem [ pos +3 ] ] = mem [ mem [ pos +1 ] ] * mem [ mem [ pos +2 ] ]
11
+ when 99
12
+ # halt and output
13
+ return mem [ 0 ]
14
+ end
15
+ # run next instruction
16
+ cpu ( mem , pos +4 )
17
+ end
18
+
19
+ def solve1 ( input )
20
+ input . dup . then do |mem |
21
+ mem [ 1 ] = 12
22
+ mem [ 2 ] = 2
23
+ cpu ( mem , 0 )
24
+ end
25
+ end
26
+
27
+ def solve2 ( input )
28
+ noun , verb = ( 0 ..99 ) . to_a . combination ( 2 ) . detect do |test |
29
+ 19690720 == input . dup . then do |mem |
30
+ mem [ 1 ] = test [ 0 ]
31
+ mem [ 2 ] = test [ 1 ]
32
+ cpu ( mem , 0 )
33
+ end
34
+ end
35
+
36
+ 100 * noun + verb
37
+ end
38
+
39
+ puts "Part 1: #{ solve1 ( input ) } "
40
+ puts "Part 2: #{ solve2 ( input ) } "
You can’t perform that action at this time.
0 commit comments