Compare commits

..

3 Commits

Author SHA1 Message Date
195ef4651f Add day 21 2022-12-21 13:40:35 +01:00
a264e1e6d3 Add day 20 2022-12-20 18:07:58 +01:00
47a7381575 Add structure for missing days 2022-12-20 14:05:21 +01:00
14 changed files with 7097 additions and 0 deletions

View File

@ -69,4 +69,28 @@ path = "src/day14/main.rs"
name = "day15"
path = "src/day15/main.rs"
[[bin]]
name = "day16"
path = "src/day16/main.rs"
[[bin]]
name = "day17"
path = "src/day17/main.rs"
[[bin]]
name = "day18"
path = "src/day18/main.rs"
[[bin]]
name = "day19"
path = "src/day19/main.rs"
[[bin]]
name = "day20"
path = "src/day20/main.rs"
[[bin]]
name = "day21"
path = "src/day21/main.rs"
[dependencies]

10
day16_test.txt Normal file
View File

@ -0,0 +1,10 @@
Valve AA has flow rate=0; tunnels lead to valves DD, II, BB
Valve BB has flow rate=13; tunnels lead to valves CC, AA
Valve CC has flow rate=2; tunnels lead to valves DD, BB
Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE
Valve EE has flow rate=3; tunnels lead to valves FF, DD
Valve FF has flow rate=0; tunnels lead to valves EE, GG
Valve GG has flow rate=0; tunnels lead to valves FF, HH
Valve HH has flow rate=22; tunnel leads to valve GG
Valve II has flow rate=0; tunnels lead to valves AA, JJ
Valve JJ has flow rate=21; tunnel leads to valve II

0
day18.txt Normal file
View File

0
day18_test.txt Normal file
View File

5000
day20.txt Normal file

File diff suppressed because it is too large Load Diff

7
day20_test.txt Normal file
View File

@ -0,0 +1,7 @@
1
2
-3
3
-2
0
4

1819
day21.txt Normal file

File diff suppressed because it is too large Load Diff

15
day21_test.txt Normal file
View File

@ -0,0 +1,15 @@
root: pppw + sjmn
dbpl: 5
cczh: sllz + lgvd
zczc: 2
ptdq: humn - dvpt
dvpt: 3
lfqf: 4
humn: 5
ljgn: 2
sjmn: drzm * dbpl
sllz: 4
pppw: cczh / lfqf
lgvd: ljgn * ptdq
drzm: hmdt - zczc
hmdt: 32

71
src/day16/main.rs Normal file
View File

@ -0,0 +1,71 @@
use std::collections::{HashMap, HashSet};
use base::read_file;
#[derive(Debug)]
struct ScanEntry {
id: usize,
flow_rate: u32,
next: Vec<usize>,
}
fn find_path(graph: &HashMap<usize, ScanEntry>, start_id: usize, visited: &mut HashSet<usize>) {
if let Some(current) = graph.get(&start_id) {
visited.insert(current.id);
for next_id in &current.next {
if !visited.contains(next_id) {
println!("Found {}", next_id);
find_path(graph, *next_id, visited);
}
}
}
}
fn main() {
let lines = read_file("day16_test.txt");
let mut ids = HashMap::new();
let mut next_id = 0;
let mut entries = HashMap::new();
let mut start_id = None;
for line in lines {
let mut my_id = None;
let mut flow = None;
let mut next_ids = vec![];
for x in line.split([' ', ',']) {
if x.len() > 0 {
let mut uppercase = true;
for char in x.chars() {
uppercase = char.is_uppercase();
if !uppercase { break; };
}
if uppercase {
let id = *ids.entry(x.to_owned()).or_insert_with(|| {
next_id += 1;
next_id
});
match my_id {
None => my_id = Some(id),
Some(_) => next_ids.push(id),
}
match start_id {
None => start_id = Some(id),
_ => {}
}
} else if x.contains('=') {
let flow_value = x.split(['=', ';']).filter_map(|s| s.parse::<u32>().ok()).next().unwrap();
flow = Some(flow_value);
}
}
}
let entry = ScanEntry {
id: my_id.unwrap(),
flow_rate: flow.unwrap(),
next: next_ids,
};
entries.insert(entry.id, entry);
}
let mut visited = HashSet::new();
dbg!(&entries);
find_path(&entries, start_id.unwrap(), &mut visited);
}

3
src/day17/main.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Not ready");
}

3
src/day18/main.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Not ready");
}

3
src/day19/main.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Not ready");
}

52
src/day20/main.rs Normal file
View File

@ -0,0 +1,52 @@
use base::read_file;
fn do_encryption(key: &Vec<(usize, i64)>, data: &mut Vec<(usize, i64)>) {
let wrap_size = data.len() - 1;
for (index_to_find, enc_datum) in key {
let index = data.iter().position(|(org_index, _)| org_index == index_to_find).unwrap();
if *enc_datum == 0 {
continue;
}
let element = data.remove(index);
if *enc_datum > 0 {
let enc_datum = *enc_datum as usize;
data.rotate_left((enc_datum % wrap_size) as usize);
} else {
let enc_datum = enc_datum.abs() as usize;
data.rotate_right((enc_datum % wrap_size) as usize);
}
data.insert(index, element);
}
}
fn calc_result(data: Vec<(usize, i64)>) -> i64 {
let zero_index = data.iter().position(|(_, value)| *value == 0).unwrap();
println!("Found zero at {}", zero_index);
let wrap_size = data.len();
let number_1 = data[(zero_index + 1000) % wrap_size].1;
let number_2 = data[(zero_index + 2000) % wrap_size].1;
let number_3 = data[(zero_index + 3000) % wrap_size].1;
number_1 + number_2 + number_3
}
fn main() {
let enc_data = read_file("day20.txt").iter().filter_map(|line| line.parse().ok()).enumerate().collect::<Vec<(usize, i64)>>();
let mut result = enc_data.clone();
do_encryption(&enc_data, &mut result);
println!("Task 1: {}", calc_result(result));
let enc_data = enc_data.iter().map(|(idx, value)| (*idx, value * 811589153)).collect::<Vec<_>>();
let mut result = enc_data.clone();
for _ in 0..10 {
do_encryption(&enc_data, &mut result);
}
println!("Task 2: {}", calc_result(result));
}

90
src/day21/main.rs Normal file
View File

@ -0,0 +1,90 @@
use std::collections::HashMap;
use base::read_file;
#[derive(Clone, Debug)]
enum UnparsedOp {
Constant(i64),
Add(String, String),
Sub(String, String),
Mul(String, String),
Div(String, String),
}
fn solve_op(op: &UnparsedOp, data: &HashMap<String, UnparsedOp>) -> i64 {
return match op {
UnparsedOp::Constant(number) => *number,
UnparsedOp::Add(f, s) => solve_op(&data[f], data) + solve_op(&data[s], data),
UnparsedOp::Sub(f, s) => solve_op(&data[f], data) - solve_op(&data[s], data),
UnparsedOp::Mul(f, s) => solve_op(&data[f], data) * solve_op(&data[s], data),
UnparsedOp::Div(f, s) => solve_op(&data[f], data) / solve_op(&data[s], data),
};
}
fn solve(data: &HashMap<String, UnparsedOp>) -> i64 {
let root = &data["root"];
solve_op(root, data)
}
fn solve_eq(data: &mut HashMap<String, UnparsedOp>) -> i64 {
let root = data["root"].clone();
if let UnparsedOp::Add(f, s) = root {
let mut guess = 0;
let mut add = 100000000000;
loop {
let new_guess = guess + add;
data.insert("humn".to_owned(), UnparsedOp::Constant(new_guess));
let left = solve_op(&data[&f], data);
let right = solve_op(&data[&s], data);
if left < right {
add /= 10;
} else {
guess = new_guess;
}
if left == right {
return guess;
}
}
} else {
panic!("Can't handle this");
}
}
fn main() {
let lines = read_file("day21.txt");
let mut data = HashMap::new();
for line in lines {
let splits = line.split(": ").collect::<Vec<_>>();
let name = splits[0];
if let Ok(number) = splits[1].parse::<i64>() {
data.insert(name.to_owned(), UnparsedOp::Constant(number));
} else {
let split_op = splits[1].split_whitespace().collect::<Vec<_>>();
let first = split_op[0].to_owned();
let second = split_op[2].to_owned();
data.insert(name.to_owned(), match split_op[1].chars().next().unwrap() {
'+' => UnparsedOp::Add(first, second),
'-' => UnparsedOp::Sub(first, second),
'*' => UnparsedOp::Mul(first, second),
'/' => UnparsedOp::Div(first, second),
_ => {
panic!("Unknown operation");
}
});
}
}
let result = solve(&data);
println!("Task1: {}", result);
let result = solve_eq(&mut data);
println!("Task2: {}", result);
}