Skip to content

Instantly share code, notes, and snippets.

@RuyiLi
Last active January 21, 2020 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RuyiLi/0a12544174c84aa210deb9ab7ac3af7e to your computer and use it in GitHub Desktop.
Save RuyiLi/0a12544174c84aa210deb9ab7ac3af7e to your computer and use it in GitHub Desktop.
AOC 2018 Solutions For Google Code-In
# Author: weCryOpen (Ruyi Li)
function d1p1(file)
open(file) do f
total = 0
for line in eachline(f)
# Iterate through each line in the file
# Convert it to an integer,
n = parse(Int, line)
# and add it to the total.
total += n
end
# Return the total.
total
end
end
# Author: weCryOpen (Ruyi Li)
function d1p2(file)
# Keep track of the list of instructions so we can
# cycle over it again for future iterations.
lst = Array{Int, 1}()
# Keeps track of if a value is already present
# within the set
freq = Set{Int32}()
curr = 0
open(file) do f
for line in eachline(f)
# Iterate through each line in the file
# and convert it to an integer.
n = parse(Int32, line)
# Add it to the list of instructions.
push!(lst, n)
# Add the numbers to the current total
curr += n
# If the current number has already appeared,
# stop the function early and return it.
curr in freq && return curr
# If not, add it to the set.
push!(freq, curr)
end
end
# Cycle through the instructions and perform the
# same tasks as earlier until we encounter a duplicate.
for (_, n) in enumerate(Iterators.cycle(lst))
curr += n
curr in freq && return curr
push!(freq, curr)
end
end
# Author: weCryOpen (Ruyi Li)
function d2p1(file)
open(file) do f
# Keep track of the number of 2s and 3s
twos = threes = 0
for line in eachline(f)
# Frequency Dictionary
freq = Dict{Char, Int}()
for chr in line
# Loop through each character in each line
# in the file
if haskey(freq, chr)
# If the key exists, add 1 to it
freq[chr] += 1
else
# Otherwise, set it to 1
freq[chr] = 1
end
end
# true evaluates to 1 and false to 0, so we
# can directly add the booleans to the
# frequency trackers if 2 or 3 appear at least
# once within the dictionary's values.
twos += 2 in values(freq)
threes += 3 in values(freq)
end
# Return the product of the frequencies.
twos * threes
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment