solution: refactor day12 with serde_json

This commit is contained in:
Matej Janezic 2022-12-04 22:58:22 +01:00
parent 00000210a8
commit 00000220a6
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
3 changed files with 11 additions and 17 deletions

16
Cargo.lock generated
View File

@ -30,12 +30,12 @@ dependencies = [
"hashbrown 0.13.1",
"hex-literal",
"itertools",
"json",
"lazy_static",
"pico-args",
"regex",
"reqwest",
"rust-crypto",
"serde_json",
]
[[package]]
@ -395,12 +395,6 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "json"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd"
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -409,9 +403,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.137"
version = "0.2.138"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89"
checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8"
[[package]]
name = "log"
@ -800,9 +794,9 @@ dependencies = [
[[package]]
name = "syn"
version = "1.0.104"
version = "1.0.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ae548ec36cf198c0ef7710d3c230987c2d6d7bd98ad6edc0274462724c585ce"
checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908"
dependencies = [
"proc-macro2",
"quote",

View File

@ -12,9 +12,9 @@ dotenv = "0.15.0"
hashbrown = "0.13.1"
hex-literal = "0.3.4"
itertools = "0.10.5"
json = "0.12.4"
lazy_static = "1.4.0"
pico-args = "0.5.0"
regex = "1.7.0"
reqwest = { version = "0.11.13", features = ["blocking"] }
rust-crypto = "0.2.36"
serde_json = "1.0.89"

View File

@ -1,12 +1,12 @@
use json::{parse, JsonValue::*, JsonValue};
use serde_json::{Value, Value::*, from_str};
fn parse_add(json_object: &JsonValue, skip_red: bool) -> i32 {
fn parse_add(json_object: &Value, skip_red: bool) -> i32 {
match json_object {
Number(x) => x.to_string().parse::<i32>().unwrap_or(0),
Object(x) => {
if skip_red {
for (_, node) in x.iter() {
if let Short { .. } = node {
if let String { .. } = node {
if node.as_str() == Some("red") {
return 0;
}
@ -21,10 +21,10 @@ fn parse_add(json_object: &JsonValue, skip_red: bool) -> i32 {
}
pub fn part_one(input: &str) -> Option<u32> {
Some(parse_add(&parse(input).unwrap(), false) as u32)
Some(parse_add(&from_str(input).unwrap(), false) as u32)
}
pub fn part_two(input: &str) -> Option<u32> {
Some(parse_add(&parse(input).unwrap(), true) as u32)
Some(parse_add(&from_str(input).unwrap(), true) as u32)
}
fn main() {
let input = &aoc::read_file("inputs", 12);