-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution_2017_14.rs
83 lines (67 loc) · 1.86 KB
/
solution_2017_14.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use std::collections::HashSet;
use advent_of_code_2017::knot_hash::knot_hash_as_u8;
use advent_of_code_common::coords2d::Coords2D;
use pathfinding::prelude::connected_components;
type Coords = Coords2D<i32>;
fn bytes(input: &str) -> Vec<Vec<u8>> {
(0u8 .. 128)
.map(|x| {
let s = format!("{input}-{x}");
knot_hash_as_u8(&s)
})
.collect()
}
fn part_1(input: &str) -> u32 {
let b = bytes(input);
b.iter()
.map(|r| r.iter().map(|x| x.count_ones()).sum::<u32>())
.sum()
}
fn part_2(input: &str) -> usize {
let b = bytes(input);
let mut graph: HashSet<Coords> = HashSet::new();
for (y, r) in b.iter().enumerate() {
for (idx, c) in r.iter().enumerate() {
for bit_idx in 0usize .. 8 {
let is_set = c & (1 << bit_idx) > 0;
if is_set {
let x = idx * 8 + (8 - bit_idx);
let coords = Coords::new(i32::try_from(x).unwrap(), i32::try_from(y).unwrap());
graph.insert(coords);
}
}
}
}
let starts: Vec<Coords> = graph.iter().copied().collect::<Vec<_>>();
connected_components(&starts, |x| {
x.adjacent4()
.into_iter()
.filter(|n| graph.contains(n))
.collect::<Vec<_>>()
})
.len()
}
const DATA: &str = "xlqgujun";
fn main() {
let result_1 = part_1(DATA);
println!("Part 1: {result_1}");
let result_2 = part_2(DATA);
println!("Part 2: {result_2}");
}
#[cfg(test)]
mod tests {
use super::*;
const TEST: &str = "flqrgnkx";
#[test]
fn test_solve_1_real() {
assert_eq!(part_1(DATA), 8204);
}
#[test]
fn test_solve_2_test() {
assert_eq!(part_2(TEST), 1242);
}
#[test]
fn test_solve_2_real() {
assert_eq!(part_2(DATA), 1089);
}
}