mirror of
https://github.com/mx42/aoc2016.git
synced 2026-01-14 13:49:53 +01:00
feat: add day2
This commit is contained in:
65
Cargo.lock
generated
65
Cargo.lock
generated
@@ -77,11 +77,11 @@ checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cc"
|
name = "cc"
|
||||||
version = "1.0.83"
|
version = "1.2.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
|
checksum = "1aeb932158bd710538c73702db6945cb68a8fb08c519e6e12706b94263b36db8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"shlex",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -163,9 +163,9 @@ checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "js-sys"
|
name = "js-sys"
|
||||||
version = "0.3.66"
|
version = "0.3.72"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca"
|
checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
@@ -178,9 +178,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.150"
|
version = "0.2.162"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c"
|
checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lock_api"
|
name = "lock_api"
|
||||||
@@ -225,9 +225,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num-traits"
|
name = "num-traits"
|
||||||
version = "0.2.17"
|
version = "0.2.19"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
|
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"autocfg",
|
"autocfg",
|
||||||
]
|
]
|
||||||
@@ -278,18 +278,18 @@ checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.70"
|
version = "1.0.89"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b"
|
checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quote"
|
name = "quote"
|
||||||
version = "1.0.33"
|
version = "1.0.37"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
|
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
@@ -329,18 +329,18 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.193"
|
version = "1.0.215"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89"
|
checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde_derive",
|
"serde_derive",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
version = "1.0.193"
|
version = "1.0.215"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3"
|
checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@@ -358,6 +358,12 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "shlex"
|
||||||
|
version = "1.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "smallvec"
|
name = "smallvec"
|
||||||
version = "1.11.2"
|
version = "1.11.2"
|
||||||
@@ -366,9 +372,9 @@ checksum = "4dccd0940a2dcdf68d092b8cbab7dc0ad8fa938bf95787e1b916b0e3d0e8e970"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.39"
|
version = "2.0.87"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a"
|
checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@@ -405,19 +411,20 @@ checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen"
|
name = "wasm-bindgen"
|
||||||
version = "0.2.89"
|
version = "0.2.95"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e"
|
checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
|
"once_cell",
|
||||||
"wasm-bindgen-macro",
|
"wasm-bindgen-macro",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-backend"
|
name = "wasm-bindgen-backend"
|
||||||
version = "0.2.89"
|
version = "0.2.95"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826"
|
checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bumpalo",
|
"bumpalo",
|
||||||
"log",
|
"log",
|
||||||
@@ -430,9 +437,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-macro"
|
name = "wasm-bindgen-macro"
|
||||||
version = "0.2.89"
|
version = "0.2.95"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2"
|
checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"quote",
|
"quote",
|
||||||
"wasm-bindgen-macro-support",
|
"wasm-bindgen-macro-support",
|
||||||
@@ -440,9 +447,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-macro-support"
|
name = "wasm-bindgen-macro-support"
|
||||||
version = "0.2.89"
|
version = "0.2.95"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283"
|
checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@@ -453,9 +460,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-shared"
|
name = "wasm-bindgen-shared"
|
||||||
version = "0.2.89"
|
version = "0.2.95"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f"
|
checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows-core"
|
name = "windows-core"
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
ULL
|
||||||
|
RRDDD
|
||||||
|
LURDL
|
||||||
|
UUUUD
|
||||||
|
|||||||
182
src/bin/02.rs
182
src/bin/02.rs
@@ -1,26 +1,198 @@
|
|||||||
advent_of_code::solution!(2);
|
advent_of_code::solution!(2);
|
||||||
|
|
||||||
pub fn part_one(input: &str) -> Option<u32> {
|
trait Keypad {
|
||||||
None
|
fn init() -> Self;
|
||||||
|
fn move_to(self, dir: &Dir) -> Self;
|
||||||
|
fn to_digit(self) -> char;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part_two(input: &str) -> Option<u32> {
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
struct PosP1 {
|
||||||
|
x: i8,
|
||||||
|
y: i8,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PosP1 {
|
||||||
|
// y=1 1 2 3
|
||||||
|
// y=0 4 5 6
|
||||||
|
// y=-1 7 8 9
|
||||||
|
// x=-1 x=0 x=1
|
||||||
|
|
||||||
|
fn from_pos(x: i8, y: i8) -> Self {
|
||||||
|
let x = match x {
|
||||||
|
_x if _x < -1 => -1,
|
||||||
|
_x if _x > 1 => 1,
|
||||||
|
_ => x,
|
||||||
|
};
|
||||||
|
let y = match y {
|
||||||
|
_y if _y < -1 => -1,
|
||||||
|
_y if _y > 1 => 1,
|
||||||
|
_ => y,
|
||||||
|
};
|
||||||
|
PosP1 {x: x, y: y}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Keypad for PosP1 {
|
||||||
|
fn init() -> PosP1 {
|
||||||
|
Self {
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_digit(self) -> char {
|
||||||
|
let row_offset = (self.y * -1) + 1;
|
||||||
|
let col_offset = self.x + 1;
|
||||||
|
char::from_digit(
|
||||||
|
((row_offset * 3) + col_offset + 1).try_into().unwrap(),
|
||||||
|
10
|
||||||
|
).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn move_to(self, dir: &Dir) -> PosP1 {
|
||||||
|
match dir {
|
||||||
|
Dir::Up => PosP1::from_pos(self.x, self.y + 1),
|
||||||
|
Dir::Down => PosP1::from_pos(self.x, self.y - 1),
|
||||||
|
Dir::Left => PosP1::from_pos(self.x - 1, self.y),
|
||||||
|
Dir::Right => PosP1::from_pos(self.x + 1, self.y),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const DISP_P2: [[char; 5]; 5]= [
|
||||||
|
['0', '0', '1', '0', '0', ],
|
||||||
|
['0', '2', '3', '4', '0', ],
|
||||||
|
['5', '6', '7', '8', '9', ],
|
||||||
|
['0', 'A', 'B', 'C', '0', ],
|
||||||
|
['0', '0', 'D', '0', '0', ],
|
||||||
|
];
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
struct PosP2 {
|
||||||
|
row: usize,
|
||||||
|
col: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PosP2 {
|
||||||
|
fn from(row: usize, col: usize) -> Option<Self> {
|
||||||
|
if row > 4 || col > 4 {
|
||||||
|
return None
|
||||||
|
}
|
||||||
|
let res = PosP2 {
|
||||||
|
row: row,
|
||||||
|
col: col,
|
||||||
|
};
|
||||||
|
if res.to_digit() == '0' {
|
||||||
None
|
None
|
||||||
|
} else {
|
||||||
|
Some(res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Keypad for PosP2 {
|
||||||
|
fn init() -> PosP2 {
|
||||||
|
Self {
|
||||||
|
row: 2,
|
||||||
|
col: 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_digit(self) -> char {
|
||||||
|
DISP_P2[self.row][self.col]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn move_to(self, dir: &Dir) -> PosP2 {
|
||||||
|
match dir {
|
||||||
|
Dir::Up => PosP2::from(self.row.saturating_sub(1), self.col).unwrap_or(self),
|
||||||
|
Dir::Down => PosP2::from(self.row + 1, self.col).unwrap_or(self),
|
||||||
|
Dir::Left => PosP2::from(self.row, self.col.saturating_sub(1)).unwrap_or(self),
|
||||||
|
Dir::Right => PosP2::from(self.row, self.col + 1).unwrap_or(self),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
enum Dir {
|
||||||
|
Up,
|
||||||
|
Down,
|
||||||
|
Left,
|
||||||
|
Right,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compute_digit<T: Keypad>(input: T, path: &Vec<Dir>) -> T {
|
||||||
|
path
|
||||||
|
.into_iter()
|
||||||
|
.fold(input, |pos, dir| pos.move_to(dir))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn parse_input(input: &str) -> Vec<Vec<Dir>> {
|
||||||
|
input
|
||||||
|
.to_string()
|
||||||
|
.strip_suffix("\n")
|
||||||
|
.unwrap_or(&input)
|
||||||
|
.split("\n")
|
||||||
|
.map(|s| s
|
||||||
|
.chars()
|
||||||
|
.map(|c| match c {
|
||||||
|
'U' => Dir::Up,
|
||||||
|
'D' => Dir::Down,
|
||||||
|
'L' => Dir::Left,
|
||||||
|
'R' => Dir::Right,
|
||||||
|
_ => panic!("Invalid input!"),
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve<T: Keypad + Copy>(input: &str) -> Option<String> {
|
||||||
|
let dirs = parse_input(input);
|
||||||
|
let digit: T = T::init();
|
||||||
|
let res: Vec<char> = dirs.into_iter().scan(digit, |digit, ds| {
|
||||||
|
*digit = compute_digit(*digit, &ds);
|
||||||
|
Some(digit.to_digit())
|
||||||
|
}).collect();
|
||||||
|
Some(String::from_iter(res))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn part_one(input: &str) -> Option<String> {
|
||||||
|
solve::<PosP1>(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part_two(input: &str) -> Option<String> {
|
||||||
|
solve::<PosP2>(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_posp1_to_digit() {
|
||||||
|
assert_eq!(PosP1 {x: -1, y: 1}.to_digit(), '1');
|
||||||
|
assert_eq!(PosP1 {x: 0, y: 1}.to_digit(), '2');
|
||||||
|
assert_eq!(PosP1 {x: 1, y: 1}.to_digit(), '3');
|
||||||
|
assert_eq!(PosP1 {x: -1, y: 0}.to_digit(), '4');
|
||||||
|
assert_eq!(PosP1 {x: 0, y: 0}.to_digit(), '5');
|
||||||
|
assert_eq!(PosP1 {x: 1, y: 0}.to_digit(), '6');
|
||||||
|
assert_eq!(PosP1 {x: -1, y: -1}.to_digit(), '7');
|
||||||
|
assert_eq!(PosP1 {x: 0, y: -1}.to_digit(), '8');
|
||||||
|
assert_eq!(PosP1 {x: 1, y: -1}.to_digit(), '9');
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part_one() {
|
fn test_part_one() {
|
||||||
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
|
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
|
||||||
assert_eq!(result, None);
|
assert_eq!(result, Some("1985".to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part_two() {
|
fn test_part_two() {
|
||||||
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
|
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
|
||||||
assert_eq!(result, None);
|
assert_eq!(result, Some("5DB3".to_string()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user