Solve day 10
This commit is contained in:
Родитель
fc289313a0
Коммит
8e871f7532
|
@ -186,6 +186,14 @@ dependencies = [
|
|||
"criterion",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day-10"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"aoc-lib",
|
||||
"criterion",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day-2"
|
||||
version = "0.1.0"
|
||||
|
|
|
@ -10,4 +10,5 @@ members = [
|
|||
"day-7",
|
||||
"day-8",
|
||||
"day-9",
|
||||
"day-10",
|
||||
]
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
[package]
|
||||
name = "day-10"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
aoc-lib = { path = "../aoc-lib" }
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = "0.3"
|
||||
|
||||
[[bench]]
|
||||
name = "bench"
|
||||
harness = false
|
|
@ -0,0 +1,46 @@
|
|||
#![allow(dead_code)]
|
||||
|
||||
// uncomment for unstable version
|
||||
// #![allow(unused_attributes, incomplete_features)]
|
||||
// #![feature(generic_const_exprs, const_for, const_mut_refs)]
|
||||
|
||||
use aoc_lib::AdventOfCode;
|
||||
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
||||
|
||||
#[path = "../src/main.rs"]
|
||||
mod main;
|
||||
|
||||
fn bench_main(c: &mut Criterion) {
|
||||
c.bench_function("parse input", |b| {
|
||||
let input = include_str!("../input.txt");
|
||||
b.iter(|| main::Day10::parse_input(black_box(input)))
|
||||
});
|
||||
|
||||
c.bench_function("solve 1", |b| {
|
||||
let input = main::Day10::parse_input(include_str!("../input.txt"));
|
||||
b.iter(|| main::Day10::solve_1(black_box(&input)))
|
||||
});
|
||||
|
||||
c.bench_function("solve 2", |b| {
|
||||
let input = main::Day10::parse_input(include_str!("../input.txt"));
|
||||
b.iter(|| main::Day10::solve_2(black_box(&input)))
|
||||
});
|
||||
|
||||
c.bench_function("parse sample input", |b| {
|
||||
let input = include_str!("../sample.txt");
|
||||
b.iter(|| main::Day10::parse_input(black_box(input)))
|
||||
});
|
||||
|
||||
c.bench_function("solve 1 (sample input)", |b| {
|
||||
let input = main::Day10::parse_input(include_str!("../sample.txt"));
|
||||
b.iter(|| main::Day10::solve_1(black_box(&input)))
|
||||
});
|
||||
|
||||
c.bench_function("solve 2 (sample input)", |b| {
|
||||
let input = main::Day10::parse_input(include_str!("../sample.txt"));
|
||||
b.iter(|| main::Day10::solve_2(black_box(&input)))
|
||||
});
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_main);
|
||||
criterion_main!(benches);
|
|
@ -0,0 +1,94 @@
|
|||
[[<[<{[<{{<[{(()[])[[]()]}[((){})<[]{}>]}<<{[]{}}<(){}>><([]<>)>>>[[[<<>>[<><>]][<<>>(<><>)]][{
|
||||
((<[(<<(<{<((<()<>>{()()}){<<>><()[]>})([{<>{}}(()[])]{(<><>>})>(<((()())<[][]>)>([(<>{})({}())][<
|
||||
<<<(<<[[((({<([]<>)[[]]>{<{}>{(){}}}}{[<[][]><<>[]>]({<><>}{()<>})})[<<({}())(()<>)>[([]{})<[]()>]>({[[]
|
||||
<(<<<[((([{({((){})<<>{}>}[<(){}>[<>()]])[[<{}[]>({}{}]](<<>[]><[]<>>)]}](<[({[][]}<()<>>)<[
|
||||
{<<<{[({{(<<(<<>[]><[]()>){<[][]>{<>{}}}>((({}())([]<>))[<{}{}>({}())])>(<<<()[]>><<<>{}>(()<
|
||||
{<{<<[[(<<[{<<{}[]>>([<>()]{{}{}})}]<(<{<>()}{{}()}>{({}[])<<>()>})>>>){[{[{<[[]{}][<><>]>}{(<{}(
|
||||
(((([(({((<[<(<>{})[{}{}]>](<[()<>]{<><>}>)><<[<[]{}>((){})]<[()[]]>>{[{<>()}((){})]({()[]}(()[]))}>))}(
|
||||
<<[(({({(<{{((<>[])[[]<>])([<>]{()()})}<<{<>[]}><<{}()>>>}>)(<(<{<()[]><(){}>}{{(){}}[{}<>]}>{
|
||||
(([[{<[<<{[<{<()[]>{<>[]}}{<{}{}>{[]<>}}>[{<{}{}>(<>[])}]]{<<({}[])>(<[]<>>[{}])>({{<>()}(<
|
||||
(<{{(<<{[([<<{()[]}>{({}<>){<>[]}]>((<[]{}><{}{}>))]([{(()<>)(<>[])}]<<((){})<<>{}>>[(()[])
|
||||
{{[[<<{<{{{([{()[]}[{}()]]{(<>{}){<><>}}){<[[][]][()[]]>}}<{{<[][]>[{}[]]>(<<>{}><{}{}>)}>}}<<{[{([](
|
||||
{[<({{{[<{<{[([][])]{({}())<[][]>}}((<(){}>{()<>})<<<>{}>>)>}(((<{<>{}}<{}()>>)[((()()))[<<>{}>(
|
||||
{{{[{{[(([{{[[{}<>]([]<>)]([(){}][[]()])}[{{[]<>}[{}()]}(<<><>>]]}][[([[[]<>]<<>{}>][{[]{}
|
||||
<<{{{[{{[<({<<[]<>><{}<>>>}[[{()<>}{[]<>}]({(){}}{{}()})])[[(<<>[]>[[]])[{{}{}}[[]()]]]<((
|
||||
({{[{{{{<[[({[{}{}]{[]<>}}<<()<>>(()<>)>)<{{(){}]<()()>}{{(){}}[<><>]}>]<<({{}{}}<(){}>)([{}
|
||||
{([[[<<([[{{[<()<>>(()<>))({<>[]}(<>{}))}{(<{}()>{()[]})}}([[{{}[]}<[]>]<([]<>){<>}>])]<{[[([]{})]{(<><>)[[]<
|
||||
([<<([<[[({((({}[]){[]{}}){({}[])({}<>)})>)[(<{{{}{}}[(){}]}(([][])<{}<>>)><[[<><>]{<>{}}]{{{}[]}{[][]}}>)<[[
|
||||
{[<<{[<{[<{[[({}[])({}<>)]<<{}{}>{{}()}}]{[[<>{}]<[]{}>]}}(([(<><>){[][]}]([<>()]<()[]>))<[<()<>>(<><>)
|
||||
(<[<[(({{<{<<[{}()]>(([]())<<>[]>)>}[{<[[]{}]([]<>)><{<><>}[()<>]>}{{{(){}}{[]{}}}}]>}}){{<{([<([]())({}())
|
||||
<<{({{<[<[{([{{}<>}{<><>}]([(){}](()[]))){[{()[]}<<>()>]({()()}({}()))}}]<[<<(())(()[])>>{(<{}<>>{
|
||||
{{{(<<[(<[([[[()<>]<()[]>]]{[[[]{}]{<>{}}]((<><>)({}{}))})]([(<{<>()}{[]{}}><<{}{}><<>[]>>)[<<()[]>((){
|
||||
[{(<<{[<<([{[<[][]>][{()()}({}())]}]([{([]{})<{}<>>}(<()>[{}])]{<([]<>)<{}{}>>(({}<>)<<><>>)}))>([[((<<>{}
|
||||
[[(<({{(({{(<[{}<>]([]{})>{[<>[]]([][])})[(<[]<>><{}()>)[<{}()>(())]]}<[<[[]{}]<{}{}>>{<()()>({}<>)}]<
|
||||
([(<({(<{{(((<[]<>>{<>()})<{[]{}>([]{})>)[<[()<>]({}[])>[(<>{})(<>[])]]){<((<>{}){()[]})><{<[]<>><<><>>
|
||||
({{<({(<<({[[{<><>}]<<<><>>({}[])>]([<[]<>>[{}()]]{{<>{}}({}[])}}}<{({[]<>}({}{}))[(<>())(<>())]}((([]{}){[][
|
||||
(((({[(<([<[(<<>>[[][]])<{<><>}[{}<>]>]{({<>()}(<><>))(({}<>))}>])<([[<<()<>>{[][]}>]<[[[][
|
||||
[<<(([({((<<{({}())}[(()()){[]<>}]>>){({<[{}{}]<<>()>>{<{}()>}})})})])[[<((<[{([<>()][{}[]])((()[])
|
||||
{{{<{[[(<[{{<<{}()>[{}<>]>{({}{})<()>}}(<[(){}]>(<(){}>[[]]))}(<<[[]{}}({})><[<>[]]{<><>}>><[<[]<>>{<><>}](((
|
||||
{<[[<[{{<((<[<<>()><[]{}>][<{}()><{}{}>]>)[[[[[][]]<()[]}][([]{})(()())]]])>[[((<{[]<>}<()>><[{}[]](<><>)>
|
||||
<{([{[{[{([({<{}[]>([]<>)}[[[][]]<()()>])[<[{}()]>{<<><>>(()<>)}]]){({(<[]<>>([]<>))[(<>){
|
||||
[{{<(({<{[{<{({})(<>[])}({<><>}{[]()})>(({[]<>}(<>{}))[{()()}{{}<>}])}{<{{<>()}<[]{})}(<()><(
|
||||
{(<[{[[{{<(([{{}{}}<()[]>]<[<><>]>)([(<><>)])){<<{[]{}}[[][]]>{[[]<>]{<>{}}}>[<<[][]><<>()>><<{}<>>[<>()>>]}>
|
||||
<[{{(<<{{{[[{<[]<>>(()<>)}<{()<>}{<>[]}>]<[<()()>]{<<>>([]())}>]{([<<>{}>({}<>)])[({{}}{[]<>})[
|
||||
<<[[<<[(<{{{[{()[]}([]())][[[]]((){})]}}(([{[][]}[()()]]<<()>[{}[]]>))}>)<{({{<[[][]]{()()}><<{}{}
|
||||
{[<(([([{({([({}()){<><>}][((){})([]())])[<<()<>>(<>())>{[[]()}(()[])}]}<{{<(){}>{()[]}}((<><>){{}<>})}((
|
||||
{(({([[{{((([<()()>([][])]<<{}[]>[<>]>)(<{<><>}[{}{}]>[[<>{}]]))[{<<[][]>[()]><[{}{}]>}{<([]<>)([]<>)>}]
|
||||
(({[[<<<{[{([(<>[])<()[]>])(<([])({}{})>)}[{(<<>{}>({}<>))}<<[<>[]>[(){}]>(<{}{}>)>]]{([[<<>()>
|
||||
[{[[{([({[{(({()[]}[[]<>])<[[]<>]([])>)([{{}{}}[{}[]]]<{{}<>}>)][<(<<>[]>{<><>})<(()<>){[][]}>>[[{[]{}}([]())
|
||||
([[{({([({<<(<[]<>>)[{()[]}<<>()>]><<{<>()}<<>[]>>[[<>{}]<<><>>]>)<<<<<>[]><<><>>>[[[]]]>[{
|
||||
[{{([(<[[<{((([]{})[()()])[<[]{}>[<>()]])<[{()()}(<>{})]({[]}[<>{}])>}((<{[]()}>)({<{}{}>[
|
||||
[[[{{[[[<<(<(({}())[<>{}])<{{}<>}(()())>>[[((){})][[[]{}]{()}]])>>{[(<([{}{}]({}<>))([{}()]<<>[]])>({<
|
||||
{<{({(({{{[{<(<>{})>{<{}<>>(<><>)}}<(<[]()>[[][]])<[{}()]>>]<{{<()[]>{{}()}}{<{}[]>{{}{}}}}{{<()<>><<>
|
||||
[{[<{[<<({(<{{<>()}({}<>)}<(<>[])[()()]>>{{{<>()}{()[]}}})<{<({}()){<>{}}>({<>[]}({}())]}>}(
|
||||
[[{<([((({([{{()[]}<[]{}>}({<>[]}[<><>])]{{<[]<>>[<><>]}[{()()}[<>[]]]})}[{[<{()()}(<>))<({}){()<>}>]([
|
||||
(<(<{[{<({[{{{[]()}({}{})}{<{}[]><<>{}>}}]})>}<{[[(<[<[]{}>([]())](<{}>[()()])>)][(([{[]<>
|
||||
<<<<{<{<<[<({<[]>({}<>)})<[{[][]}[[]()]][[{}<>]<{}[]>])>([(([][])[[]()])<[{}[]]{[]{}}>](<[{}()]{{}()}
|
||||
({[[([([<{[[({()[]}[<>[]])[<[][]><<>{}>>][(({}{})(<>[])){(<>[])}]]<{{{{}()}[<>[]]}<[[]<>]([]())>}>}{<{({
|
||||
((<{<{{[((<<<(()<>)<[]{}>>[<[][]>[(){}]]>>)){([[[[<>]({}<>)]{{{}<>}([]{})}]<[<<>()>(()())][{[]{}}{[]<>}]>]
|
||||
{<<{{[({[<{{(<{}[]><<>[]>)([[]()])}<[{{}{}}{<><>}]<({})<{}()>>>}<[([{}<>][<>{}])](<<{}[]><<
|
||||
{[{[[<<[(({[(((){}){<>[]})]([[<>[]]][<<>{}>[<><>]])})({<[({}[])<{}[]>]>}<(<(()<>)[[]{}]][[()()][<><
|
||||
((<<[({<{[[({<[]()>([]())}[<[]{}>(()<>)])]<<{[{}[]][<>()]}><<{<>[]}[()<>]>[<[]{}>{[]()}]>>][(({[<>
|
||||
{{<[{<([({{([<<><>>{{}()}][<[][]>{[]()}])[<[[]]{[]<>}>]}(({<()[]>[{}()]}<[[]<>]{<>()}>))})])<{[{<[<[{}{}]
|
||||
[(({({[[<{(<{{<>[]}[()<>]}((<>{})<<>[]>)>{[<(){}>{{}<>}]])[<{(<>[]){{}[]}}([()()][<>[]])>((({}())
|
||||
{(([<{([[{([<<{}()><[][]>>[(<>()){()[]}]]<[<()()>[{}]]([{}<>][{}[]])>){<{[[]<>]<<>{}>}{([]{})}>[([
|
||||
<{[{({([(<<{<[()<>]<(){}>>(<()>{[][]})})>{[([<{}{}>[<>()]])[{([]()){<>{}}}<<{}<>>>]]})])([<(([{<(){}>[{
|
||||
<<<(([<[{[[<{({}()>{[]()}}>]]([<[[()][{}[]]]<[()()]{(){}}>><<({}[])(()<>)>{(()[])}>]{[(<[]()>)[{[]{}}
|
||||
(({(([[<{(<{[(<>[]]]}<(({}{}))([[][]]([][]))>><[{(()())[()<>]}<{(){}}>]>)((<{{()[]}{<>()}}<([
|
||||
[[[{<[{<[<[[{(<>{})<[]()>}{(()[])[<>()]}][<({}()>[<><>]>{<[]{}><[][]>}]]><<{[{(){}}{{}{}}][<<><
|
||||
((([<<((<[{(([<>[]]([]{}))([<>]{<>()})}([[(){}][[]()]][(<><>){{}()}])}{[<[()<>]({}<>)>[<{}{}>]][<(<>())<[]
|
||||
{<<{([(((([{<[()]{(){}}>{{{}[]}}}[{(()[]){()<>}}{<<>>[<>{}]}]]<[{(<>())(()[])}]>){[[<({}{})[()[]]>{[{}(
|
||||
([{(({([([(<<{<>[]}<{}<>>>[[[]{}]<[]{}>]>((([]<>)<<>[]>)[[(){}]]))[<{{<>{}}}[(<>}{()()}]><{<<>{}><
|
||||
{(<[<{({([{{([[][]]>({()()}[[][]])}[<(()())([]())><(<><>)([]{})>]}[(({{}{}}{{}<>})([<><>]<()[]>))]
|
||||
((<(<<{{{{<{{(()<>)({}{})}{{<>[]}<()[]>}}<<<[][]>(()[])>[[()[]][[]()]]>>[[{{[]{}}[[][]]}][<[()[]]{[]<>}>
|
||||
(((<(<<<{{<{<{{}{}}(()<>)>[((){})[()[]]]}{[[<>[]]{{}()}][{()[]}({}{})]}>}}>{[{<<{[{}()]}<[[]()]
|
||||
({(([[<{[{<(([[]()](<><>))[<{}[]){{}()}])(<[()[]]<[]()>>[<{}<>>[()<>]])>[<[[()()]<<>[]>][([]())([]{})]>{{<<>(
|
||||
[[(([((([<[[<{{}<>}({}[])>[([]<>)[()[]]]]<[(()())({}<>)](([]<>)<[]()>)>]>[{{{(()<>)}<{[]()}
|
||||
(<(({[<<{((<[[[]()]{{}<>}]<<{}()>({}[])>>)<{{({}){()()}}<{[][]}>}>){[(<<[]<>>[{}{}]>{{(){}}{[
|
||||
{<(<(<<{<(<{{[{}[]]([]{})}([{}[]](<>))}[([<>()]{[]{}})<({}<>){<><>}>]>[{<[[][]]>}[<[<><>](()())>(([
|
||||
{(<(<{<(<<{{{<(){}>}({[][]}{{}{}})}}<(<<<>()>[(){}]>(<{}()>{{}}))>>[{{[{[]{}}(()[])]}[((<>())<<
|
||||
([((([[{([{<<((){})>{{<>}{[]()}}>{[((){})<()[]>]<{[]{}}[(){}]>}}{((<{}()><{}>)){{<{}[]>[[]
|
||||
{{{(<<{((([[{(<>{})({}())}[<[]()>({}{})]]<<[{}]>{[[]()]{()[]}}>]{({<()[]>((){})})<{<[]<>>{{}{}
|
||||
(<<[[([{{(<<({{}}[{}{}])<([]{}){<>()}>>>(({<()()>({}[])}<<{}()>[(){}]>)))[{([{<>[]}([]())]([()()][<>{}])){
|
||||
<(<(<<<([[[(<{{}[]}{{}[]}>[[<>()]{()[]}])]<{({{}()}{{}<>})<({}[])([]<>)>}{(({}[])(<>{}))((()<>)<[]()>)}>](<
|
||||
<<{(([<([[[[<{<>()}({}[])>{((){})[{}()]}]}[<{<<>[]><[]<>>}<[{}<>]{{}()}>>{{(()())[()[]]}{<()<>
|
||||
(({<<[{([{<(([{}[]][(){}])[<[]()>(()<>)]}>}<[(<{{}()}{{}<>}><[[]()][[]()]>)]<(<(<>())>({<>[]})){[{(){}}<{}{}>
|
||||
({[[{[{{<(([[[{}{}]<(){}>]((()[])(()[]))]({([]<>)({}{})}{<<>{}>{<>}}))<{(<[]{}>(()()))[[()[]](<><>)
|
||||
([{[{{<{{(<[((<>){[]()})(({}())[<><>])]>({[(<><>}{{}[]}]{(()[])[()]}}{([[]{}](<><>))<<{}[]>(<>())>}))
|
||||
<[(([[[<{[[(({()[]}<<>[]>){{<><>}<(){}>})<([()]{<>[]}){{<>{}}}>]]<[{{([]())({}{}>}<{[][]}<<>{}>
|
||||
({(<{<([[<{[<{[]()}[{}<>>>[[(){}][[]{}]]][{[()<>][<><>]}{<()()>}]}<({{(){}}<()()>}{[<><>](<>())})[(
|
||||
([({<[<[{({<<[<>{})[<><>]>{{[]{}}}>[[{()<>}([]{})](({}<>)<<>>)]}<{<<<>>{()[]}>}<{<[]()>}>>)(<<<{
|
||||
({<{[[({[([{[{[]{}}{[]<>}](((){})({}()))}]([<{<>{}}{()<>}>{({}{})(()())}])}]<{({{<()[]>{()}}
|
||||
({[[{[((<[[[{<<>[]>(<>[])}]]{<({{}[]}){[{}{}]<<>()>}>[([<>()]{{}()})(<()()><<>[]>)]}]>(<([{<[]{}>(()())}<{[
|
||||
[<<<{<({({{<{[<><>]{{}<>}}{<(){}>}>}})})[<<[{[{({}{})<<>>}[{[]}(()())]]{{([]())([]())}{({}[])[<>
|
||||
<<<[[((<{{<(((<>[])(()))<<<>()><<>{}>>)<[{<>[]}{(){}}]{[{}]({}())}>>}}[[((({()}{[][]}){<{}[]>}
|
||||
(<[<({[[{[<<[(<>[])]([[]<>]<[]{}>)>({(<>[])([]())}<[{}<>)<<>{}>>)>[{<[[][]]><[{}<>]<[]()>>}<<[
|
||||
{[({<<<<(<([{{[][]}}]{<{<>()}<<>()>>[({}())<[]{}>]})>)>>><<<{{((([{}[]]<<><>>)[[[][]]({}[])])<{<{}{}>(
|
||||
[[[({((({<([({[][]}{[]})<({}[]}[{}{}]>][([[]()]([]()))<<()<>>>])<[<<()[]><()[]>>]>>([<<{[]()}{[][]}><
|
||||
[{{<<<({<({(<<<><>>>{(<>[])[<><>]})<(<<>{}>(()()))[{{}()}{(){}}]}}[(<{[]()}<[]<>>>({<>{}}[{}
|
||||
([<<{<[{([<[<{<>{}}<[]{}>><([][]>>][{([][])[()()]}<<[]<>>>]>][{<({<>()}<[][]>)[[[]<>]{()<>}
|
||||
({<<[<(<<<(<((()<>)<{}<>>)>)<<([<>{}][<>[]])<<()()>({}[])>>([(()())(()<>)]{{{}<>}<[]{}>})>>>>[
|
||||
<{[{<[[<[(<{({<><>}{[]<>}){<[]{}><()>}}({({})[[]{}]})>{(<{[]<>}{<><>}>([{}[]]{<><>}))(<<{}()>[
|
||||
<{{<[[({{{<({<[]()>[{}[]]}<{<><>}{()<>}>)[<<[]{}>{{}<>}>[<<>[]>((){})]]>}}(<[[(([][])[{}<>]){(()<>)[()[]]}]((
|
||||
[{[{<[(({{({<{{}{}}<()()>>(<[]<>>{<>[]})}(<<()[]>{()<>}>))<<<[{}{}]<<>[]>>>[[{[]{}}]((()<>){<>
|
||||
{[[<{({[<(<((<{}[]>){[{}[]]})[{[{}{}]({}())}]>(<{{()<>}(<><>)}[(<><>)<[]{}]]><{<()[]>([][])
|
|
@ -0,0 +1,10 @@
|
|||
[({(<(())[]>[[{[]{<()<>>
|
||||
[(()[<>])]({[<{<<[]>>(
|
||||
{([(<{}[<>[]}>{[]{[(<()>
|
||||
(((({<>}<{<{<>}{[]{[]{}
|
||||
[[<[([]))<([[{}[[()]]]
|
||||
[{[{({}]{}}([{[{{{}}([]
|
||||
{<[[]]>}<{[{[{[]{()[[[]
|
||||
[<(<(<(<{}))><([]([]()
|
||||
<{([([[(<>()){}]>(<<{{
|
||||
<{([{{}}[<[[[<>{}]]]>[]]
|
|
@ -0,0 +1,167 @@
|
|||
use aoc_lib::*;
|
||||
|
||||
aoc_setup!(Day10, sample 1: 26397, sample 2: 288957, part 1: 344193, part 2: 3241238967);
|
||||
|
||||
pub struct Day10;
|
||||
|
||||
impl AdventOfCode for Day10 {
|
||||
type Input = String;
|
||||
type Output = usize;
|
||||
|
||||
fn parse_input(s: &str) -> Self::Input {
|
||||
String::from(s)
|
||||
}
|
||||
|
||||
fn solve_1(input: &Self::Input) -> Self::Output {
|
||||
input
|
||||
.lines()
|
||||
.map(|l| match parse(l) {
|
||||
b')' => 3,
|
||||
b']' => 57,
|
||||
b'}' => 1197,
|
||||
b'>' => 25137,
|
||||
0 => 0,
|
||||
_ => unreachable!(),
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn solve_2(input: &Self::Input) -> Self::Output {
|
||||
let mut scores = input
|
||||
.lines()
|
||||
.filter_map(|line| parse_recursive_counting_closing(line.as_bytes(), 0).map(|x| x.2))
|
||||
.collect::<Vec<usize>>();
|
||||
|
||||
scores.sort_unstable();
|
||||
|
||||
scores[scores.len() / 2]
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(line: &str) -> u8 {
|
||||
if line.is_empty() {
|
||||
return 0;
|
||||
}
|
||||
let bytes = line.as_bytes();
|
||||
|
||||
match parse_recursive_until_closing(bytes) {
|
||||
Ok(_) => 0,
|
||||
Err(e) => e,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_recursive_until_closing(bytes: &[u8]) -> Result<(u8, &[u8]), u8> {
|
||||
if bytes.is_empty() {
|
||||
return Ok((0, &[]));
|
||||
}
|
||||
let first = bytes[0];
|
||||
let rest = &bytes[1..];
|
||||
|
||||
match first {
|
||||
b'(' => {
|
||||
let (x, rest) = parse_recursive_until_closing(rest)?;
|
||||
if x != b')' {
|
||||
Err(x)
|
||||
} else {
|
||||
parse_recursive_until_closing(rest)
|
||||
}
|
||||
}
|
||||
b'[' => {
|
||||
let (x, rest) = parse_recursive_until_closing(rest)?;
|
||||
if x != b']' {
|
||||
Err(x)
|
||||
} else {
|
||||
parse_recursive_until_closing(rest)
|
||||
}
|
||||
}
|
||||
b'{' => {
|
||||
let (x, rest) = parse_recursive_until_closing(rest)?;
|
||||
if x != b'}' {
|
||||
Err(x)
|
||||
} else {
|
||||
parse_recursive_until_closing(rest)
|
||||
}
|
||||
}
|
||||
b'<' => {
|
||||
let (x, rest) = parse_recursive_until_closing(rest)?;
|
||||
if x != b'>' {
|
||||
Err(x)
|
||||
} else {
|
||||
parse_recursive_until_closing(rest)
|
||||
}
|
||||
}
|
||||
_ => Ok((first, rest)),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_recursive_counting_closing(bytes: &[u8], points: usize) -> Option<(u8, &[u8], usize)> {
|
||||
if bytes.is_empty() {
|
||||
return Some((0, &[], points));
|
||||
}
|
||||
let first = bytes[0];
|
||||
let rest = &bytes[1..];
|
||||
|
||||
match first {
|
||||
b'(' => {
|
||||
let (x, rest, points) = parse_recursive_counting_closing(rest, points)?;
|
||||
if x != b')' {
|
||||
let points = points * 5 + 1;
|
||||
if x != 0 {
|
||||
None
|
||||
} else if rest.is_empty() {
|
||||
Some((0, &[], points))
|
||||
} else {
|
||||
Some((rest[0], &rest[1..], points))
|
||||
}
|
||||
} else {
|
||||
parse_recursive_counting_closing(rest, points)
|
||||
}
|
||||
}
|
||||
b'[' => {
|
||||
let (x, rest, points) = parse_recursive_counting_closing(rest, points)?;
|
||||
if x != b']' {
|
||||
let points = points * 5 + 2;
|
||||
if x != 0 {
|
||||
None
|
||||
} else if rest.is_empty() {
|
||||
Some((0, &[], points))
|
||||
} else {
|
||||
Some((rest[0], &rest[1..], points))
|
||||
}
|
||||
} else {
|
||||
parse_recursive_counting_closing(rest, points)
|
||||
}
|
||||
}
|
||||
b'{' => {
|
||||
let (x, rest, points) = parse_recursive_counting_closing(rest, points)?;
|
||||
if x != b'}' {
|
||||
let points = points * 5 + 3;
|
||||
if x != 0 {
|
||||
None
|
||||
} else if rest.is_empty() {
|
||||
Some((0, &[], points))
|
||||
} else {
|
||||
Some((rest[0], &rest[1..], points))
|
||||
}
|
||||
} else {
|
||||
parse_recursive_counting_closing(rest, points)
|
||||
}
|
||||
}
|
||||
b'<' => {
|
||||
let (x, rest, points) = parse_recursive_counting_closing(rest, points)?;
|
||||
if x != b'>' {
|
||||
let points = points * 5 + 4;
|
||||
if x != 0 {
|
||||
None
|
||||
} else if rest.is_empty() {
|
||||
Some((0, &[], points))
|
||||
} else {
|
||||
Some((rest[0], &rest[1..], points))
|
||||
}
|
||||
} else {
|
||||
parse_recursive_counting_closing(rest, points)
|
||||
}
|
||||
}
|
||||
_ => Some((first, rest, points)),
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче