1
0
This commit is contained in:
Jack Bond-Preston 2022-12-27 01:46:31 +00:00
parent 8fbaaa4bbc
commit e871837dc6
Signed by: jack
GPG Key ID: 010071F1482BA852
11 changed files with 110 additions and 86 deletions

View File

@ -1,13 +1,13 @@
fn do_cycle(cycle: u64, x_reg: i64) {
let col = (cycle - 1) % 40;
// println!("{} <= ")
if x_reg - 1 <= col.try_into().unwrap() && x_reg + 1 >= col.try_into().unwrap() {
print!("#");
} else {
print!(".");
}
if col == 39 {
println!();
}
@ -23,9 +23,7 @@ fn main() {
do_cycle(cycles, x_reg);
for line in INPUT.lines() {
if line.starts_with("noop") {
cycles += 1;
} else {

View File

@ -17,17 +17,13 @@ fn op_func(op: char, operand: &str) -> Box<dyn Fn(i64) -> i64> {
let op = match op {
'*' => std::ops::Mul::mul,
'+' => std::ops::Add::add,
_ => panic!("unsupported op {}", op),
_ => panic!("unsupported op {}", op),
};
if operand == "old" {
Box::new(move |x| {
op(x, x)
})
Box::new(move |x| op(x, x))
} else {
let operand: i64 = operand.parse().unwrap();
Box::new(move |x| {
op(x, operand)
})
Box::new(move |x| op(x, operand))
}
}
@ -35,7 +31,7 @@ fn main() {
const INPUT: &str = include_str!("../input.txt");
let mut monkeys: Vec<Monkey> = Vec::new();
let mut cur_monkey = -1i64;
let mut cur_test_divisor = -1i64;
let mut cur_test_true_ret = -1i64;
@ -47,38 +43,34 @@ fn main() {
if line == "" {
continue;
}
else if line.starts_with("Monkey") {
} else if line.starts_with("Monkey") {
cur_monkey += 1;
monkeys.push(Monkey {
items: Vec::new(),
operation: Box::new(empty_op),
test: Box::new(empty_test),
inspections: 0
inspections: 0,
});
continue;
}
else if line.starts_with("Starting items: ") {
} else if line.starts_with("Starting items: ") {
let items = line[16..].split(", ");
for item in items {
monkeys[cur_monkey as usize].items.push(item.parse().unwrap());
monkeys[cur_monkey as usize]
.items
.push(item.parse().unwrap());
}
}
else if line.starts_with("Operation: ") {
} else if line.starts_with("Operation: ") {
let mut items = line[21..].split(' ');
let op = items.next().unwrap().chars().next().unwrap();
let operand = items.next().unwrap();
monkeys[cur_monkey as usize].operation = op_func(op, operand);
}
else if line.starts_with("Test: ") {
} else if line.starts_with("Test: ") {
cur_test_divisor = line[19..].parse().unwrap();
lcm = num::integer::lcm(lcm, cur_test_divisor);
}
else if line.starts_with("If true: ") {
} else if line.starts_with("If true: ") {
cur_test_true_ret = line[25..].parse().unwrap();
}
else if line.starts_with("If false: ") {
} else if line.starts_with("If false: ") {
let target: i64 = line[26..].parse().unwrap();
monkeys[cur_monkey as usize].test = Box::new(move |x| {
@ -107,7 +99,7 @@ fn main() {
}
}
let mut active_monkeys = monkeys.iter().map(|m| { m.inspections }).collect::<Vec<u64>>();
let mut active_monkeys = monkeys.iter().map(|m| m.inspections).collect::<Vec<u64>>();
active_monkeys.sort();
active_monkeys.reverse();

View File

@ -1,10 +1,10 @@
use petgraph::{prelude::*, algo::dijkstra::*, visit::Reversed};
use petgraph::{algo::dijkstra::*, prelude::*, visit::Reversed};
fn char_elevation(c: char) -> i16 {
let elevation = match c {
'S' => 'a',
'E' => 'z',
c => c,
c => c,
};
((elevation as u8) - b'a') as i16
@ -16,9 +16,12 @@ fn travelable(dest: char, src: char) -> bool {
fn main() {
const INPUT: &str = include_str!("../input.txt");
let grid = INPUT.lines().collect::<Vec<&str>>();
let grid = grid.iter().map(|s| { s.chars().collect::<Vec<char>>() }).collect::<Vec<Vec<char>>>();
let grid = grid
.iter()
.map(|s| s.chars().collect::<Vec<char>>())
.collect::<Vec<Vec<char>>>();
let mut graph: DiGraphMap<(usize, usize), u8> = DiGraphMap::new();

View File

@ -3,14 +3,14 @@
enum Move {
ROCK = 1,
PAPER = 2,
SCISSORS = 3
SCISSORS = 3,
}
#[repr(u8)]
enum Outcome {
LOSS = 0,
DRAW = 3,
WIN = 6
WIN = 6,
}
impl TryFrom<char> for Move {
@ -38,12 +38,9 @@ impl std::fmt::Display for Move {
}
}
fn main() {
const INPUT: &str = include_str!("../input.txt");
let mut your_score: u64 = 0;
let mut their_score: u64 = 0;
@ -60,15 +57,24 @@ fn main() {
let outcome: Outcome;
if their_move == your_move {
outcome = Outcome::DRAW
}
else if their_move == Move::ROCK {
outcome = if your_move == Move::PAPER { Outcome::WIN } else { Outcome::LOSS };
}
else if their_move == Move::PAPER {
outcome = if your_move == Move::SCISSORS { Outcome::WIN } else { Outcome::LOSS };
}
else {
outcome = if your_move == Move::ROCK { Outcome::WIN } else { Outcome::LOSS };
} else if their_move == Move::ROCK {
outcome = if your_move == Move::PAPER {
Outcome::WIN
} else {
Outcome::LOSS
};
} else if their_move == Move::PAPER {
outcome = if your_move == Move::SCISSORS {
Outcome::WIN
} else {
Outcome::LOSS
};
} else {
outcome = if your_move == Move::ROCK {
Outcome::WIN
} else {
Outcome::LOSS
};
}
let outcome: u64 = outcome as u64;

View File

@ -4,8 +4,7 @@ fn char_to_prio(c: char) -> u64 {
if c.is_ascii_uppercase() {
let ci = c as u8;
return u64::from(ci - b'A' + 27);
}
else if c.is_ascii_lowercase() {
} else if c.is_ascii_lowercase() {
let ci = c as u8;
return u64::from(ci - b'a' + 1);
}
@ -23,7 +22,7 @@ fn main() {
let items: HashSet<char> = HashSet::from_iter(compartments.0.chars());
let mut new_items: HashSet<char> = HashSet::new();
for c in compartments.1.chars() {
if new_items.insert(c) && items.contains(&c) {
priority_sum += char_to_prio(c);
@ -36,7 +35,11 @@ fn main() {
let mut input = INPUT.lines().peekable();
priority_sum = 0;
while input.peek().is_some() {
let lines = [input.next().unwrap().clone(), input.next().unwrap().clone(), input.next().unwrap().clone()];
let lines = [
input.next().unwrap().clone(),
input.next().unwrap().clone(),
input.next().unwrap().clone(),
];
for c in b'A'..=b'Z' {
let c = c as char;

View File

@ -1,4 +1,4 @@
use std::{collections::HashSet, cmp::min};
use std::{cmp::min, collections::HashSet};
fn main() {
const INPUT: &str = include_str!("../input.txt");
@ -10,10 +10,16 @@ fn main() {
let (first, second) = line.split_once(',').unwrap();
let first: (&str, &str) = first.split_once('-').unwrap();
let first: (u64, u64) = (first.0.parse::<u64>().unwrap(), first.1.parse::<u64>().unwrap());
let first: (u64, u64) = (
first.0.parse::<u64>().unwrap(),
first.1.parse::<u64>().unwrap(),
);
let second: (&str, &str) = second.split_once('-').unwrap();
let second: (u64, u64) = (second.0.parse::<u64>().unwrap(), second.1.parse::<u64>().unwrap());
let second: (u64, u64) = (
second.0.parse::<u64>().unwrap(),
second.1.parse::<u64>().unwrap(),
);
let first_set: HashSet<u64> = HashSet::from_iter(first.0..=first.1);
let second_set: HashSet<u64> = HashSet::from_iter(second.0..=second.1);

View File

@ -5,21 +5,24 @@ fn print_stacks(stacks: &Vec<Vec<char>>) {
}
fn stacks_message(stacks: &Vec<Vec<char>>) -> String {
stacks.iter().map(|v| {
v[v.len() - 1]
}).fold(String::new(), |mut s, c| {
s.push(c);
s
})
stacks
.iter()
.map(|v| v[v.len() - 1])
.fold(String::new(), |mut s, c| {
s.push(c);
s
})
}
fn main() {
const INPUT: &str = include_str!("../input.txt");
let chunks = INPUT.split_once("\n\n").unwrap();
let mut stack_lines: Vec<String> = chunks.0.lines().map(|x| -> String {
x.to_string() + " "
}).collect();
let mut stack_lines: Vec<String> = chunks
.0
.lines()
.map(|x| -> String { x.to_string() + " " })
.collect();
stack_lines.pop();
let num_stacks = (stack_lines[0].len() + 1) / 4;
@ -30,11 +33,11 @@ fn main() {
line += " ";
for i in 0..num_stacks {
let c = line.chars().nth(i*4 + 1).unwrap();
let c = line.chars().nth(i * 4 + 1).unwrap();
if c.is_whitespace() {
continue;
}
stacks[i].push(c);
}
}
@ -66,7 +69,7 @@ fn main() {
let to = split[5].parse::<usize>().unwrap() - 1;
let len = stacks[from].len();
let mut x: Vec<char> = stacks[from].drain(len-n..len).collect();
let mut x: Vec<char> = stacks[from].drain(len - n..len).collect();
stacks[to].append(x.as_mut());
}

View File

@ -28,5 +28,4 @@ fn main() {
}
}
}
}

View File

@ -1,8 +1,8 @@
use std::borrow::BorrowMut;
use petgraph::*;
use petgraph::dot::*;
use petgraph::prelude::*;
use petgraph::*;
fn do_weights(g: &mut Graph<(String, u64), ()>, root_dir: NodeIndex<u32>) {
for edge in g.clone().edges_directed(root_dir, Outgoing) {
@ -29,21 +29,23 @@ fn main() {
let new_dir = &line[5..];
if new_dir == ".." {
curr_dir = g.edges_directed(curr_dir, Incoming).next().unwrap().source();
curr_dir = g
.edges_directed(curr_dir, Incoming)
.next()
.unwrap()
.source();
} else {
let prev_dir = curr_dir;
curr_dir = g.add_node((new_dir.to_string() + "/", 0));
if prev_dir == invalid_index {
if prev_dir == invalid_index {
root_dir = curr_dir;
} else {
g.add_edge(prev_dir, curr_dir, ());
}
}
}
else if line.starts_with("$ ls") {
} else if line.starts_with("$ ls") {
continue;
}
else {
} else {
let (size, name) = line.split_once(' ').unwrap();
if size == "dir" {
continue;
@ -60,13 +62,22 @@ fn main() {
println!("{:?}\n\n", Dot::with_config(&g, &[Config::EdgeNoLabel]));
let total = g.node_weights().filter(|(n, w)| { n.ends_with("/") && *w <= 100000 }).map(|(_, w)| { w }).sum::<u64>();
let total = g
.node_weights()
.filter(|(n, w)| n.ends_with("/") && *w <= 100000)
.map(|(_, w)| w)
.sum::<u64>();
println!("total: {total}");
let used_space = g.node_weight(root_dir).unwrap().1;
let free_space = 70000000 - used_space;
let need_to_free = 30000000 - free_space;
let delete = g.node_weights().filter(|(n, w)| { n.ends_with("/") && *w > need_to_free }).map(|(_, w)| { w }).min().unwrap();
let delete = g
.node_weights()
.filter(|(n, w)| n.ends_with("/") && *w > need_to_free)
.map(|(_, w)| w)
.min()
.unwrap();
println!("delete size: {delete}");
}

View File

@ -1,7 +1,7 @@
use std::borrow::BorrowMut;
fn directional_scenic_score(seen: &Vec<i16>, height: i16) -> u32 {
(match seen.iter().rev().position(|x| { *x >= height }) {
(match seen.iter().rev().position(|x| *x >= height) {
None => seen.len(),
Some(x) => x + 1,
}) as u32
@ -31,7 +31,7 @@ fn main() {
tree.1 = true;
}
}
max_seen = -1;
seen.clear();
for tree in row.iter_mut().rev() {
@ -70,13 +70,16 @@ fn main() {
}
}
let n_trees_visible = grid.iter().map(|v| -> usize {
v.iter().filter(|(_, vis, _)| { *vis }).count()
}).sum::<usize>();
let n_trees_visible = grid
.iter()
.map(|v| -> usize { v.iter().filter(|(_, vis, _)| *vis).count() })
.sum::<usize>();
println!("{n_trees_visible}");
let max_scenic_score = grid.iter().map(|v| {
v.iter().map(|(_, _, score)| { score }).max().unwrap()
}).max().unwrap();
let max_scenic_score = grid
.iter()
.map(|v| v.iter().map(|(_, _, score)| score).max().unwrap())
.max()
.unwrap();
println!("{max_scenic_score}");
}

View File

@ -13,7 +13,7 @@ const ZERO: IVec2 = IVec2::new(0, 0);
fn main() {
const INPUT: &str = include_str!("../input.txt");
let mut visited: HashSet<IVec2> = HashSet::new();
let mut knots: [IVec2; 10] = [ZERO; 10];
@ -25,7 +25,7 @@ fn main() {
'L' => LEFT,
'U' => UP,
'D' => DOWN,
_ => ZERO,
_ => ZERO,
};
let steps: u32 = steps.parse().unwrap();
@ -46,7 +46,7 @@ fn main() {
if diff.x == 0 {
knot.y += diff.y.signum();
} else if diff.y == 0 {
knot.x += diff.x.signum();
knot.x += diff.x.signum();
} else {
knot.x += diff.x.signum();
knot.y += diff.y.signum();