From 9651b3a12c69765e007d0289bf678fb16f4e2ee2 Mon Sep 17 00:00:00 2001 From: Ty Date: Mon, 8 Jan 2024 23:00:36 -0700 Subject: [PATCH] Push work so far --- .vscode/aoc.code-snippets | 46 ++++++++++++++++++++++ Cargo.lock | 7 ++++ cli/src/main.rs | 7 ++-- cli/src/utils.rs | 2 +- lib/Cargo.toml | 1 + lib/src/day17/mod.rs | 80 +++++++++++++++++++++++++++++++++++++++ lib/src/lib.rs | 5 ++- 7 files changed, 143 insertions(+), 5 deletions(-) create mode 100644 .vscode/aoc.code-snippets create mode 100644 lib/src/day17/mod.rs diff --git a/.vscode/aoc.code-snippets b/.vscode/aoc.code-snippets new file mode 100644 index 0000000..4ae25db --- /dev/null +++ b/.vscode/aoc.code-snippets @@ -0,0 +1,46 @@ +{ + // Place your advent-of-code-2023 workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and + // description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope + // is left empty or omitted, the snippet gets applied to all languages. The prefix is what is + // used to trigger the snippet and the body will be expanded and inserted. Possible variables are: + // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. + // Placeholders with the same ids are connected. + // Example: + // "Print to console": { + // "scope": "javascript,typescript", + // "prefix": "log", + // "body": [ + // "console.log('$1');", + // "$2" + // ], + // "description": "Log output to console" + // } + "Day template": { + "scope": "rust", + "prefix": "day", + "body": [ + "use crate::utils::Day;", + "", + "#[derive(Debug, Default)]", + "pub struct Day$1 {", + "\tpub input: String,", + "}", + "", + "impl Day for Day$1 {", + "\tfn part1(&mut self) -> String {", + "\t\t", + "\t\t", + "\t\ttodo!()", + "\t}", + "", + "\tfn part2(&mut self) -> String {", + "\t\t", + "\t\t", + "\t\ttodo!()", + "\t}", + "}" + + ], + "description": "Creates a template for an AOC day" + } +} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 9f9e7b7..db2863e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21,6 +21,7 @@ checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" name = "advent" version = "0.1.0" dependencies = [ + "array2d", "clru", "getrandom", "itertools", @@ -122,6 +123,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "array2d" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8b39cb2c1bf5a7c0dd097aa95ab859cf87dab5a4328900f5388942dc1889f74" + [[package]] name = "autocfg" version = "1.1.0" diff --git a/cli/src/main.rs b/cli/src/main.rs index 1c501b9..ac96e9d 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -160,11 +160,12 @@ async fn main() { let deviation = (((max - min).pow(2) / 16) as f64).sqrt() as u128; let range = max - min; let median = if timings.len() % 2 == 0 { - (timings[timings.len()/2-1]+timings[timings.len()/2]).as_nanos() as f64 / 2.0 + (timings[timings.len() / 2 - 1] + timings[timings.len() / 2]).as_nanos() as f64 + / 2.0 } else { - timings[timings.len()/2].as_nanos() as f64 + timings[timings.len() / 2].as_nanos() as f64 }; - + let (avg, avg_units) = utils::unitify(avg as f64); let (max, max_units) = utils::unitify(max as f64); let (min, min_units) = utils::unitify(min as f64); diff --git a/cli/src/utils.rs b/cli/src/utils.rs index e197567..9832e6b 100644 --- a/cli/src/utils.rs +++ b/cli/src/utils.rs @@ -8,4 +8,4 @@ pub fn unitify(num: f64) -> (f64, &'static str) { } else { (num, "ns") } -} \ No newline at end of file +} diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 68fffb4..7086b53 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -12,6 +12,7 @@ scraper = "0.18.1" reqwest = "0.11.22" itertools = "0.12.0" clru = "0.6.1" +array2d = "0.3.2" [dependencies.getrandom] version = "0.2.11" diff --git a/lib/src/day17/mod.rs b/lib/src/day17/mod.rs new file mode 100644 index 0000000..b7f04bd --- /dev/null +++ b/lib/src/day17/mod.rs @@ -0,0 +1,80 @@ +use crate::utils::Day; +use array2d::Array2D; + +#[derive(Debug, Default)] +pub struct Day17 { + pub input: String, +} + +#[derive(PartialEq)] +pub enum Direction { + North, + East, + South, + West, +} + +pub struct State { + cost: usize, + x: usize, + y: usize, + tentative_distance: Option, + last_direction: Option, + moves_without_turn: Option, +} + +impl Day for Day17 { + fn part1(&mut self) -> String { + let columns = self.input.find('\n').unwrap(); + let rows = (self.input.len() + 1) / columns - 1; + + let mut parsed = Array2D::from_iter_row_major( + self.input.split('\n').enumerate().flat_map(|(i, str)| { + str.chars().enumerate().map(move |(j, b)| State { + cost: b.to_digit(10).unwrap() as usize, + x: i, + y: j, + tentative_distance: if i == 0 && j == 0 { + Some(b.to_digit(10).unwrap() as usize) + } else { + None + }, + moves_without_turn: if i == 0 && j == 0 { + Some(1) + } else { + None + }, + last_direction: if i == 0 && j == 0 { + Some(Direction::East) + } else { + None + }, + }) + }), + rows, + columns, + ) + .unwrap(); + let unvisited = Vec::<(usize, usize)>::from_iter( + (0..columns).flat_map(|i| (0..rows).map(move |j| (i, j))), + ); + let mut current = (0usize, 0usize); + + loop { + let current = &parsed[current]; + + if current.last_direction != Some(Direction::North) + && !(current.last_direction == Some(Direction::West) && current.moves_without_turn == Some(3)) + && let Some(neighbor) = parsed.get_mut(current.x - 1, current.y) + { // Going west + + } + } + + todo!() + } + + fn part2(&mut self) -> String { + todo!() + } +} diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 0d6918f..d5c9bf6 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -1,4 +1,4 @@ -#![feature(inline_const)] +#![feature(inline_const, let_chains)] use std::{collections::HashMap, num::NonZeroUsize}; @@ -11,6 +11,7 @@ use day13::Day13; use day14::Day14; use day15::Day15; use day16::Day16; +use day17::Day17; use day2::Day2; use day3::Day3; use day4::Day4; @@ -32,6 +33,7 @@ pub mod day13; pub mod day14; pub mod day15; pub mod day16; +pub mod day17; pub mod day2; pub mod day3; pub mod day4; @@ -79,6 +81,7 @@ pub fn get_day(day: u8, input: String) -> Box { 14 => Box::new(Day14 { input }), 15 => Box::new(Day15 { input }), 16 => Box::new(Day16 { input }), + 17 => Box::new(Day17 { input }), _ => panic!("Invalid day #"), } }