File tree Expand file tree Collapse file tree 2 files changed +79
-0
lines changed
Expand file tree Collapse file tree 2 files changed +79
-0
lines changed Original file line number Diff line number Diff line change 1+ def intcode ( input )
2+ arr = input . dup
3+ index = 0
4+ loop do
5+ opcode = arr [ index ]
6+ case opcode
7+ when 1
8+ p1 , p2 , output_index = arr [ index + 1 ..index + 3 ]
9+ arr [ output_index ] = arr [ p1 ] + arr [ p2 ]
10+ index += 4
11+ when 2
12+ p1 , p2 , output_index = arr [ index + 1 ..index + 3 ]
13+ arr [ output_index ] = arr [ p1 ] * arr [ p2 ]
14+ index += 4
15+ when 3
16+
17+ when 99
18+ return arr [ 0 ]
19+ end
20+ end
21+ end
22+
23+ def process_instruction ( instruction , noun , verb )
24+ if instruction == 1
25+ noun + verb
26+ elsif
27+ noun * verb
28+ end
29+ end
30+
31+ def run ( noun , verb , input )
32+ input [ 1 ] = noun
33+ input [ 2 ] = verb
34+ intcode ( input )
35+ end
36+
37+ f = File . open ( 'day_2_input.txt' )
38+ input = f . read . split ( "," ) . map ( &:to_i )
39+ puts "Part 1"
40+ puts run ( 12 , 2 , input )
41+ puts "Part 2"
42+
43+ ( 0 ..99 ) . each do |noun |
44+ ( 0 ..99 ) . each do |verb |
45+ if run ( noun , verb , input ) == 19690720
46+ puts 100 * noun + verb
47+ break
48+ end
49+ end
50+ end
Original file line number Diff line number Diff line change 1+ def orbit_map ( input )
2+ orbits = { }
3+ input . each do |line |
4+ orbitee , orbiter = line . split ( ")" )
5+ orbits [ orbiter ] = orbitee
6+ end
7+ orbits
8+ end
9+
10+ def count_orbits ( obj , orbit_map , counter )
11+ if orbit_map [ obj ]
12+ count_orbits ( orbit_map [ obj ] , orbit_map , counter + 1 )
13+ else
14+ return counter
15+ end
16+ end
17+
18+ f = File . open ( 'day_6_input.txt' )
19+ input = f . read . split ( "\n " )
20+ all_objects = input . map { |line | line . split ( ")" ) } . flatten . uniq
21+ puts "Part 1"
22+ running_counter = 0
23+ orbit_map = orbit_map ( input )
24+ all_objects . each do |obj |
25+ running_counter += count_orbits ( obj , orbit_map , 0 )
26+ end
27+ puts running_counter
28+ orbit_counts = Hash [ all_objects . map { |obj | [ obj , nil ] } ]
29+ puts "Part 2"
You can’t perform that action at this time.
0 commit comments