Compare commits
2 Commits
30ef81cfac
...
f472ace665
| Author | SHA1 | Date | |
|---|---|---|---|
| f472ace665 | |||
| a9b3c90a6c |
1405
input/day25.txt
Normal file
1405
input/day25.txt
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,3 +1,3 @@
|
||||
fn main() {
|
||||
aoc_2018::tasks::day18::task2();
|
||||
aoc_2018::tasks::day25::task1();
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ pub fn task1() {
|
||||
for line in input.lines() {
|
||||
let mut counts = [0u8; 26];
|
||||
for c in line.chars() {
|
||||
counts[(c as usize - 'a' as usize)] += 1;
|
||||
counts[c as usize - 'a' as usize] += 1;
|
||||
}
|
||||
if counts.iter().any(|count| *count == 2) {
|
||||
count_two += 1;
|
||||
@@ -138,7 +138,7 @@ fn check_children_for_match(node: &Node<'_>) -> Option<String> {
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
// go over all suffixes and count their occurences. If # = 2, match!
|
||||
// go over all suffixes and count their occurrences. If # = 2, match!
|
||||
let mut suffix_counter: HashMap<&str, usize> = HashMap::new();
|
||||
for suffix_set in suffix_candidates.values() {
|
||||
for suffix in suffix_set {
|
||||
@@ -164,7 +164,7 @@ fn add_id_to_tree<'a>(root: &mut Node<'a>, id: &'a str) {
|
||||
current.same_prefix.insert(id);
|
||||
for (i, c) in id.chars().enumerate() {
|
||||
{
|
||||
let mut next = current.outgoing.entry(c).or_insert(Node::default());
|
||||
let next = current.outgoing.entry(c).or_insert(Node::default());
|
||||
next.depth = i + 1;
|
||||
next.same_prefix.insert(id);
|
||||
next.prefix = &id[..=i];
|
||||
|
||||
@@ -71,7 +71,7 @@ fn perform_round(map: &[Vec<char>], carts: &mut Vec<Cart>) {
|
||||
.collect();
|
||||
|
||||
for cart_index in 0..carts.len() {
|
||||
let mut cart = &mut carts[cart_index];
|
||||
let cart = &mut carts[cart_index];
|
||||
if !cart.active {
|
||||
continue;
|
||||
}
|
||||
|
||||
138
src/tasks/day25.rs
Normal file
138
src/tasks/day25.rs
Normal file
@@ -0,0 +1,138 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use crate::utils;
|
||||
|
||||
pub fn task1() {
|
||||
println!("{}", solve(&utils::read_file("input/day25.txt")));
|
||||
}
|
||||
|
||||
fn solve(input: &str) -> u32 {
|
||||
let mut points: Vec<STP> = input.lines().map(STP::from).collect();
|
||||
let mut constellations = 0;
|
||||
while let Some(next_origin) = points.pop() {
|
||||
constellations += 1;
|
||||
let mut queue = VecDeque::new();
|
||||
queue.push_back(next_origin);
|
||||
|
||||
while let Some(next) = queue.pop_front() {
|
||||
let (neighbors, remainder) = points.into_iter().partition(|p| next.is_neighbor_to(p));
|
||||
points = remainder;
|
||||
for n in neighbors {
|
||||
queue.push_back(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
constellations
|
||||
}
|
||||
|
||||
#[derive(Hash, Eq, PartialEq, Debug)]
|
||||
struct STP(i32, i32, i32, i32);
|
||||
|
||||
impl STP {}
|
||||
|
||||
impl STP {
|
||||
fn neighbors(&self) -> Vec<Self> {
|
||||
let mut result = Vec::new();
|
||||
for x in -3..=3 {
|
||||
for y in -3..=3 {
|
||||
for z in -3..=3 {
|
||||
for t in -3..=3 {
|
||||
let candidate = STP(self.0 + x, self.1 + y, self.2 + z, self.3 + t);
|
||||
if self.is_neighbor_to(&candidate) {
|
||||
result.push(candidate);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn distance(&self, other: &Self) -> u32 {
|
||||
((self.0 - other.0).abs()
|
||||
+ (self.1 - other.1).abs()
|
||||
+ (self.2 - other.2).abs()
|
||||
+ (self.3 - other.3).abs()) as u32
|
||||
}
|
||||
|
||||
fn is_neighbor_to(&self, other: &Self) -> bool {
|
||||
self.distance(other) <= 3
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for STP {
|
||||
fn from(value: &str) -> Self {
|
||||
let mut split = value.split(",");
|
||||
|
||||
STP(
|
||||
split.next().unwrap().parse().unwrap(),
|
||||
split.next().unwrap().parse().unwrap(),
|
||||
split.next().unwrap().parse().unwrap(),
|
||||
split.next().unwrap().parse().unwrap(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
mod test {
|
||||
use crate::tasks::day25::solve;
|
||||
|
||||
#[test]
|
||||
fn example1() {
|
||||
let input = "0,0,0,0
|
||||
3,0,0,0
|
||||
0,3,0,0
|
||||
0,0,3,0
|
||||
0,0,0,3
|
||||
0,0,0,6
|
||||
9,0,0,0
|
||||
12,0,0,0";
|
||||
assert_eq!(solve(input), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example2() {
|
||||
let input = "-1,2,2,0
|
||||
0,0,2,-2
|
||||
0,0,0,-2
|
||||
-1,2,0,0
|
||||
-2,-2,-2,2
|
||||
3,0,2,-1
|
||||
-1,3,2,2
|
||||
-1,0,-1,0
|
||||
0,2,1,-2
|
||||
3,0,0,0";
|
||||
assert_eq!(solve(input), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example3() {
|
||||
let input = "1,-1,0,1
|
||||
2,0,-1,0
|
||||
3,2,-1,0
|
||||
0,0,3,1
|
||||
0,0,-1,-1
|
||||
2,3,-2,0
|
||||
-2,2,0,0
|
||||
2,-2,0,-1
|
||||
1,-1,0,-1
|
||||
3,2,0,2
|
||||
";
|
||||
assert_eq!(solve(input), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example4() {
|
||||
let input = "1,-1,-1,-2
|
||||
-2,-2,0,1
|
||||
0,2,1,3
|
||||
-2,3,-2,1
|
||||
0,2,3,-2
|
||||
-1,-1,1,-2
|
||||
0,-2,-1,0
|
||||
-2,2,3,-1
|
||||
1,2,2,0
|
||||
-1,-2,0,-2
|
||||
";
|
||||
assert_eq!(solve(input), 8);
|
||||
}
|
||||
}
|
||||
@@ -19,3 +19,4 @@ pub mod day20;
|
||||
pub mod day22;
|
||||
pub mod day23;
|
||||
pub mod day24;
|
||||
pub mod day25;
|
||||
|
||||
Reference in New Issue
Block a user