diff --git a/day10/src/main.rs b/day10/src/main.rs index 0979435..8bd3f04 100644 --- a/day10/src/main.rs +++ b/day10/src/main.rs @@ -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 { diff --git a/day11/src/main.rs b/day11/src/main.rs index 6980ac3..26ee4cf 100644 --- a/day11/src/main.rs +++ b/day11/src/main.rs @@ -17,17 +17,13 @@ fn op_func(op: char, operand: &str) -> Box 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 = 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::>(); + let mut active_monkeys = monkeys.iter().map(|m| m.inspections).collect::>(); active_monkeys.sort(); active_monkeys.reverse(); diff --git a/day12/src/main.rs b/day12/src/main.rs index 8e3c6b7..4579a5f 100644 --- a/day12/src/main.rs +++ b/day12/src/main.rs @@ -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::>(); - let grid = grid.iter().map(|s| { s.chars().collect::>() }).collect::>>(); + let grid = grid + .iter() + .map(|s| s.chars().collect::>()) + .collect::>>(); let mut graph: DiGraphMap<(usize, usize), u8> = DiGraphMap::new(); diff --git a/day2/src/main.rs b/day2/src/main.rs index 36f30ed..1129c37 100644 --- a/day2/src/main.rs +++ b/day2/src/main.rs @@ -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 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; diff --git a/day3/src/main.rs b/day3/src/main.rs index 7145305..48bf072 100644 --- a/day3/src/main.rs +++ b/day3/src/main.rs @@ -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 = HashSet::from_iter(compartments.0.chars()); let mut new_items: HashSet = 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; diff --git a/day4/src/main.rs b/day4/src/main.rs index 37f62ca..caa5bc5 100644 --- a/day4/src/main.rs +++ b/day4/src/main.rs @@ -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::().unwrap(), first.1.parse::().unwrap()); + let first: (u64, u64) = ( + first.0.parse::().unwrap(), + first.1.parse::().unwrap(), + ); let second: (&str, &str) = second.split_once('-').unwrap(); - let second: (u64, u64) = (second.0.parse::().unwrap(), second.1.parse::().unwrap()); + let second: (u64, u64) = ( + second.0.parse::().unwrap(), + second.1.parse::().unwrap(), + ); let first_set: HashSet = HashSet::from_iter(first.0..=first.1); let second_set: HashSet = HashSet::from_iter(second.0..=second.1); diff --git a/day5/src/main.rs b/day5/src/main.rs index 2464d41..421117b 100644 --- a/day5/src/main.rs +++ b/day5/src/main.rs @@ -5,21 +5,24 @@ fn print_stacks(stacks: &Vec>) { } fn stacks_message(stacks: &Vec>) -> 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 = chunks.0.lines().map(|x| -> String { - x.to_string() + " " - }).collect(); + + let mut stack_lines: Vec = 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::().unwrap() - 1; let len = stacks[from].len(); - let mut x: Vec = stacks[from].drain(len-n..len).collect(); + let mut x: Vec = stacks[from].drain(len - n..len).collect(); stacks[to].append(x.as_mut()); } diff --git a/day6/src/main.rs b/day6/src/main.rs index fae1ddf..b114e26 100644 --- a/day6/src/main.rs +++ b/day6/src/main.rs @@ -28,5 +28,4 @@ fn main() { } } } - } diff --git a/day7/src/main.rs b/day7/src/main.rs index 088f7a4..6f70a7d 100644 --- a/day7/src/main.rs +++ b/day7/src/main.rs @@ -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) { 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::(); + let total = g + .node_weights() + .filter(|(n, w)| n.ends_with("/") && *w <= 100000) + .map(|(_, w)| w) + .sum::(); 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}"); } diff --git a/day8/src/main.rs b/day8/src/main.rs index e5dbbe4..6a228a9 100644 --- a/day8/src/main.rs +++ b/day8/src/main.rs @@ -1,7 +1,7 @@ use std::borrow::BorrowMut; fn directional_scenic_score(seen: &Vec, 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::(); + let n_trees_visible = grid + .iter() + .map(|v| -> usize { v.iter().filter(|(_, vis, _)| *vis).count() }) + .sum::(); 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}"); } diff --git a/day9/src/main.rs b/day9/src/main.rs index 6d8f820..71ea7df 100644 --- a/day9/src/main.rs +++ b/day9/src/main.rs @@ -13,7 +13,7 @@ const ZERO: IVec2 = IVec2::new(0, 0); fn main() { const INPUT: &str = include_str!("../input.txt"); - + let mut visited: HashSet = 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();