Day 02
This commit is contained in:
parent
bdb00950fa
commit
0a7b93e8ab
23
src/Year_2024/Day_02.jl
Normal file
23
src/Year_2024/Day_02.jl
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
using BenchmarkTools
|
||||||
|
using StatsBase
|
||||||
|
|
||||||
|
function is_safe(report)
|
||||||
|
diffs = diff(report)
|
||||||
|
all(3 .>= diffs .> 0) || all(0 .> diffs .>= -3)
|
||||||
|
end
|
||||||
|
|
||||||
|
function is_safer(report)
|
||||||
|
any(is_safe(vcat(report[1:i-1], report[i+1:end])) for i in 1:length(report))
|
||||||
|
end
|
||||||
|
|
||||||
|
function Day_02()
|
||||||
|
reports = [parse.(Int, split(line)) for line in readlines("files/P2.txt")]
|
||||||
|
|
||||||
|
p1 = count(is_safe, reports)
|
||||||
|
p2 = count(is_safer, reports)
|
||||||
|
|
||||||
|
return p1, p2
|
||||||
|
end
|
||||||
|
|
||||||
|
println(Day_02())
|
||||||
|
@btime Day_02()
|
29
src/Year_2024/Day_02.py
Normal file
29
src/Year_2024/Day_02.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
from utils import timeit
|
||||||
|
|
||||||
|
|
||||||
|
def is_safe(report):
|
||||||
|
diff = [(int(j) - int(i)) for i, j in zip(report[:-1], report[1:])]
|
||||||
|
return all(3 >= num > 0 for num in diff) or all(
|
||||||
|
0 > num >= -3 for num in diff
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def is_safer(report):
|
||||||
|
return any(
|
||||||
|
is_safe([*report[:i], *report[i + 1 :]]) for i in range(len(report))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@timeit()
|
||||||
|
def Day_02():
|
||||||
|
with open("files/P2.txt") as f:
|
||||||
|
reports = [lines.split() for lines in f.read().strip().split("\n")]
|
||||||
|
|
||||||
|
p1 = sum(1 for level in reports if is_safe(level))
|
||||||
|
p2 = sum(1 for report in reports if is_safer(report))
|
||||||
|
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
Day_02()
|
1000
src/Year_2024/files/P2.txt
Normal file
1000
src/Year_2024/files/P2.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user