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

@ -23,9 +23,7 @@ fn main() {
do_cycle(cycles, x_reg); do_cycle(cycles, x_reg);
for line in INPUT.lines() { for line in INPUT.lines() {
if line.starts_with("noop") { if line.starts_with("noop") {
cycles += 1; cycles += 1;
} else { } else {

View File

@ -20,14 +20,10 @@ fn op_func(op: char, operand: &str) -> Box<dyn Fn(i64) -> i64> {
_ => panic!("unsupported op {}", op), _ => panic!("unsupported op {}", op),
}; };
if operand == "old" { if operand == "old" {
Box::new(move |x| { Box::new(move |x| op(x, x))
op(x, x)
})
} else { } else {
let operand: i64 = operand.parse().unwrap(); let operand: i64 = operand.parse().unwrap();
Box::new(move |x| { Box::new(move |x| op(x, operand))
op(x, operand)
})
} }
} }
@ -47,38 +43,34 @@ fn main() {
if line == "" { if line == "" {
continue; continue;
} } else if line.starts_with("Monkey") {
else if line.starts_with("Monkey") {
cur_monkey += 1; cur_monkey += 1;
monkeys.push(Monkey { monkeys.push(Monkey {
items: Vec::new(), items: Vec::new(),
operation: Box::new(empty_op), operation: Box::new(empty_op),
test: Box::new(empty_test), test: Box::new(empty_test),
inspections: 0 inspections: 0,
}); });
continue; continue;
} } else if line.starts_with("Starting items: ") {
else if line.starts_with("Starting items: ") {
let items = line[16..].split(", "); let items = line[16..].split(", ");
for item in items { 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 mut items = line[21..].split(' ');
let op = items.next().unwrap().chars().next().unwrap(); let op = items.next().unwrap().chars().next().unwrap();
let operand = items.next().unwrap(); let operand = items.next().unwrap();
monkeys[cur_monkey as usize].operation = op_func(op, operand); 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(); cur_test_divisor = line[19..].parse().unwrap();
lcm = num::integer::lcm(lcm, cur_test_divisor); 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(); 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(); let target: i64 = line[26..].parse().unwrap();
monkeys[cur_monkey as usize].test = Box::new(move |x| { 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.sort();
active_monkeys.reverse(); active_monkeys.reverse();

View File

@ -1,4 +1,4 @@
use petgraph::{prelude::*, algo::dijkstra::*, visit::Reversed}; use petgraph::{algo::dijkstra::*, prelude::*, visit::Reversed};
fn char_elevation(c: char) -> i16 { fn char_elevation(c: char) -> i16 {
let elevation = match c { let elevation = match c {
@ -18,7 +18,10 @@ fn main() {
const INPUT: &str = include_str!("../input.txt"); const INPUT: &str = include_str!("../input.txt");
let grid = INPUT.lines().collect::<Vec<&str>>(); 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(); let mut graph: DiGraphMap<(usize, usize), u8> = DiGraphMap::new();

View File

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

View File

@ -4,8 +4,7 @@ fn char_to_prio(c: char) -> u64 {
if c.is_ascii_uppercase() { if c.is_ascii_uppercase() {
let ci = c as u8; let ci = c as u8;
return u64::from(ci - b'A' + 27); return u64::from(ci - b'A' + 27);
} } else if c.is_ascii_lowercase() {
else if c.is_ascii_lowercase() {
let ci = c as u8; let ci = c as u8;
return u64::from(ci - b'a' + 1); return u64::from(ci - b'a' + 1);
} }
@ -36,7 +35,11 @@ fn main() {
let mut input = INPUT.lines().peekable(); let mut input = INPUT.lines().peekable();
priority_sum = 0; priority_sum = 0;
while input.peek().is_some() { 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' { for c in b'A'..=b'Z' {
let c = c as char; 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() { fn main() {
const INPUT: &str = include_str!("../input.txt"); const INPUT: &str = include_str!("../input.txt");
@ -10,10 +10,16 @@ fn main() {
let (first, second) = line.split_once(',').unwrap(); let (first, second) = line.split_once(',').unwrap();
let first: (&str, &str) = first.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: (&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 first_set: HashSet<u64> = HashSet::from_iter(first.0..=first.1);
let second_set: HashSet<u64> = HashSet::from_iter(second.0..=second.1); let second_set: HashSet<u64> = HashSet::from_iter(second.0..=second.1);

View File

@ -5,9 +5,10 @@ fn print_stacks(stacks: &Vec<Vec<char>>) {
} }
fn stacks_message(stacks: &Vec<Vec<char>>) -> String { fn stacks_message(stacks: &Vec<Vec<char>>) -> String {
stacks.iter().map(|v| { stacks
v[v.len() - 1] .iter()
}).fold(String::new(), |mut s, c| { .map(|v| v[v.len() - 1])
.fold(String::new(), |mut s, c| {
s.push(c); s.push(c);
s s
}) })
@ -17,9 +18,11 @@ fn main() {
const INPUT: &str = include_str!("../input.txt"); const INPUT: &str = include_str!("../input.txt");
let chunks = INPUT.split_once("\n\n").unwrap(); let chunks = INPUT.split_once("\n\n").unwrap();
let mut stack_lines: Vec<String> = chunks.0.lines().map(|x| -> String { let mut stack_lines: Vec<String> = chunks
x.to_string() + " " .0
}).collect(); .lines()
.map(|x| -> String { x.to_string() + " " })
.collect();
stack_lines.pop(); stack_lines.pop();
let num_stacks = (stack_lines[0].len() + 1) / 4; let num_stacks = (stack_lines[0].len() + 1) / 4;

View File

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

View File

@ -1,8 +1,8 @@
use std::borrow::BorrowMut; use std::borrow::BorrowMut;
use petgraph::*;
use petgraph::dot::*; use petgraph::dot::*;
use petgraph::prelude::*; use petgraph::prelude::*;
use petgraph::*;
fn do_weights(g: &mut Graph<(String, u64), ()>, root_dir: NodeIndex<u32>) { fn do_weights(g: &mut Graph<(String, u64), ()>, root_dir: NodeIndex<u32>) {
for edge in g.clone().edges_directed(root_dir, Outgoing) { for edge in g.clone().edges_directed(root_dir, Outgoing) {
@ -29,7 +29,11 @@ fn main() {
let new_dir = &line[5..]; let new_dir = &line[5..];
if new_dir == ".." { 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 { } else {
let prev_dir = curr_dir; let prev_dir = curr_dir;
curr_dir = g.add_node((new_dir.to_string() + "/", 0)); curr_dir = g.add_node((new_dir.to_string() + "/", 0));
@ -39,11 +43,9 @@ fn main() {
g.add_edge(prev_dir, curr_dir, ()); g.add_edge(prev_dir, curr_dir, ());
} }
} }
} } else if line.starts_with("$ ls") {
else if line.starts_with("$ ls") {
continue; continue;
} } else {
else {
let (size, name) = line.split_once(' ').unwrap(); let (size, name) = line.split_once(' ').unwrap();
if size == "dir" { if size == "dir" {
continue; continue;
@ -60,13 +62,22 @@ fn main() {
println!("{:?}\n\n", Dot::with_config(&g, &[Config::EdgeNoLabel])); 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}"); println!("total: {total}");
let used_space = g.node_weight(root_dir).unwrap().1; let used_space = g.node_weight(root_dir).unwrap().1;
let free_space = 70000000 - used_space; let free_space = 70000000 - used_space;
let need_to_free = 30000000 - free_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}"); println!("delete size: {delete}");
} }

View File

@ -1,7 +1,7 @@
use std::borrow::BorrowMut; use std::borrow::BorrowMut;
fn directional_scenic_score(seen: &Vec<i16>, height: i16) -> u32 { 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(), None => seen.len(),
Some(x) => x + 1, Some(x) => x + 1,
}) as u32 }) as u32
@ -70,13 +70,16 @@ fn main() {
} }
} }
let n_trees_visible = grid.iter().map(|v| -> usize { let n_trees_visible = grid
v.iter().filter(|(_, vis, _)| { *vis }).count() .iter()
}).sum::<usize>(); .map(|v| -> usize { v.iter().filter(|(_, vis, _)| *vis).count() })
.sum::<usize>();
println!("{n_trees_visible}"); println!("{n_trees_visible}");
let max_scenic_score = grid.iter().map(|v| { let max_scenic_score = grid
v.iter().map(|(_, _, score)| { score }).max().unwrap() .iter()
}).max().unwrap(); .map(|v| v.iter().map(|(_, _, score)| score).max().unwrap())
.max()
.unwrap();
println!("{max_scenic_score}"); println!("{max_scenic_score}");
} }