add day 1
This commit is contained in:
commit
14b097c61d
76
aoc.jl
Executable file
76
aoc.jl
Executable file
@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env julia
|
||||
|
||||
module Aoc
|
||||
using HTTP
|
||||
export @aoc
|
||||
|
||||
_data_path(day) = joinpath(readchomp(`git rev-parse --show-toplevel`), "data", "day_$day.txt")
|
||||
|
||||
_input(day) = readlines(_data_path(day))
|
||||
|
||||
function _cookie()
|
||||
if "AOC_TOKEN" ∈ keys(ENV)
|
||||
return Dict("session" => ENV["AOC_TOKEN"])
|
||||
end
|
||||
error("Environement variable AOC_TOKEN not set")
|
||||
end
|
||||
|
||||
function _get_input(year, day)
|
||||
res = HTTP.get("https://adventofcode.com/$year/day/$day/input", cookies = _cookie())
|
||||
if res.status ≠ 200
|
||||
error("Unable to fetch infput for AOC $year day $day")
|
||||
end
|
||||
return res.body
|
||||
end
|
||||
|
||||
function _setup_aoc(year, day)
|
||||
path = _data_path(day)
|
||||
if isfile(path)
|
||||
return
|
||||
end
|
||||
open(path, "w") do f
|
||||
write(f, _get_input(year, day))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function _run_parts(day)
|
||||
input = _input(day)
|
||||
if "precompile" ∈ ARGS
|
||||
@info "precompilation is on"
|
||||
end
|
||||
if isdefined(Main, :p1)
|
||||
if "precompile" ∈ ARGS
|
||||
i = copy(input)
|
||||
Main.p1(i)
|
||||
end
|
||||
i = copy(input)
|
||||
p1_res = @time Main.p1(i)
|
||||
@info "Part 1: $p1_res"
|
||||
else
|
||||
@info "p1() not defined. skipping..."
|
||||
end
|
||||
|
||||
if isdefined(Main, :p2)
|
||||
if "precompile" ∈ ARGS
|
||||
i = copy(input)
|
||||
Main.p2(i)
|
||||
end
|
||||
i = copy(input)
|
||||
p2_res = @time Main.p2(i)
|
||||
@info "Part 2: $p2_res"
|
||||
else
|
||||
@info "p2() not defined. skipping..."
|
||||
end
|
||||
end
|
||||
|
||||
function _run(year, day)
|
||||
_setup_aoc(year, day)
|
||||
_run_parts(day)
|
||||
end
|
||||
|
||||
macro aoc(year, day)
|
||||
return :( _run($year, $day))
|
||||
end
|
||||
|
||||
end
|
2255
data/day_1.txt
Normal file
2255
data/day_1.txt
Normal file
File diff suppressed because it is too large
Load Diff
30
src/day_1.jl
Normal file
30
src/day_1.jl
Normal file
@ -0,0 +1,30 @@
|
||||
include(joinpath(readchomp(`git rev-parse --show-toplevel`), "aoc.jl"))
|
||||
import .Aoc: @aoc
|
||||
|
||||
function split_by(λ, a::Vector)
|
||||
out = []
|
||||
tmp = []
|
||||
for e in a
|
||||
if λ(e)
|
||||
push!(out, tmp)
|
||||
tmp = []
|
||||
else
|
||||
push!(tmp, parse(Int, e))
|
||||
end
|
||||
end
|
||||
out
|
||||
end
|
||||
|
||||
calories_per_elf(in::Vector{String}) = map((x) -> sum(x), split_by(==(""), in))
|
||||
|
||||
|
||||
function p1(input::Vector{String})
|
||||
maximum(calories_per_elf(input))
|
||||
end
|
||||
|
||||
|
||||
function p2(input::Vector{String})
|
||||
sum(sort(calories_per_elf(input))[end-2:end])
|
||||
end
|
||||
|
||||
@aoc(2022, 1)
|
49
src/day_1.sh
Normal file
49
src/day_1.sh
Normal file
@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env bash
|
||||
unset PATH
|
||||
|
||||
INPUT="$(<data/day_1.txt)"
|
||||
|
||||
qsort() {
|
||||
read -ra arr <<< "$@"
|
||||
[[ "${#arr[@]}" == 0 ]] && echo "" && return
|
||||
local smaller=() bigger=() pivot="${arr[0]}" res=()
|
||||
|
||||
for i in "${arr[@]}"; do
|
||||
if [[ "$i" -lt "$pivot" ]]; then
|
||||
smaller+=("$i")
|
||||
elif [[ "$i" -gt "$pivot" ]]; then
|
||||
bigger+=("$i")
|
||||
fi
|
||||
done
|
||||
|
||||
read -ra smaller_sorted <<< "$(qsort "${smaller[@]}")"
|
||||
read -ra bigger_sorted <<< "$(qsort "${bigger[@]}")"
|
||||
res=("${smaller_sorted[@]}" "$pivot" "${bigger_sorted[@]}")
|
||||
echo "${res[@]}"
|
||||
}
|
||||
|
||||
cals_per_elf() {
|
||||
mapfile -t list <<< "$1"
|
||||
local cals=() tmp=0
|
||||
for i in "${list[@]}"; do
|
||||
[[ "$i" == '' ]] && cals+=("$tmp") && tmp=0 && continue
|
||||
((tmp+=i))
|
||||
done
|
||||
cals+=("$tmp")
|
||||
qsort "${cals[@]}"
|
||||
}
|
||||
|
||||
p1() {
|
||||
read -ra cals <<< "$(cals_per_elf "$1")"
|
||||
echo "${cals[@]: -1}"
|
||||
}
|
||||
p2() {
|
||||
read -ra cals <<< "$(cals_per_elf "$1")"
|
||||
local result=0
|
||||
for i in "${cals[@]: -3}"; do ((result+=i)); done
|
||||
echo "$result"
|
||||
}
|
||||
|
||||
p1 "$INPUT"
|
||||
p2 "$INPUT"
|
||||
|
Loading…
x
Reference in New Issue
Block a user