rustfmt
This commit is contained in:
parent
8fbaaa4bbc
commit
e871837dc6
@ -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 {
|
||||||
|
@ -17,17 +17,13 @@ fn op_func(op: char, operand: &str) -> Box<dyn Fn(i64) -> i64> {
|
|||||||
let op = match op {
|
let op = match op {
|
||||||
'*' => std::ops::Mul::mul,
|
'*' => std::ops::Mul::mul,
|
||||||
'+' => std::ops::Add::add,
|
'+' => std::ops::Add::add,
|
||||||
_ => 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();
|
||||||
|
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
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 {
|
||||||
'S' => 'a',
|
'S' => 'a',
|
||||||
'E' => 'z',
|
'E' => 'z',
|
||||||
c => c,
|
c => c,
|
||||||
};
|
};
|
||||||
|
|
||||||
((elevation as u8) - b'a') as i16
|
((elevation as u8) - b'a') as i16
|
||||||
@ -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();
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
@ -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;
|
||||||
|
@ -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);
|
||||||
|
@ -5,21 +5,24 @@ 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])
|
||||||
s.push(c);
|
.fold(String::new(), |mut s, c| {
|
||||||
s
|
s.push(c);
|
||||||
})
|
s
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
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;
|
||||||
|
|
||||||
@ -30,7 +33,7 @@ fn main() {
|
|||||||
line += " ";
|
line += " ";
|
||||||
|
|
||||||
for i in 0..num_stacks {
|
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() {
|
if c.is_whitespace() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -66,7 +69,7 @@ fn main() {
|
|||||||
let to = split[5].parse::<usize>().unwrap() - 1;
|
let to = split[5].parse::<usize>().unwrap() - 1;
|
||||||
|
|
||||||
let len = stacks[from].len();
|
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());
|
stacks[to].append(x.as_mut());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,5 +28,4 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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}");
|
||||||
}
|
}
|
||||||
|
@ -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}");
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ fn main() {
|
|||||||
'L' => LEFT,
|
'L' => LEFT,
|
||||||
'U' => UP,
|
'U' => UP,
|
||||||
'D' => DOWN,
|
'D' => DOWN,
|
||||||
_ => ZERO,
|
_ => ZERO,
|
||||||
};
|
};
|
||||||
let steps: u32 = steps.parse().unwrap();
|
let steps: u32 = steps.parse().unwrap();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user