aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2022')
-rw-r--r--AoC2022/01/solver.jl2
-rw-r--r--AoC2022/02/solver.jl10
-rw-r--r--AoC2022/03/solver.jl7
-rw-r--r--AoC2022/04/solver.jl8
-rw-r--r--AoC2022/05/solver.jl49
-rw-r--r--AoC2022/06/solver.jl12
-rw-r--r--AoC2022/07/solver.jl1
7 files changed, 77 insertions, 12 deletions
diff --git a/AoC2022/01/solver.jl b/AoC2022/01/solver.jl
index a73deb7..104d224 100644
--- a/AoC2022/01/solver.jl
+++ b/AoC2022/01/solver.jl
@@ -1,7 +1,7 @@
using Test
rations = open("input", "r") do f
- map(x->parse.(Int, eachsplit(x)) |> sum, eachsplit(read(f, String), "\n\n")) |> sort
+ map(x -> parse.(Int, eachsplit(x)) |> sum, eachsplit(read(f, String), "\n\n")) |> sort
end
@testset "solutions" begin
diff --git a/AoC2022/02/solver.jl b/AoC2022/02/solver.jl
index b93a88f..0516fc7 100644
--- a/AoC2022/02/solver.jl
+++ b/AoC2022/02/solver.jl
@@ -2,7 +2,7 @@ using Match
using Test
plays = open("input") do f
- split.(eachsplit(read(f, String),"\n", keepempty=false))
+ split.(eachsplit(read(f, String), "\n", keepempty = false))
end
translate(play) = @match play begin
@@ -11,7 +11,7 @@ translate(play) = @match play begin
"C" || "Z" => :scissors
end
-weigth(play)= @match play begin
+weigth(play) = @match play begin
:rock => 1
:paper => 2
:scissors => 3
@@ -58,12 +58,12 @@ function reactive_play(oponent, my)
"Y" => oponent_hand
"Z" => looses(oponent_hand) # I win
_ => error("Invalid input")
- end
+ end
points(my_hand, oponent_hand) + weigth(my_hand)
end
@testset "solutions" begin
- @test map(((op, my),) -> fixed_play(op,my), plays) |> sum == 12535
- @test map(((op, my),) -> reactive_play(op,my), plays) |> sum == 15457
+ @test map(((op, my),) -> fixed_play(op, my), plays) |> sum == 12535
+ @test map(((op, my),) -> reactive_play(op, my), plays) |> sum == 15457
end
diff --git a/AoC2022/03/solver.jl b/AoC2022/03/solver.jl
index 1ebadbf..13bb2a9 100644
--- a/AoC2022/03/solver.jl
+++ b/AoC2022/03/solver.jl
@@ -4,9 +4,12 @@ data = open("input") do f
split(read(f, String))
end
-priority(x) = islowercase(x) ? x - 'a' + 1 : x - 'A' +27
+priority(x) = islowercase(x) ? x - 'a' + 1 : x - 'A' + 27
@testset "solutions" begin
- @test map(s-> intersect(s[1:length(s)÷2], s[length(s)÷2+1:end])[1] |> priority, data) |> sum == 8072
+ @test map(
+ s -> intersect(s[1:length(s)÷2], s[length(s)÷2+1:end])[1] |> priority,
+ data,
+ ) |> sum == 8072
@test [intersect(data[i:i+2]...)[1] for i ∈ 1:3:length(data)] .|> priority |> sum == 2567
end
diff --git a/AoC2022/04/solver.jl b/AoC2022/04/solver.jl
index 0f512ae..31cabce 100644
--- a/AoC2022/04/solver.jl
+++ b/AoC2022/04/solver.jl
@@ -1,15 +1,15 @@
using Test
-subinterval(a0, a1, b0, b1) = a0<= b0 && a1 >= b1
+subinterval(a0, a1, b0, b1) = a0 <= b0 && a1 >= b1
subcontained(a0, a1, b0, b1) = subinterval(a0, a1, b0, b1) || subinterval(b0, b1, a0, a1)
overlap(a0, a1, b0, b1) = a0 <= b1 && b0 <= a1
data = open("input") do f
- map(l-> parse.(Int, l), eachsplit.(eachsplit(read(f,String)), r"[,-]"))
+ map(l -> parse.(Int, l), eachsplit.(eachsplit(read(f, String)), r"[,-]"))
end
@testset "solutions" begin
- @test map(l-> subcontained(l...) ,data) |>sum == 515
- @test map(l-> overlap(l...) ,data) |>sum == 883
+ @test map(l -> subcontained(l...), data) |> sum == 515
+ @test map(l -> overlap(l...), data) |> sum == 883
end
diff --git a/AoC2022/05/solver.jl b/AoC2022/05/solver.jl
new file mode 100644
index 0000000..78ff27e
--- /dev/null
+++ b/AoC2022/05/solver.jl
@@ -0,0 +1,49 @@
+using Test
+
+function parse_input()
+ stacks, instruct = open(f -> split(read(f, String), "\n\n"), "input")
+
+ stacks_reversed = reverse(split(stacks, "\n"))
+ number = parse.(Int, eachsplit(stacks_reversed[1]))
+ crates = Dict(i => Char[] for i ∈ number)
+
+ for line ∈ stacks_reversed[2:end]
+ for (i, c) ∈ enumerate(line[2:4:end])
+ if c != ' '
+ push!(crates[i], c)
+ end
+ end
+ end
+
+ instruction_regex = r"move (\d+) from (\d+) to (\d+)"
+ crates,
+ map(
+ l -> parse.(Int, match(instruction_regex, l).captures),
+ eachsplit(instruct, "\n", keepempty = false),
+ )
+end
+
+
+function single_move!(crates, amount, from, to)
+ for _ = 1:amount
+ push!(crates[to], pop!(crates[from]))
+ end
+end
+
+function bulk_move!(crates, amount, from, to)
+ l = length(crates[from])
+ push!(crates[to], splice!(crates[from], l-amount+1:l)...)
+end
+
+function solution(mover)
+ crates, instructions = parse_input()
+ for inst ∈ instructions
+ mover(crates, inst...)
+ end
+ join(crates[i][end] for i ∈ sort(collect(keys(crates))))
+end
+
+@testset "solution" begin
+ @test solution(single_move!) == "TPGVQPFDH"
+ @test solution(bulk_move!) == "DMRDFRHHH"
+end
diff --git a/AoC2022/06/solver.jl b/AoC2022/06/solver.jl
new file mode 100644
index 0000000..0e3ed39
--- /dev/null
+++ b/AoC2022/06/solver.jl
@@ -0,0 +1,12 @@
+using Test
+
+input = open(read, "input")
+
+unique_slice(arr, i, l) = arr[i-l+1:i] |> unique |> length == l
+
+@testset "solution" begin
+ @test [
+ findfirst(unique_slice(input, i, k) for i = k:length(input)-k+1) + k - 1 for
+ k ∈ (4, 14)
+ ] == [1655, 2665]
+end
diff --git a/AoC2022/07/solver.jl b/AoC2022/07/solver.jl
new file mode 100644
index 0000000..cc57e86
--- /dev/null
+++ b/AoC2022/07/solver.jl
@@ -0,0 +1 @@
+using Test