Skip to content

Commit 5cf16c7

Browse files
committed
[Day 23] Add solution
1 parent 739e5c8 commit 5cf16c7

File tree

5 files changed

+110
-3
lines changed

5 files changed

+110
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ This Julia package contains my solutions for [Advent of Code 2024](https://adven
3131
| 19 | [:white_check_mark:](https://adventofcode.com/2024/day/19) | 59.678 ms | 3.41 MiB| [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day19.jl) |
3232
| 20 | [:white_check_mark:](https://adventofcode.com/2024/day/20) | 227.453 ms | 224.97 MiB| [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day20.jl) |
3333
| 22 | [:white_check_mark:](https://adventofcode.com/2024/day/22) | 241.724 ms | 78.80 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day22.jl) |
34+
| 23 | [:white_check_mark:](https://adventofcode.com/2024/day/23) | 4.197 ms | 3.82 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day23.jl) |
3435
<!-- | 21 | [:white_check_mark:](https://adventofcode.com/2024/day/21) | 9.675 ms | 7.19 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day21.jl) | -->
35-
<!-- | 23 | [:white_check_mark:](https://adventofcode.com/2024/day/23) | 2.979 s | 9.69 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day23.jl) | -->
3636
<!-- | 24 | [:white_check_mark:](https://adventofcode.com/2024/day/24) | 43.214 ms | 49.77 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day24.jl) | -->
3737
<!-- | 25 | [:white_check_mark:](https://adventofcode.com/2024/day/25) | 69.476 ms | 62.03 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day25.jl) | -->
3838

data

Submodule data updated from 4340d98 to 0f380de

src/AdventOfCode2024.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module AdventOfCode2024
33
using BenchmarkTools
44
using Printf
55

6-
solvedDays = 1:22
6+
solvedDays = 1:23
77
# Include the source files:
88
for day in solvedDays
99
ds = @sprintf("%02d", day)

src/day23.jl

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
module Day23
2+
3+
using AdventOfCode2024
4+
5+
6+
function day23(input::String = readInput(joinpath(@__DIR__, "..", "data", "day23.txt")))
7+
connections = split.(split(rstrip(input), '\n'), '-')
8+
network = zeros(Bool, _computer_to_index("zz"), _computer_to_index("zz"))
9+
for (a, b) connections
10+
ind_a = _computer_to_index(a)
11+
ind_b = _computer_to_index(b)
12+
network[ind_a, ind_b] = true
13+
network[ind_b, ind_a] = true
14+
end
15+
triangles = find_triangles(network)
16+
p1 = count(any(t[1] == 't' for t tri) for tri triangles)
17+
return [p1, join(_index_to_computer.(find_largest_network(network)) |> sort, ',')]
18+
end
19+
20+
function parse_input(input)
21+
split.(split(rstrip(input), '\n'), '-')
22+
end
23+
24+
function find_triangles(network::Matrix{Bool})
25+
triangles = Tuple{String,String,String}[]
26+
for i axes(network, 1)
27+
for j i + 1:size(network,2)
28+
if network[i, j]
29+
for k j + 1:size(network,2)
30+
if network[j, k] && network[k, i]
31+
push!(triangles, (_index_to_computer(i), _index_to_computer(j), _index_to_computer(k)))
32+
end
33+
end
34+
end
35+
end
36+
end
37+
return triangles
38+
end
39+
40+
function find_largest_network(network::Matrix{Bool})
41+
visited = zeros(Bool, size(network,1))
42+
largest_component = Int[]
43+
44+
for vertex 1:size(network, 1)
45+
if !visited[vertex]
46+
current_component = Int[]
47+
component = dfs!(current_component, visited, network, vertex)
48+
if length(component) > length(largest_component)
49+
largest_component = copy(component)
50+
end
51+
end
52+
end
53+
return largest_component
54+
end
55+
56+
function dfs!(component::Vector{Int}, visited::Vector{Bool}, network::Matrix{Bool}, vertex::Int)
57+
visited[vertex] = true
58+
push!(component, vertex)
59+
for neighbour 1:size(network, 1)
60+
if !visited[neighbour] && network[vertex, neighbour] && all(network[neighbour, comp] for comp component)
61+
dfs!(component, visited, network, neighbour)
62+
end
63+
end
64+
return component
65+
end
66+
67+
_computer_to_index(c::AbstractString) = (Int(c[1] - Int('a'))) * 26 + (Int(c[2]) - Int('a'))
68+
_index_to_computer(index::Int) = Char(Int('a') + index ÷ 26) * Char(Int('a') + index % 26)
69+
70+
end # module

test/runtests.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,41 @@ end
342342
@test AdventOfCode2024.Day22.day22(sample2) == [37990510, 23]
343343

344344
@test AdventOfCode2024.Day22.day22() == [13185239446, 1501]
345+
end
346+
347+
@testset "Day 23" begin
348+
sample = "kh-tc\n" *
349+
"qp-kh\n" *
350+
"de-cg\n" *
351+
"ka-co\n" *
352+
"yn-aq\n" *
353+
"qp-ub\n" *
354+
"cg-tb\n" *
355+
"vc-aq\n" *
356+
"tb-ka\n" *
357+
"wh-tc\n" *
358+
"yn-cg\n" *
359+
"kh-ub\n" *
360+
"ta-co\n" *
361+
"de-co\n" *
362+
"tc-td\n" *
363+
"tb-wq\n" *
364+
"wh-td\n" *
365+
"ta-ka\n" *
366+
"td-qp\n" *
367+
"aq-cg\n" *
368+
"wq-ub\n" *
369+
"ub-vc\n" *
370+
"de-ta\n" *
371+
"wq-aq\n" *
372+
"wq-vc\n" *
373+
"wh-yn\n" *
374+
"ka-de\n" *
375+
"kh-ta\n" *
376+
"co-tc\n" *
377+
"wh-qp\n" *
378+
"tb-vc\n" *
379+
"td-yn\n"
380+
@test AdventOfCode2024.Day23.day23(sample) == [7, "co,de,ka,ta"]
381+
@test AdventOfCode2024.Day23.day23() == [1368, "dd,ig,il,im,kb,kr,pe,ti,tv,vr,we,xu,zi"]
345382
end

0 commit comments

Comments
 (0)