day 23 puzzle 2
This commit is contained in:
parent
d830919adc
commit
16a0d9b3f8
BIN
progress.png
BIN
progress.png
Binary file not shown.
Before Width: | Height: | Size: 134 KiB After Width: | Height: | Size: 134 KiB |
@ -43,4 +43,4 @@
|
|||||||
22:
|
22:
|
||||||
stars: 1
|
stars: 1
|
||||||
23:
|
23:
|
||||||
stars: 1
|
stars: 2
|
@ -0,0 +1,65 @@
|
|||||||
|
#import "/src/utils.typ": *
|
||||||
|
|
||||||
|
#let bron-kerbosch(links, R, P, X) = {
|
||||||
|
if P.len() == 0 and X.len() == 0 {
|
||||||
|
return if R.len() > 2 {
|
||||||
|
R.sorted()
|
||||||
|
} else {
|
||||||
|
none
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let longest-len = 0
|
||||||
|
let longest = none
|
||||||
|
let to-visit = P
|
||||||
|
for v in to-visit {
|
||||||
|
let neighbors = links.at(v)
|
||||||
|
let clique = bron-kerbosch(
|
||||||
|
links,
|
||||||
|
R + (v,),
|
||||||
|
P.filter(n => n in neighbors),
|
||||||
|
X.filter(n => n in neighbors)
|
||||||
|
)
|
||||||
|
if clique != none {
|
||||||
|
let l = clique.len()
|
||||||
|
if longest == none or l > longest-len {
|
||||||
|
longest = clique
|
||||||
|
longest-len = l
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let _ = P.remove(0)
|
||||||
|
X.push(v)
|
||||||
|
}
|
||||||
|
return longest
|
||||||
|
}
|
||||||
|
|
||||||
|
#let solve(input) = {
|
||||||
|
let links = input.split("\n")
|
||||||
|
|
||||||
|
let links-dict = (:)
|
||||||
|
|
||||||
|
let to-test = ()
|
||||||
|
for link in links {
|
||||||
|
let (a, b) = link.split("-")
|
||||||
|
if a not in links-dict {
|
||||||
|
links-dict.insert(a, ())
|
||||||
|
}
|
||||||
|
if b not in links-dict {
|
||||||
|
links-dict.insert(b, ())
|
||||||
|
}
|
||||||
|
links-dict.at(a).push(b)
|
||||||
|
links-dict.at(b).push(a)
|
||||||
|
}
|
||||||
|
|
||||||
|
let clique = bron-kerbosch(links-dict, (), links-dict.keys(), ())
|
||||||
|
|
||||||
|
return clique.join(",")
|
||||||
|
}
|
||||||
|
|
||||||
|
#show-puzzle(
|
||||||
|
23, 2,
|
||||||
|
solve,
|
||||||
|
example: "co,de,ka,ta",
|
||||||
|
only-example: true
|
||||||
|
)
|
||||||
|
#show-result("ab,al,cq,cr,da,db,dr,fw,ly,mn,od,py,uh")
|
BIN
src/main.pdf
BIN
src/main.pdf
Binary file not shown.
Loading…
Reference in New Issue
Block a user