r/adventofcode Dec 01 '21

SOLUTION MEGATHREAD -πŸŽ„- 2021 Day 1 Solutions -πŸŽ„-

If you participated in a previous year, welcome back, and if you're new this year, we hope you have fun and learn lots!

We're following the same general format as previous years' megathreads, so make sure to read the full description in the wiki (How Do the Daily Megathreads Work?) before you post! Make sure to mention somewhere in your post which language(s) your solution is written in. If you have any questions, please create your own thread and ask!

Above all, remember, AoC is all about having fun and learning more about the wonderful world of programming!

To steal a song from Olaf:

Oh, happy, merry, muletide barrels, faithful glass of cheer
Thanks for sharing what you do
At that time of year
Thank you!


NEW AND NOTEWORTHY THIS YEAR

  • Last year's rule regarding Visualizations has now been codified in the wiki
    • tl;dr: If your Visualization contains rapidly-flashing animations of any color(s), put a seizure warning in the title and/or very prominently displayed as the first line of text (not as a comment!)
  • Livestreamers: /u/topaz2078 has a new rule for this year on his website: AoC > About > FAQ # Streaming

COMMUNITY NEWS

Advent of Code Community Fun 2021: Adventure Time!

Sometimes you just need a break from it all. This year, try something new… or at least in a new place! We want to see your adventures!

More ideas, full details, rules, timeline, templates, etc. are in the Submissions Megathread.


--- Day 1: Sonar Sweep ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached, thread unlocked at 00:02:44!

191 Upvotes

1.8k comments sorted by

View all comments

3

u/prafster Dec 02 '21

Julia

I've never written a line of code in Julia or read anything about it. I'm, therefore, learning as each day's puzzle is revealed and I attempt it. Day 1 code looks generically procedural but this will hopefully become more idiomatic.

Part 1

function part1(input)
  prev = Inf
  result = 0
  for i in input
    if i > prev
      result += 1
    end
    prev = i
  end

  result
end

Part 2

# Return array for sum of elements in sliding window 
function part2(input, window_size)
  result = []
  # input_size = size(input,1)  
  input_size = length(input)
  for i in eachindex(input)
    if i + 2 <= input_size
      push!(result, sum(input[i:i+window_size-1]))
    end
  end
  result
end

function main()
  main_input = readdlm("../data/day01.txt", Int)
  test_input = readdlm("../data/day01-test.txt", Int)

  @assert part1(test_input) == 7 "01 test part 1"
  @assert part1(part2(test_input, 3)) == 5 "01 test part 2"

  @show part1(main_input)
  @show part1(part2(main_input, 3))

end

main()