Skip to content

Commit 9464313

Browse files
committed
Refactored the day_main and solved day 10
1 parent 11d253b commit 9464313

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+618
-361
lines changed

2021/input/day10.txt

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
{<<<(<(((({[[([])([][])]<<[]<>>>]}{{<{{}[]}(<>[])><{[][]}[[]{}]>}<{<()[]>}<{[]{}}{()<>}>>})){<<<[{[]<>}[[]

2021/input/day10_example.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[({(<(())[]>[[{[]{<()<>>
2+
[(()[<>])]({[<{<<[]>>(
3+
{([(<{}[<>[]}>{[]{[(<()>
4+
(((({<>}<{<{<>}{[]{[]{}
5+
[[<[([]))<([[{}[[()]]]
6+
[{[{({}]{}}([{[{{{}}([]
7+
{<[[]]>}<{[{[{[]{()[[[]
8+
[<(<(<(<{}))><([]([]()
9+
<{([([[(<>()){}]>(<<{{
10+
<{([{{}}[<[[[<>{}]]]>[]]

2021/src/bin/day1.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
use advent_lib::{day_main, day_test};
44

5-
#[allow(clippy::ptr_arg)]
6-
fn calculate_part1(input: &Vec<u32>) -> usize {
5+
fn calculate_part1(input: &[u32]) -> usize {
76
input.windows(2).filter(|w| w[1] > w[0]).count()
87
}
98

10-
#[allow(clippy::ptr_arg)]
11-
fn calculate_part2(input: &Vec<u32>) -> usize {
9+
fn calculate_part2(input: &[u32]) -> usize {
1210
input
1311
.windows(3)
1412
.map(|w| w.iter().sum())
@@ -18,7 +16,7 @@ fn calculate_part2(input: &Vec<u32>) -> usize {
1816
.count()
1917
}
2018

21-
day_main!();
19+
day_main!(Vec<u32>);
2220

2321
day_test!( 1, example => 7, 5 );
2422
day_test!( 1 => 1681, 1704 );

2021/src/bin/day10.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#![feature(test)]
2+
3+
use advent_lib::{builder::with_default, *};
4+
5+
#[derive(Default)]
6+
struct MatchingPair {
7+
stack: Vec<u8>,
8+
corruption_char: Option<u8>,
9+
}
10+
11+
impl nom_parse_trait::ParseFrom<&[u8]> for MatchingPair {
12+
fn parse(input: &[u8]) -> nom::IResult<&[u8], Self> {
13+
use nom::Parser;
14+
nom::combinator::map(nom::bytes::complete::is_a("()<>{}[]"), |bs: &[u8]| {
15+
with_default(|it: &mut MatchingPair| {
16+
for b in bs {
17+
if !it.add_char(*b) {
18+
break;
19+
}
20+
}
21+
})
22+
})
23+
.parse(input)
24+
}
25+
}
26+
27+
impl MatchingPair {
28+
fn pop_matching_char(&mut self, next: u8, expected: u8) -> bool {
29+
if self.stack.pop() == Some(expected) {
30+
true
31+
} else {
32+
self.corruption_char = Some(next);
33+
false
34+
}
35+
}
36+
37+
fn add_char(&mut self, next: u8) -> bool {
38+
self.corruption_char.is_none()
39+
&& match next {
40+
b'(' | b'[' | b'{' | b'<' => {
41+
self.stack.push(next);
42+
true
43+
}
44+
b')' => self.pop_matching_char(next, b'('),
45+
b'>' => self.pop_matching_char(next, b'<'),
46+
b']' => self.pop_matching_char(next, b'['),
47+
b'}' => self.pop_matching_char(next, b'{'),
48+
_ => unreachable!(),
49+
}
50+
}
51+
}
52+
53+
fn calculate_part1(input: &[MatchingPair]) -> u64 {
54+
input
55+
.iter()
56+
.flat_map(|mp| mp.corruption_char)
57+
.map(|b| match b {
58+
b')' => 3,
59+
b']' => 57,
60+
b'}' => 1197,
61+
b'>' => 25137,
62+
_ => 0,
63+
})
64+
.sum()
65+
}
66+
67+
fn calculate_part2(input: &[MatchingPair]) -> u64 {
68+
let mut scores: Vec<u64> = input
69+
.iter()
70+
.filter(|mp| mp.corruption_char.is_none())
71+
.map(|mp| {
72+
mp.stack.iter().rev().fold(0, |curr, next| {
73+
5 * dbg!(curr)
74+
+ match next {
75+
b'(' => 1,
76+
b'[' => 2,
77+
b'{' => 3,
78+
b'<' => 4,
79+
_ => 0,
80+
}
81+
})
82+
})
83+
.collect();
84+
scores.sort();
85+
scores[scores.len() / 2]
86+
}
87+
88+
day_main!(Vec<MatchingPair>);
89+
90+
day_test!( 10, example => 26397, 288957 );
91+
day_test!( 10 => 436497, 2377613374 );

2021/src/bin/day2.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl Position {
2626
}
2727

2828
#[allow(clippy::ptr_arg)]
29-
fn calculate_part1(commands: &Vec<Command>) -> i32 {
29+
fn calculate_part1(commands: &[Command]) -> i32 {
3030
commands
3131
.iter()
3232
.fold(Position::default(), |mut pos, command| {
@@ -41,7 +41,7 @@ fn calculate_part1(commands: &Vec<Command>) -> i32 {
4141
}
4242

4343
#[allow(clippy::ptr_arg)]
44-
fn calculate_part2(commands: &Vec<Command>) -> i32 {
44+
fn calculate_part2(commands: &[Command]) -> i32 {
4545
commands
4646
.iter()
4747
.fold(Position::default(), |mut pos, command| {
@@ -58,7 +58,7 @@ fn calculate_part2(commands: &Vec<Command>) -> i32 {
5858
.get_result()
5959
}
6060

61-
day_main!();
61+
day_main!(Vec<Command>);
6262

6363
day_test!( 2, example => 150, 900 );
6464
day_test!( 2 => 1938402, 1947878632 );

2021/src/bin/day3.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ struct Input {
1212
all_bits: Vec<u32>,
1313
}
1414

15-
impl<'a> ParseFrom<&'a [u8]> for Input {
16-
fn parse(input: &'a [u8]) -> nom::IResult<&'a [u8], Self, nom::error::Error<&'a [u8]>> {
15+
impl ParseFrom<&[u8]> for Input {
16+
fn parse(input: &[u8]) -> nom::IResult<&[u8], Self> {
1717
map(
1818
separated_list0(
1919
line_ending,
20-
map(is_a("01"), |s: &'a [u8]| {
20+
map(is_a("01"), |s: &[u8]| {
2121
s.iter()
2222
.fold(0u32, |acc, &b| (acc << 1) | ((b - b'0') as u32))
2323
}),
@@ -70,7 +70,7 @@ fn calculate_part2(input: &Input) -> u32 {
7070
oxygen * co2
7171
}
7272

73-
day_main!(calculate_part1, calculate_part2);
73+
day_main!(Input);
7474

7575
day_test!( 3, example => 198, 230 );
7676
day_test!( 3 => 2261546, 6775520 );

2021/src/bin/day4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn calculate_part2(input: &Input) -> u32 {
8282
unreachable!()
8383
}
8484

85-
day_main!();
85+
day_main!(Input);
8686

8787
day_test!( 4, example => 4512, 1924 );
8888
day_test!( 4 => 46920, 12635 );

2021/src/bin/day5.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn write_line_to(grid: &mut Grid<u8>, line: &LineSegment<2, i32>) {
4949
}
5050
}
5151

52-
fn calculate_part1(input: &Vec<Line>) -> u64 {
52+
fn calculate_part1(input: &[Line]) -> u64 {
5353
let mut grid = Grid::<u8>::new_empty(
5454
input.iter().map(|l| l.max_x()).max().unwrap() + 1,
5555
input.iter().map(|l| l.max_y()).max().unwrap() + 1,
@@ -62,7 +62,7 @@ fn calculate_part1(input: &Vec<Line>) -> u64 {
6262
grid.entries().filter(|(_, &value)| value >= 2).count() as u64
6363
}
6464

65-
fn calculate_part2(input: &Vec<Line>) -> u64 {
65+
fn calculate_part2(input: &[Line]) -> u64 {
6666
let mut grid = Grid::<u8>::new_empty(
6767
input.iter().map(|l| l.max_x()).max().unwrap() + 1,
6868
input.iter().map(|l| l.max_y()).max().unwrap() + 1,
@@ -73,7 +73,7 @@ fn calculate_part2(input: &Vec<Line>) -> u64 {
7373
grid.entries().filter(|(_, &value)| value >= 2).count() as u64
7474
}
7575

76-
day_main!();
76+
day_main!(Vec<Line>);
7777

7878
day_test!( 5, example => 5, 12 );
7979
day_test!( 5 => 4728, 17717 );

2021/src/bin/day6.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn calculate_part2(input: &Input) -> u64 {
2626
input.count_all_fishes(256)
2727
}
2828

29-
day_main!();
29+
day_main!(Input);
3030

3131
day_test!( 6, example => 5934, 26984457539 );
3232
day_test!( 6 => 379414, 1705008653296 );

2021/src/bin/day7.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn calculate_part2(input: &Input) -> i32 {
3030
input.calc(|nr| (nr.abs() * (nr.abs() + 1)) / 2)
3131
}
3232

33-
day_main!();
33+
day_main!(Input);
3434

3535
day_test!( 7, example => 37, 168 );
3636
day_test!( 7 => 351901, 101079875 );

0 commit comments

Comments
 (0)