Skip to content

Commit

Permalink
day02
Browse files Browse the repository at this point in the history
FrederikSchnack committed Dec 2, 2024
1 parent 1038039 commit e33f7d4
Showing 4 changed files with 62 additions and 10 deletions.
2 changes: 1 addition & 1 deletion inputs
14 changes: 5 additions & 9 deletions solutions/2024/src/day01.jl
Original file line number Diff line number Diff line change
@@ -9,18 +9,14 @@ module Day01
"""

function day01(input::String = readInput(01))
lines = split(input, "\n")
r = r"(\d+)\s*(\d+)"

left = Int[]
right = Int[]

for l in lines
d1, d2 = match(r, l).captures

push!(left, parse(Int, d1))
push!(right, parse(Int, d2))
end
r = r"(\d+)\s*(\d+)\n?"
for m in eachmatch(r, input)
push!(left, parse(Int, m.captures[1]))
push!(right, parse(Int, m.captures[2]))
end

sort!(left)
sort!(right)
51 changes: 51 additions & 0 deletions solutions/2024/src/day02.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module Day02
using ..AdventOfCode24

"""
day02()
Solves the two puzzles of day 02.
"""

function day02(input::String = readInput(02))
r = r"(\d+)"

s0 = 0
s1 = 0

for l in split(input, "\n")

n = [parse(Int, m.match) for m in eachmatch(r, l)]

if is_safe(n)
s0 += 1
s1 += 1
else

for k in eachindex(n)
# n0 = deleteat!(copy(n), k)
if is_safe(n[begin:end .!= k])
s1 += 1
break
end
end

end

end

return [s0, s1]
end


function is_safe(n::Vector{Int})
diffs = diff(n)

if all(@. 0 < diffs <= 3 ) || all(@. 0 > diffs >= -3)
return true
else
return false
end
end

end
5 changes: 5 additions & 0 deletions solutions/2024/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -3,3 +3,8 @@ using AdventOfCode.AdventOfCode24
@testset "Day 01" begin
@test AdventOfCode24.Day01.day01() == [2970687, 23963899]
end

@testset "Day 02" begin
@test AdventOfCode24.Day02.day02() == [356 , 413]
end

0 comments on commit e33f7d4

Please sign in to comment.