DEV Community

Erik Guzman
Erik Guzman

Posted on • Updated on

Advent Of Code Stream 12/20/19

Checkout Friday Zeal Live Streams @ 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

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

Share Time:

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

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}"
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)}"

Top comments (0)