Initial commit. day01 and da02.

This commit is contained in:
Pedro de Oliveira 2023-05-05 18:10:17 +01:00
commit 1484319343
12 changed files with 946 additions and 0 deletions

13
.gitignore vendored Normal file
View File

@ -0,0 +1,13 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/
input/
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
.idea/

594
Cargo.lock generated Normal file
View File

@ -0,0 +1,594 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anes"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bumpalo"
version = "3.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba"
[[package]]
name = "cast"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "ciborium"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0c137568cc60b904a7724001b35ce2630fd00d5d84805fbb608ab89509d788f"
dependencies = [
"ciborium-io",
"ciborium-ll",
"serde",
]
[[package]]
name = "ciborium-io"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "346de753af073cc87b52b2083a506b38ac176a44cfb05497b622e27be899b369"
[[package]]
name = "ciborium-ll"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "213030a2b5a4e0c0892b6652260cf6ccac84827b83a85a534e178e3906c4cf1b"
dependencies = [
"ciborium-io",
"half",
]
[[package]]
name = "clap"
version = "3.2.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71655c45cb9845d3270c9d6df84ebe72b4dad3c2ba3f7023ad47c144e4e473a5"
dependencies = [
"bitflags",
"clap_lex",
"indexmap",
"textwrap",
]
[[package]]
name = "clap_lex"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "criterion"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7c76e09c1aae2bc52b3d2f29e13c6572553b30c4aa1b8a49fd70de6412654cb"
dependencies = [
"anes",
"atty",
"cast",
"ciborium",
"clap",
"criterion-plot",
"itertools",
"lazy_static",
"num-traits",
"oorandom",
"plotters",
"rayon",
"regex",
"serde",
"serde_derive",
"serde_json",
"tinytemplate",
"walkdir",
]
[[package]]
name = "criterion-plot"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1"
dependencies = [
"cast",
"itertools",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521"
dependencies = [
"cfg-if",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "715e8152b692bba2d374b53d4875445368fdf21a94751410af607a5ac677d1fc"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01a9af1f4c2ef74bb8aa1f7e19706bc72d03598c8a570bb5de72243c7a9d9d5a"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
"memoffset",
"scopeguard",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fb766fa798726286dbbb842f174001dab8abc7b627a1dd86e0b7222a95d929f"
dependencies = [
"cfg-if",
]
[[package]]
name = "day01"
version = "0.1.0"
dependencies = [
"criterion",
]
[[package]]
name = "day02"
version = "0.1.0"
dependencies = [
"criterion",
]
[[package]]
name = "either"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]]
name = "half"
version = "1.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7"
[[package]]
name = "hashbrown"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "indexmap"
version = "1.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc"
[[package]]
name = "js-sys"
version = "0.3.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89"
[[package]]
name = "log"
version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
dependencies = [
"cfg-if",
]
[[package]]
name = "memoffset"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4"
dependencies = [
"autocfg",
]
[[package]]
name = "num-traits"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6058e64324c71e02bc2b150e4f3bc8286db6c83092132ffa3f6b1eab0f9def5"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "once_cell"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860"
[[package]]
name = "oorandom"
version = "11.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575"
[[package]]
name = "os_str_bytes"
version = "6.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee"
[[package]]
name = "plotters"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2538b639e642295546c50fcd545198c9d64ee2a38620a628724a3b266d5fbf97"
dependencies = [
"num-traits",
"plotters-backend",
"plotters-svg",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "plotters-backend"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "193228616381fecdc1224c62e96946dfbc73ff4384fba576e052ff8c1bea8142"
[[package]]
name = "plotters-svg"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9a81d2759aae1dae668f783c308bc5c8ebd191ff4184aaa1b37f65a6ae5a56f"
dependencies = [
"plotters-backend",
]
[[package]]
name = "proc-macro2"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rayon"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e060280438193c554f654141c9ea9417886713b7acd75974c85b18a69a88e0b"
dependencies = [
"crossbeam-deque",
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cac410af5d00ab6884528b4ab69d1e8e146e8d471201800fa1b4524126de6ad3"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"num_cpus",
]
[[package]]
name = "regex"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a"
dependencies = [
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
[[package]]
name = "ryu"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "serde"
version = "1.0.148"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e53f64bb4ba0191d6d0676e1b141ca55047d83b74f5607e6d8eb88126c52c2dc"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.148"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a55492425aa53521babf6137309e7d34c20bbfbbfcfe2c7f3a047fd1f6b92c0c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "020ff22c755c2ed3f8cf162dbb41a7268d934702f3ed3631656ea597e08fc3db"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "syn"
version = "1.0.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "textwrap"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d"
[[package]]
name = "tinytemplate"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "unicode-ident"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"
[[package]]
name = "walkdir"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
dependencies = [
"same-file",
"winapi",
"winapi-util",
]
[[package]]
name = "wasm-bindgen"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f"
[[package]]
name = "web-sys"
version = "0.3.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcda906d8be16e728fd5adc5b729afad4e444e106ab28cd1c7256e54fa61510f"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

5
Cargo.toml Normal file
View File

@ -0,0 +1,5 @@
[workspace]
members = [
"day01",
"day02",
]

1
README.md Normal file
View File

@ -0,0 +1 @@
# advent2016

11
day01/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "day01"
version = "0.1.0"
edition = "2021"
[dev-dependencies]
criterion = "0.4.0"
[[bench]]
name = "day01"
harness = false

19
day01/benches/day01.rs Normal file
View File

@ -0,0 +1,19 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use day01::{parse_input, part1, part2};
pub fn criterion_benchmark(c: &mut Criterion) {
let input = include_str!("../../input/day01.txt");
c.bench_function("day01 parse_input", |b| {
b.iter(|| parse_input(black_box(input)))
});
let input = parse_input(input);
c.bench_function("day01 part1", |b| b.iter(|| part1(black_box(&input))));
c.bench_function("day01 part2", |b| b.iter(|| part2(black_box(&input))));
}
criterion_group! {
name = benches;
config = Criterion::default().sample_size(1000);
targets = criterion_benchmark
}
criterion_main!(benches);

96
day01/src/lib.rs Normal file
View File

@ -0,0 +1,96 @@
use std::collections::HashSet;
#[derive(Debug)]
pub enum Move {
Left(u16),
Right(u16),
}
#[derive(Debug)]
enum Direction {
North,
South,
East,
West,
}
#[derive(Debug)]
struct Position {
x: i32,
y: i32,
direction: Direction,
}
pub fn parse_input(input: &str) -> Vec<Move> {
input
.split(", ")
.map(|item| {
let num = item[1..].parse::<u16>().unwrap();
match &item[0..1] {
"L" => Move::Left(num),
"R" => Move::Right(num),
_ => panic!("invalid"),
}
})
.collect()
}
fn do_movement(movement: &Move, pos: &mut Position) -> u16 {
match (movement, &mut pos.direction) {
(Move::Left(_), Direction::North) => pos.direction = Direction::West,
(Move::Left(_), Direction::South) => pos.direction = Direction::East,
(Move::Left(_), Direction::West) => pos.direction = Direction::South,
(Move::Left(_), Direction::East) => pos.direction = Direction::North,
(Move::Right(_), Direction::North) => pos.direction = Direction::East,
(Move::Right(_), Direction::South) => pos.direction = Direction::West,
(Move::Right(_), Direction::West) => pos.direction = Direction::North,
(Move::Right(_), Direction::East) => pos.direction = Direction::South,
}
match movement {
Move::Left(v) | Move::Right(v) => *v,
}
}
pub fn part1(moves: &Vec<Move>) -> u32 {
let mut pos = Position {
x: 0,
y: 0,
direction: Direction::North,
};
for movement in moves {
let value = do_movement(movement, &mut pos) as i32;
match pos.direction {
Direction::North => pos.y += value,
Direction::South => pos.y -= value,
Direction::East => pos.x += value,
Direction::West => pos.x -= value,
}
}
(pos.x.abs() + pos.y.abs()) as u32
}
pub fn part2(moves: &Vec<Move>) -> u32 {
let mut pos = Position {
x: 0,
y: 0,
direction: Direction::North,
};
let mut positions = HashSet::new();
'out: for movement in moves {
let value = do_movement(movement, &mut pos) as i32;
for _ in 0..value {
match pos.direction {
Direction::North => pos.y += 1,
Direction::South => pos.y -= 1,
Direction::East => pos.x += 1,
Direction::West => pos.x -= 1,
}
if positions.contains(&(pos.x, pos.y)) {
break 'out;
} else {
positions.insert((pos.x, pos.y));
}
}
}
(pos.x.abs() + pos.y.abs()) as u32
}

29
day01/src/main.rs Normal file
View File

@ -0,0 +1,29 @@
use ::day01::{parse_input, part1, part2};
fn main() {
let input = include_str!("../../input/day01.txt");
let movements = parse_input(input);
let part1 = part1(&movements);
println!("Part1: {}", part1);
let part2 = part2(&movements);
println!("Part2: {}", part2);
}
#[cfg(test)]
mod day01 {
use ::day01::{parse_input, part1, part2};
#[test]
fn part1_test() {
let inputs = vec!["R2, L3", "R2, R2, R2", "R5, L5, R5, R3"];
assert_eq!(part1(&parse_input(inputs[0])), 5);
assert_eq!(part1(&parse_input(inputs[1])), 2);
assert_eq!(part1(&parse_input(inputs[2])), 12);
}
#[test]
fn part2_test() {
let input = "R8, R4, R4, R8";
assert_eq!(part2(&parse_input(input)), 4);
}
}

11
day02/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "day02"
version = "0.1.0"
edition = "2021"
[dev-dependencies]
criterion = "0.4.0"
[[bench]]
name = "day02"
harness = false

23
day02/benches/day02.rs Normal file
View File

@ -0,0 +1,23 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use day02::*;
pub fn criterion_benchmark(c: &mut Criterion) {
let input = include_str!("../../input/day02.txt");
c.bench_function("day02 read_input", |b| {
b.iter(|| parse_input(black_box(input)))
});
let input = parse_input(input);
c.bench_function("day02 solve_part1", |b| {
b.iter(|| part1(black_box(&input)))
});
c.bench_function("day02 solve_part2", |b| {
b.iter(|| part2(black_box(&input)))
});
}
criterion_group!{
name = benches;
config = Criterion::default().sample_size(1000);
targets = criterion_benchmark
}
criterion_main!(benches);

115
day02/src/lib.rs Normal file
View File

@ -0,0 +1,115 @@
#[derive(Debug)]
pub enum Direction {
Up,
Down,
Left,
Right,
}
struct Position {
x: usize,
y: usize,
}
const GRID1: [[char; 3]; 3] = [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']];
const GRID2: [[char; 5]; 5] = [
[' ', ' ', '1', ' ', ' '],
[' ', '2', '3', '4', ' '],
['5', '6', '7', '8', '9'],
[' ', 'A', 'B', 'C', ' '],
[' ', ' ', 'D', ' ', ' '],
];
pub fn parse_input(input: &str) -> Vec<Vec<Direction>> {
input
.lines()
.map(|line| {
line.chars()
.map(|ch| match ch {
'U' => Direction::Up,
'D' => Direction::Down,
'L' => Direction::Left,
'R' => Direction::Right,
_ => panic!("Invalid direction: {}", ch),
})
.collect()
})
.collect()
}
fn do_moves_pt1(movements: &Vec<Direction>, grid: &[[char; 3]; 3], position: &mut Position) {
for movement in movements {
match movement {
Direction::Up => {
if position.x > 0 {
position.x -= 1;
}
}
Direction::Down => {
if position.x < grid.len() - 1 {
position.x += 1;
}
}
Direction::Left => {
if position.y > 0 {
position.y -= 1;
}
}
Direction::Right => {
if position.y < grid[position.x].len() - 1 {
position.y += 1;
}
}
}
}
}
fn do_moves_pt2(movements: &Vec<Direction>, grid: &[[char; 5]; 5], position: &mut Position) {
for movement in movements {
match movement {
Direction::Up => {
if position.x > 0 && grid[position.x - 1][position.y] != ' ' {
position.x -= 1;
}
}
Direction::Down => {
if position.x < grid.len() - 1 && grid[position.x + 1][position.y] != ' ' {
position.x += 1;
}
}
Direction::Left => {
if position.y > 0 && grid[position.x][position.y - 1] != ' ' {
position.y -= 1;
}
}
Direction::Right => {
if position.y < grid[0].len() - 1 && grid[position.x][position.y + 1] != ' ' {
position.y += 1;
}
}
}
}
}
pub fn part1(moves: &Vec<Vec<Direction>>) -> String {
let mut position = Position { x: 1, y: 1 };
moves
.iter()
.map(|digit| {
do_moves_pt1(digit, &GRID1, &mut position);
GRID1[position.x][position.y]
})
.collect()
}
pub fn part2(moves: &Vec<Vec<Direction>>) -> String {
let mut position = Position { x: 2, y: 0 };
moves
.iter()
.map(|digit| {
do_moves_pt2(digit, &GRID2, &mut position);
GRID2[position.x][position.y]
})
.collect()
}

29
day02/src/main.rs Normal file
View File

@ -0,0 +1,29 @@
use ::day02::{parse_input, part1, part2};
fn main() {
let input = include_str!("../../input/day02.txt");
let movements = parse_input(input);
let part1 = part1(&movements);
println!("Part1: {}", part1);
let part2 = part2(&movements);
println!("Part2: {}", part2);
}
#[cfg(test)]
mod day02 {
use day02::{parse_input, part1, part2};
const INPUT: &str = r#"ULL
RRDDD
LURDL
UUUUD"#;
#[test]
fn part1_test() {
assert_eq!(part1(&parse_input(INPUT)), "1985");
}
#[test]
fn part2_test() {
assert_eq!(part2(&parse_input(INPUT)), "5DB3");
}
}