Compare commits
2 Commits
920945061f
...
b433dadf5f
Author | SHA1 | Date |
---|---|---|
Andrew Coleman | b433dadf5f | |
Andrew Coleman | 59498dd72c |
|
@ -13,4 +13,43 @@ name = "aoc2022"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"petgraph",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "fixedbitset"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "1.9.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "petgraph"
|
||||
version = "0.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6d5014253a1331579ce62aa67443b4a658c5e7dd03d4bc6d302b94474888143"
|
||||
dependencies = [
|
||||
"fixedbitset",
|
||||
"indexmap",
|
||||
]
|
||||
|
|
|
@ -11,3 +11,4 @@ test_input = []
|
|||
|
||||
[dependencies]
|
||||
anyhow = "1.0.66"
|
||||
petgraph = "0.6.2"
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
Monkey 0:
|
||||
Starting items: 64
|
||||
Operation: new = old * 7
|
||||
Test: divisible by 13
|
||||
If true: throw to monkey 1
|
||||
If false: throw to monkey 3
|
||||
|
||||
Monkey 1:
|
||||
Starting items: 60, 84, 84, 65
|
||||
Operation: new = old + 7
|
||||
Test: divisible by 19
|
||||
If true: throw to monkey 2
|
||||
If false: throw to monkey 7
|
||||
|
||||
Monkey 2:
|
||||
Starting items: 52, 67, 74, 88, 51, 61
|
||||
Operation: new = old * 3
|
||||
Test: divisible by 5
|
||||
If true: throw to monkey 5
|
||||
If false: throw to monkey 7
|
||||
|
||||
Monkey 3:
|
||||
Starting items: 67, 72
|
||||
Operation: new = old + 3
|
||||
Test: divisible by 2
|
||||
If true: throw to monkey 1
|
||||
If false: throw to monkey 2
|
||||
|
||||
Monkey 4:
|
||||
Starting items: 80, 79, 58, 77, 68, 74, 98, 64
|
||||
Operation: new = old * old
|
||||
Test: divisible by 17
|
||||
If true: throw to monkey 6
|
||||
If false: throw to monkey 0
|
||||
|
||||
Monkey 5:
|
||||
Starting items: 62, 53, 61, 89, 86
|
||||
Operation: new = old + 8
|
||||
Test: divisible by 11
|
||||
If true: throw to monkey 4
|
||||
If false: throw to monkey 6
|
||||
|
||||
Monkey 6:
|
||||
Starting items: 86, 89, 82
|
||||
Operation: new = old + 2
|
||||
Test: divisible by 7
|
||||
If true: throw to monkey 3
|
||||
If false: throw to monkey 0
|
||||
|
||||
Monkey 7:
|
||||
Starting items: 92, 81, 70, 96, 69, 84, 83
|
||||
Operation: new = old + 4
|
||||
Test: divisible by 3
|
||||
If true: throw to monkey 4
|
||||
If false: throw to monkey 5
|
File diff suppressed because it is too large
Load Diff
Binary file not shown.
After Width: | Height: | Size: 417 KiB |
|
@ -0,0 +1,41 @@
|
|||
abcccccccccccccccccaaccccccccccccaaaaaaaacccccccccccaaaaaccccaaaaaaccaaaaaaaaaaaaaaaaaccccccccccccccccaaacccccaaaaaaaacccaaaccccccccccccccccccccccccccccccccccccccccccccccccccaaaaa
|
||||
abccccccccccccccccaaacaacccccccccccaaaacccccccccccccaaaaaacccaaaaaaccaaaaaaaaaaaaaaaaaaaacccccccccaaacaaacccccaaaaaaaaaccaaaaccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa
|
||||
abcccccccccccccccccaaaaacccccccccccaaaaaccccccccccccaaaaaaccccaaaacccaaaacccaaaaaaaaaaaaacccccccccaaaaaaaaaacccaaaaaaaaccaaaaccccccccccccccccccccccccccccccccccaaacccccccccccaaaaaa
|
||||
abcccccccccccccccaaaaaacccccccccccaaacaaccaaccccccccaaaaaaccccaaaacccaaaccccaaaaaaaaaaaaaccccccccccaaaaaaaaaccaaaaaacccccaaacccccccccccccccccccccccccccccccccccaaaccccccccccccccaaa
|
||||
abcccccccccccccccaaaaaaaacccccccccaacccacaaacaacccccccaaccccccaccaccccccccaaaaaaaaaaaaccccccccccccccaaaaaaacccaaaaaaacccccccccccccaacccccccccccccccccccccccccccaaaccccccccccccccaaa
|
||||
abcccccccccccccccaacaaaaacccccccccccccccccaaaaacccccccccccccccccccccccccccaaaaaaaaaaaaccccccccccccccaaaaaaccccaaccaaacccccccccccaaaaaaccccccccccccccccccccccccccdccccccccccccccccaa
|
||||
abccaacccccccaaacccaaacaccccccccccaaacccaaaaaaccccccccccccccccccccccccccccaaacccaaaaaacaaaaccccccccaaaaaaaccccccccaaacccccccccccaaaaaacccccccccccccccccllllllcccdddddcccccccccccccc
|
||||
abaaaacccacccaaccccaacccccccccccccaaacccaaaaaaaacccccccccccccccccaaccccccccacccaaaaccccaaaaccccccccaaacaaaccccccccccccccccccccccaaaaaaccccccccccccccccllllllllldddddddddddccaaccccc
|
||||
abaaaaccaaaaaaaacccccccccccccccaaaaaaaacaacaaaaacccccccccccccccccaaacccccccaaacaaccccccaaaacccccccccccccaaccccccccccccccccccccccaaaaaccccccccccccccccclllllllllldddddddddeeaaaccccc
|
||||
abaaaccccaaaaaaaaccccccccaaacccaaaaaaaacccaaacccccccccccccccccaaaaaaaaccccccaaaaacccccccaacccccccccccccccccccccccccccccccccccccccaaaaccccaaaccccccccckllppppplllmmmmmmmdeeeeaaccccc
|
||||
abaaaacccaaaaaaaaacccccaaaaaaccccaaaaaccccaaccccccccccccccccccaaaaaaaaccccccaaaaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccaaaacccccccckklpppppppplmmmmmmmmmeeeeaccccc
|
||||
abaaaacccaaaaaaaaacccccaaaaaacccaaaaaaccccccccaacccccccccccccccaaaaaaccccccaaaaaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccaaaacccccccckkkppppppppqmmmmmmmmmmeeeaacccc
|
||||
abaaaaaccaaaaaaaaccccccaaaaaacccaaaaaaccccccacaaaacccccccccccccaaaaaaccccccaaaaaaaaccccccccaaaaccccccccaaacccccccccccccccccccccccccccccccaaacccccccckkkpppuuuppqqqqqqqqmmmeeeeacccc
|
||||
abacccccaaaaaaaccccccccaaaaaccccaccaaaccccccaaaaaacccccacccccccaaaaaaccccccaaaaaacaaaccccccaaaaccccccccaaaacccccccccccccccccccccccccccccccccccccccckkkpppuuuuuuqqqqqqqqqnnneeeccccc
|
||||
abcccccccaccaaaccccccccaaaaacccccccccccccccccaaaacccaaaacccccccaacaaacccccccccaaacaaaaaccccaaaaccccccccaaaaccccccccccccccccccccccccccccccccccccccckkkkpppuuuuuuuqvvvvqqqnnneeeccccc
|
||||
abcccccccccccaaacaaccccccccccccccccccccccccccaaaacccaaaaaacccccccccccccccccccccccaaaaaaccccaaacccccccccaaaccccccccccccccccccccccccccccccccccccccckkkkrrpuuuxxxuvvvvvvvqqnnneeeccccc
|
||||
abcccccccccccccccaacaaaccccccccccccccccccccccaacaacccaaaaacccccccccccccccccccccccaaaaaaccccccccccccccccccccccccccccccccccccccccccccccccccccccccckkkkrrrruuxxxxuvvvvvvvqqnnneeeccccc
|
||||
abcccccccccccccccaaaaacccccccccccccccccccccccccccaccaaaaacccccaaccccccccccccccccccaaaaaccccccccccccccccaaaccccccccccccccaaacccccccccccccccccccckkkkrrrruuuxxxxyyyyyvvvqqnnneecccccc
|
||||
abcccccccccccccaaaaaacccccccccccaacaacccccccccccaaaaaaaaacccacaaaacaaccaaccccccccaaacaaccccccccccaaccccaaaaaacccaacaacccaaacacccccccccccccccccjjkkrrrruuuuuxxxyyyyyvvqrqnneffcccccc
|
||||
abcccccccccccccaaaaaaaacccaaacccaaaaaccccccaacccaaaaaaaaacccaaaaaacaaaaaaccccccccaaacaaacccccccaaaaaacaaaaaaacccaaaaacaaaaaaaaccccccccccccccccjjjrrrtttuuxxxxxyyyyyvvrrnnnfffcccccc
|
||||
SbccccccccccccccccaaaaacccaaaaccaaaaaaccccaaaaaacaaaaaaaaacccaaaacccaaaaaccccccccaaaaaaacccccccaaaaaaaaaaaaaaccaaaaaccaaaaaaaaccccccccccccccccjjjrrrtttxxxEzzzzyyyvvrrrnnnfffcccccc
|
||||
abccccccccccaaaccaaccaacccaaaaccaaaaaacccccaaaaacaaaaaaaaacccaaaaccaaaaaacccccccccaaaaaaccccccccaaaacaaaaaaacccaaaaaaccaaaaaacccccccccccccccccjjjrrrtttxxxxxyyyyyyvvrrrnnnfffcccccc
|
||||
abcccccccccaaaaccaacccccccaaacccaaaaaacccaaaaaaaaaaaaaaaaacccaacacaaaaaaaacccccaaaaaaaacccccccccaaaacccaaaaaaccccaaaacccaaaaacccccccccccccccccjjjrrrtttxxxxxyyyyyyywvrrnnnfffcccccc
|
||||
abcccccccccaaaacccccccccccccccccccaaaccccaaaaaaaaaaaaaaaaaacccccccaaaaaaaacccccaaaaaaaaaccccccccaccacccaaaaaaccccacccccaaaaaacccccccccccccccccjjjrrrrttttxxxyyyyyyywwrrroooffcccccc
|
||||
abccccccccccaaaccccccccccccccccccccccccccaaaaaaaaaccaaaaaaaccccccccccaaccccccccaaaaaaaaaaccccccccccccccaacccccccccccccccaaccccccccccccccccccccjjjjqqqqttttxxyywwwwwwwwrrooofffccccc
|
||||
abcccccccccccccccccccccccccccccccccccccccccaaacacccccaaaaccccccccccccaacccccccccccaaacaaacccccccccccccccccccccccccccccaaacccccccccccccccccccaacjjjjqqqqqttwwwwwwwwwwwrrrooofffccccc
|
||||
abcccccccccccccccccccccccccccccccccccccccccaaccccccccccaacccccccccccccccccccccccccaaacccccccccccccccccccccccccccccccccaaacccccccccccccccccaaaaaajjjjqqqqttwwwwwwsswwrrrrooofffccccc
|
||||
abcccccaaaaccccccccccccccaacaaccccccccccccccccccccccccccccccccccccccccccccccccccccaacccccccccccccccccccccccccccccccaaaaaaaaccaaaacccccccccaaaaaacjjjiqqqtttwwwwsssssrrrrooofffccccc
|
||||
abccccaaaaaccccccccccccccaaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaaaaccaaaaacccccccccaaaaacciiiiqqttswwwssssssrrroooogffccccc
|
||||
abccccaaaaaacccccccccccccaaaaaacccaaaccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaccaaaaaaccccccccaaaaacccciiiqqqssssssspppooooooogggaccccc
|
||||
abccccaaaaaaccccccccaacccaaaaaaccccaacccccccccccccccccccccccccccccccccaaaaccccccccccccccccccccccccccccccccccccccccccaaaaaaccaaaaaaccccccccaaaaaccccciiiqqsssssspppppoooooggggaacccc
|
||||
abccccaaaaaccccccaaaaacccaaaaaacaacaaaaaccccccccccccccccccccaacccccccaaaaacccccccccccaaaccccccccccccccccccccccccccccaaaaaacccaaaaacccccccccacccccccciiiqqqpssspppppgggggggggaaacccc
|
||||
abccccccaaacccccccaaaaaccccaaaccaaaaaaaacccccccaacccccccaaccaacccccccaaaaaacccccccccaaaacccccccccccaaaaccccccccccccaaccaaacccaaacccccaaacaaaccccccccciiqqppppppphhhggggggggaaaacccc
|
||||
abccccccccccccccccaaaaaccccccccccaaaaaccccccccaaacaaccccaaaaaacccccccaaaaaaaccccccccaaaacccccccccccaaaacccccccccaaaaaacccccccccccccccaaaaaaaccccccccciiippppppphhhhggggggcaaacccccc
|
||||
abaacccccccccccccaaaaaccccccccccccaaaaaccccccccaaaaacccccaaaaaaacccccaaaaacaaacccccccaaacccccccccccaaaacccccccccaaaaacccccccccccccccccaaaaaacccccccccciiiippphhhhhhcccccccaaacccccc
|
||||
abaacccccccccccccccaaacccccccccccaaacaaccccccaaaaaaccccccaaaaaaacccaaccaaacaaaaccaaaccccccccccccccccaaacccccccccaaaaaaacccccccccccccccaaaaaaaacccccccciiihhhhhhhhaaaccccccccccccccc
|
||||
abaaccccccccccccccccccccccccccccccaacccccccccaaaaaaaacccaaaaaaccaaaaaacccccaaaacaaaaaccccccccccccccccccccccccccaaaaaaaaccccccccccccccaaaaaaaaaccccccccciihhhhhhcaaaacccccccccccccca
|
||||
abaaccccccccccccccccccccccccccccccccaacccccccaacaaaaacccaaaaaaccaaaaacccccccaaaaaaaacccccccccccccccccccccccccccaaaaaaaacccccccccaaacaaaaaaaaaacccccccccccchhhaccccaacccccccccccccca
|
||||
abaaccccccccccccccccccccccccccccccccaaaaaaccccccaaccccccccccaaccaaaaaaacccccaaaaaaaccccccccccccccccccccccccaaaccacaaacccccccccccaaaaaaacaaacaaaaaacccccccccaaacccccccccccccccaaaaaa
|
||||
abccccccccccccccccccccccccccccccccccaaaaaccccccaaccccccccccccccaaaaaaaacaaaaaaaaaaccccccccccccccccccccccccaaaaaaccaaaccccccccccccaaaaaacaaacaaaaaacccccccccaaaccccccccccccccccaaaaa
|
||||
abccccccccccccccccccccccccccccccccaaaaaaaacccccccccccccccccccccaaaaaaaacaaaaaaaaaaaaacccccccccccccccccccccaaaaaacccccccccccccccaaaaaaaaaaaaaaaaaacccccccccccccccccccccccccccccaaaaa
|
|
@ -0,0 +1,168 @@
|
|||
use anyhow::{anyhow, Result};
|
||||
use std::collections::{BinaryHeap, VecDeque};
|
||||
use std::fs;
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
enum Ops {
|
||||
Plus(u64),
|
||||
Mult(u64),
|
||||
#[default]
|
||||
Square,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
struct Monkey {
|
||||
items: VecDeque<u64>,
|
||||
op: Ops,
|
||||
test: u64,
|
||||
result: [usize; 2],
|
||||
}
|
||||
|
||||
pub fn run() -> Result<()> {
|
||||
#[cfg(not(feature = "test_input"))]
|
||||
let file_contents = fs::read_to_string("day11.txt")?;
|
||||
|
||||
#[cfg(feature = "test_input")]
|
||||
let file_contents = fs::read_to_string("tests/day11.txt")?;
|
||||
|
||||
let mut monkeys: Vec<Monkey> = Vec::with_capacity(8);
|
||||
let mut lines = file_contents.lines();
|
||||
|
||||
while let Some(line) = lines.next() {
|
||||
if line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
if line.starts_with("Monkey") {
|
||||
let mut m = Monkey::default();
|
||||
for i in lines.next().unwrap().split_whitespace().skip(2) {
|
||||
if let Some(s) = i.strip_suffix(",") {
|
||||
m.items.push_back(s.parse::<u64>().unwrap());
|
||||
} else {
|
||||
m.items.push_back(i.parse::<u64>().unwrap());
|
||||
}
|
||||
}
|
||||
let mut ops = lines.next().unwrap().split_whitespace().skip(4);
|
||||
let op_symbol = ops.next().unwrap();
|
||||
let op_val = ops.next().unwrap();
|
||||
match op_symbol {
|
||||
"*" => {
|
||||
m.op = if op_val == "old" {
|
||||
Ops::Square
|
||||
} else {
|
||||
let val: u64 = op_val.parse().unwrap();
|
||||
Ops::Mult(val)
|
||||
}
|
||||
}
|
||||
"+" => {
|
||||
let val: u64 = op_val.parse().unwrap();
|
||||
m.op = Ops::Plus(val);
|
||||
}
|
||||
_ => return Err(anyhow!("invalid op code")),
|
||||
}
|
||||
let t = lines
|
||||
.next()
|
||||
.unwrap()
|
||||
.split_whitespace()
|
||||
.skip(3)
|
||||
.next()
|
||||
.unwrap()
|
||||
.parse::<u64>()
|
||||
.unwrap();
|
||||
m.test = t;
|
||||
let tmonkey: usize = lines
|
||||
.next()
|
||||
.unwrap()
|
||||
.split_whitespace()
|
||||
.skip(5)
|
||||
.next()
|
||||
.unwrap()
|
||||
.parse()
|
||||
.unwrap();
|
||||
let fmonkey: usize = lines
|
||||
.next()
|
||||
.unwrap()
|
||||
.split_whitespace()
|
||||
.skip(5)
|
||||
.next()
|
||||
.unwrap()
|
||||
.parse()
|
||||
.unwrap();
|
||||
m.result = [tmonkey, fmonkey];
|
||||
monkeys.push(m);
|
||||
}
|
||||
}
|
||||
|
||||
part_one_items(&mut monkeys);
|
||||
part_two_items(&mut monkeys);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn match_op(op: &Ops, old: u64) -> u64 {
|
||||
match op {
|
||||
Ops::Plus(t) => old + t,
|
||||
Ops::Mult(t) => old * t,
|
||||
Ops::Square => old * old,
|
||||
}
|
||||
}
|
||||
|
||||
fn part_one_items(monkeys: &mut Vec<Monkey>) {
|
||||
let mut inspections: Vec<usize> = Vec::with_capacity(8);
|
||||
let mut items: Vec<VecDeque<u64>> = monkeys.iter().map(|m| m.items.clone()).collect();
|
||||
|
||||
for _ in 1..=20 {
|
||||
for m in 0..monkeys.len() {
|
||||
if let None = inspections.get(m) {
|
||||
inspections.push(0);
|
||||
}
|
||||
while let Some(item) = items[m].pop_front() {
|
||||
inspections[m] += 1;
|
||||
let score = (match_op(&monkeys[m].op, item) as f32 / 3.0).floor() as u64;
|
||||
if score % monkeys[m].test == 0 {
|
||||
items[monkeys[m].result[0]].push_back(score);
|
||||
} else {
|
||||
items[monkeys[m].result[1]].push_back(score);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut bh: BinaryHeap<usize> = BinaryHeap::from(inspections);
|
||||
let one = bh.pop().unwrap();
|
||||
let two = bh.pop().unwrap();
|
||||
println!("part one {}*{}={}", one, two, one * two);
|
||||
}
|
||||
|
||||
fn part_two_items(monkeys: &mut Vec<Monkey>) {
|
||||
let mut inspections: Vec<usize> = Vec::with_capacity(8);
|
||||
let mut items: Vec<VecDeque<u64>> = monkeys.iter().map(|m| m.items.clone()).collect();
|
||||
|
||||
#[cfg(feature = "test_input")]
|
||||
let cd = 96577;
|
||||
|
||||
// grep Test day11.txt | awk 'BEGIN { S=1} { S=S*$4} END {print S}'
|
||||
#[cfg(not(feature = "test_input"))]
|
||||
let cd = 9699690;
|
||||
|
||||
for _ in 1..=10000 {
|
||||
for m in 0..monkeys.len() {
|
||||
if let None = inspections.get(m) {
|
||||
inspections.push(0);
|
||||
}
|
||||
while let Some(item) = items[m].pop_front() {
|
||||
inspections[m] += 1;
|
||||
let score = match_op(&monkeys[m].op, item);
|
||||
if score % monkeys[m].test == 0 {
|
||||
items[monkeys[m].result[0]].push_back(score % cd);
|
||||
} else {
|
||||
items[monkeys[m].result[1]].push_back(score % cd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut bh: BinaryHeap<usize> = BinaryHeap::from(inspections);
|
||||
let one = bh.pop().unwrap();
|
||||
let two = bh.pop().unwrap();
|
||||
println!("part two {}*{}={}", one, two, one * two);
|
||||
}
|
|
@ -0,0 +1,141 @@
|
|||
use anyhow::{anyhow, Result};
|
||||
use petgraph::algo::astar;
|
||||
use petgraph::dot::{Config, Dot};
|
||||
use petgraph::graph::{DefaultIx, NodeIndex};
|
||||
use petgraph::prelude::*;
|
||||
//use petgraph::{Directed, Graph};
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
|
||||
type Map = [[i32; 180]; 180];
|
||||
type WGraph = Graph<Coord, i32, Directed, DefaultIx>;
|
||||
type Coord = (usize, usize);
|
||||
type Indexes = HashMap<Coord, NodeIndex<DefaultIx>>;
|
||||
|
||||
pub fn run() -> Result<()> {
|
||||
#[cfg(not(feature = "test_input"))]
|
||||
let file_contents = fs::read_to_string("day12.txt")?;
|
||||
|
||||
#[cfg(feature = "test_input")]
|
||||
let file_contents = fs::read_to_string("tests/day12.txt")?;
|
||||
|
||||
let mut world: Map = [[0; 180]; 180];
|
||||
let mut rows = 0;
|
||||
let mut cols = 0;
|
||||
let mut graph: WGraph = Graph::new();
|
||||
let mut indexes: Indexes = HashMap::with_capacity(180 * 180);
|
||||
let mut start: Coord = (0, 0);
|
||||
let mut end: Coord = (0, 0);
|
||||
let mut p2starts: Vec<NodeIndex<DefaultIx>> = Vec::new();
|
||||
|
||||
for line in file_contents.lines() {
|
||||
cols = 0;
|
||||
for char in line.chars() {
|
||||
world[rows][cols] = get_height(&char);
|
||||
let index = graph.add_node((rows, cols));
|
||||
if world[rows][cols] == 1 {
|
||||
p2starts.push(index);
|
||||
}
|
||||
indexes.insert((rows, cols), index);
|
||||
if char == 'S' {
|
||||
start = (rows, cols);
|
||||
} else if char == 'E' {
|
||||
end = (rows, cols);
|
||||
}
|
||||
cols += 1;
|
||||
}
|
||||
rows += 1;
|
||||
}
|
||||
println!("finding distance between {:?} and {:?}", start, end);
|
||||
|
||||
for x in 0..rows {
|
||||
for y in 0..cols {
|
||||
let cur = (x, y);
|
||||
if cur == end {
|
||||
continue;
|
||||
}
|
||||
if x > 0 {
|
||||
check_coord(&mut graph, &world, &indexes, &cur, &(x - 1, y));
|
||||
}
|
||||
if x < rows - 1 {
|
||||
check_coord(&mut graph, &world, &indexes, &cur, &(x + 1, y));
|
||||
}
|
||||
if y > 0 {
|
||||
check_coord(&mut graph, &world, &indexes, &cur, &(x, y - 1));
|
||||
}
|
||||
if y < cols - 1 {
|
||||
check_coord(&mut graph, &world, &indexes, &cur, &(x, y + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let dotpath = Path::new("day12.dot");
|
||||
let mut file = match File::create(&dotpath) {
|
||||
Err(why) => return Err(anyhow!("could not write file {}", why)),
|
||||
Ok(file) => file,
|
||||
};
|
||||
match file
|
||||
.write_all(format!("{:?}", Dot::with_config(&graph, &[Config::EdgeNoLabel])).as_bytes())
|
||||
{
|
||||
Err(why) => return Err(anyhow!("could not write dot file {}", why)),
|
||||
Ok(_) => println!("wrote day12.dot"),
|
||||
};
|
||||
|
||||
let start_index = indexes.get(&start).unwrap();
|
||||
let end_index = indexes.get(&end).unwrap();
|
||||
#[allow(unused_variables)]
|
||||
if let Some((len, path)) = astar(
|
||||
&graph,
|
||||
*start_index,
|
||||
|finish| finish == *end_index,
|
||||
|e| *e.weight(),
|
||||
|_| 0,
|
||||
) {
|
||||
println!("part one steps ({})", len);
|
||||
#[cfg(feature = "test_input")]
|
||||
for n in path.iter() {
|
||||
println!("{:?}", graph.node_weight(*n).unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
let mut min_len: i32 = rows as i32 * cols as i32;
|
||||
for s in p2starts.iter() {
|
||||
if let Some((len, _)) = astar(
|
||||
&graph,
|
||||
*s,
|
||||
|finish| finish == *end_index,
|
||||
|e| *e.weight(),
|
||||
|_| 0,
|
||||
) {
|
||||
if len < min_len {
|
||||
min_len = len;
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("part 2 minimum steps {}", min_len);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_coord(graph: &mut WGraph, world: &Map, indexes: &Indexes, source: &Coord, dest: &Coord) {
|
||||
let h = world[source.0][source.1];
|
||||
let hi = indexes.get(source).unwrap();
|
||||
let d = world[dest.0][dest.1];
|
||||
let di = indexes.get(dest).unwrap();
|
||||
|
||||
if (d > h && d == h + 1) || d == h || d < h {
|
||||
graph.update_edge(*hi, *di, 1);
|
||||
}
|
||||
}
|
||||
|
||||
fn get_height(c: &char) -> i32 {
|
||||
if *c == 'S' {
|
||||
1
|
||||
} else if *c == 'E' {
|
||||
27
|
||||
} else {
|
||||
*c as i32 - 96
|
||||
}
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
pub mod day1;
|
||||
pub mod day10;
|
||||
pub mod day11;
|
||||
pub mod day12;
|
||||
pub mod day2;
|
||||
pub mod day3;
|
||||
pub mod day4;
|
||||
|
|
|
@ -42,6 +42,12 @@ fn run_day(number: i32) -> Result<()> {
|
|||
10 => {
|
||||
days::day10::run()?;
|
||||
}
|
||||
11 => {
|
||||
days::day11::run()?;
|
||||
}
|
||||
12 => {
|
||||
days::day12::run()?;
|
||||
}
|
||||
_ => return Err(anyhow!("Invalid day provided")),
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
Monkey 0:
|
||||
Starting items: 79, 98
|
||||
Operation: new = old * 19
|
||||
Test: divisible by 23
|
||||
If true: throw to monkey 2
|
||||
If false: throw to monkey 3
|
||||
|
||||
Monkey 1:
|
||||
Starting items: 54, 65, 75, 74
|
||||
Operation: new = old + 6
|
||||
Test: divisible by 19
|
||||
If true: throw to monkey 2
|
||||
If false: throw to monkey 0
|
||||
|
||||
Monkey 2:
|
||||
Starting items: 79, 60, 97
|
||||
Operation: new = old * old
|
||||
Test: divisible by 13
|
||||
If true: throw to monkey 1
|
||||
If false: throw to monkey 3
|
||||
|
||||
Monkey 3:
|
||||
Starting items: 74
|
||||
Operation: new = old + 3
|
||||
Test: divisible by 17
|
||||
If true: throw to monkey 0
|
||||
If false: throw to monkey 1
|
|
@ -0,0 +1,5 @@
|
|||
Sabqponm
|
||||
abcryxxl
|
||||
accszExk
|
||||
acctuvwj
|
||||
abdefghi
|
Loading…
Reference in New Issue