advent_of_code_2022

My (attempted) solutions to the 2022 Advent of Code
git clone https://git.eamoncaddigan.net/advent_of_code_2022.git
Log | Files | Refs | README

day01.jl (1317B)


      1 #!/usr/bin/env julia
      2 
      3 #############################################################################
      4 # Part 1
      5 #############################################################################
      6 
      7 # Rejoice! Julia supports multi-line string literals
      8 part1_example = """
      9     1000
     10     2000
     11     3000
     12 
     13     4000
     14 
     15     5000
     16     6000
     17 
     18     7000
     19     8000
     20     9000
     21 
     22     10000
     23     """
     24 
     25 # I'm sure this is hideous to Julia programmers.
     26 # This function returns an array of the total snack calories for each elf
     27 function snack_totals(input)
     28     [sum(parse.(Int64, i))
     29      for i = split.(split(rstrip(input), "\n\n"), "\n")]
     30 end
     31 
     32 function solve1(input)
     33     maximum(snack_totals(input))
     34 end
     35 
     36 @assert solve1(part1_example) == 24000
     37 
     38 # I found some code to download puzzle input from AoC, but I don't know how to
     39 # use other people's modules yet. So I'm just downloading the file to the local
     40 # directory for now. :/
     41 println("Part 1: ", solve1(read("day01_part1_input.txt", String)))
     42 
     43 #############################################################################
     44 # Part 2
     45 #############################################################################
     46 
     47 function solve2(input)
     48     sum(partialsort(snack_totals(input), 1:3, rev=true))
     49 end
     50 
     51 @assert solve2(part1_example) == 45000
     52 
     53 println("Part 2: ", solve2(read("day01_part1_input.txt", String)))
     54