generated from janezicmatej/aoc-template
Compare commits
2 Commits
0000030073
...
000003208e
Author | SHA1 | Date |
---|---|---|
Matej Janezic | 000003208e | |
Matej Janezic | 000003103e |
|
@ -0,0 +1,32 @@
|
|||
kh-tc
|
||||
qp-kh
|
||||
de-cg
|
||||
ka-co
|
||||
yn-aq
|
||||
qp-ub
|
||||
cg-tb
|
||||
vc-aq
|
||||
tb-ka
|
||||
wh-tc
|
||||
yn-cg
|
||||
kh-ub
|
||||
ta-co
|
||||
de-co
|
||||
tc-td
|
||||
tb-wq
|
||||
wh-td
|
||||
ta-ka
|
||||
td-qp
|
||||
aq-cg
|
||||
wq-ub
|
||||
ub-vc
|
||||
de-ta
|
||||
wq-aq
|
||||
wq-vc
|
||||
wh-yn
|
||||
ka-de
|
||||
kh-ta
|
||||
co-tc
|
||||
wh-qp
|
||||
tb-vc
|
||||
td-yn
|
|
@ -200,10 +200,16 @@ mod tests {
|
|||
use super::*;
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
assert_eq!(part_one(&aoc::template::read_file("examples", 15)), Some(10092));
|
||||
assert_eq!(
|
||||
part_one(&aoc::template::read_file("examples", 15)),
|
||||
Some(10092)
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
assert_eq!(part_two(&aoc::template::read_file("examples", 15)), Some(9021));
|
||||
assert_eq!(
|
||||
part_two(&aoc::template::read_file("examples", 15)),
|
||||
Some(9021)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -91,4 +91,3 @@ pub fn part_two(input: &str) -> Option<String> {
|
|||
}
|
||||
|
||||
aoc::solution!(18);
|
||||
|
||||
|
|
|
@ -66,6 +66,9 @@ mod tests {
|
|||
}
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
assert_eq!(part_two(&aoc::template::read_file("examples", 19)), Some(16));
|
||||
assert_eq!(
|
||||
part_two(&aoc::template::read_file("examples", 19)),
|
||||
Some(16)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,88 @@
|
|||
use std::collections::BTreeSet;
|
||||
|
||||
use hashbrown::{HashMap, HashSet};
|
||||
use itertools::Itertools;
|
||||
|
||||
fn parse_graph(input: &str) -> HashMap<&str, BTreeSet<&str>> {
|
||||
let nodes: HashSet<_> = input.lines().filter_map(|x| x.split_once('-')).collect();
|
||||
let mut graph: HashMap<_, BTreeSet<_>> = HashMap::new();
|
||||
for (n1, n2) in nodes.iter().copied() {
|
||||
graph.entry(n1).or_default().insert(n2);
|
||||
graph.entry(n2).or_default().insert(n1);
|
||||
}
|
||||
|
||||
graph
|
||||
}
|
||||
|
||||
pub fn part_one(input: &str) -> Option<usize> {
|
||||
let graph = parse_graph(input);
|
||||
|
||||
let mut triples = HashSet::new();
|
||||
for (n1, neigh) in graph.iter() {
|
||||
for n2 in neigh.iter().copied() {
|
||||
for n3 in graph[n2].iter() {
|
||||
if graph[n3].contains(n1) && n1.starts_with('t') {
|
||||
let mut triple = [n1, n2, n3];
|
||||
triple.sort();
|
||||
triples.insert(triple);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
triples.len().into()
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> Option<String> {
|
||||
let graph = parse_graph(input);
|
||||
|
||||
let p: BTreeSet<_> = graph.keys().copied().collect();
|
||||
let r = HashSet::new();
|
||||
let x = BTreeSet::new();
|
||||
|
||||
let mut maximals = HashSet::new();
|
||||
|
||||
let mut stack = Vec::new();
|
||||
stack.push((r, p, x));
|
||||
|
||||
while let Some((r, mut p, mut x)) = stack.pop() {
|
||||
if p.is_empty() && x.is_empty() {
|
||||
let mut password = r.iter().copied().collect_vec();
|
||||
password.sort();
|
||||
|
||||
maximals.insert(password.join(","));
|
||||
}
|
||||
|
||||
while let Some(pp) = p.pop_last() {
|
||||
let mut nr = r.clone();
|
||||
nr.insert(pp);
|
||||
|
||||
let np = &p & &graph[pp];
|
||||
let nx = &x & &graph[pp];
|
||||
|
||||
stack.push((nr, np, nx));
|
||||
|
||||
x.insert(pp);
|
||||
}
|
||||
}
|
||||
|
||||
maximals.into_iter().max_by_key(|x| x.len())
|
||||
}
|
||||
|
||||
aoc::solution!(23);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
assert_eq!(part_one(&aoc::template::read_file("examples", 23)), Some(7));
|
||||
}
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
assert_eq!(
|
||||
part_two(&aoc::template::read_file("examples", 23)),
|
||||
Some("co,de,ka,ta".to_string())
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,9 +1,7 @@
|
|||
mod grid;
|
||||
mod direction;
|
||||
mod grid;
|
||||
mod point;
|
||||
|
||||
pub use grid::Grid;
|
||||
pub use direction::Direction;
|
||||
pub use grid::Grid;
|
||||
pub use point::Point;
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue