generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86aad75
commit a13cca3
Showing
7 changed files
with
202 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,5 +14,6 @@ doctest = false | |
test_lib = [] | ||
|
||
[dependencies] | ||
gcd = "2.3.0" | ||
itertools = "0.12.0" | ||
pico-args = "0.5.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
RL | ||
|
||
AAA = (BBB, CCC) | ||
BBB = (DDD, EEE) | ||
CCC = (ZZZ, GGG) | ||
DDD = (DDD, DDD) | ||
EEE = (EEE, EEE) | ||
GGG = (GGG, GGG) | ||
ZZZ = (ZZZ, ZZZ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
LLR | ||
|
||
AAA = (BBB, BBB) | ||
BBB = (AAA, ZZZ) | ||
ZZZ = (ZZZ, ZZZ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
LR | ||
|
||
11A = (11B, XXX) | ||
11B = (XXX, 11Z) | ||
11Z = (11B, XXX) | ||
22A = (22B, XXX) | ||
22B = (22C, 22C) | ||
22C = (22Z, 22Z) | ||
22Z = (22B, 22B) | ||
XXX = (XXX, XXX) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
use gcd::Gcd; | ||
use std::collections::HashMap; | ||
advent_of_code::solution!(8); | ||
|
||
#[derive(Copy, Clone, Debug)] | ||
enum Direction { | ||
Left, | ||
Right, | ||
} | ||
|
||
impl From<char> for Direction { | ||
fn from(value: char) -> Self { | ||
match value { | ||
'L' => Self::Left, | ||
'R' => Self::Right, | ||
_ => panic!(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, Hash, Eq, PartialOrd, PartialEq, Ord)] | ||
struct Node { | ||
inner: [char; 3], | ||
} | ||
|
||
impl Node { | ||
pub fn new(values: [char; 3]) -> Self { | ||
Self { inner: values } | ||
} | ||
|
||
pub fn start() -> Self { | ||
Self::new(['A', 'A', 'A']) | ||
} | ||
} | ||
|
||
impl From<&str> for Node { | ||
fn from(value: &str) -> Self { | ||
Self::new( | ||
value | ||
.chars() | ||
.filter(|c| *c != '(' && *c != ')') | ||
.collect::<Vec<char>>() | ||
.try_into() | ||
.unwrap(), | ||
) | ||
} | ||
} | ||
|
||
fn solve<F>( | ||
from: Node, | ||
map: &HashMap<Node, (Node, Node)>, | ||
sequence: &[Direction], | ||
target_evaluation: F, | ||
) -> (usize, Node) | ||
where | ||
F: Fn(Node) -> bool, | ||
{ | ||
let mut current = from; | ||
let mut steps = 0; | ||
|
||
for direction in sequence.iter().cycle() { | ||
steps += 1; | ||
|
||
let (left, right) = map[¤t]; | ||
|
||
current = match direction { | ||
Direction::Left => left, | ||
Direction::Right => right, | ||
}; | ||
|
||
if target_evaluation(current) { | ||
break; | ||
} | ||
} | ||
|
||
(steps, current) | ||
} | ||
|
||
pub fn part_one(input: &str) -> Option<usize> { | ||
let mut lines = input.lines(); | ||
let sequence: Vec<Direction> = lines.next().unwrap().chars().map(Direction::from).collect(); | ||
|
||
let nodes_to_indices: HashMap<Node, (Node, Node)> = lines | ||
.skip(1) | ||
.map(|line| { | ||
let (departure, targets) = line.split_once(" = ").unwrap(); | ||
let (left, right) = targets.split_once(", ").unwrap(); | ||
|
||
(Node::from(departure), (Node::from(left), Node::from(right))) | ||
}) | ||
.collect(); | ||
|
||
Some( | ||
solve(Node::start(), &nodes_to_indices, &sequence, |node| { | ||
node.inner == ['Z', 'Z', 'Z'] | ||
}) | ||
.0, | ||
) | ||
} | ||
|
||
pub fn part_two(input: &str) -> Option<usize> { | ||
let mut lines = input.lines(); | ||
let sequence: Vec<Direction> = lines.next().unwrap().chars().map(Direction::from).collect(); | ||
|
||
let nodes_to_indices: HashMap<Node, (Node, Node)> = lines | ||
.skip(1) | ||
.map(|line| { | ||
let (departure, targets) = line.split_once(" = ").unwrap(); | ||
let (left, right) = targets.split_once(", ").unwrap(); | ||
|
||
(Node::from(departure), (Node::from(left), Node::from(right))) | ||
}) | ||
.collect(); | ||
|
||
let starting_positions: Vec<Node> = nodes_to_indices | ||
.keys() | ||
.filter(|node| node.inner[2] == 'A') | ||
.cloned() | ||
.collect(); | ||
|
||
Some( | ||
starting_positions | ||
.iter() | ||
.map(|starting_position| { | ||
let (steps_to_first_target, _) = | ||
solve(*starting_position, &nodes_to_indices, &sequence, |node| { | ||
node.inner[2] == 'Z' | ||
}); | ||
|
||
steps_to_first_target | ||
}) | ||
.fold(1, |acc, next| (acc * next) / acc.gcd(next)), | ||
) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_part_one() { | ||
let result = part_one(&advent_of_code::template::read_file_part( | ||
"examples", DAY, 1, | ||
)); | ||
assert_eq!(result, Some(2)); | ||
|
||
let result = part_one(&advent_of_code::template::read_file_part( | ||
"examples", DAY, 2, | ||
)); | ||
assert_eq!(result, Some(6)); | ||
} | ||
|
||
#[test] | ||
fn test_part_two() { | ||
let result = part_two(&advent_of_code::template::read_file_part( | ||
"examples", DAY, 3, | ||
)); | ||
assert_eq!(result, Some(6)); | ||
} | ||
} |