1
0
Fork 0

Compare commits

...

3 Commits

Author SHA1 Message Date
Andrew Coleman c43f19f2e3 day 08 solution 2023-12-08 13:44:47 -05:00
Andrew Coleman 5eea26dd13 add helpful crates for problem solving 2023-12-08 13:44:41 -05:00
Andrew Coleman 62099a8bfc remove println debugging 2023-12-08 13:44:26 -05:00
7 changed files with 335 additions and 4 deletions

174
2023/Cargo.lock generated
View File

@ -2,6 +2,29 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
dependencies = [
"memchr",
]
[[package]]
name = "aoc-parse"
version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b5d6981822b91def794ea9433e7547dc607e601dbd03b864dbe307a53af5798"
dependencies = [
"lazy_static",
"num-bigint",
"num-traits",
"regex",
"thiserror",
"tuple_utils",
]
[[package]]
name = "aoc-runner"
version = "0.3.0"
@ -35,16 +58,112 @@ dependencies = [
name = "aoc2023"
version = "0.1.0"
dependencies = [
"aoc-parse",
"aoc-runner",
"aoc-runner-derive",
"num",
]
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "itoa"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "memchr"
version = "2.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
[[package]]
name = "num"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af"
dependencies = [
"num-bigint",
"num-complex",
"num-integer",
"num-iter",
"num-rational",
"num-traits",
]
[[package]]
name = "num-bigint"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-complex"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214"
dependencies = [
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-iter"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
"autocfg",
"num-bigint",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
dependencies = [
"autocfg",
]
[[package]]
name = "proc-macro2"
version = "1.0.70"
@ -63,6 +182,35 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "regex"
version = "1.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]]
name = "ryu"
version = "1.0.15"
@ -122,6 +270,32 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "1.0.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.39",
]
[[package]]
name = "tuple_utils"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cffaaf9392ef73cd30828797152476aaa2fa37a17856934fa63d4843f34290e9"
[[package]]
name = "unicode-ident"
version = "1.0.12"

View File

@ -10,3 +10,5 @@ path = "src/lib.rs"
[dependencies]
aoc-runner = "0.3.0"
aoc-runner-derive = "0.3.0"
aoc-parse = "0.2.17"
num = "0.4"

View File

@ -55,7 +55,6 @@ fn letter_digit_at(line: &str, index: usize) -> Option<u32> {
let index2 = index + d.len();
let substring: &str = line[index..index2].as_ref();
if *d == substring {
// println!("compare {} dindex {} d {}", index, dindex, d);
return Some(dindex as u32);
}
}
@ -90,7 +89,6 @@ fn sub_letters_for_digits(input: &str) -> Vec<u32> {
}
}
let num = first_digit * 10 + second_digit;
println!("line {} num {}", line, num);
Some(num)
} else {
None

View File

@ -70,7 +70,7 @@ fn parse(input: &str) -> Vec<Game> {
"red" => red = count,
"green" => green = count,
"blue" => blue = count,
_ => println!("invalid color! {}", dice_parts[0]),
_ => panic!("invalid color! {}", dice_parts[0]),
}
}
turns.push(Turn { red, green, blue });

View File

@ -84,7 +84,6 @@ fn associate_parts(input: &str) -> PartList {
}
}
}
// println!("{:?}", result);
result
}

157
2023/src/day08.rs Normal file
View File

@ -0,0 +1,157 @@
use aoc_runner_derive::aoc;
use num::integer::lcm;
use std::collections::{BTreeMap, HashSet};
type NodePath<'a> = BTreeMap<&'a str, &'a str>;
struct Path<'a> {
path: Vec<&'a str>,
left: NodePath<'a>,
right: NodePath<'a>,
}
fn parse(input: &str) -> Path {
let mut lines = input.lines();
let path = lines
.next()
.unwrap()
.split("")
.filter_map(|s| if s.is_empty() { None } else { Some(s) })
.collect::<Vec<&str>>();
let mut left: NodePath = BTreeMap::new();
let mut right: NodePath = BTreeMap::new();
for line in lines {
if line.is_empty() {
continue;
}
let parts1 = line.split(" = ").collect::<Vec<&str>>();
let src = parts1[0];
let parts2 = parts1[1]
.trim_start_matches("(")
.trim_end_matches(")")
.split(", ")
.collect::<Vec<&str>>();
left.insert(src, parts2[0]);
right.insert(src, parts2[1]);
}
Path { path, left, right }
}
impl<'a> Path<'a> {
fn traverse(&'a mut self) -> u64 {
let mut cur = "AAA";
let mut steps = 0;
for dir in self.path.iter().cycle() {
steps += 1;
match *dir {
"L" => cur = self.left.get(cur).unwrap(),
"R" => cur = self.right.get(cur).unwrap(),
n @ _ => panic!("Invalid character {:?}!", n),
}
if cur == "ZZZ" {
break;
}
}
steps
}
fn ghost_traverse(&'a mut self) -> u64 {
let mut unique: HashSet<&str> = HashSet::new();
for (src, _dest) in &self.left {
if src.ends_with("A") {
unique.insert(src);
}
}
for (src, _dest) in &self.right {
if src.ends_with("A") {
unique.insert(src);
}
}
let mut steps = 1;
for ghost in unique.into_iter() {
let mut gsteps = 0;
let mut cur = ghost;
for dir in self.path.iter().cycle() {
gsteps += 1;
match *dir {
"L" => cur = self.left.get(cur).unwrap(),
"R" => cur = self.right.get(cur).unwrap(),
n @ _ => panic!("Invalid path character {:?}!", n),
}
if cur.ends_with("Z") {
steps = lcm(gsteps, steps);
break;
}
}
}
steps
}
}
#[aoc(day8, part1)]
fn part1(input: &str) -> u64 {
parse(input).traverse()
}
#[aoc(day8, part2)]
fn part2(input: &str) -> u64 {
parse(input).ghost_traverse()
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE_DATA1: &'static str = r#"RL
AAA = (BBB, CCC)
BBB = (DDD, EEE)
CCC = (ZZZ, GGG)
DDD = (DDD, DDD)
EEE = (EEE, EEE)
GGG = (GGG, GGG)
ZZZ = (ZZZ, ZZZ)
"#;
#[test]
fn sample_data1() {
assert_eq!(part1(&SAMPLE_DATA1), 2);
}
const SAMPLE_DATA2: &'static str = r#"LLR
AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)
"#;
#[test]
fn sample_data2() {
assert_eq!(part1(&SAMPLE_DATA2), 6);
}
const SAMPLE_DATA_P2: &'static str = r#"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)
"#;
#[test]
fn sample_data_part2() {
assert_eq!(part2(&SAMPLE_DATA_P2), 6);
}
}

View File

@ -7,5 +7,6 @@ mod day04;
mod day05;
mod day06;
mod day07;
mod day08;
aoc_lib! { year = 2023 }