Add days 11, 12
This commit is contained in:
70
12-rain_risk/Cargo.lock
generated
Normal file
70
12-rain_risk/Cargo.lock
generated
Normal file
@@ -0,0 +1,70 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.38"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "afddf7f520a80dbf76e6f50a35bca42a2331ef227a28b3b6dc5c2e2338d114b1"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rain_risk"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.60"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "76cc616c6abf8c8928e2fdcc0dbfab37175edd8fb49a4641066ad1364fdab146"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9be73a2caec27583d0046ef3796c3794f868a5bc813db689eed00c7631275cd1"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
|
||||
11
12-rain_risk/Cargo.toml
Normal file
11
12-rain_risk/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "rain_risk"
|
||||
version = "0.1.0"
|
||||
authors = ["Guilhem MARION <gmarion@netc.fr>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.38"
|
||||
thiserror = "1.0.23"
|
||||
776
12-rain_risk/data/input.txt
Normal file
776
12-rain_risk/data/input.txt
Normal file
@@ -0,0 +1,776 @@
|
||||
R180
|
||||
E1
|
||||
N1
|
||||
R90
|
||||
E4
|
||||
F84
|
||||
W3
|
||||
F19
|
||||
E5
|
||||
N1
|
||||
W5
|
||||
W4
|
||||
R90
|
||||
S1
|
||||
F70
|
||||
R90
|
||||
W2
|
||||
S3
|
||||
L180
|
||||
E2
|
||||
R90
|
||||
E1
|
||||
R90
|
||||
N2
|
||||
E3
|
||||
F55
|
||||
W3
|
||||
R90
|
||||
S2
|
||||
E4
|
||||
L90
|
||||
F33
|
||||
W4
|
||||
S3
|
||||
F11
|
||||
N4
|
||||
W4
|
||||
F88
|
||||
W3
|
||||
F62
|
||||
L90
|
||||
S3
|
||||
W3
|
||||
N1
|
||||
E3
|
||||
E2
|
||||
N1
|
||||
E3
|
||||
S3
|
||||
E5
|
||||
S4
|
||||
W5
|
||||
L90
|
||||
W5
|
||||
R90
|
||||
E4
|
||||
F36
|
||||
N4
|
||||
E4
|
||||
E5
|
||||
F47
|
||||
R90
|
||||
N3
|
||||
E3
|
||||
L180
|
||||
S5
|
||||
R90
|
||||
S5
|
||||
W2
|
||||
S3
|
||||
F43
|
||||
W2
|
||||
R180
|
||||
W2
|
||||
S2
|
||||
L180
|
||||
E2
|
||||
F49
|
||||
L90
|
||||
F77
|
||||
S5
|
||||
E1
|
||||
S2
|
||||
F39
|
||||
L180
|
||||
F12
|
||||
W1
|
||||
L90
|
||||
F60
|
||||
S2
|
||||
E3
|
||||
N1
|
||||
E5
|
||||
R90
|
||||
E1
|
||||
N4
|
||||
W4
|
||||
S4
|
||||
E2
|
||||
L90
|
||||
F100
|
||||
E3
|
||||
F58
|
||||
S2
|
||||
E4
|
||||
F83
|
||||
W3
|
||||
N1
|
||||
R90
|
||||
F99
|
||||
W5
|
||||
W4
|
||||
F19
|
||||
N3
|
||||
W1
|
||||
W5
|
||||
F70
|
||||
R180
|
||||
R90
|
||||
F58
|
||||
E5
|
||||
N3
|
||||
R90
|
||||
N4
|
||||
F86
|
||||
N4
|
||||
F70
|
||||
L90
|
||||
F36
|
||||
R90
|
||||
S4
|
||||
R180
|
||||
N5
|
||||
F81
|
||||
W5
|
||||
R90
|
||||
E5
|
||||
L90
|
||||
F73
|
||||
S5
|
||||
E3
|
||||
N1
|
||||
F27
|
||||
E2
|
||||
N3
|
||||
L180
|
||||
W2
|
||||
F24
|
||||
N2
|
||||
L90
|
||||
F69
|
||||
N2
|
||||
E3
|
||||
F49
|
||||
L90
|
||||
S4
|
||||
W5
|
||||
F18
|
||||
R180
|
||||
F62
|
||||
S2
|
||||
F65
|
||||
E3
|
||||
F100
|
||||
N1
|
||||
F96
|
||||
W1
|
||||
F75
|
||||
L180
|
||||
W2
|
||||
S2
|
||||
F93
|
||||
E5
|
||||
S3
|
||||
R90
|
||||
S2
|
||||
F34
|
||||
R90
|
||||
F83
|
||||
S1
|
||||
W1
|
||||
F58
|
||||
L180
|
||||
N4
|
||||
E1
|
||||
R90
|
||||
W2
|
||||
F93
|
||||
S3
|
||||
F20
|
||||
R90
|
||||
N3
|
||||
E3
|
||||
N3
|
||||
L90
|
||||
F8
|
||||
S3
|
||||
F18
|
||||
S3
|
||||
N2
|
||||
F6
|
||||
L270
|
||||
F70
|
||||
W5
|
||||
S3
|
||||
F54
|
||||
E2
|
||||
F75
|
||||
E3
|
||||
R90
|
||||
N2
|
||||
W3
|
||||
L90
|
||||
E4
|
||||
F58
|
||||
N5
|
||||
F97
|
||||
W2
|
||||
L90
|
||||
W5
|
||||
S2
|
||||
W1
|
||||
S5
|
||||
F85
|
||||
N1
|
||||
E1
|
||||
N4
|
||||
E4
|
||||
L90
|
||||
E1
|
||||
R90
|
||||
S5
|
||||
L180
|
||||
R270
|
||||
N1
|
||||
L90
|
||||
E4
|
||||
N3
|
||||
F45
|
||||
N2
|
||||
F68
|
||||
R90
|
||||
F36
|
||||
N5
|
||||
F82
|
||||
S5
|
||||
E4
|
||||
R180
|
||||
S2
|
||||
L180
|
||||
N3
|
||||
R270
|
||||
W5
|
||||
F70
|
||||
L90
|
||||
W5
|
||||
F80
|
||||
W4
|
||||
N2
|
||||
R90
|
||||
S3
|
||||
W1
|
||||
F23
|
||||
N1
|
||||
W1
|
||||
N4
|
||||
F70
|
||||
S3
|
||||
L180
|
||||
F57
|
||||
R90
|
||||
L90
|
||||
F55
|
||||
L90
|
||||
N4
|
||||
F87
|
||||
L90
|
||||
F1
|
||||
L270
|
||||
F17
|
||||
N5
|
||||
R180
|
||||
F84
|
||||
R90
|
||||
W5
|
||||
F7
|
||||
W1
|
||||
S1
|
||||
E3
|
||||
F46
|
||||
S5
|
||||
E2
|
||||
F23
|
||||
R90
|
||||
E4
|
||||
W2
|
||||
F96
|
||||
E5
|
||||
L90
|
||||
F65
|
||||
F3
|
||||
S5
|
||||
E5
|
||||
N3
|
||||
W4
|
||||
L90
|
||||
S2
|
||||
F57
|
||||
E1
|
||||
R90
|
||||
F68
|
||||
E3
|
||||
L90
|
||||
W1
|
||||
F29
|
||||
N5
|
||||
W5
|
||||
N1
|
||||
F95
|
||||
N1
|
||||
L90
|
||||
F31
|
||||
S5
|
||||
L180
|
||||
N2
|
||||
W5
|
||||
R90
|
||||
F27
|
||||
E1
|
||||
R90
|
||||
E3
|
||||
S5
|
||||
F10
|
||||
R90
|
||||
N4
|
||||
E2
|
||||
F25
|
||||
S4
|
||||
E5
|
||||
F51
|
||||
N3
|
||||
W2
|
||||
L90
|
||||
S3
|
||||
L180
|
||||
F17
|
||||
E4
|
||||
F93
|
||||
E3
|
||||
L90
|
||||
F41
|
||||
L90
|
||||
S5
|
||||
L90
|
||||
W5
|
||||
N1
|
||||
F81
|
||||
L90
|
||||
E4
|
||||
W2
|
||||
R90
|
||||
W1
|
||||
S5
|
||||
R90
|
||||
F39
|
||||
W3
|
||||
R90
|
||||
N5
|
||||
E1
|
||||
L90
|
||||
F82
|
||||
S3
|
||||
R90
|
||||
W4
|
||||
F66
|
||||
F4
|
||||
L90
|
||||
F77
|
||||
R90
|
||||
E1
|
||||
L90
|
||||
F53
|
||||
S4
|
||||
F35
|
||||
W1
|
||||
F64
|
||||
R90
|
||||
F9
|
||||
S1
|
||||
E1
|
||||
L90
|
||||
W4
|
||||
R90
|
||||
S2
|
||||
W5
|
||||
R90
|
||||
S4
|
||||
L90
|
||||
N3
|
||||
F8
|
||||
L180
|
||||
N5
|
||||
E5
|
||||
N4
|
||||
F35
|
||||
N5
|
||||
W1
|
||||
N1
|
||||
E5
|
||||
F15
|
||||
R180
|
||||
F92
|
||||
W3
|
||||
L90
|
||||
F4
|
||||
L90
|
||||
E1
|
||||
S3
|
||||
W3
|
||||
R90
|
||||
F37
|
||||
N5
|
||||
F19
|
||||
S2
|
||||
F98
|
||||
L90
|
||||
F24
|
||||
W3
|
||||
F68
|
||||
N5
|
||||
R90
|
||||
W3
|
||||
L90
|
||||
W3
|
||||
L90
|
||||
S1
|
||||
L90
|
||||
S4
|
||||
W3
|
||||
F56
|
||||
N4
|
||||
R90
|
||||
E3
|
||||
W1
|
||||
L90
|
||||
E4
|
||||
N3
|
||||
R180
|
||||
E1
|
||||
S1
|
||||
W2
|
||||
R90
|
||||
N3
|
||||
F82
|
||||
N2
|
||||
F37
|
||||
S3
|
||||
L180
|
||||
E2
|
||||
L180
|
||||
F6
|
||||
N2
|
||||
F96
|
||||
E2
|
||||
R180
|
||||
E2
|
||||
W3
|
||||
R90
|
||||
E2
|
||||
S5
|
||||
S1
|
||||
F23
|
||||
R90
|
||||
W5
|
||||
F75
|
||||
S1
|
||||
L90
|
||||
S3
|
||||
E1
|
||||
F83
|
||||
W4
|
||||
L180
|
||||
W5
|
||||
L90
|
||||
N1
|
||||
E1
|
||||
S2
|
||||
F17
|
||||
L90
|
||||
S2
|
||||
F53
|
||||
R90
|
||||
S3
|
||||
N3
|
||||
W1
|
||||
N4
|
||||
L180
|
||||
L90
|
||||
E3
|
||||
F9
|
||||
S5
|
||||
F24
|
||||
W3
|
||||
E5
|
||||
N2
|
||||
F73
|
||||
N1
|
||||
F28
|
||||
N2
|
||||
W4
|
||||
N3
|
||||
F53
|
||||
E5
|
||||
F47
|
||||
W2
|
||||
F60
|
||||
L90
|
||||
E2
|
||||
F19
|
||||
S1
|
||||
F63
|
||||
W5
|
||||
F100
|
||||
N3
|
||||
L180
|
||||
F83
|
||||
N4
|
||||
W5
|
||||
F37
|
||||
S1
|
||||
F50
|
||||
E1
|
||||
N2
|
||||
W3
|
||||
R90
|
||||
F85
|
||||
S4
|
||||
F72
|
||||
N4
|
||||
L90
|
||||
F48
|
||||
R90
|
||||
F99
|
||||
R90
|
||||
F58
|
||||
W3
|
||||
W4
|
||||
F64
|
||||
E1
|
||||
R90
|
||||
F74
|
||||
L90
|
||||
F23
|
||||
N3
|
||||
N3
|
||||
E1
|
||||
S1
|
||||
W5
|
||||
L180
|
||||
F98
|
||||
L90
|
||||
F36
|
||||
W4
|
||||
S2
|
||||
W3
|
||||
F9
|
||||
F72
|
||||
W5
|
||||
F78
|
||||
N2
|
||||
F65
|
||||
S3
|
||||
F47
|
||||
S5
|
||||
R90
|
||||
F68
|
||||
L180
|
||||
W2
|
||||
F7
|
||||
E2
|
||||
E3
|
||||
S4
|
||||
R90
|
||||
N2
|
||||
L180
|
||||
W2
|
||||
R180
|
||||
E4
|
||||
R90
|
||||
W3
|
||||
L90
|
||||
E4
|
||||
F54
|
||||
L180
|
||||
E2
|
||||
F6
|
||||
W5
|
||||
F82
|
||||
E4
|
||||
R90
|
||||
E4
|
||||
F25
|
||||
N2
|
||||
R270
|
||||
N4
|
||||
F18
|
||||
N5
|
||||
R90
|
||||
S3
|
||||
R90
|
||||
F38
|
||||
R90
|
||||
F97
|
||||
W4
|
||||
F85
|
||||
S4
|
||||
F56
|
||||
E4
|
||||
S1
|
||||
F40
|
||||
W3
|
||||
F52
|
||||
L90
|
||||
F76
|
||||
N4
|
||||
F15
|
||||
S2
|
||||
F22
|
||||
S5
|
||||
L180
|
||||
F91
|
||||
L180
|
||||
F8
|
||||
L90
|
||||
E4
|
||||
N4
|
||||
F67
|
||||
L90
|
||||
S3
|
||||
R180
|
||||
R90
|
||||
N4
|
||||
F71
|
||||
W3
|
||||
F34
|
||||
E2
|
||||
N1
|
||||
F43
|
||||
W5
|
||||
L180
|
||||
N5
|
||||
W2
|
||||
F42
|
||||
R90
|
||||
W3
|
||||
F39
|
||||
E1
|
||||
S2
|
||||
L180
|
||||
N5
|
||||
E3
|
||||
N5
|
||||
F28
|
||||
E1
|
||||
R90
|
||||
S3
|
||||
F40
|
||||
L90
|
||||
S2
|
||||
S2
|
||||
L90
|
||||
W5
|
||||
L90
|
||||
F93
|
||||
R180
|
||||
W4
|
||||
S4
|
||||
W4
|
||||
F100
|
||||
S3
|
||||
R90
|
||||
E2
|
||||
L180
|
||||
W1
|
||||
E3
|
||||
S5
|
||||
L90
|
||||
F87
|
||||
N1
|
||||
R90
|
||||
F3
|
||||
R90
|
||||
E5
|
||||
R90
|
||||
S3
|
||||
F45
|
||||
L90
|
||||
S2
|
||||
F42
|
||||
R90
|
||||
F95
|
||||
L90
|
||||
E1
|
||||
N3
|
||||
R90
|
||||
F73
|
||||
S3
|
||||
E1
|
||||
L90
|
||||
S2
|
||||
E3
|
||||
L90
|
||||
L270
|
||||
F38
|
||||
S5
|
||||
R90
|
||||
F42
|
||||
L90
|
||||
N1
|
||||
F7
|
||||
S3
|
||||
F65
|
||||
N2
|
||||
F42
|
||||
L180
|
||||
W5
|
||||
S4
|
||||
E4
|
||||
F65
|
||||
S4
|
||||
E5
|
||||
F51
|
||||
E4
|
||||
R180
|
||||
F70
|
||||
R90
|
||||
F28
|
||||
N5
|
||||
W5
|
||||
N1
|
||||
F96
|
||||
L90
|
||||
W4
|
||||
S3
|
||||
W3
|
||||
F89
|
||||
W1
|
||||
L90
|
||||
F75
|
||||
L270
|
||||
S3
|
||||
R90
|
||||
L90
|
||||
F7
|
||||
E2
|
||||
F24
|
||||
R180
|
||||
S2
|
||||
L180
|
||||
F48
|
||||
R90
|
||||
F37
|
||||
W2
|
||||
R90
|
||||
W4
|
||||
L90
|
||||
W3
|
||||
F81
|
||||
E4
|
||||
N2
|
||||
F39
|
||||
E4
|
||||
N1
|
||||
W1
|
||||
L90
|
||||
F59
|
||||
7
12-rain_risk/src/bin/day12.rs
Normal file
7
12-rain_risk/src/bin/day12.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
use rain_risk::{step1, step2};
|
||||
|
||||
fn main() {
|
||||
let input = include_str!("../../data/input.txt");
|
||||
println!("Step 1 : {}", step1(input));
|
||||
println!("Step 2 : {}", step2(input));
|
||||
}
|
||||
152
12-rain_risk/src/lib.rs
Normal file
152
12-rain_risk/src/lib.rs
Normal file
@@ -0,0 +1,152 @@
|
||||
|
||||
|
||||
use program::{Direction, Instruction, parse_program};
|
||||
|
||||
pub mod program;
|
||||
|
||||
struct Ship {
|
||||
position: (i64, i64),
|
||||
direction: program::Direction,
|
||||
wpt: (i64, i64),
|
||||
}
|
||||
|
||||
impl Default for Ship {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
position: (0, 0),
|
||||
direction: Direction::East,
|
||||
wpt: (10, 1),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Ship {
|
||||
fn execute(&mut self, i: &Instruction) {
|
||||
// eprintln!("Executing {}: previously at {:?}", i, self.position);
|
||||
match i {
|
||||
Instruction::Direction(d, e) => {
|
||||
match d {
|
||||
Direction::North => {
|
||||
self.position.1 += e;
|
||||
}
|
||||
Direction::South => {
|
||||
self.position.1 -= e;
|
||||
}
|
||||
Direction::East => {
|
||||
self.position.0 += e;
|
||||
}
|
||||
Direction::West => {
|
||||
self.position.0 -= e;
|
||||
}
|
||||
}
|
||||
}
|
||||
Instruction::Rotation(r, e) => {
|
||||
for _ in 0..e/90 {
|
||||
self.direction = match r {
|
||||
program::Rotation::Left => {
|
||||
match self.direction {
|
||||
Direction::North => {Direction::West}
|
||||
Direction::East => {Direction::North}
|
||||
Direction::South => {Direction::East}
|
||||
Direction::West => {Direction::South}
|
||||
}
|
||||
}
|
||||
program::Rotation::Right => {
|
||||
match self.direction {
|
||||
Direction::North => {Direction::East}
|
||||
Direction::East => {Direction::South}
|
||||
Direction::South => {Direction::West}
|
||||
Direction::West => {Direction::North}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Instruction::Forward(e) => {
|
||||
match self.direction {
|
||||
Direction::North => {
|
||||
self.position.1 += e;
|
||||
}
|
||||
Direction::East => {
|
||||
self.position.0 += e;
|
||||
}
|
||||
Direction::South => {
|
||||
self.position.1 -= e;
|
||||
}
|
||||
Direction::West => {
|
||||
self.position.0 -= e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// eprintln!("Now at {:?}", self.position);
|
||||
}
|
||||
|
||||
// Execute for step2
|
||||
fn execute2(&mut self, i: &Instruction) {
|
||||
// eprintln!("Executing {}: previously at {:?}", i, self.position);
|
||||
match i {
|
||||
Instruction::Direction(d, e) => {
|
||||
match d {
|
||||
Direction::North => {
|
||||
self.wpt.1 += e;
|
||||
}
|
||||
Direction::South => {
|
||||
self.wpt.1 -= e;
|
||||
}
|
||||
Direction::East => {
|
||||
self.wpt.0 += e;
|
||||
}
|
||||
Direction::West => {
|
||||
self.wpt.0 -= e;
|
||||
}
|
||||
}
|
||||
}
|
||||
Instruction::Rotation(r, e) => {
|
||||
for _ in 0..e/90 {
|
||||
// Don't count on me to XOR everything to avoid
|
||||
// copies, cheers !
|
||||
let wpt = self.wpt;
|
||||
match r {
|
||||
program::Rotation::Left => {
|
||||
self.wpt.0 = -wpt.1;
|
||||
self.wpt.1 = wpt.0;
|
||||
}
|
||||
program::Rotation::Right => {
|
||||
self.wpt.0 = wpt.1;
|
||||
self.wpt.1 = -wpt.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Instruction::Forward(e) => {
|
||||
self.position.0 += e * self.wpt.0;
|
||||
self.position.1 += e * self.wpt.1;
|
||||
}
|
||||
}
|
||||
// eprintln!("Now at {:?}", self.position);
|
||||
}
|
||||
pub fn manhattan(&self) -> u64 {
|
||||
(self.position.0.abs() + self.position.1.abs()) as u64
|
||||
}
|
||||
}
|
||||
|
||||
pub fn step1(input: &str) -> u64 {
|
||||
// Parse input into path
|
||||
let p = parse_program(input).unwrap();
|
||||
// Setup ship
|
||||
let mut s = Ship::default();
|
||||
// Execute ops
|
||||
p.iter().for_each(|i| s.execute(i));
|
||||
s.manhattan()
|
||||
}
|
||||
|
||||
pub fn step2(input: &str) -> u64 {
|
||||
// Parse input into path
|
||||
let p = parse_program(input).unwrap();
|
||||
// Setup ship
|
||||
let mut s = Ship::default();
|
||||
// Execute ops
|
||||
p.iter().for_each(|i| s.execute2(i));
|
||||
s.manhattan()
|
||||
}
|
||||
124
12-rain_risk/src/program.rs
Normal file
124
12-rain_risk/src/program.rs
Normal file
@@ -0,0 +1,124 @@
|
||||
use std::{fmt::{Display}, str::FromStr};
|
||||
|
||||
use anyhow::Error;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Direction {
|
||||
North,
|
||||
East,
|
||||
South,
|
||||
West,
|
||||
}
|
||||
|
||||
impl Display for Direction {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", match self {
|
||||
Direction::North => {'N'}
|
||||
Direction::South => {'S'}
|
||||
Direction::East => {'E'}
|
||||
Direction::West => {'W'}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Rotation {
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
impl Display for Rotation {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", match self {
|
||||
Rotation::Left => {'L'}
|
||||
Rotation::Right => {'R'}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub enum Instruction {
|
||||
Direction(Direction, i64),
|
||||
Rotation(Rotation, i64),
|
||||
Forward(i64),
|
||||
}
|
||||
|
||||
impl FromStr for Instruction {
|
||||
type Err = anyhow::Error;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let mut sc = s.chars();
|
||||
match sc.next().ok_or_else(|| anyhow::anyhow!("Expected NSEWLRF"))? {
|
||||
d @ 'N' | d @ 'S' | d @ 'E' | d @ 'W' => {
|
||||
Ok(Instruction::Direction(
|
||||
match d {
|
||||
'N' => Direction::North,
|
||||
'S' => Direction::South,
|
||||
'E' => Direction::East,
|
||||
'W' => Direction::West,
|
||||
_ => unreachable!() // Somehow needed ??
|
||||
},
|
||||
sc.collect::<String>().parse::<i64>()?)
|
||||
)
|
||||
},
|
||||
r @ 'L' | r @ 'R' => {
|
||||
Ok(Instruction::Rotation(
|
||||
match r {
|
||||
'L' => Rotation::Left,
|
||||
'R' => Rotation::Right,
|
||||
_ => unreachable!() // Somehow needed ?
|
||||
},
|
||||
sc.collect::<String>().parse::<i64>()?))
|
||||
},
|
||||
'F' => {
|
||||
Ok(Instruction::Forward(sc.collect::<String>().parse::<i64>()?))
|
||||
},
|
||||
e => Err(anyhow::anyhow!("Expected NSEWLRF, got {}", e)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Instruction {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Instruction::Direction(d, e) => {
|
||||
write!(f, "{}{}", d, e)
|
||||
},
|
||||
Instruction::Rotation(r, e) => {
|
||||
write!(f, "{}{}", r, e)
|
||||
},
|
||||
Instruction::Forward(e) => {write!(f, "F{}", e)},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type Program = Vec<Instruction>;
|
||||
|
||||
pub fn parse_program(input: &str) -> Result<Program, Error> {
|
||||
Ok(input.lines()
|
||||
.map(|l| l.parse::<Instruction>())
|
||||
.collect::<Result<_,_>>()?)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_readwrite() {
|
||||
let input = r"F10
|
||||
N3
|
||||
F7
|
||||
R90
|
||||
F11";
|
||||
assert_eq!(
|
||||
input
|
||||
.lines()
|
||||
.map(|l| l.parse::<Instruction>().unwrap())
|
||||
.map(|i| i.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n"),
|
||||
input
|
||||
)
|
||||
}
|
||||
}
|
||||
14
12-rain_risk/tests/tests.rs
Normal file
14
12-rain_risk/tests/tests.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use rain_risk::*;
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_step1() {
|
||||
let input = "F10\nN3\nF7\nR90\nF11";
|
||||
assert_eq!(step1(input), 25);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_step2() {
|
||||
let input = "F10\nN3\nF7\nR90\nF11";
|
||||
assert_eq!(step2(input), 286);
|
||||
}
|
||||
Reference in New Issue
Block a user