|
1 |
| -use crate::input::Input; |
| 1 | +use std::collections::{HashMap, HashSet}; |
2 | 2 |
|
3 |
| -pub const fn solve(_input: &Input) -> Result<u32, String> { |
4 |
| - Ok(0) |
| 3 | +use crate::{ |
| 4 | + common::{array_stack::ArrayStack, id_assigner::IdAssigner}, |
| 5 | + input::{on_error, Input}, |
| 6 | + year2017::disjoint_set::DisjointSet, |
| 7 | +}; |
| 8 | + |
| 9 | +pub fn solve(input: &Input) -> Result<u32, String> { |
| 10 | + let mut with_t = ArrayStack::<1000, u16>::new(); |
| 11 | + let mut to_adjacency = HashMap::new(); |
| 12 | + |
| 13 | + for line in input.text.lines() { |
| 14 | + let (s1, s2) = line.split_once('-').ok_or_else(on_error)?; |
| 15 | + let (n1, n2) = (to_num(s1), to_num(s2)); |
| 16 | + |
| 17 | + to_adjacency.entry(n1).or_insert_with(Vec::new).push(n2); |
| 18 | + to_adjacency.entry(n2).or_insert_with(Vec::new).push(n1); |
| 19 | + |
| 20 | + if s1.starts_with('t') { |
| 21 | + with_t.push(n1)?; |
| 22 | + } |
| 23 | + if s2.starts_with('t') { |
| 24 | + with_t.push(n2)?; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + let mut already_considered = HashSet::new(); |
| 29 | + let mut result = 0; |
| 30 | + let slice = with_t.slice_mut(); |
| 31 | + slice.sort_unstable(); |
| 32 | + let mut last = u16::MAX; |
| 33 | + for n in slice { |
| 34 | + if *n != last { |
| 35 | + let adjacency_list = to_adjacency.get(n).unwrap(); |
| 36 | + for (i, x) in adjacency_list.iter().enumerate() { |
| 37 | + for (j, y) in adjacency_list.iter().enumerate().skip(i + 1) { |
| 38 | + if to_adjacency.get(x).unwrap().iter().any(|e| e == y) { |
| 39 | + let mut aa = [*n, *x, *y]; |
| 40 | + aa.sort_unstable(); |
| 41 | + if already_considered.insert(aa) { |
| 42 | + result += 1; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + last = *n; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + Ok(result) |
| 52 | +} |
| 53 | + |
| 54 | +fn to_num(p: &str) -> u16 { |
| 55 | + let p = p.as_bytes(); |
| 56 | + u16::from(p[0] - b'a') + 29 * u16::from(p[1] - b'a') |
5 | 57 | }
|
6 | 58 |
|
7 | 59 | #[test]
|
8 | 60 | pub fn tests() {
|
9 | 61 | use crate::input::{test_part_one_no_allocations, test_part_two_no_allocations};
|
10 | 62 |
|
11 |
| - let test_input = ""; |
12 |
| - test_part_one_no_allocations!(test_input => 0); |
13 |
| - test_part_two_no_allocations!(test_input => 0); |
| 63 | + let test_input = "kh-tc |
| 64 | +qp-kh |
| 65 | +de-cg |
| 66 | +ka-co |
| 67 | +yn-aq |
| 68 | +qp-ub |
| 69 | +cg-tb |
| 70 | +vc-aq |
| 71 | +tb-ka |
| 72 | +wh-tc |
| 73 | +yn-cg |
| 74 | +kh-ub |
| 75 | +ta-co |
| 76 | +de-co |
| 77 | +tc-td |
| 78 | +tb-wq |
| 79 | +wh-td |
| 80 | +ta-ka |
| 81 | +td-qp |
| 82 | +aq-cg |
| 83 | +wq-ub |
| 84 | +ub-vc |
| 85 | +de-ta |
| 86 | +wq-aq |
| 87 | +wq-vc |
| 88 | +wh-yn |
| 89 | +ka-de |
| 90 | +kh-ta |
| 91 | +co-tc |
| 92 | +wh-qp |
| 93 | +tb-vc |
| 94 | +td-yn"; |
| 95 | + test_part_one_no_allocations!(test_input => 7); |
| 96 | + //test_part_two_no_allocations!(test_input => 0); |
14 | 97 |
|
15 | 98 | let real_input = include_str!("day23_input.txt");
|
16 |
| - test_part_one_no_allocations!(real_input => 0); |
17 |
| - test_part_two_no_allocations!(real_input => 0); |
| 99 | + test_part_one_no_allocations!(real_input => 1175); |
| 100 | + //test_part_two_no_allocations!(real_input => 0); |
18 | 101 | }
|
0 commit comments