-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15.rs
151 lines (132 loc) · 3.83 KB
/
day15.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use axum::{http::StatusCode, routing, Json, Router};
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
#[derive(Deserialize)]
struct Params {
input: String,
}
#[derive(Serialize)]
struct Results {
result: String,
#[serde(skip_serializing_if = "Option::is_none")]
reason: Option<String>,
}
type NaughtyOrNice = Result<Json<Results>, (StatusCode, Json<Results>)>;
async fn nice(Json(Params { input }): Json<Params>) -> NaughtyOrNice {
let vowels = input.chars().filter(|c| "aeiouy".contains(*c)).count();
if vowels <= 3 {
return Err((
StatusCode::BAD_REQUEST,
Json(Results {
result: "naughty".to_string(),
reason: None,
}),
));
}
let repeats = input
.chars()
.tuple_windows()
.filter(|(l, r)| l.is_alphabetic() && l == r)
.count();
if repeats < 1 {
return Err((
StatusCode::BAD_REQUEST,
Json(Results {
result: "naughty".to_string(),
reason: None,
}),
));
}
if ["ab", "cd", "pq", "xy"].iter().any(|c| input.contains(c)) {
return Err((
StatusCode::BAD_REQUEST,
Json(Results {
result: "naughty".to_string(),
reason: None,
}),
));
}
return Ok(Json(Results {
result: "nice".to_string(),
reason: None,
}));
}
fn naughty(status: StatusCode, reason: impl ToString) -> NaughtyOrNice {
Err((
status,
Json(Results {
result: "naughty".to_string(),
reason: Some(reason.to_string()),
}),
))
}
async fn game(Json(Params { input }): Json<Params>) -> NaughtyOrNice {
if input.len() < 8 {
return naughty(StatusCode::BAD_REQUEST, "8 chars");
}
let uppercase = input.chars().find(|c| c.is_uppercase()).is_some();
let lowercase = input.chars().find(|c| c.is_lowercase()).is_some();
let digits = input.chars().filter(|c| c.is_numeric()).count();
if !(uppercase && lowercase && digits > 0) {
return naughty(StatusCode::BAD_REQUEST, "more types of chars");
}
if digits < 5 {
return naughty(StatusCode::BAD_REQUEST, "55555");
}
let sum: usize = input
.split(|c: char| !c.is_numeric())
.flat_map(|s| s.parse::<usize>())
.sum();
if sum != 2023 {
return naughty(StatusCode::BAD_REQUEST, "math is hard");
}
let joy = "joy".to_string();
if input
.chars()
.filter(|c| joy.contains(*c))
.collect::<String>()
!= joy
{
return naughty(StatusCode::NOT_ACCEPTABLE, "not joyful enough");
}
if input
.chars()
.tuple_windows()
.find(|(a, b, c)| {
*a == *c && *a != *b && a.is_alphabetic() && b.is_alphabetic() && c.is_alphabetic()
})
.is_none()
{
return naughty(
StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS,
"illegal: no sandwich",
);
}
if input
.chars()
.find(|c| 10624 <= *c as u32 && *c as u32 <= 11263)
.is_none()
{
return naughty(StatusCode::RANGE_NOT_SATISFIABLE, "outranged");
}
if input
.chars()
.find(|c| unic::emoji::char::is_emoji(*c) && !c.is_numeric())
.is_none()
{
return naughty(StatusCode::UPGRADE_REQUIRED, "😳");
}
if !sha256::digest(input).ends_with("a") {
return naughty(StatusCode::IM_A_TEAPOT, "not a coffee brewer");
}
return Ok(Json(Results {
result: "nice".to_string(),
reason: Some("that's a nice password".to_string()),
}));
}
pub fn get_routes() -> Router<PgPool> {
Router::new()
.route("/15/nice", routing::post(nice))
.route("/15/game", routing::post(game))
}