use std::collections::HashMap;
pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
- let res = run_part1(&input)?;
+ let res = run_part1(input)?;
println!("{res}");
- let res = run_part2(&input)?;
+ let res = run_part2(input)?;
println!("{res}");
Ok(())
use std::path::Path;
pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
- let res = run_part1(&input)?;
+ let res = run_part1(input)?;
println!("{res}");
- let res = run_part2(&input)?;
+ let res = run_part2(input)?;
println!("{res}");
Ok(())
use regex::Regex;
pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
- let res = run_part1(&input)?;
+ let res = run_part1(input)?;
println!("{res}");
- let res = run_part2(&input)?;
+ let res = run_part2(input)?;
println!("{res}");
Ok(())
use std::path::Path;
pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
- let res = run_part1(&input)?;
+ let res = run_part1(input)?;
println!("{res}");
- let res = run_part2(&input)?;
+ let res = run_part2(input)?;
println!("{res}");
Ok(())
fn run_part2(input: &str) -> Result<u32, Box<dyn Error>> {
println!("Running {} - part 2", get_day());
- fn is_mas(coord: (i32, i32), dir: ((i32, i32), (i32, i32)), map: &Vec<Vec<char>>) -> bool {
+ fn is_mas(coord: (i32, i32), dir: ((i32, i32), (i32, i32)), map: &[Vec<char>]) -> bool {
let (r, c) = coord;
(
map[(r + dir.0.0) as usize][(c + dir.0.1) as usize] == 'M' &&
}
}
- pub fn is_ordered(self: &Self, line: &[u32]) -> bool {
+ pub fn is_ordered(&self, line: &[u32]) -> bool {
for i in 1..line.len() {
let v = &line[i];
for before in &line[0..i] {
- if self.order.get(v).is_some() && self.order.get(v).unwrap().contains(before) {
+ if self.order.contains_key(v) && self.order.get(v).unwrap().contains(before) {
return false
}
}
for i in 1..pages.len() {
let v = pages[i];
for j in 0..i {
- if puzzle.order.get(&v).is_some() && puzzle.order.get(&v).unwrap().contains(&pages[j]) {
- let tmp = pages[j];
- pages[j] = pages[i];
- pages[i] = tmp;
+ if puzzle.order.contains_key(&v) && puzzle.order.get(&v).unwrap().contains(&pages[j]) {
+ pages.swap(i, j);
}
}
}
}
pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
- let res = run_part1(&input)?;
+ let res = run_part1(input)?;
println!("{res}");
- let res = run_part2(&input)?;
+ let res = run_part2(input)?;
println!("{res}");
Ok(())
}
}
- fn guard_path(self: &mut Self) {
+ fn guard_path(&mut self) {
let mut turn_right: HashMap<(isize, isize), (isize, isize)> = HashMap::new();
turn_right.insert((-1,0), (0,1));
turn_right.insert((0,1), (1,0));
}
}
- fn is_guard_loop(self: &Self) -> bool {
+ fn is_guard_loop(&self) -> bool {
let mut turn_right: HashMap<(isize, isize), (isize, isize)> = HashMap::new();
turn_right.insert((-1,0), (0,1));
turn_right.insert((0,1), (1,0));
}
pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
- let res = run_part1(&input)?;
+ let res = run_part1(input)?;
println!("{res}");
- let res = run_part2(&input)?;
+ let res = run_part2(input)?;
println!("{res}");
Ok(())
}
pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
- let res = run_part1(&input)?;
+ let res = run_part1(input)?;
println!("{res}");
- let res = run_part2(&input)?;
+ let res = run_part2(input)?;
println!("{res}");
Ok(())
puzzle.map.iter()
.filter(|(k, v)| *v == value && *k != key)
- .for_each(|(k, v)| {
+ .for_each(|(k, _)| {
let (x1, x2) = if k <= key { (k, key) } else { (key, k) };
let d = (x1.0 - x2.0, x1.1 - x2.1);
for i in depth {
}
pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
- let res = run_part1(&input)?;
+ let res = run_part1(input)?;
println!("{res}");
- let res = run_part2(&input)?;
+ let res = run_part2(input)?;
println!("{res}");
Ok(())
Self { blocks, free }
}
- fn fill_free(self: &mut Self, free_idx: usize, last_block: usize) -> usize {
+ fn fill_free(&mut self, free_idx: usize, last_block: usize) -> usize {
let (free_start, free_length) = self.free[free_idx];
if free_length == 0 {
return last_block;
block_idx
}
- fn first_free_idx(self: &Self, size: u64) -> Option<usize> {
+ fn first_free_idx(&self, size: u64) -> Option<usize> {
self.free.iter().enumerate()
.filter(|(_, (_, length))| *length >= size)
.map(|(i, _)| i)
.nth(0)
}
- fn can_compact(self: &Self) -> bool {
+ fn can_compact(&self) -> bool {
let last_block = self.blocks.last().unwrap();
let last_block_end = last_block.0 + last_block.1 - 1;
self.free.iter()
.any(|(start, _)| *start < last_block_end)
}
- fn compact(self: &mut Self) {
+ fn compact(&mut self) {
let mut block_idx = 0;
while self.can_compact() {
let first_free = self.first_free_idx(1);
}
}
- fn compact_by_size(self: &mut Self) {
+ fn compact_by_size(&mut self) {
for i in (0..self.blocks.len()).rev() {
- match self.first_free_idx(self.blocks[i].1) {
- Some(first_free_idx) => {
- let first_free = self.free.get_mut(first_free_idx).unwrap();
- let block = self.blocks.get_mut(i).unwrap();
- if block.0 > first_free.0 {
- block.0 = first_free.0;
- first_free.0 += block.1;
- first_free.1 -= block.1;
- }
- },
- _ => {}
+ if let Some(first_free_idx) = self.first_free_idx(self.blocks[i].1) {
+ let first_free = self.free.get_mut(first_free_idx).unwrap();
+ let block = self.blocks.get_mut(i).unwrap();
+ if block.0 > first_free.0 {
+ block.0 = first_free.0;
+ first_free.0 += block.1;
+ first_free.1 -= block.1;
+ }
}
}
}
- fn checksum(self: &Self) -> u64 {
+ fn checksum(&self) -> u64 {
self.blocks.iter()
.map(|&(start, length, value)| {
(start..(start+length)).map(|i| i*value).sum::<u64>()
}
pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
- let res = run_part1(&input)?;
+ let res = run_part1(input)?;
println!("{res}");
- let res = run_part2(&input)?;
+ let res = run_part2(input)?;
println!("{res}");
Ok(())
let mut start_pos = Vec::new();
input.lines().enumerate()
.for_each(|(r, l)| {
- l.as_bytes().into_iter().enumerate()
+ l.as_bytes().iter().enumerate()
.for_each(|(c, b)| {
if *b != b'.' {
map.insert((r as isize, c as isize), *b);
Self { map, start_pos }
}
- fn score(self: &Self,
+ fn score(&self,
pos: (isize, isize),
value: u8,
visited: &mut Option<HashSet<(isize, isize)>>
.sum()
}
- fn solve(self: &Self, with_rating: bool) -> u32 {
+ fn solve(&self, with_rating: bool) -> u32 {
self.start_pos.iter()
.map(|pos| {
match with_rating {
}
pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
- let res = run_part1(&input)?;
+ let res = run_part1(input)?;
println!("{res}");
- let res = run_part2(&input)?;
+ let res = run_part2(input)?;
println!("{res}");
Ok(())
}
pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
- let res = run_part1(&input)?;
+ let res = run_part1(input)?;
println!("{res}");
- let res = run_part2(&input)?;
+ let res = run_part2(input)?;
println!("{res}");
Ok(())