Advent Of Code Stream 12/20/19
Tags: adventofcode, ruby
Published: 2019-12-21 00:37:46

## Checkout Friday Zeal Live Streams @ [https://www.twitch.tv/codingzeal](https://www.twitch.tv/codingzeal)

# Today Objectives

✅ To have a good time as Christmas holiday time creeps up

✅ Start working on some Advent of Code challenges for fun today [https://adventofcode.com/2019](https://adventofcode.com/2019)

## Stream Standup

This will be my second year trying to do any Advent of Code challenges so I am curious to see how far we can get today.

Since the last stream wrote a couple of articles posted to [dev.to](http://dev.to)

- [https://dev.to/talk2megooseman/mini-tut-create-alias-for-rails-webpacker-and-vs-code-compatability-4c7l](https://dev.to/talk2megooseman/mini-tut-create-alias-for-rails-webpacker-and-vs-code-compatability-4c7l)
- [https://dev.to/talk2megooseman/year-end-1-on-1-meeting-with-myself-17kf](https://dev.to/talk2megooseman/year-end-1-on-1-meeting-with-myself-17kf)

Share Time:

- Cool TypeScript CLI Framework to use. Looks like it comes with a lot of good features for building a create CLI tool written in TS/JS: [https://oclif.io/](https://oclif.io/)
- Nice Gist explaining how to using binging.pry debugger when your Rails application is running in a docker container: [https://gist.github.com/nvick/b4458670f9395de7d19a8e2d06894d9b](https://gist.github.com/nvick/b4458670f9395de7d19a8e2d06894d9b)
- Interesting tool for Dockerizing your Rails application, havent tried out yet but it looks cool: [https://github.com/hintmedia/railsdock](https://github.com/hintmedia/railsdock)
- Great article on React Hooks and how to go from Class bases component to functional component [https://wattenberger.com/blog/react-hooks](https://wattenberger.com/blog/react-hooks)

# Notes

Advent of code challenge was fun. Made it through day one and day two of the problems.

Day one, part 2 ended up being a little more challenging since I was accidentally double count the initial fuel needed for the mass when calculation fuel needed for the added fuel mass.

Day two went really smoothly. It actually reminded me of my time in college, dealing with Assembly and Compiler class. Those classes heavily dealt with deal with memory space, instruction sets, and operation on the memory space. So when getting to part 2 it was really simple.

All the code isnt pretty but it gets the job code 
```ruby
Day 1:

    # Day 1
    ## Part 1
    def fuel_need(mass)
      (mass/3).floor - 2
    end
    
    def part1
      total_fuel_needs = 0
      file = File.open("1-input.txt")
    
      file.each do |mass|
        total_fuel_needs  = total_fuel_needs + fuel_need(mass.to_i)
      end
      file.close
    
      total_fuel_needs
    end
    
    total_fuel = part1
    puts "Part 1: #{total_fuel}"
    # 3150224
    
    ## Part 2
    def calculate_fuels_need(fuel)
      print fuel
      total_fuel = 0
      current_fuels_mass = fuel
      loop do
        current_fuels_mass = fuel_need(current_fuels_mass)
        break if current_fuels_mass <= 0
        print " + "
        print current_fuels_mass
        total_fuel = total_fuel + current_fuels_mass
      end
      puts ""
      total_fuel
    end
    
    def part2
      total_fuel_needs = 0
      file = File.open("1-input.txt")
    
      file.each do |module_mass|
        fuel_for_mass = fuel_need(module_mass.to_i)
        total_fuel_needs = calculate_fuels_need(fuel_for_mass)
      end
      file.close
    
      total_fuel_needs
    end
    
    puts "Part 2: #{part2}"
```
```ruby
Day 2:

    # Intcode: Command sep values
    # opcodes: 1, 2, 99
    # 99: exit
    # Other codes mean error
    # 1: Add operation - 1,indexV1,indexV2,storeIndex
    # 1: Multi operation - 1,indexV1,indexV2,storeIndex
    # 4 bit memory space
    INSTRUCTION_SIZE = 4
    EXIT_CODE = 99
    ADD_CODE = 1
    MUTLI_CODE = 2
    
    
    def add!(instruction, memory)
      left_value_address = instruction[1]
      right_value_address = instruction[2]
      result_value_address = instruction[3]
    
      result = memory[left_value_address] + memory[right_value_address]
    
      memory[result_value_address] = result
    end
    
    def multi!(instruction, memory)
      left_value_address = instruction[1]
      right_value_address = instruction[2]
      result_value_address = instruction[3]
    
      result = memory[left_value_address] * memory[right_value_address]
    
      memory[result_value_address] = result
    end
    
    def process(intcode)
      pointer = 0
    
      loop do
        opcode = intcode[pointer]
    
        if opcode == ADD_CODE
          instruction = intcode.slice(pointer, INSTRUCTION_SIZE)
          add!(instruction, intcode)
        elsif opcode == MUTLI_CODE
          instruction = intcode.slice(pointer, INSTRUCTION_SIZE)
          multi!(instruction, intcode)
        elsif opcode == EXIT_CODE
          break;
        end
    
        pointer += INSTRUCTION_SIZE
      end
    
      intcode
    end
    
    def part1
      File.open("2-input.txt", "r") do |f|
        f.each_line do |intcode|
          intcodeArray = intcode.split(',')
          intcodeArray.map!{ |v| v.to_i }
          result = process(intcodeArray)
          return result.join(',')
        end
      end
    end
    
    puts "Part1: #{part1}"
    
    def part2(expectation)
      File.open("2-input.txt", "r") do |f|
        f.each_line do |intcode|
          intcodeArray = intcode.split(',')
          intcodeArray.map!{ |v| v.to_i }
    
          for noun in 0..99 do
            for verb in 0..99 do
              new_memory_set = Array.new(intcodeArray)
              new_memory_set[1] = noun
              new_memory_set[2] = verb
              result = process(new_memory_set)
    
              return 100 * noun + verb if result[0] == expectation
            end
          end
    
        end
      end
    end
    
    puts "Part2: #{part2(19690720)}"
```