Crystal Anon

The first anon to submit something! He’s writing code in Crystal, which I hadn’t heard of before.
crystanon/day01.cr
crystanon/day01.cr
puts "What file would you like to solve for?"
    print "> "
    
    file = gets.not_nil!
    
    unless File.exists? file
      puts "You must provide a valid file!"
      exit
    end
    
    numbers = File.read(file).split("\n").map { |num| num.to_i }
    
    def getFirstAnswer(numbers)
      currentnum = numbers[0]
      count = 0
    
      numbers.each do |num|
        count += 1 if num > currentnum
    
        currentnum = num
      end
    
      count
    end
    
    def getSecondAnswer(numbers)
      previoussum = 0
      count = 0
    
      numbers.each_with_index do |num, index|
        unless (index + 2) >= numbers.size
          sum = num + numbers[index + 1] + numbers[index + 2]
          count += 1 if sum > previoussum
    
          previoussum = sum
        end
      end
    
      count - 1
    end
    
    puts "The first answer is: #{getFirstAnswer numbers}"
    puts "The second answer is: #{getSecondAnswer numbers}"
    
crystanon/day01-smol.cr
crystanon/day01-smol.cr
puts "What file would you like to solve for?"
    
    print "> "
    
    file = gets.not_nil!
    
    unless File.exists? file
      puts "You must provide a valid file!"
      exit
    end
    
    numbers = File.read(file).lines.map{|num| num.to_i}
    
    def getFirstAnswer(numbers)
      numbers.each.cons_pair.count{|x,y| y>x}
    end
    
    def getSecondAnswer(numbers)
      numbers.each.cons(3).cons_pair.count{|x,y| y.sum>x.sum}
    end
    
    puts "The first answer is: #{getFirstAnswer numbers}"
    puts "The second answer is: #{getSecondAnswer numbers}"
    
crystanon/day02.cr
crystanon/day02.cr
puts "What file would you like to solve for?"
    print "> "
    
    file = gets.not_nil!
    
    unless File.exists? file
      puts "You must provide a valid file!"
      exit
    end
    
    instructions = File.read(file).lines.map {|line| line.split(" ")}
    
    def getFirstAnswer(instructions)
      horizontal = 0
      depth = 0
      instructions.each do |instruction|
        command = instruction[0]
        argument = instruction[1].to_i
        case command
          when "forward"
            horizontal += argument
          when "up"
            depth -= argument
          when "down"
            depth += argument
        end
      end
      horizontal \* depth
    end
    
    def getSecondAnswer(instructions)
      horizontal = 0
      aim = 0
      depth = 0
      instructions.each do |instruction|
        command = instruction[0]
        argument = instruction[1].to_i
        case command
          when "down"
            aim += argument
          when "up"
            aim -= argument
          when "forward"
            horizontal += argument
            depth += aim \* argument
        end
      end
      horizontal \* depth
    end
    
    puts "The first answer is: #{getFirstAnswer instructions}"
    puts "The second answer is: #{getSecondAnswer instructions}"
    

Tags: