Added year directories for advent of code
This commit is contained in:
2000
advent_of_code/2021/d1/1.txt
Normal file
2000
advent_of_code/2021/d1/1.txt
Normal file
File diff suppressed because it is too large
Load Diff
7
advent_of_code/2021/d1/Cargo.lock
generated
Normal file
7
advent_of_code/2021/d1/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "d1"
|
||||
version = "0.1.0"
|
||||
8
advent_of_code/2021/d1/Cargo.toml
Normal file
8
advent_of_code/2021/d1/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "d1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
48
advent_of_code/2021/d1/src/main.rs
Normal file
48
advent_of_code/2021/d1/src/main.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use std::io::{self, BufRead};
|
||||
use std::path::Path;
|
||||
use std::fs::File;
|
||||
|
||||
fn main(){
|
||||
println!("Advent of Code #1!");
|
||||
println!();
|
||||
|
||||
let path = Path::new("./1.txt");
|
||||
let display = path.display();
|
||||
|
||||
let file = match File::open(&path) {
|
||||
Err(why) => panic!("Couldn't open {}: {}", display, why),
|
||||
Ok(file) => file,
|
||||
};
|
||||
|
||||
let lines = io::BufReader::new(file).lines();
|
||||
let mut n = 0;
|
||||
let mut prev = 0;
|
||||
let mut line_vec = Vec::new();
|
||||
|
||||
for line in lines {
|
||||
if let Ok(l) = line{
|
||||
let i: u32 = l.parse().unwrap();
|
||||
if i > prev {
|
||||
n += 1;
|
||||
}
|
||||
prev = i;
|
||||
line_vec.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
println!("Number of increases: {}", n - 1);
|
||||
println!();
|
||||
|
||||
prev = 0;
|
||||
n = 0;
|
||||
|
||||
for i in 0..(line_vec.len() - 2) {
|
||||
let sum = line_vec[i] + line_vec[i+1] + line_vec[i+2];
|
||||
if sum > prev {
|
||||
n += 1;
|
||||
}
|
||||
prev = sum;
|
||||
}
|
||||
|
||||
println!("Number of increases with shifting window: {}", n - 1);
|
||||
}
|
||||
102
advent_of_code/2021/d10/input.txt
Normal file
102
advent_of_code/2021/d10/input.txt
Normal file
@@ -0,0 +1,102 @@
|
||||
{<{<{[<[{{([{{{}()}<{}()>}<[{}]>]<<{{}<>}([]<>)>[({}<>){()[]}]>)[[([(){}][<><>])<<()>{()()}>]]}}[(<{[(<>
|
||||
[<{{[[(({({<{{<>[]}[[][]]}[{{}()}[[][]]]>}{<[{()<>]<[]()>]{([][]){{}[]}}>})(([<[[]()][()()]><{[]}(<><
|
||||
{{<{[{[{<{[[[[[]()]]<<{}{}}[{}()]>]]}[<<<<[]()><[][]>><<()<>>({}<>)>>{{{{}{}}[{}<>]}[[<><>]<[
|
||||
([{((<{((<[(({{}[]}<()()>)[{{}<>}[()()]})<{{()()}[<><>]}>]>))}({<<[({{[]<>}[<>()]}<((){}){[][]}>)[{[{}[
|
||||
{([<[((<[[<<({[]()}[[]{}]){<{}<>>{()[]}}>[{<{}{}>[(){}]}<({}<>)[[]()]>]>](({{<[]()>(()[])}{<<>()>({
|
||||
[({[([{<<{<<(<()()>)<[()<>]([]{})>>{<({}[]>{[][]}>([()[]]{(){}})}>{{[{()()}{{}<>}]({<>{}}<{}{}>)}[
|
||||
<[({(<([{<<<(([]()){()()})[<{}><<>>]><({{}<>](<><>))([{}[]]{(){}})>>(((<[]<>>({}[]))([<>()](<>
|
||||
<{[[{<<[{(([<([][])<{}<>>>{{<>[]}{()}}]{(<()<>>(()<>)){<{}[]>[[]()]}})<(<({}()){[]{}}>(<[]()>[[]{}])){<(<>
|
||||
[<[[({<<[[<{{(<>())}<<{}[]>{{}<>}>}[(((){})<()[])){<{}()>[<>()]}]>{[([{}[]](<>{}))<{<>()}{[]()}>](({(){}}<{
|
||||
(<([<[<{{<[{[(<>())<()()>](<<>[]>{()()})}<<{[]{}}>[(<><>){<>[]}]>]<(<<[]<>><<>{}>>(([])[<>{}]))>>>({<<
|
||||
(([{(({<{<{(({{}[]}{<>[]}){<()()><<>{}>})[[({}[]){(){}>]]}[[{{<>[]}{()<>}}[{{}}{[][]}]]<<<()()>([][]
|
||||
[<([{{<[[{<([<{}{}>{(){}}]{[[]{}]{<>[]}})([(<>{})[()[]]])><([{<><>}([]{})]({()()}(<>[])))[[[(){}]{<><>}]<<[
|
||||
<((<<<[<<([({{()[]}})[{{<>[]}([]{})}{{[]<>)({}<>)}]][[<{<>}(()())>[[<>()]<{}{}>]][<({}()){<>}>{{()()}{<><>}
|
||||
[{{[{[({([[<({()}((){}))>{([[]<>]<[][]>)}]{({([]<>)<()()>}(<()<>><<>()>)){{<<>{}>{()[]}}[(()())<[
|
||||
(([[<[[{[{[([[{}()]<[]()>){<()<>>{{}<>}})<{[{}[]]{()<>}}{(()){<>[]}}>]({[{()[]}[<>]]}<(<{}
|
||||
[{[[[<(<<([[<{()<>}<(){}>>({{}()}<<>{}>)](<{{}()}><<{}[]}<<>[]>>)]<<({[][]}[()[]])[<{}{}>(<>[])]>>){<{{{()()}
|
||||
(<[<<{[{[[<{<<()()>{<>()}>}>]{[<([<>[]][()[]])<[<>[]]{{}()}>>((<()()>({}<>))<{(){}}{<>{}}>)][<{(<>{}>}{
|
||||
((<[{<<<<([{{(<>())<(){}>}(<{}<>>[<>{}])}({[<>()]{[]<>}})])[{((<{}<>><()<>>)(<[][]>[()]))<({<>[]}
|
||||
([((((([[{(<<<<>()>(<>[])>>[[(<>[])<<>{}>}<[[]{}](()[])>])([[({}<>)<[]()>]<[{}{}]{<>[]}>](<(<>())>{<
|
||||
<<({[{[{((<<({()<>}<[]{}>)({<>[]})><{([]{})}((()[]))>>(({([]<>){{}{}}})<[{{}[]}[(){}]](<()[]><<>{}>)>)
|
||||
{<[(({<[[([((<()<>><<><>>)(({}())<{}()>))<<<()()>{[]}>[{[][]}[{}[]]]>]{({{<>{}}<(){}>}[<()[]>{<>[]}>)(<[<>
|
||||
{{({<[((([([<<[]{}>[()()]>(<[]{}>)]{[{[]()}]<<(){}>[()<>]>})[{{[{}<>]}[[<>{}]{<><>}]}]]((<{{{}[]}}>[[<[]<>
|
||||
<[(<(<[((<{{[<(){}>[[][]]]}}>))]>)([({<(<<{([][])[()[]]}(<{}[]><()<>>)>{[<(){}>([][])]}>{[<<{
|
||||
(<{{<{<<[((<(<{}{}><[]<>>)<<{}{}><(){}>>>([{<><>}[()()]][{<>[]}([]<>)])))<<{{<<><>>}(<[]{}>(
|
||||
[(((({{[[{{([(<>{})<[][]>])([[()()][{}]](<{}<>>{<><>}))}}<[<{[()]<[]()>}{(<>[]){<>()}}>[[(<>[])[(){}]](({}())
|
||||
({{{<{[<{[{<{{{}{}}}[[(){}]{()[]}]>[[<[]<>>{<><>}]([(){}])]}[({{()[]}[{}[]]})[[{<>[]}<{}[]>]]]]}>](({{[[
|
||||
<{<[{{[{<[[({<[]<>>}{{()()}(<>[])})]]>}[{([[[<<>[]><[]{}>][{<><>}]][((()())(()()))([{}()])]
|
||||
<<<[([({{<<{[({}<>)[()[]]]({()[]}({}[]))}{[{<>[]][[]]]}>>}}[[({[({()[]}<()>)]<[[()<>]{()<>}][
|
||||
{[<({{{{<{{{[<()<>>[{}<>]]<[()()]<[][]>>}({<[][]>[()()]}<<[][]><[]()>>)}{([(()<>)<()()>])}>(([<[<
|
||||
[{<{[<<{{[{[([()<>](()[]))]((<{}{}>)(<[]<>>[<><>]))}]}}>><<<({{{[((){})(<><>)]((()<>){[][]})}(<
|
||||
[({(<{<{[<{([{[]()}[()()]])><[<{[]<>}{{}()}><(()())(()<>)>](<[[]{}]>[([]{})<()()>])>>][{([<([]())[[]<>]>
|
||||
{{(<(<{{({((({{}[]}<<>{}>){<{}()>[[][]]})<{{[]()}{()()}}(<{}[]>{()()})>)})}[<<[[{{<>[]}{<>{}}}{{[
|
||||
<<[<{{[<<{({{(<><>)({}())}({{}<>}[<>])}{[(()<>)<(){}>]{<<><>><[][]>}})<({<[]{}>[[][]]}<<<>{}>{()}>)>}>[<
|
||||
<{(<[<(((<({[[{}[]>(<>[])]<{[]<>}>}({(()())<{}{}>}<<<>[]>[<>()]>)){<<<[]{}>{()[]}>([[]{}]([]{}))><<{<>[]}
|
||||
<{{{[(({<{<[<<[]()>({}())>](<((){})[(){}]>(<<>>([]()))))}>([[<[<{}[]><[]()>]{({}[])({}())}>[[({
|
||||
{([{(<{([<{([{[]()}(<>[])]<<{}<>>(<>{})})[{<<>[]>}(<<><>>[<><>])]}[[{([][])({}())}](<(<>{})(<>)>(([]()){{}
|
||||
([<<{[[<((([<{()[]}>]<{[[][]]({}<>)}])<([{()[]}<()()>]{<<><>>[()()]})>))<[<<<([]<>)[()[]]>
|
||||
[[{([{([{[<(<({})[()[]]>[{[]()}[()()]]}[<{{}()}({}[])>(<[]<>>[()[]])]>([((()[])[(){}])]<[{
|
||||
{{[([[(<(([[[{[]{}}[()()]][[[]{}][<>[]]]][{<<>[]>}]][(<<(){}>{{}<>}>)[<[[]{}]{()()}>[((){})
|
||||
[({[[([[(<[{<([]{})(<><>)>}][<[[()()]<(){}>][[(){}][{}]]>([({}<>)[[]<>]]{[()[]]<[]()>})]>[[<(([]())<
|
||||
[<(<[<<{[<{<<[{}()]<<><>>>{[()<>]<[]<>>}><[([]<>)[{}])<{[]()}[<><>]>>}[[<<<>{}><()[]>>(({}[]
|
||||
(({[{{{[<[{[[<()[]>({}{})][(()[])[(){}]]]({{[]()}}[(<>())<<>[]>])}]<([<<<>[]>[()[]]>{[()<>]<<><>>}]([(<
|
||||
({<(({([[<(<({<>()}[()<>])({<><>}<[][]>)>([[<>()]([]())]))>]{{[{{{{}[]}}<[<><>]<<>[]>>}}}[{
|
||||
[(<([({<<(<{<{{}()}{[]()}>(<()()>{{}{}})}{{([]{})<[]>}<(<><>)>}>[<{<[]>([]<>)}({<><>}{[]{}}
|
||||
<{<[[((([{<[({{}[]}[<>()])(<{}[]>(<>{}))][{{()[]}([][])}({[]})]>({<[()[]]<{}{}>>{<[]{}>{()[]}}}([[{}][{}
|
||||
[([[<(<[((<[({{}{}}[{}[]]){[[]()](<>())}]>){[({[()()]([]<>)}<<<>{}>(<>()>>)]{{((<>())[{}()])[[()<>]<[]
|
||||
{{[<{([<[(<([(<>())<<>{}>])[{[()[]][<>{}]}([[]()]<{}<>>)]>)]<[{[{(())(<>{})}[({}{}){[]()}]]}<<{([]{})}[<
|
||||
([{{<<<([<((({{}{}}((){})){[<><>][[]<>]})<[{[][]}[()()]]([[]<>]{[]()})>)[[<([][]){<><>}>][<
|
||||
((<(<[([<<<({[<>[]][{}<>]}<{[]()}{{}{}}>)[({{}{}}([]()))(<()><<>>)}>(([<(){}>((){})]){<<<>[]>[{}{}]>({(){}}<
|
||||
(<<{{[[[<({({[{}{}]((){})}({<><>}({}<>)))}[(<[{}[]][()<>]>{({}{})(()())})<<[[]{}](<>)>([(){}][<><>])>])[
|
||||
<({{<<(({<[((<{}()>{<>{}}){{{}<>}{<>{}}}){({[]()}<{}[]>)<{()[]}[{}()]>}]{{[{()<>}(<><>)][<<>
|
||||
<[{[[(<<{{(({{{}<>}{<>()}}(<[]{}>))]([[[<>{}](<>)]{(()())<(){}>}][(({}()){<>()})[(()())[{}{}]]])}<<{{<()()
|
||||
[{{([<<{[{<([(())]<<{}>[[]<>]>){([()()]){(<>[]]([][])}}>{<[([]{})]{{{}}((){})}><{[<>[]][<>[]]}{{<><>}<
|
||||
{{{{<((<<{({<(<>[])[<>{}]>{([]<>){[][]}}}[[{[]()}{{}[]}]{[[]{}]<<>()>}])}(<([<{}{})][[{}()]<()()>])(<([][])(
|
||||
<{{[{[[[[{((((()())(()()))<<()>{()<>}>){[{[]{}]][(<><>)[{}{}]]}){{([<>[]]({}<>))((<>()){[]<>})}({<<>{}><
|
||||
({({[<<<[{[{([{}()}{<>[]})[(()[]){{}{}}]}]{<([()<>]((){})){(<>{})}>[<{<>()}([])><{[]{}}<[]()>>]}}]{([(<[()
|
||||
<{({(<{<((<([[{}[]][<>()]])[{(<>)(()[])}([<>()]<[]()>)]>)<<{[<{}<>>[[]()]]}[<((){})<<>()>>
|
||||
((({(((<<[{<<([]<>)[{}()]>((<><>)({}()))>([[<>[]](<>{})][<{}{}>{[][]}])}]>>)))}){[<{{{(<<[[<{}()>
|
||||
[<{<<{[<<<[{[{{}[]}[{}[]]]((<>[]){(){}})}{<<{}<>>{<>()}>(([]{}))>][{[<<>[]>]({{}{}}<[]<>>)}]>>
|
||||
({{[(<{[{[{{(<()[]>(()()))<<{}()><[]()>>}[(<[]>[{}()])[<{}{}>(()[])]]}]}({{<<{[]<>}((){})><<[]
|
||||
{(<[(<<<[({([((){})]({()<>}{[]<>}))({{<>{}}([][])})}{(({<>{}}<{}<>>)(({}{}){()[]}))[{([]())(
|
||||
<{([[[(<{[{{{[()<>]}{[<><>][[]<>]}}((([]){{}[]})<<{}>(()<>)>)}<((<(){}>{{}{}})){{(()[]}[()<>]}{[[]()][<>()]
|
||||
<[{{<([{{({(<{<>[]}{<>{}}>)[{{[][]}<<>{}>}[[(){}]<()<>>]]}<(({[]{}}({}{})){{{}{}}<<>()>})(
|
||||
{[{{{([({{<<([{}[]][[][])){<[][]>(<>())}><<(<>())<{}{}>>(<[][]>{<><>})>>(<([(){}]<<>{}>)[<[]
|
||||
(<(<({[[[<<{<[{}<>]([]())>{([]())[()<>]}}<{{<><>}[{}<>]}{[<><>]<[][]>}>>[({[(){}][{}()]}<<[]>([]<>)>)]>[<
|
||||
{{[({[<[<<<<<{[]{}}<<>[]>>({[]<>}{[]()})><<[[]<>]>[<()<>>{<><>}]]>[[{<()<>><[]<>>}<([]{})(<>{})>]({([
|
||||
{<[([([[<<<{{<()[]>[<>[]]}[[<>[]]([][])]}([[<><>]<()<>>])>({{[[]{}]{{}<>}}<[<><>][<>()]>}<[[{}[]
|
||||
<<<<[[<[<[[{{[[]{}]{{}<>}}(<<>()>([]{}))}{[{()[]}{{}{}}][{<><>}(())]}]<((<{}[]>[<>{}])({[]{}}(<>{})))[<
|
||||
<{[{({{{((<<[<<>[]>[{}()]]>{[([]())((){})]([[]()])}>[[({[]<>]{()()}){{()[]}{{}[]}}]<{{<>{}}([]
|
||||
{{(<<{[[[[(<([{}[]]([]{}))>[{({}<>)<<><>>}({[][]}({}{}))])]]](<{<[({[]{}}([]<>))({{}[]}<()[]>)]{{
|
||||
<{[<{[<<<<[<[<{}()><<>>](<()>({}()))>{[<<>[]>{<><>}](({}()))}]{{((()<>)((){})){({}[])<<><>>}
|
||||
({<{[({([{({<<<><>>([]<>)>(<[]()>[{}()])}<[(()())<()()>][{{}{}}]>)}])(({{(<{(){}}<{}()>>[<<>()><{}<>
|
||||
{{(({[([<((<{<{}[]>{<>()}}[[<>{}][()<>]]>))[[<{[[][]][[][]]}[<[]>(()[])]>{<[{}[]]{<>{}}><<{}()>[{
|
||||
<([{{[[<{{[[{{<>}{{}[]}}(([]())[<><>])][[<[]{}>([]{})]{({}){<><>}}]]<<[<[]()><{}{}>][{()}[<>[]]]>{<<<>{}>(<>
|
||||
{[[[([[(<<[{<<<>()><()[]>>}][<[<[]{}>(<><>))[<[]<>>[<>[]]]><<{[]()}{{}<>}><{<>}<<>{}>>>]><{<([()
|
||||
<((<[<[([(({{{{}<>}{()[]}}{[<><>](()())}})>{<(((())({}[]))[{{}<>}{<><>}]){<[{}{}]{<>{}}>({()
|
||||
[{[<<({({{<[[[[]<>]<<>()]]]([[<>[]]<<>{}>]<{[]<>}>)>}}{[[<[{[]{}}<{}[]>](((){}){<><>})>{<[[][]](<>())>
|
||||
<[[{{[(<{<(([[(){}]{<>())]))[<[<{}[]>{{}[]}][{()<>}[[]<>]]>{{<<>{}><[]<>>}[([]<>)[[]]]}]>}<<<<{[()<
|
||||
<(<<{{(<<({<{<[]()>}({[]()}[()<>])>(([()()][{}<>])<<(){}>{[][]}>)}(<{{[]{}}{<>()}}[{()<>}(()<>)]>))((({[{}][[
|
||||
{<<<{({{<([[<{<>{}}[()()]><[<>[]]>][<<<><>>>[(<><>)<<>()>]]]([({(){}}[<>()])]<<<<>[]>>({{}[]}[()[]])>
|
||||
[{<[{[[[({(<<[()[]]>{{()<>}{<>()}}>)[({[[][]][{}<>]}<<[]{}>[()<>]>){[<<>{}>{{}[]}](([]{}){[][]})}]}{{[(
|
||||
{(<<<{{<[({([({}[])[{}()]]{(<>{})<{}{}>})(<(<>())>({()<>}<[]{}>))}<{<([][]]{()()}>}<<(<>()){[][]
|
||||
((<{{[{{[([({[{}()]}[{()<>}(()<>)])]([([<><>]<<><>>)<{[][]}<<>[]>>]((<<>{}>({}<>))([[]()]<{}()>))))]}
|
||||
[(<<<{<{(({(<<[]{}>><[[]{}][()()]>)[(({}{}}[<>[]])[({}<>){()[]}]]}))[(<<<{[]<>}>{[(){}][()[]]}>>[{[([]<>)([]
|
||||
({({([<([{([[{[][]}{{}[]}]<[<>[]]({}())>])}(({<{()[]}{[]{}}>{({}())<{}[]>}})({{{{}[]}<{}()>}}))][[[[
|
||||
([<[<{<[[<{[((<>())(()))[<(){}>{{}[]}]]{<{()[]}{[]()}><<<><>>>}}(<{[()<>]<<>()>}>)><{(([<>()]({}<>))([
|
||||
[<[(({[<{{[[((())[<>[]])(<[]<>><<>{}>)][{{<>()}<()<>>}[{{}()}([][])]]]{{<<[]()>>[<{}()>[<><>]]){[{<>()
|
||||
[(<([[<{{[(([<<><>>(()())](([][]]{<>()}))[<<[]<>><()()>>[({}<>){()<>}]]){{<(()[]){()[]}>}<(<
|
||||
(<[{[<{[[{{([[{}()]<{}()>]<[()[]](()())>)[<[{}{}][[]()]><[{}<>]{{}<>}>]}}{{[{[{}<>][[][]]}<({}()){[]{}}>]
|
||||
({{<[{<<<<[<<[(){}](<><>)>({{}()}{<><>})>}[({({}<>){<>{}}}<[()<>][(){}]>)[{(()<>)({}[])}]]>
|
||||
<{<({<([[((<[{[][]}{<>()]]>{<([][])[[]<>]>[(<>{})[{}()]]})[{[({}())[[][]]]{{[]()}}}<[((){})[[]
|
||||
<<<({{[<(<<{(<[][]>(<>()))({()[]})}[[<<>[]>({}[])]{[[][]]}]>{(<(())<{}<>>>(([])[<>{}])){(<<><>>[[][]])}}>(<[[
|
||||
[<<({[[(<[[[{<[]{}>{()[]}}<[<>()]>](<{[][]}<<>()>><[[][]]([]())>)]](({{{{}()}{<>[]}}}<({[][]}[
|
||||
<{[({{{<[(({<({}{})[[]<>]>(({})(<>{}))}[{<{}()>[[][]]}]))]>{<{{{([{}{}]({}{}))(<[]{}>({}[]))}{<{{}()}({}<>)
|
||||
({{<((({({<[{[<>{}]([][])}]<[{[]<>}]{[[]()][[][]]}]>[{({{}{}}<[][]>){{(){}}}}]})<[(<<({}())<{}()>>({<>}{()()}
|
||||
<{{{[(({([{<{<[]<>><<>[]>}>[[<[]{}>{()()}]<<(){}>>]}{[{((){})(<>())}<([][])}]}])[({<<{{}[]}>><{{<>()}
|
||||
(<<<<<<[{{{[[{{}<>}(<>)](<[][]>({}()))]}<<[(<>[])(<>[])]({<>[]}<<>{}>)>{{{[]<>}({}<>>}([<><>]{<>[]})}>}
|
||||
{[({<{{[({[[{[{}[]]{{}}}]({<{}>{()}}(({}{})({}[])))]{({{[]<>}{()[]}}<[{}<>][[]()]>)[{((){})(<><>)
|
||||
{{<{{{[({(<{{{{}[]}{[]()}}((<>[])([]<>))}[{{<>{}}([]{})}{[[]<>]<[]<>>}]><{{(<>)}({[]}[()<>])}{<[[]
|
||||
((<{{{{(([(<{{{}()}{<><>}}>)]{([{<<>[]>{<><>}}<[[][]]<()[]>>])({({[][]}<[][]>)[[[]{}]{[]<>}]})}))({{[
|
||||
<<{<([{[{[<<<[()()]>[(<><>)[()<>]]>({[{}]<{}>}<{{}()}<[]{}>>)>][<[{<{}}}{[()<>]{<>()}}]([([][]){
|
||||
(({<([[[({{(({[]<>}[()[]])[({}{})<()<>>))<(([][])<<>[]>){<[]<>>}>}(([<<><>>[{}<>]]{<[]()>{<><>}})([<<><>>(
|
||||
163
advent_of_code/2021/d10/main.cpp
Normal file
163
advent_of_code/2021/d10/main.cpp
Normal file
@@ -0,0 +1,163 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
enum HOOKIES {
|
||||
// PARENTHESIS = 3,
|
||||
// BRACKET = 57,
|
||||
// ACCOLADE = 1197,
|
||||
// POINTY_BRACKET = 25137,
|
||||
|
||||
PARENTHESIS = 1,
|
||||
BRACKET = 2,
|
||||
ACCOLADE = 3,
|
||||
POINTY_BRACKET = 4,
|
||||
};
|
||||
|
||||
unsigned int line_score_corrupted(const string& s){
|
||||
stack<HOOKIES> chars;
|
||||
unsigned int score = 0;
|
||||
|
||||
for(auto c : s){
|
||||
if(c == '(' || c == '[' || c == '{' || c == '<'){
|
||||
if(c == '('){
|
||||
chars.push(PARENTHESIS);
|
||||
} else if(c == '['){
|
||||
chars.push(BRACKET);
|
||||
} else if(c == '{'){
|
||||
chars.push(ACCOLADE);
|
||||
} else{
|
||||
chars.push(POINTY_BRACKET);
|
||||
}
|
||||
} else if(c == ')' || c == ']' || c == '}' || c == '>'){
|
||||
if(chars.empty()){
|
||||
score = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
if(c == ')'){
|
||||
if(chars.top() == PARENTHESIS){
|
||||
chars.pop();
|
||||
} else{
|
||||
score = PARENTHESIS;
|
||||
break;
|
||||
}
|
||||
} else if(c == ']'){
|
||||
if(chars.top() == BRACKET){
|
||||
chars.pop();
|
||||
} else{
|
||||
score = BRACKET;
|
||||
break;
|
||||
}
|
||||
} else if(c == '}'){
|
||||
if(chars.top() == ACCOLADE){
|
||||
chars.pop();
|
||||
} else{
|
||||
score = ACCOLADE;
|
||||
break;
|
||||
}
|
||||
} else{
|
||||
if(chars.top() == POINTY_BRACKET){
|
||||
chars.pop();
|
||||
} else{
|
||||
score = POINTY_BRACKET;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
unsigned int line_score_missing(const string& s){
|
||||
stack<HOOKIES> chars;
|
||||
|
||||
bool corrupted = false;
|
||||
|
||||
for(auto c : s){
|
||||
if(c == '(' || c == '[' || c == '{' || c == '<'){
|
||||
if(c == '('){
|
||||
chars.push(PARENTHESIS);
|
||||
} else if(c == '['){
|
||||
chars.push(BRACKET);
|
||||
} else if(c == '{'){
|
||||
chars.push(ACCOLADE);
|
||||
} else{
|
||||
chars.push(POINTY_BRACKET);
|
||||
}
|
||||
} else if(c == ')' || c == ']' || c == '}' || c == '>'){
|
||||
if(chars.empty()){
|
||||
corrupted = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if(c == ')'){
|
||||
if(chars.top() == PARENTHESIS){
|
||||
chars.pop();
|
||||
} else{
|
||||
corrupted = true;
|
||||
break;
|
||||
}
|
||||
} else if(c == ']'){
|
||||
if(chars.top() == BRACKET){
|
||||
chars.pop();
|
||||
} else{
|
||||
corrupted = true;
|
||||
break;
|
||||
}
|
||||
} else if(c == '}'){
|
||||
if(chars.top() == ACCOLADE){
|
||||
chars.pop();
|
||||
} else{
|
||||
corrupted = true;
|
||||
break;
|
||||
}
|
||||
} else{
|
||||
if(chars.top() == POINTY_BRACKET){
|
||||
chars.pop();
|
||||
} else{
|
||||
corrupted = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else{
|
||||
corrupted = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int score = 0;
|
||||
if(!corrupted){
|
||||
while(!chars.empty()){
|
||||
auto h = chars.top();
|
||||
score = score * 5 + h;
|
||||
chars.pop();
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
int main(){
|
||||
ifstream input_file("input.txt");
|
||||
string line;
|
||||
|
||||
unsigned int total_score = 0;
|
||||
|
||||
if(input_file.is_open()){
|
||||
while(getline(input_file, line)){
|
||||
// total_score += line_score_corrupted(line);
|
||||
|
||||
total_score += line_score_missing(line);
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Total syntax score: " << total_score << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
10
advent_of_code/2021/d10/test.txt
Normal file
10
advent_of_code/2021/d10/test.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
[({(<(())[]>[[{[]{<()<>>
|
||||
[(()[<>])]({[<{<<[]>>(
|
||||
{([(<{}[<>[]}>{[]{[(<()>
|
||||
(((({<>}<{<{<>}{[]{[]{}
|
||||
[[<[([]))<([[{}[[()]]]
|
||||
[{[{({}]{}}([{[{{{}}([]
|
||||
{<[[]]>}<{[{[{[]{()[[[]
|
||||
[<(<(<(<{}))><([]([]()
|
||||
<{([([[(<>()){}]>(<<{{
|
||||
<{([{{}}[<[[[<>{}]]]>[]]
|
||||
1000
advent_of_code/2021/d2/2.txt
Normal file
1000
advent_of_code/2021/d2/2.txt
Normal file
File diff suppressed because it is too large
Load Diff
7
advent_of_code/2021/d2/Cargo.lock
generated
Normal file
7
advent_of_code/2021/d2/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "d2"
|
||||
version = "0.1.0"
|
||||
8
advent_of_code/2021/d2/Cargo.toml
Normal file
8
advent_of_code/2021/d2/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "d2"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
64
advent_of_code/2021/d2/src/main.rs
Normal file
64
advent_of_code/2021/d2/src/main.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use std::io::{self, BufRead};
|
||||
use std::path::Path;
|
||||
use std::fs::File;
|
||||
|
||||
fn main(){
|
||||
println!("Advent of Code #2!\n");
|
||||
|
||||
let path = Path::new("./2.txt");
|
||||
let display = path.display();
|
||||
|
||||
let file = match File::open(&path) {
|
||||
Err(why) => panic!("Couldn't open {}: {}", display, why),
|
||||
Ok(file) => file,
|
||||
};
|
||||
|
||||
let lines = io::BufReader::new(file).lines();
|
||||
let mut instructions = Vec::<(String, u32)>::new();
|
||||
|
||||
for line in lines {
|
||||
if let Ok(l) = line {
|
||||
let mut words_iter = l.split_whitespace();
|
||||
let direction = words_iter.next().unwrap().to_string();
|
||||
let distance = words_iter.next().unwrap().parse::<u32>().unwrap();
|
||||
|
||||
instructions.push((direction, distance));
|
||||
}
|
||||
}
|
||||
|
||||
let mut depth = 0;
|
||||
let mut position = 0;
|
||||
for (direction, distance) in &instructions {
|
||||
match direction.as_str() {
|
||||
"forward" => position += distance,
|
||||
"up" => depth -= distance,
|
||||
"down" => depth += distance,
|
||||
s => println!("Stumbled upon unknown input: {}", s),
|
||||
}
|
||||
}
|
||||
|
||||
println!("Position of {} * depth of {} equals {}", position, depth, position * depth);
|
||||
|
||||
|
||||
// Now for the second part of day 2
|
||||
// So instead of just adding to the depth immediately through the up and down commands, we now use
|
||||
// a so-called aim
|
||||
|
||||
depth = 0;
|
||||
position = 0;
|
||||
let mut aim = 0;
|
||||
|
||||
for (direction, distance) in &instructions {
|
||||
match direction.as_str() {
|
||||
"forward" => {
|
||||
depth += aim * distance;
|
||||
position += distance
|
||||
},
|
||||
"up" => aim -= distance,
|
||||
"down" => aim += distance,
|
||||
s => println!("Stumbled upon unknown input: {}", s),
|
||||
}
|
||||
}
|
||||
|
||||
println!("Position of {} * depth of {} equals {}", position, depth, position * depth);
|
||||
}
|
||||
1000
advent_of_code/2021/d3/3.txt
Normal file
1000
advent_of_code/2021/d3/3.txt
Normal file
File diff suppressed because it is too large
Load Diff
7
advent_of_code/2021/d3/Cargo.lock
generated
Normal file
7
advent_of_code/2021/d3/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "d3"
|
||||
version = "0.1.0"
|
||||
8
advent_of_code/2021/d3/Cargo.toml
Normal file
8
advent_of_code/2021/d3/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "d3"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
205
advent_of_code/2021/d3/src/main.rs
Normal file
205
advent_of_code/2021/d3/src/main.rs
Normal file
@@ -0,0 +1,205 @@
|
||||
use std::io::{self, BufRead};
|
||||
use std::path::Path;
|
||||
use std::fs::File;
|
||||
|
||||
fn main(){
|
||||
// Submarine is breaking, do diagnostics by checking power consumption
|
||||
// Calculate gamma rate * epsilon rate
|
||||
// Diagnostics are encoded by series of binary numbers
|
||||
|
||||
// Gamma rate is most common bit for each bit in the series
|
||||
// Epsilon rate is the opposite, i.e. least common bit
|
||||
|
||||
// So when gamma rate would be 10011, epsilon rate automatically is 01100
|
||||
|
||||
|
||||
println!("Advent of Code #3!\n");
|
||||
|
||||
let path = Path::new("./3.txt");
|
||||
let display = path.display();
|
||||
|
||||
let file = match File::open(&path) {
|
||||
Err(why) => panic!("Couldn't open {}: {}", display, why),
|
||||
Ok(file) => file,
|
||||
};
|
||||
|
||||
let lines = io::BufReader::new(file).lines();
|
||||
let mut diagnostics = Vec::<Vec::<bool>>::new();
|
||||
|
||||
for line in lines{
|
||||
let mut d_line = Vec::<bool>::new();
|
||||
if let Ok(l) = line {
|
||||
for c in l.chars() {
|
||||
match c {
|
||||
'0' => d_line.push(false),
|
||||
'1' => d_line.push(true),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
diagnostics.push(d_line);
|
||||
}
|
||||
|
||||
let mut line_numbers = 0;
|
||||
let b_len = diagnostics[0].len();
|
||||
let mut ones = vec![0; b_len];
|
||||
|
||||
for line in &diagnostics {
|
||||
line_numbers += 1;
|
||||
|
||||
for i in 0 .. b_len {
|
||||
match line[i] {
|
||||
true => ones[i] += 1,
|
||||
false => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut gamma_rate = Vec::new();
|
||||
for n in ones {
|
||||
if n > line_numbers / 2 {
|
||||
gamma_rate.push(true);
|
||||
} else {
|
||||
gamma_rate.push(false);
|
||||
}
|
||||
}
|
||||
|
||||
let mut gamma_rate_decimal = 0;
|
||||
let mut pow = 1;
|
||||
|
||||
gamma_rate.reverse();
|
||||
for b in &gamma_rate {
|
||||
match b {
|
||||
true => gamma_rate_decimal += pow,
|
||||
false => (),
|
||||
}
|
||||
pow *= 2;
|
||||
}
|
||||
|
||||
println!("Gamma rate in decimal is {}, epsilon rate in decimal is {}, yielding product of {}",
|
||||
gamma_rate_decimal, pow - gamma_rate_decimal - 1,
|
||||
gamma_rate_decimal * (pow - gamma_rate_decimal - 1));
|
||||
|
||||
|
||||
|
||||
// Now on to the second part of day 3
|
||||
|
||||
// Verify life support rating, by multiplying oxygen generator rating by co2 scrubber rating
|
||||
// Oxygen generator rating is determined by finding the most common bit for the first bit
|
||||
// Then, throw out all diagnostics line that do not have the same most common bit for the first position
|
||||
// Repeat until only one line remains, this is the oxygen generator rating
|
||||
// For the co2 scrubber rating, do the same but for the least common bit per position
|
||||
|
||||
gamma_rate.reverse();
|
||||
let mut viable_oxygen = vec![true; line_numbers];
|
||||
let mut viable_scrubber = vec![true; line_numbers];
|
||||
|
||||
let mut n_oxygen = line_numbers;
|
||||
let mut n_scrubber = line_numbers;
|
||||
|
||||
for bit_n in 0 .. b_len {
|
||||
let mcb = most_common_bit(bit_n, &viable_oxygen, &diagnostics);
|
||||
let lcb = !most_common_bit(bit_n, &viable_scrubber, &diagnostics);
|
||||
|
||||
for line_n in 0 .. line_numbers {
|
||||
if viable_oxygen[line_n] && n_oxygen > 1 {
|
||||
if diagnostics[line_n][bit_n] != mcb {
|
||||
n_oxygen -= 1;
|
||||
viable_oxygen[line_n] = false;
|
||||
}
|
||||
}
|
||||
|
||||
if viable_scrubber[line_n] && n_scrubber > 1 {
|
||||
if diagnostics[line_n][bit_n] != lcb {
|
||||
n_scrubber -= 1;
|
||||
viable_scrubber[line_n] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// println!("\nIteration {}:", bit_n);
|
||||
// for line_n in 0 .. line_numbers {
|
||||
// if viable_oxygen[line_n] {
|
||||
// print_bool_vec(&diagnostics[line_n]);
|
||||
// }
|
||||
// }
|
||||
|
||||
if n_oxygen == 1 && n_scrubber == 1 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// println!("n_oxygen: {}, n_scrubber: {}", n_oxygen, n_scrubber);
|
||||
|
||||
let mut oxygen_ans = 0;
|
||||
let mut scrubber_ans = 0;
|
||||
|
||||
for i in 0 .. line_numbers {
|
||||
if viable_oxygen[i] {
|
||||
oxygen_ans = i;
|
||||
}
|
||||
|
||||
if viable_scrubber[i] {
|
||||
scrubber_ans = i;
|
||||
}
|
||||
}
|
||||
|
||||
print_bool_vec(&diagnostics[oxygen_ans]);
|
||||
// print_bool_vec(&diagnostics[scrubber_ans]);
|
||||
|
||||
diagnostics[oxygen_ans].reverse();
|
||||
diagnostics[scrubber_ans].reverse();
|
||||
|
||||
let oxygen_dec = bit_to_decimal(&diagnostics[oxygen_ans]);
|
||||
let scrubber_dec = bit_to_decimal(&diagnostics[scrubber_ans]);
|
||||
|
||||
println!("Oxygen generator rating is {}, co2 scrubber rating is {}, yields product of {}",
|
||||
oxygen_dec, scrubber_dec, oxygen_dec * scrubber_dec);
|
||||
}
|
||||
|
||||
fn bit_to_decimal(bits: &Vec::<bool>) -> u64 {
|
||||
let mut pow = 1;
|
||||
let mut result = 0;
|
||||
|
||||
for b in bits {
|
||||
match b {
|
||||
true => result += pow,
|
||||
false => (),
|
||||
}
|
||||
pow *= 2;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
fn most_common_bit(index: usize, valid_series: &Vec::<bool>, bit_series: &Vec::<Vec::<bool>>) -> bool {
|
||||
let mut ones = 0;
|
||||
let mut valids = 0;
|
||||
|
||||
for i in 0 .. valid_series.len() {
|
||||
if valid_series[i] {
|
||||
valids += 1;
|
||||
|
||||
if bit_series[i][index] {
|
||||
ones += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut v = valids as f32;
|
||||
v /= 2.0;
|
||||
v = v.ceil();
|
||||
valids = v as u64;
|
||||
|
||||
if ones >= valids {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
fn print_bool_vec(input: &Vec::<bool>) -> () {
|
||||
for b in input {
|
||||
print!("{},", b);
|
||||
} println!();
|
||||
}
|
||||
601
advent_of_code/2021/d4/4.txt
Normal file
601
advent_of_code/2021/d4/4.txt
Normal file
@@ -0,0 +1,601 @@
|
||||
23,30,70,61,79,49,19,37,64,48,72,34,69,53,15,74,89,38,46,36,28,32,45,2,39,58,11,62,97,40,14,87,96,94,91,92,80,99,6,31,57,98,65,10,33,63,42,17,47,66,26,22,73,27,7,0,55,8,56,29,86,25,4,12,51,60,35,50,5,75,95,44,16,93,21,3,24,52,77,76,43,41,9,84,67,71,83,88,59,68,85,82,1,18,13,78,20,90,81,54
|
||||
|
||||
50 98 65 14 47
|
||||
0 22 3 83 46
|
||||
87 93 81 84 58
|
||||
40 35 28 74 48
|
||||
45 99 59 37 64
|
||||
|
||||
85 66 90 32 88
|
||||
95 6 4 74 27
|
||||
1 10 70 41 92
|
||||
54 36 42 9 39
|
||||
60 99 31 67 16
|
||||
|
||||
4 44 66 10 58
|
||||
33 64 93 42 46
|
||||
19 63 6 83 54
|
||||
60 51 76 8 30
|
||||
71 49 73 7 55
|
||||
|
||||
17 67 52 61 98
|
||||
46 5 4 51 76
|
||||
73 59 74 8 33
|
||||
48 96 20 26 15
|
||||
55 19 86 29 43
|
||||
|
||||
20 75 12 67 41
|
||||
89 36 65 66 92
|
||||
40 19 1 0 28
|
||||
99 61 85 58 50
|
||||
44 72 57 35 86
|
||||
|
||||
69 87 27 59 33
|
||||
47 34 60 93 9
|
||||
71 84 46 24 96
|
||||
15 91 5 61 19
|
||||
57 78 55 31 8
|
||||
|
||||
19 10 1 81 96
|
||||
27 71 2 52 56
|
||||
15 22 48 82 34
|
||||
64 47 42 49 51
|
||||
26 72 61 12 57
|
||||
|
||||
71 94 40 34 26
|
||||
12 80 57 38 55
|
||||
4 56 11 73 49
|
||||
75 60 61 9 50
|
||||
91 70 23 1 90
|
||||
|
||||
39 86 30 73 38
|
||||
6 53 58 14 36
|
||||
85 12 75 88 5
|
||||
0 29 41 21 15
|
||||
47 66 59 54 1
|
||||
|
||||
99 97 50 17 60
|
||||
36 13 29 80 32
|
||||
49 85 75 71 15
|
||||
10 79 41 61 66
|
||||
68 57 55 74 98
|
||||
|
||||
68 33 87 89 59
|
||||
96 35 76 78 55
|
||||
4 63 51 10 65
|
||||
58 38 22 54 9
|
||||
66 18 37 60 6
|
||||
|
||||
43 86 50 23 77
|
||||
10 42 19 61 2
|
||||
40 29 20 84 0
|
||||
70 59 96 80 57
|
||||
76 12 39 36 6
|
||||
|
||||
73 43 92 37 99
|
||||
36 42 10 77 87
|
||||
3 57 4 20 35
|
||||
18 7 46 91 11
|
||||
17 98 8 53 61
|
||||
|
||||
22 37 89 51 9
|
||||
71 6 72 87 32
|
||||
13 79 86 53 98
|
||||
16 2 93 48 38
|
||||
63 82 66 61 69
|
||||
|
||||
73 90 85 54 65
|
||||
9 66 28 5 63
|
||||
91 50 70 59 80
|
||||
95 68 92 72 67
|
||||
69 88 36 43 53
|
||||
|
||||
36 81 66 78 90
|
||||
2 25 94 82 55
|
||||
34 45 1 14 37
|
||||
13 4 70 48 75
|
||||
67 73 32 18 91
|
||||
|
||||
33 93 71 48 47
|
||||
8 79 69 53 82
|
||||
5 31 80 45 37
|
||||
67 77 41 56 97
|
||||
65 46 62 42 81
|
||||
|
||||
67 70 59 24 88
|
||||
84 11 29 52 78
|
||||
4 39 12 90 2
|
||||
44 3 10 75 89
|
||||
30 93 22 14 8
|
||||
|
||||
79 60 98 99 49
|
||||
23 26 86 91 38
|
||||
77 45 95 66 75
|
||||
81 42 85 21 3
|
||||
40 37 65 20 50
|
||||
|
||||
12 54 0 86 52
|
||||
15 56 29 39 94
|
||||
66 79 14 65 26
|
||||
3 4 59 60 40
|
||||
47 48 19 13 85
|
||||
|
||||
32 44 69 90 21
|
||||
35 8 1 59 56
|
||||
72 71 84 18 11
|
||||
96 38 23 37 79
|
||||
92 20 33 94 17
|
||||
|
||||
1 94 42 21 82
|
||||
92 60 9 32 38
|
||||
71 3 37 77 18
|
||||
89 16 74 76 2
|
||||
83 30 28 11 70
|
||||
|
||||
94 3 1 71 87
|
||||
6 66 19 76 28
|
||||
10 86 22 62 2
|
||||
67 0 31 46 27
|
||||
8 33 43 92 29
|
||||
|
||||
35 90 8 30 27
|
||||
67 60 82 68 1
|
||||
5 29 93 44 34
|
||||
56 65 48 37 51
|
||||
57 45 63 94 77
|
||||
|
||||
67 80 45 57 43
|
||||
37 81 25 84 82
|
||||
50 8 9 64 7
|
||||
29 18 52 16 14
|
||||
73 28 11 76 6
|
||||
|
||||
5 76 67 18 16
|
||||
68 47 15 29 59
|
||||
46 32 40 9 84
|
||||
30 17 20 22 3
|
||||
35 80 38 72 88
|
||||
|
||||
35 44 14 89 72
|
||||
75 67 56 2 3
|
||||
58 41 49 12 52
|
||||
92 9 22 34 88
|
||||
65 39 93 61 47
|
||||
|
||||
38 67 33 18 60
|
||||
34 50 69 31 83
|
||||
29 30 9 12 95
|
||||
79 2 24 54 87
|
||||
46 68 48 58 42
|
||||
|
||||
61 87 46 26 34
|
||||
74 85 9 54 38
|
||||
50 29 84 40 4
|
||||
49 39 33 99 53
|
||||
77 59 0 42 35
|
||||
|
||||
86 68 23 62 5
|
||||
96 92 7 4 1
|
||||
50 70 12 83 46
|
||||
34 63 91 56 11
|
||||
76 90 71 88 95
|
||||
|
||||
19 18 13 3 62
|
||||
42 29 57 79 85
|
||||
39 64 14 28 98
|
||||
99 36 91 9 63
|
||||
69 66 2 17 31
|
||||
|
||||
51 43 49 98 94
|
||||
31 64 53 54 57
|
||||
3 28 10 12 2
|
||||
24 99 95 35 17
|
||||
76 27 48 0 41
|
||||
|
||||
80 62 13 38 98
|
||||
32 15 16 8 96
|
||||
93 43 81 99 40
|
||||
20 57 37 24 3
|
||||
94 17 70 14 7
|
||||
|
||||
52 71 49 95 84
|
||||
76 38 45 59 89
|
||||
1 7 27 0 98
|
||||
92 64 8 50 68
|
||||
13 91 26 51 2
|
||||
|
||||
31 45 25 1 5
|
||||
50 68 77 61 53
|
||||
74 20 99 38 63
|
||||
76 44 15 42 51
|
||||
67 87 86 12 24
|
||||
|
||||
49 0 70 82 9
|
||||
2 24 96 74 60
|
||||
68 16 40 32 20
|
||||
48 6 98 11 65
|
||||
94 10 54 8 95
|
||||
|
||||
74 41 11 33 76
|
||||
2 10 44 89 23
|
||||
56 45 78 60 34
|
||||
15 5 26 83 71
|
||||
20 72 85 75 54
|
||||
|
||||
15 59 93 53 8
|
||||
4 10 84 44 36
|
||||
17 62 24 27 98
|
||||
87 54 73 13 35
|
||||
9 48 52 33 7
|
||||
|
||||
56 80 70 74 35
|
||||
53 69 75 25 27
|
||||
47 91 85 62 32
|
||||
93 26 89 18 52
|
||||
16 73 49 55 77
|
||||
|
||||
42 40 54 67 73
|
||||
11 10 49 35 59
|
||||
12 93 37 15 69
|
||||
97 41 47 39 2
|
||||
75 99 21 29 26
|
||||
|
||||
23 75 41 10 86
|
||||
71 67 66 38 99
|
||||
91 92 63 40 28
|
||||
69 97 42 77 60
|
||||
44 53 12 84 57
|
||||
|
||||
72 51 31 90 37
|
||||
35 89 55 73 87
|
||||
46 32 45 0 58
|
||||
50 81 13 18 66
|
||||
38 4 40 62 22
|
||||
|
||||
14 48 35 76 83
|
||||
13 70 26 4 1
|
||||
30 22 91 93 29
|
||||
69 41 74 40 63
|
||||
80 65 66 72 23
|
||||
|
||||
23 65 33 56 38
|
||||
84 41 34 21 2
|
||||
4 78 27 17 11
|
||||
22 53 52 32 80
|
||||
24 25 42 91 99
|
||||
|
||||
54 51 0 23 52
|
||||
92 69 10 46 7
|
||||
20 35 12 37 73
|
||||
19 56 26 79 32
|
||||
27 74 34 5 57
|
||||
|
||||
75 10 24 32 7
|
||||
96 54 22 78 5
|
||||
23 69 65 43 20
|
||||
29 85 44 92 71
|
||||
41 87 73 0 48
|
||||
|
||||
54 92 16 36 37
|
||||
42 59 4 9 44
|
||||
52 14 12 6 47
|
||||
57 38 70 82 0
|
||||
53 81 32 35 3
|
||||
|
||||
17 22 62 80 30
|
||||
8 28 15 42 46
|
||||
79 64 32 29 75
|
||||
5 0 9 90 69
|
||||
41 71 85 1 6
|
||||
|
||||
68 89 40 31 39
|
||||
32 48 64 38 28
|
||||
80 98 88 14 97
|
||||
6 60 52 11 55
|
||||
95 34 63 81 4
|
||||
|
||||
80 33 14 83 68
|
||||
78 69 81 59 15
|
||||
72 0 74 21 75
|
||||
49 6 67 73 64
|
||||
8 25 87 3 45
|
||||
|
||||
34 97 86 1 79
|
||||
49 12 63 10 59
|
||||
88 30 84 74 87
|
||||
67 47 26 0 57
|
||||
71 40 2 76 98
|
||||
|
||||
15 89 23 65 44
|
||||
27 87 54 38 12
|
||||
43 29 18 39 94
|
||||
48 0 7 57 61
|
||||
70 28 60 68 50
|
||||
|
||||
13 34 49 67 40
|
||||
88 74 99 20 26
|
||||
63 69 62 24 32
|
||||
35 45 96 79 1
|
||||
92 7 17 76 30
|
||||
|
||||
95 21 75 46 74
|
||||
39 7 58 23 90
|
||||
61 64 37 81 82
|
||||
92 36 54 9 53
|
||||
17 51 33 10 27
|
||||
|
||||
67 35 44 22 23
|
||||
28 96 1 56 29
|
||||
0 12 5 50 99
|
||||
70 42 8 24 25
|
||||
39 53 51 89 85
|
||||
|
||||
50 15 94 84 27
|
||||
72 26 51 3 85
|
||||
63 45 1 64 44
|
||||
17 80 13 88 2
|
||||
12 97 91 25 18
|
||||
|
||||
59 14 9 67 63
|
||||
6 18 26 98 50
|
||||
86 74 75 56 34
|
||||
48 7 99 20 64
|
||||
8 53 10 15 57
|
||||
|
||||
6 35 13 68 24
|
||||
90 19 91 71 86
|
||||
95 58 10 44 98
|
||||
8 41 60 1 16
|
||||
29 59 43 84 48
|
||||
|
||||
48 56 8 74 4
|
||||
66 30 77 35 90
|
||||
94 0 75 49 84
|
||||
5 39 11 54 87
|
||||
33 58 96 22 2
|
||||
|
||||
5 38 57 63 65
|
||||
74 58 22 8 81
|
||||
45 96 78 3 11
|
||||
28 42 30 39 51
|
||||
87 33 34 75 14
|
||||
|
||||
56 34 67 70 17
|
||||
7 80 10 31 85
|
||||
68 59 63 74 40
|
||||
13 81 99 62 6
|
||||
92 84 71 37 39
|
||||
|
||||
85 99 74 16 10
|
||||
12 21 91 2 83
|
||||
4 94 38 51 36
|
||||
41 97 45 65 24
|
||||
50 82 92 52 35
|
||||
|
||||
28 65 6 13 23
|
||||
7 57 86 18 67
|
||||
26 85 29 22 89
|
||||
99 62 94 31 96
|
||||
14 17 50 56 9
|
||||
|
||||
98 10 63 4 8
|
||||
46 21 58 89 3
|
||||
27 12 11 55 16
|
||||
61 38 43 33 54
|
||||
53 14 99 31 25
|
||||
|
||||
25 70 24 40 14
|
||||
75 82 58 68 41
|
||||
22 71 72 93 1
|
||||
47 97 6 81 45
|
||||
92 42 2 76 12
|
||||
|
||||
31 84 30 0 85
|
||||
55 70 72 45 57
|
||||
78 52 67 60 22
|
||||
43 32 8 44 34
|
||||
14 64 91 89 18
|
||||
|
||||
70 19 62 16 56
|
||||
84 49 41 3 20
|
||||
85 5 76 95 22
|
||||
63 55 37 31 72
|
||||
42 17 28 65 1
|
||||
|
||||
85 17 57 62 48
|
||||
34 29 69 52 28
|
||||
90 64 54 21 38
|
||||
0 50 84 44 60
|
||||
93 80 75 89 83
|
||||
|
||||
39 84 78 12 5
|
||||
29 4 35 7 85
|
||||
73 25 58 27 45
|
||||
22 90 91 47 74
|
||||
60 96 15 24 26
|
||||
|
||||
13 30 82 31 43
|
||||
23 71 1 51 36
|
||||
40 62 25 54 86
|
||||
8 83 2 47 34
|
||||
33 41 27 98 24
|
||||
|
||||
13 25 53 50 56
|
||||
77 4 41 19 22
|
||||
68 70 75 9 65
|
||||
30 33 60 74 80
|
||||
31 83 34 79 11
|
||||
|
||||
11 90 38 78 73
|
||||
17 16 14 37 4
|
||||
80 68 21 70 92
|
||||
47 26 81 67 25
|
||||
10 31 23 41 22
|
||||
|
||||
90 62 2 50 79
|
||||
77 51 8 11 13
|
||||
32 29 43 88 33
|
||||
39 34 89 45 23
|
||||
91 9 6 68 3
|
||||
|
||||
62 70 89 27 87
|
||||
45 65 96 80 29
|
||||
1 54 90 68 16
|
||||
72 50 28 95 12
|
||||
21 71 81 10 60
|
||||
|
||||
33 14 60 44 78
|
||||
6 65 87 11 8
|
||||
79 21 59 35 19
|
||||
26 69 67 42 27
|
||||
25 36 80 10 45
|
||||
|
||||
71 24 80 87 56
|
||||
7 61 43 38 18
|
||||
52 46 41 28 48
|
||||
0 74 20 34 63
|
||||
3 84 42 85 9
|
||||
|
||||
36 64 41 7 49
|
||||
91 92 13 94 88
|
||||
73 98 79 0 12
|
||||
76 66 86 67 9
|
||||
2 85 74 5 34
|
||||
|
||||
8 81 7 56 28
|
||||
36 13 42 29 75
|
||||
12 27 85 45 9
|
||||
26 25 62 41 22
|
||||
79 11 95 0 24
|
||||
|
||||
72 76 81 67 16
|
||||
96 41 94 58 7
|
||||
0 79 38 27 11
|
||||
61 36 56 88 39
|
||||
89 63 31 75 8
|
||||
|
||||
62 51 5 46 28
|
||||
77 97 89 86 13
|
||||
87 55 73 90 57
|
||||
84 44 40 49 34
|
||||
25 0 58 6 21
|
||||
|
||||
7 56 15 41 94
|
||||
42 89 16 18 74
|
||||
57 79 96 35 3
|
||||
14 45 20 19 80
|
||||
87 85 28 69 17
|
||||
|
||||
27 88 54 62 65
|
||||
44 93 69 13 9
|
||||
85 63 43 11 47
|
||||
83 57 30 20 56
|
||||
71 46 49 7 77
|
||||
|
||||
45 24 75 39 69
|
||||
48 74 44 49 64
|
||||
65 25 22 46 93
|
||||
88 52 27 37 50
|
||||
19 35 47 54 67
|
||||
|
||||
44 32 71 13 57
|
||||
7 38 26 98 65
|
||||
46 1 21 8 55
|
||||
30 62 92 27 3
|
||||
69 50 99 85 11
|
||||
|
||||
86 6 64 34 97
|
||||
47 98 7 38 9
|
||||
26 68 75 92 54
|
||||
58 42 13 78 37
|
||||
85 28 81 16 51
|
||||
|
||||
82 74 15 4 86
|
||||
55 0 70 88 24
|
||||
50 79 63 40 21
|
||||
47 39 61 49 36
|
||||
89 16 13 2 37
|
||||
|
||||
89 19 9 82 13
|
||||
84 34 58 56 10
|
||||
27 92 46 4 94
|
||||
44 24 52 86 55
|
||||
39 23 22 99 5
|
||||
|
||||
65 92 8 86 77
|
||||
98 79 72 28 78
|
||||
16 23 3 55 48
|
||||
68 95 66 30 43
|
||||
50 31 15 11 45
|
||||
|
||||
32 70 25 59 31
|
||||
47 68 77 56 23
|
||||
66 78 54 88 50
|
||||
55 60 58 89 83
|
||||
84 99 86 97 95
|
||||
|
||||
53 46 1 94 87
|
||||
8 80 38 77 81
|
||||
17 51 47 19 69
|
||||
86 50 71 5 93
|
||||
61 66 36 58 0
|
||||
|
||||
90 58 17 29 92
|
||||
67 1 8 64 99
|
||||
63 22 57 19 68
|
||||
78 36 93 53 2
|
||||
27 48 62 39 14
|
||||
|
||||
8 49 22 90 54
|
||||
26 4 99 27 34
|
||||
78 25 11 85 28
|
||||
31 42 36 53 15
|
||||
64 75 60 45 35
|
||||
|
||||
99 84 26 53 90
|
||||
61 51 98 39 86
|
||||
47 37 52 80 63
|
||||
67 49 35 70 11
|
||||
32 45 94 73 43
|
||||
|
||||
91 92 74 94 32
|
||||
27 56 50 33 54
|
||||
67 46 35 25 10
|
||||
93 97 30 90 4
|
||||
57 15 69 83 39
|
||||
|
||||
71 68 74 81 11
|
||||
44 98 60 17 73
|
||||
43 40 32 38 39
|
||||
61 56 97 94 70
|
||||
23 2 86 91 54
|
||||
|
||||
19 98 93 42 88
|
||||
0 16 30 32 71
|
||||
89 86 81 76 68
|
||||
29 2 14 72 63
|
||||
7 27 67 59 1
|
||||
|
||||
24 18 28 98 95
|
||||
10 62 80 71 36
|
||||
3 89 20 63 46
|
||||
47 65 84 22 6
|
||||
82 19 81 38 45
|
||||
|
||||
54 85 67 34 79
|
||||
25 58 38 73 61
|
||||
72 98 4 19 40
|
||||
32 10 29 31 89
|
||||
15 33 5 7 63
|
||||
|
||||
49 48 71 81 88
|
||||
70 5 39 41 22
|
||||
19 20 7 75 23
|
||||
69 46 63 14 54
|
||||
80 45 94 6 55
|
||||
|
||||
88 62 76 78 95
|
||||
64 65 36 58 22
|
||||
7 21 98 93 42
|
||||
79 99 9 89 10
|
||||
6 5 33 92 72
|
||||
16
advent_of_code/2021/d4/Cargo.lock
generated
Normal file
16
advent_of_code/2021/d4/Cargo.lock
generated
Normal file
@@ -0,0 +1,16 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "d4"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"text_io",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "text_io"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ee74b5019b48991b09803402aaf9e65a053b3993fe9d9c475ab67a395358ba76"
|
||||
9
advent_of_code/2021/d4/Cargo.toml
Normal file
9
advent_of_code/2021/d4/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "d4"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
text_io = "0.1.9"
|
||||
185
advent_of_code/2021/d4/main.cpp
Normal file
185
advent_of_code/2021/d4/main.cpp
Normal file
@@ -0,0 +1,185 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string FILE_NAME = "4.txt";
|
||||
|
||||
struct Card {
|
||||
map<int, pair<int, int>> numbers = {};
|
||||
array<array<bool, 5>, 5> opens;
|
||||
|
||||
Card(){
|
||||
for(int x = 0; x < 5; ++x){
|
||||
opens[x].fill(true);
|
||||
}
|
||||
}
|
||||
|
||||
bool check(pair<int, int> coords){
|
||||
bool x_result = true, y_result = true;
|
||||
|
||||
for(int x = 0; x < 5; ++x){
|
||||
if(opens[x][coords.second]){
|
||||
x_result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for(int y = 0; y < 5; ++y){
|
||||
if(opens[coords.first][y]){
|
||||
y_result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return x_result || y_result;
|
||||
}
|
||||
|
||||
void insert(int n, pair<int, int> coords){
|
||||
numbers[n] = coords;
|
||||
}
|
||||
|
||||
int score(){
|
||||
int result = 0;
|
||||
|
||||
for(auto n : numbers){
|
||||
auto coords = n.second;
|
||||
|
||||
if(opens[coords.first][coords.second]){
|
||||
result += n.first;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool call(int n){
|
||||
bool result = false;
|
||||
|
||||
if(numbers.find(n) != numbers.end()){
|
||||
auto coords = numbers[n];
|
||||
opens[coords.first][coords.second] = false;
|
||||
|
||||
result = check(coords);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
vector<int> string_to_int_vector(const string& s){
|
||||
vector<int> result;
|
||||
int carry = 0;
|
||||
|
||||
for(auto c : s){
|
||||
if(c >= 48 && c <= 57){
|
||||
carry *= 10;
|
||||
carry += c - 48;
|
||||
} else{
|
||||
result.push_back(carry);
|
||||
carry = 0;
|
||||
}
|
||||
}
|
||||
result.push_back(carry);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(){
|
||||
ios::sync_with_stdio(0);
|
||||
cin.tie(0);
|
||||
|
||||
ifstream file_stream;
|
||||
file_stream.open(FILE_NAME);
|
||||
|
||||
if(!file_stream.is_open()){
|
||||
cerr << "File " << FILE_NAME << " not opened correctly\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
// the first line will always be the sequence of bingo calls
|
||||
string line_1;
|
||||
getline(file_stream, line_1);
|
||||
|
||||
auto calls = string_to_int_vector(line_1);
|
||||
|
||||
// The second line will always be empty and not parsed or whatever
|
||||
getline(file_stream, line_1);
|
||||
|
||||
vector<Card> bingo_cards;
|
||||
string cur_line;
|
||||
|
||||
while(getline(file_stream, cur_line)){
|
||||
Card card;
|
||||
|
||||
for(int i = 0; i < 5; ++i){
|
||||
istringstream ss(cur_line);
|
||||
|
||||
for(int j = 0; j < 5; ++j){
|
||||
int a;
|
||||
ss >> a;
|
||||
|
||||
card.insert(a, {i, j});
|
||||
}
|
||||
|
||||
getline(file_stream, cur_line);
|
||||
}
|
||||
|
||||
bingo_cards.push_back(card);
|
||||
}
|
||||
|
||||
bool found_result = false;
|
||||
|
||||
cout << "Best board score:" << endl;
|
||||
for(auto n : calls){
|
||||
for(auto& board: bingo_cards){
|
||||
bool r = board.call(n);
|
||||
if(r){
|
||||
cout << n << " * " << board.score() << " = " << n * board.score() << '\n';
|
||||
found_result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(found_result) break;
|
||||
}
|
||||
|
||||
// Now for part 2, find the worst board. So we go through all boards and see when they finish
|
||||
// Reset all boards first
|
||||
// Then go through all boards and do all calls on them and keep track of the #numbers needed
|
||||
|
||||
int worst_board = 0, calls_needed_worst_board = 0, current_board = 0;
|
||||
|
||||
for(auto& b : bingo_cards){
|
||||
for(int y = 0; y < 5; ++y){
|
||||
b.opens[y].fill(true);
|
||||
}
|
||||
|
||||
int calls_needed = 0;
|
||||
|
||||
for(auto c : calls){
|
||||
bool call_result = b.call(c);
|
||||
|
||||
if(call_result){
|
||||
if(calls_needed_worst_board < calls_needed){
|
||||
worst_board = current_board;
|
||||
calls_needed_worst_board = calls_needed;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
calls_needed++;
|
||||
}
|
||||
|
||||
current_board++;
|
||||
}
|
||||
|
||||
int last_call = calls[calls_needed_worst_board];
|
||||
int worst_board_score = bingo_cards[worst_board].score();
|
||||
|
||||
cout << "Worst board:" << endl;
|
||||
cout << last_call << " * " << worst_board_score << " = " << last_call * worst_board_score << endl;
|
||||
|
||||
cout << flush;
|
||||
|
||||
file_stream.close();
|
||||
return 0;
|
||||
}
|
||||
133
advent_of_code/2021/d4/src/main.rs
Normal file
133
advent_of_code/2021/d4/src/main.rs
Normal file
@@ -0,0 +1,133 @@
|
||||
use std::io::{self, BufRead};
|
||||
use std::path::Path;
|
||||
use std::fs::File;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use text_io::scan;
|
||||
|
||||
|
||||
// -- PART 1 --
|
||||
|
||||
// We playin bingo now
|
||||
// Input is first a sequence of integers
|
||||
// Followed by a number of bingo cards
|
||||
// Goal is to find the bingo card that has bingo first and calculate some metrics of it
|
||||
// Metrics are the last called number multiplied by sum of all numbers that were not needed for bingo
|
||||
|
||||
#[derive(Debug,PartialEq)]
|
||||
struct Card {
|
||||
numbers : HashMap<u64, (usize, usize)>,
|
||||
opens : [[bool; 5]; 5],
|
||||
score : u64,
|
||||
}
|
||||
|
||||
impl Card {
|
||||
fn new() -> Card {
|
||||
Card { numbers: HashMap::new(), opens: [[true; 5]; 5], score: 0 }
|
||||
}
|
||||
|
||||
fn call(&mut self, n: u64) -> Option<u64> {
|
||||
let search_result = self.numbers.get(&n);
|
||||
|
||||
match search_result {
|
||||
None => return None,
|
||||
Some(&coords) => return self.take(n, coords),
|
||||
}
|
||||
}
|
||||
|
||||
fn take(&mut self, n: u64, (x, y): (usize, usize)) -> Option<u64> {
|
||||
self.score -= n;
|
||||
self.opens[x][y] = false;
|
||||
|
||||
match self.check((x, y)) {
|
||||
false => return None,
|
||||
true => return Some(self.score),
|
||||
}
|
||||
}
|
||||
|
||||
fn check(&self, (x, y): (usize, usize)) -> bool {
|
||||
let mut result_x = true;
|
||||
let mut result_y = true;
|
||||
|
||||
for b in self.opens[x] {
|
||||
if b {
|
||||
result_x = false;
|
||||
}
|
||||
}
|
||||
|
||||
for i in 0..self.opens.len() {
|
||||
if self.opens[i][y] {
|
||||
result_y = false;
|
||||
}
|
||||
}
|
||||
|
||||
return result_x || result_y;
|
||||
}
|
||||
|
||||
fn insert(&mut self, n: u64, coords: (usize, usize)) -> () {
|
||||
self.numbers.insert(n, coords);
|
||||
self.score += n;
|
||||
}
|
||||
}
|
||||
|
||||
fn main(){
|
||||
println!("Advent of Code #4!\n");
|
||||
|
||||
let path = Path::new("./4.txt");
|
||||
let display = path.display();
|
||||
|
||||
let file = match File::open(&path) {
|
||||
Err(why) => panic!("Couldn't open {}: {}", display, why),
|
||||
Ok(file) => file,
|
||||
};
|
||||
|
||||
let mut lines = io::BufReader::new(file).lines();
|
||||
let mut numbers = Vec::<u32>::new();
|
||||
let mut bingo_cards = Vec::<Card>::new();
|
||||
|
||||
// let mut c = Card::new();
|
||||
// c.insert(1, (0, 1));
|
||||
// c.insert(2, (1, 1));
|
||||
// c.insert(3, (2, 1));
|
||||
// c.insert(4, (3, 1));
|
||||
// c.insert(5, (4, 1));
|
||||
|
||||
// assert_eq!(c.call(1), None);
|
||||
// assert_eq!(c.call(2), None);
|
||||
// assert_eq!(c.call(3), None);
|
||||
// assert_eq!(c.call(4), None);
|
||||
// assert_eq!(c.call(5), Some(0));
|
||||
|
||||
let first_line = lines.nth(0).unwrap().unwrap();
|
||||
let mut acc: u32 = 0;
|
||||
for c in first_line.chars() {
|
||||
if c != ',' && c != '\n' {
|
||||
acc *= 10;
|
||||
acc += c.to_digit(10).unwrap();
|
||||
} else{
|
||||
numbers.push(acc);
|
||||
acc = 0;
|
||||
}
|
||||
}
|
||||
|
||||
let mut n_start = 2;
|
||||
let mut bingo_start_line = lines.nth(n_start);
|
||||
|
||||
while !bingo_start_line.is_none() {
|
||||
let card = Card::new();
|
||||
|
||||
for i in 0..5 {
|
||||
let cur_line = lines.nth(n_start + i).unwrap().unwrap();
|
||||
let a: i32;
|
||||
let b: i32;
|
||||
let c: i32;
|
||||
let d: i32;
|
||||
let e: i32;
|
||||
scan!(cur_line.bytes() => "{} {} {} {} {}", a, b, c, d, e);
|
||||
println!("{} {} {} {} {}", a, b, c, d, e);
|
||||
}
|
||||
|
||||
n_start += 6;
|
||||
bingo_start_line = lines.nth(n_start);
|
||||
}
|
||||
}
|
||||
500
advent_of_code/2021/d5/5.txt
Normal file
500
advent_of_code/2021/d5/5.txt
Normal file
@@ -0,0 +1,500 @@
|
||||
445,187 -> 912,654
|
||||
820,46 -> 25,841
|
||||
216,621 -> 458,379
|
||||
955,898 -> 67,10
|
||||
549,572 -> 549,520
|
||||
796,107 -> 109,794
|
||||
729,698 -> 338,698
|
||||
11,987 -> 968,30
|
||||
381,840 -> 381,409
|
||||
80,467 -> 80,48
|
||||
132,197 -> 132,92
|
||||
343,96 -> 343,710
|
||||
42,854 -> 346,550
|
||||
503,56 -> 804,56
|
||||
599,206 -> 60,206
|
||||
702,920 -> 474,920
|
||||
496,790 -> 223,517
|
||||
969,579 -> 583,579
|
||||
897,66 -> 604,66
|
||||
484,754 -> 640,910
|
||||
330,49 -> 949,49
|
||||
908,132 -> 714,132
|
||||
517,153 -> 97,573
|
||||
317,865 -> 678,504
|
||||
800,61 -> 800,51
|
||||
179,242 -> 179,202
|
||||
529,757 -> 529,838
|
||||
288,953 -> 393,953
|
||||
372,15 -> 925,568
|
||||
100,85 -> 654,639
|
||||
663,562 -> 737,636
|
||||
576,981 -> 245,981
|
||||
347,240 -> 347,928
|
||||
91,119 -> 413,441
|
||||
637,397 -> 330,90
|
||||
534,257 -> 950,257
|
||||
155,636 -> 694,97
|
||||
539,274 -> 539,327
|
||||
329,795 -> 824,300
|
||||
103,51 -> 961,909
|
||||
87,868 -> 168,787
|
||||
21,250 -> 157,386
|
||||
591,316 -> 670,395
|
||||
722,670 -> 630,670
|
||||
28,167 -> 28,489
|
||||
480,210 -> 68,622
|
||||
573,700 -> 354,919
|
||||
186,59 -> 700,59
|
||||
121,186 -> 670,186
|
||||
797,985 -> 671,985
|
||||
836,804 -> 335,804
|
||||
701,835 -> 104,238
|
||||
456,718 -> 456,964
|
||||
694,891 -> 694,839
|
||||
205,637 -> 205,318
|
||||
40,958 -> 773,225
|
||||
151,391 -> 151,779
|
||||
693,894 -> 417,894
|
||||
418,700 -> 656,700
|
||||
196,985 -> 896,985
|
||||
357,509 -> 608,258
|
||||
552,558 -> 552,482
|
||||
184,412 -> 170,412
|
||||
122,746 -> 643,225
|
||||
268,930 -> 247,930
|
||||
979,820 -> 407,248
|
||||
755,893 -> 845,893
|
||||
706,843 -> 706,225
|
||||
162,726 -> 895,726
|
||||
140,888 -> 289,888
|
||||
614,432 -> 903,721
|
||||
272,725 -> 272,598
|
||||
529,672 -> 967,234
|
||||
903,989 -> 785,871
|
||||
422,355 -> 422,360
|
||||
313,722 -> 713,322
|
||||
460,121 -> 460,151
|
||||
55,944 -> 946,944
|
||||
795,744 -> 221,744
|
||||
816,953 -> 471,953
|
||||
865,186 -> 557,186
|
||||
94,976 -> 747,323
|
||||
302,961 -> 811,452
|
||||
361,966 -> 921,406
|
||||
197,988 -> 571,988
|
||||
310,905 -> 722,493
|
||||
699,91 -> 272,518
|
||||
295,306 -> 84,95
|
||||
220,116 -> 395,291
|
||||
183,364 -> 523,364
|
||||
16,986 -> 16,319
|
||||
54,980 -> 635,399
|
||||
340,110 -> 651,421
|
||||
788,76 -> 788,635
|
||||
933,375 -> 458,375
|
||||
12,434 -> 494,916
|
||||
253,892 -> 962,183
|
||||
240,508 -> 240,234
|
||||
763,934 -> 506,677
|
||||
308,135 -> 239,66
|
||||
117,649 -> 751,15
|
||||
95,535 -> 428,868
|
||||
16,937 -> 902,51
|
||||
547,404 -> 547,830
|
||||
128,581 -> 970,581
|
||||
959,810 -> 564,415
|
||||
971,738 -> 378,145
|
||||
919,210 -> 295,210
|
||||
737,43 -> 231,43
|
||||
82,577 -> 455,204
|
||||
821,337 -> 570,337
|
||||
688,753 -> 538,753
|
||||
891,844 -> 124,844
|
||||
74,957 -> 946,85
|
||||
43,942 -> 43,210
|
||||
100,391 -> 100,142
|
||||
975,527 -> 175,527
|
||||
510,844 -> 395,959
|
||||
558,231 -> 558,858
|
||||
839,915 -> 262,338
|
||||
784,290 -> 875,199
|
||||
644,824 -> 812,824
|
||||
899,657 -> 500,657
|
||||
263,668 -> 263,964
|
||||
157,374 -> 820,374
|
||||
530,301 -> 530,67
|
||||
15,688 -> 15,572
|
||||
216,844 -> 479,581
|
||||
973,59 -> 68,964
|
||||
104,92 -> 104,547
|
||||
421,472 -> 421,176
|
||||
887,805 -> 231,149
|
||||
140,980 -> 852,980
|
||||
248,602 -> 346,602
|
||||
334,961 -> 334,471
|
||||
892,892 -> 958,958
|
||||
270,83 -> 270,135
|
||||
950,105 -> 404,651
|
||||
979,476 -> 930,427
|
||||
416,430 -> 879,430
|
||||
796,937 -> 796,415
|
||||
670,679 -> 72,679
|
||||
733,884 -> 733,302
|
||||
745,196 -> 306,196
|
||||
174,353 -> 667,846
|
||||
285,978 -> 254,978
|
||||
10,63 -> 936,989
|
||||
242,107 -> 242,725
|
||||
238,341 -> 238,800
|
||||
975,102 -> 174,903
|
||||
530,474 -> 530,853
|
||||
931,47 -> 467,47
|
||||
86,141 -> 821,141
|
||||
263,15 -> 654,15
|
||||
688,542 -> 378,232
|
||||
826,793 -> 989,793
|
||||
729,285 -> 729,192
|
||||
587,915 -> 587,79
|
||||
548,667 -> 877,667
|
||||
15,836 -> 783,68
|
||||
662,673 -> 71,82
|
||||
312,681 -> 910,83
|
||||
760,418 -> 491,418
|
||||
175,502 -> 443,502
|
||||
817,878 -> 29,90
|
||||
798,569 -> 811,582
|
||||
703,141 -> 743,181
|
||||
941,849 -> 941,778
|
||||
63,24 -> 500,461
|
||||
697,183 -> 119,761
|
||||
705,672 -> 152,672
|
||||
150,567 -> 656,567
|
||||
158,411 -> 965,411
|
||||
702,872 -> 276,446
|
||||
141,179 -> 741,779
|
||||
533,886 -> 817,886
|
||||
569,949 -> 285,949
|
||||
699,764 -> 699,780
|
||||
333,863 -> 805,391
|
||||
861,804 -> 524,467
|
||||
791,501 -> 718,501
|
||||
976,265 -> 976,713
|
||||
129,342 -> 339,132
|
||||
322,738 -> 212,738
|
||||
700,534 -> 622,456
|
||||
68,314 -> 14,314
|
||||
146,112 -> 215,181
|
||||
170,211 -> 482,211
|
||||
159,412 -> 159,32
|
||||
312,939 -> 312,95
|
||||
232,18 -> 912,698
|
||||
950,114 -> 950,826
|
||||
620,848 -> 620,11
|
||||
624,288 -> 544,208
|
||||
752,479 -> 752,577
|
||||
784,796 -> 784,872
|
||||
130,55 -> 974,899
|
||||
434,82 -> 434,481
|
||||
988,230 -> 892,134
|
||||
159,252 -> 159,291
|
||||
462,14 -> 462,977
|
||||
553,981 -> 553,390
|
||||
231,936 -> 51,936
|
||||
58,759 -> 60,759
|
||||
572,891 -> 584,891
|
||||
705,303 -> 124,303
|
||||
144,894 -> 970,68
|
||||
865,275 -> 865,956
|
||||
492,491 -> 470,491
|
||||
971,15 -> 977,15
|
||||
750,521 -> 33,521
|
||||
913,947 -> 387,421
|
||||
368,677 -> 570,677
|
||||
795,186 -> 882,186
|
||||
404,840 -> 678,840
|
||||
187,488 -> 403,488
|
||||
824,706 -> 642,706
|
||||
330,541 -> 330,195
|
||||
564,531 -> 774,531
|
||||
271,857 -> 20,606
|
||||
976,975 -> 976,843
|
||||
323,341 -> 21,39
|
||||
575,643 -> 267,643
|
||||
827,295 -> 827,854
|
||||
749,486 -> 749,780
|
||||
656,716 -> 656,470
|
||||
635,187 -> 417,187
|
||||
503,488 -> 503,393
|
||||
592,688 -> 592,567
|
||||
515,408 -> 128,795
|
||||
608,158 -> 780,158
|
||||
677,96 -> 11,762
|
||||
127,452 -> 339,452
|
||||
117,985 -> 291,811
|
||||
157,371 -> 157,916
|
||||
640,758 -> 983,758
|
||||
906,413 -> 906,776
|
||||
224,842 -> 627,439
|
||||
903,728 -> 903,459
|
||||
358,138 -> 822,602
|
||||
30,16 -> 929,915
|
||||
440,900 -> 294,900
|
||||
809,73 -> 987,73
|
||||
55,410 -> 304,161
|
||||
441,672 -> 315,672
|
||||
939,40 -> 234,40
|
||||
334,698 -> 309,698
|
||||
572,738 -> 572,226
|
||||
445,71 -> 445,468
|
||||
225,660 -> 427,458
|
||||
390,320 -> 449,320
|
||||
507,635 -> 507,169
|
||||
47,116 -> 738,807
|
||||
127,14 -> 689,14
|
||||
316,760 -> 316,432
|
||||
831,101 -> 250,682
|
||||
370,807 -> 370,898
|
||||
678,186 -> 491,186
|
||||
866,83 -> 539,83
|
||||
518,848 -> 518,962
|
||||
188,135 -> 81,28
|
||||
378,226 -> 597,226
|
||||
646,534 -> 141,534
|
||||
275,672 -> 275,854
|
||||
67,421 -> 676,421
|
||||
386,323 -> 988,323
|
||||
903,984 -> 10,91
|
||||
37,348 -> 529,840
|
||||
872,134 -> 358,648
|
||||
42,826 -> 42,822
|
||||
688,922 -> 21,922
|
||||
47,539 -> 942,539
|
||||
739,483 -> 375,847
|
||||
23,217 -> 800,217
|
||||
589,512 -> 589,953
|
||||
292,229 -> 107,229
|
||||
873,678 -> 873,770
|
||||
794,295 -> 739,240
|
||||
464,559 -> 936,559
|
||||
685,736 -> 368,736
|
||||
114,941 -> 114,307
|
||||
571,643 -> 74,643
|
||||
281,185 -> 273,177
|
||||
497,937 -> 497,469
|
||||
152,815 -> 702,815
|
||||
76,43 -> 980,947
|
||||
272,149 -> 101,149
|
||||
934,945 -> 107,118
|
||||
532,476 -> 759,476
|
||||
955,942 -> 397,942
|
||||
31,918 -> 931,18
|
||||
790,420 -> 389,420
|
||||
36,496 -> 215,317
|
||||
252,209 -> 139,209
|
||||
704,148 -> 719,133
|
||||
413,571 -> 165,571
|
||||
690,433 -> 864,607
|
||||
976,417 -> 517,876
|
||||
803,568 -> 443,568
|
||||
335,558 -> 335,334
|
||||
405,807 -> 691,521
|
||||
194,482 -> 486,190
|
||||
377,856 -> 377,802
|
||||
313,842 -> 313,254
|
||||
449,961 -> 198,710
|
||||
197,916 -> 197,797
|
||||
82,965 -> 959,88
|
||||
371,239 -> 829,697
|
||||
471,70 -> 596,70
|
||||
835,144 -> 835,950
|
||||
283,486 -> 506,486
|
||||
147,29 -> 147,747
|
||||
187,485 -> 187,195
|
||||
781,144 -> 480,144
|
||||
801,839 -> 925,715
|
||||
415,960 -> 415,442
|
||||
877,939 -> 29,91
|
||||
22,118 -> 22,439
|
||||
460,315 -> 450,315
|
||||
982,960 -> 71,49
|
||||
105,231 -> 105,331
|
||||
98,174 -> 551,174
|
||||
721,978 -> 38,295
|
||||
167,290 -> 167,133
|
||||
218,158 -> 218,908
|
||||
819,812 -> 758,812
|
||||
123,92 -> 123,132
|
||||
66,721 -> 66,906
|
||||
478,441 -> 967,930
|
||||
284,58 -> 464,58
|
||||
958,15 -> 37,936
|
||||
310,337 -> 359,288
|
||||
212,763 -> 212,373
|
||||
101,279 -> 101,267
|
||||
622,409 -> 106,925
|
||||
318,657 -> 318,432
|
||||
938,631 -> 938,650
|
||||
142,881 -> 254,881
|
||||
848,987 -> 848,451
|
||||
686,223 -> 481,223
|
||||
124,248 -> 812,248
|
||||
246,267 -> 246,148
|
||||
96,670 -> 324,442
|
||||
645,888 -> 385,628
|
||||
417,555 -> 417,858
|
||||
543,495 -> 543,150
|
||||
73,350 -> 440,717
|
||||
459,704 -> 459,179
|
||||
871,493 -> 871,764
|
||||
911,34 -> 64,881
|
||||
544,791 -> 703,791
|
||||
447,218 -> 62,218
|
||||
202,649 -> 396,649
|
||||
935,916 -> 55,36
|
||||
124,408 -> 477,761
|
||||
608,850 -> 484,850
|
||||
935,876 -> 582,876
|
||||
377,612 -> 269,612
|
||||
413,727 -> 365,679
|
||||
64,451 -> 850,451
|
||||
684,807 -> 357,807
|
||||
323,364 -> 372,364
|
||||
887,300 -> 419,300
|
||||
837,831 -> 837,927
|
||||
294,255 -> 768,729
|
||||
878,23 -> 141,760
|
||||
36,627 -> 157,627
|
||||
824,703 -> 824,968
|
||||
356,109 -> 657,109
|
||||
799,266 -> 313,752
|
||||
71,600 -> 650,21
|
||||
564,863 -> 564,54
|
||||
36,720 -> 109,720
|
||||
318,488 -> 682,488
|
||||
249,350 -> 979,350
|
||||
560,502 -> 255,502
|
||||
132,327 -> 132,246
|
||||
287,906 -> 791,906
|
||||
818,110 -> 818,882
|
||||
937,17 -> 113,841
|
||||
50,710 -> 673,87
|
||||
702,952 -> 702,533
|
||||
666,552 -> 611,552
|
||||
612,962 -> 112,462
|
||||
260,529 -> 351,529
|
||||
440,313 -> 440,663
|
||||
605,341 -> 405,141
|
||||
277,287 -> 461,287
|
||||
268,890 -> 268,92
|
||||
764,526 -> 877,639
|
||||
165,697 -> 832,697
|
||||
240,716 -> 801,155
|
||||
872,429 -> 578,429
|
||||
88,816 -> 338,816
|
||||
981,881 -> 981,138
|
||||
457,351 -> 457,679
|
||||
850,526 -> 850,447
|
||||
139,449 -> 165,449
|
||||
127,544 -> 127,934
|
||||
160,890 -> 745,305
|
||||
526,113 -> 303,336
|
||||
17,500 -> 17,621
|
||||
796,311 -> 181,926
|
||||
260,218 -> 787,218
|
||||
536,989 -> 536,261
|
||||
257,826 -> 257,180
|
||||
531,37 -> 531,493
|
||||
961,942 -> 206,187
|
||||
536,668 -> 536,868
|
||||
154,967 -> 154,931
|
||||
808,317 -> 808,873
|
||||
487,258 -> 599,258
|
||||
59,962 -> 802,219
|
||||
322,945 -> 322,837
|
||||
378,973 -> 33,628
|
||||
668,556 -> 691,556
|
||||
819,728 -> 787,728
|
||||
484,261 -> 484,874
|
||||
333,271 -> 278,271
|
||||
733,515 -> 741,523
|
||||
775,854 -> 523,602
|
||||
67,215 -> 616,215
|
||||
951,685 -> 951,433
|
||||
372,105 -> 372,494
|
||||
917,788 -> 917,23
|
||||
890,584 -> 245,584
|
||||
748,276 -> 893,276
|
||||
733,721 -> 733,747
|
||||
225,908 -> 897,908
|
||||
437,140 -> 423,140
|
||||
456,513 -> 136,833
|
||||
413,135 -> 413,596
|
||||
143,245 -> 879,981
|
||||
870,639 -> 942,639
|
||||
28,175 -> 696,843
|
||||
393,303 -> 393,197
|
||||
169,986 -> 458,986
|
||||
43,44 -> 952,953
|
||||
236,405 -> 60,229
|
||||
266,845 -> 292,845
|
||||
529,98 -> 95,532
|
||||
95,658 -> 695,658
|
||||
368,454 -> 112,710
|
||||
506,776 -> 662,776
|
||||
928,494 -> 604,170
|
||||
179,138 -> 900,859
|
||||
45,560 -> 408,197
|
||||
655,654 -> 37,36
|
||||
56,432 -> 56,456
|
||||
844,614 -> 844,898
|
||||
240,191 -> 240,112
|
||||
639,911 -> 213,911
|
||||
47,887 -> 830,104
|
||||
57,50 -> 977,970
|
||||
899,928 -> 111,928
|
||||
962,676 -> 962,518
|
||||
129,585 -> 469,245
|
||||
988,775 -> 988,553
|
||||
417,344 -> 842,769
|
||||
468,110 -> 506,72
|
||||
687,204 -> 687,345
|
||||
828,553 -> 765,490
|
||||
75,894 -> 75,93
|
||||
26,798 -> 11,783
|
||||
967,44 -> 967,478
|
||||
240,481 -> 947,481
|
||||
794,254 -> 162,254
|
||||
502,944 -> 812,944
|
||||
331,417 -> 410,417
|
||||
850,275 -> 850,980
|
||||
671,130 -> 671,941
|
||||
240,99 -> 240,381
|
||||
771,399 -> 318,399
|
||||
946,11 -> 28,929
|
||||
731,939 -> 824,846
|
||||
268,71 -> 832,635
|
||||
968,37 -> 968,642
|
||||
935,365 -> 515,365
|
||||
199,792 -> 932,792
|
||||
32,116 -> 371,116
|
||||
324,67 -> 941,67
|
||||
453,181 -> 453,128
|
||||
958,982 -> 115,139
|
||||
962,168 -> 154,976
|
||||
474,215 -> 333,215
|
||||
458,675 -> 458,315
|
||||
577,302 -> 300,302
|
||||
704,493 -> 704,876
|
||||
887,549 -> 887,439
|
||||
81,328 -> 724,328
|
||||
575,490 -> 670,490
|
||||
576,17 -> 576,218
|
||||
21,46 -> 963,988
|
||||
532,235 -> 532,615
|
||||
796,213 -> 796,407
|
||||
55,948 -> 980,23
|
||||
775,471 -> 272,471
|
||||
26,138 -> 344,138
|
||||
635,518 -> 915,518
|
||||
727,365 -> 727,216
|
||||
175
advent_of_code/2021/d5/main.cpp
Normal file
175
advent_of_code/2021/d5/main.cpp
Normal file
@@ -0,0 +1,175 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string FILE_NAME = "5.txt";
|
||||
|
||||
// First task: given line segments, find the number of intersected points
|
||||
// So don't count multiple intersections on the same point multiple times
|
||||
|
||||
|
||||
struct Point{
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
using Line_Segment = pair<Point, Point>;
|
||||
|
||||
istream &operator>> ( istream &is, char const &c ) {
|
||||
istream::sentry sentry (is);
|
||||
if (sentry) { if ( is.peek() == c ) is.get(); else is.setstate(ios_base::failbit); }
|
||||
return is;
|
||||
}
|
||||
|
||||
istream &operator>> ( istream &is, char const *s ) {
|
||||
istream::sentry sentry (is);
|
||||
if (sentry) {
|
||||
for ( char c; is && (c = *s) != 0; ++s )
|
||||
if ( c == is.peek() ) is.get(); else is.setstate(ios_base::failbit);
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
istream &operator>> (istream &in, Point &p){
|
||||
return in >> p.x >> ',' >> p.y;
|
||||
}
|
||||
|
||||
ostream &operator<< (ostream &out, Point const &p){
|
||||
return out << p.x << ',' << p.y;
|
||||
}
|
||||
|
||||
bool operator< (const Point &l, const Point &r){
|
||||
return l.x < r.x || (l.x == r.x && l.y < r.y);
|
||||
}
|
||||
|
||||
istream &operator>> (istream &in, Line_Segment &l){
|
||||
return in >> l.first >> "->" >> l.second;
|
||||
}
|
||||
|
||||
ostream &operator<< (ostream &out, Line_Segment const &l){
|
||||
return out << l.first << " -> " << l.second;
|
||||
}
|
||||
|
||||
int main(){
|
||||
ios::sync_with_stdio(0);
|
||||
cin.tie(0);
|
||||
|
||||
ifstream file_stream;
|
||||
|
||||
file_stream.open(FILE_NAME);
|
||||
if(!file_stream.is_open()){
|
||||
cerr << "File " << FILE_NAME << " not found\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
cin.rdbuf(file_stream.rdbuf());
|
||||
|
||||
vector<Line_Segment> lines;
|
||||
while(cin){
|
||||
Line_Segment l;
|
||||
cin >> l;
|
||||
cin >> ws;
|
||||
lines.push_back(l);
|
||||
|
||||
cin.peek();
|
||||
}
|
||||
|
||||
// for(auto &l : lines){
|
||||
// cout << l << '\n';
|
||||
// }
|
||||
|
||||
int result = 0;
|
||||
map<Point, int> line_points;
|
||||
|
||||
for(auto const &line : lines){
|
||||
Point p1 = line.first, p2 = line.second;
|
||||
|
||||
if(p1.x == p2.x){
|
||||
if(p1.y > p2.y){
|
||||
swap(p1, p2);
|
||||
}
|
||||
|
||||
for(int i = p1.y; i <= p2.y; ++i){
|
||||
line_points[p1]++;
|
||||
p1.y++;
|
||||
}
|
||||
} else if(p1.y == p2.y){
|
||||
if(p1.x > p2.x){
|
||||
swap(p1, p2);
|
||||
}
|
||||
|
||||
for(int i = p1.x; i <= p2.x; ++i){
|
||||
line_points[p1]++;
|
||||
p1.x++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(auto const &lp : line_points){
|
||||
if(lp.second > 1){
|
||||
result++;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Result part 1: " << result << endl;
|
||||
|
||||
line_points.clear();
|
||||
result = 0;
|
||||
|
||||
for(auto const &line : lines){
|
||||
Point p1 = line.first, p2 = line.second;
|
||||
|
||||
if(p1.x == p2.x){
|
||||
if(p1.y > p2.y){
|
||||
swap(p1, p2);
|
||||
}
|
||||
|
||||
for(int i = p1.y; i <= p2.y; ++i){
|
||||
line_points[p1]++;
|
||||
p1.y++;
|
||||
}
|
||||
} else if(p1.y == p2.y){
|
||||
if(p1.x > p2.x){
|
||||
swap(p1, p2);
|
||||
}
|
||||
|
||||
for(int i = p1.x; i <= p2.x; ++i){
|
||||
line_points[p1]++;
|
||||
p1.x++;
|
||||
}
|
||||
} else{
|
||||
if(p1.x > p2.x){
|
||||
swap(p1, p2);
|
||||
}
|
||||
|
||||
int dif = p2.x - p1.x;
|
||||
|
||||
if(p1.y > p2.y){
|
||||
for(int i = 0; i <= dif; ++i){
|
||||
line_points[p1]++;
|
||||
p1.y--;
|
||||
p1.x++;
|
||||
}
|
||||
} else{
|
||||
for(int i = 0; i <= dif; ++i){
|
||||
line_points[p1]++;
|
||||
p1.y++;
|
||||
p1.x++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(auto const &lp : line_points){
|
||||
if(lp.second > 1){
|
||||
result++;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Result part 2: " << result << endl;
|
||||
|
||||
file_stream.close();
|
||||
cout << flush;
|
||||
|
||||
return 0;
|
||||
}
|
||||
111
advent_of_code/2021/d6/main.cpp
Normal file
111
advent_of_code/2021/d6/main.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef unsigned long long ll;
|
||||
|
||||
size_t reproduction(int stage, int days){
|
||||
vector<int> fishies(1, stage);
|
||||
vector<int> new_fishies;
|
||||
|
||||
for(int i = 0; i < days; ++i){
|
||||
new_fishies.clear();
|
||||
|
||||
for(auto& fishie : fishies){
|
||||
if(fishie == 0){
|
||||
new_fishies.push_back(6);
|
||||
new_fishies.push_back(8);
|
||||
} else{
|
||||
new_fishies.push_back(--fishie);
|
||||
}
|
||||
}
|
||||
|
||||
fishies.clear();
|
||||
fishies = new_fishies;
|
||||
|
||||
// cout << "Day " << i << ": ";
|
||||
// for(auto fishie : new_fishies){
|
||||
// cout << fishie << " ";
|
||||
// } cout << endl;
|
||||
}
|
||||
|
||||
return fishies.size();
|
||||
}
|
||||
|
||||
ll smart_reproduce(int days, const array<ll, 9> &start){
|
||||
array<ll, 9> old_fishies = start;
|
||||
array<ll, 9> new_fishies;
|
||||
|
||||
while(days > 0){
|
||||
for(size_t i = 0; i < old_fishies.size(); ++i){
|
||||
if(i == 8){
|
||||
new_fishies[i] = old_fishies[0];
|
||||
} else if(i == 6){
|
||||
new_fishies[i] = old_fishies[i + 1] + old_fishies[0];
|
||||
} else{
|
||||
new_fishies[i] = old_fishies[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
old_fishies = new_fishies;
|
||||
days--;
|
||||
}
|
||||
|
||||
ll result = 0;
|
||||
for(auto &fishie : old_fishies){
|
||||
result += fishie;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(){
|
||||
ios::sync_with_stdio(0);
|
||||
cin.tie(0);
|
||||
|
||||
vector<int> input_fishies = {5,1,1,3,1,1,5,1,2,1,5,2,5,1,1,1,4,1,1,5,1,1,4,1,1,1,3,5,1,1,1,1,1,1,1,1,1,4,4,4,1,1,1,1,1,4,1,1,1,1,1,5,1,1,1,4,1,1,1,1,1,3,1,1,4,1,4,1,1,2,3,1,1,1,1,4,1,2,2,1,1,1,1,1,1,3,1,1,1,1,1,2,1,1,1,1,1,1,1,4,4,1,4,2,1,1,1,1,1,4,3,1,1,1,1,2,1,1,1,2,1,1,3,1,1,1,2,1,1,1,3,1,3,1,1,1,1,1,1,1,1,1,3,1,1,1,1,3,1,1,1,1,1,1,2,1,1,2,3,1,2,1,1,4,1,1,5,3,1,1,1,2,4,1,1,2,4,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,4,3,1,2,1,2,1,5,1,2,1,1,5,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,1,1,1,1,1,3,1,1,5,1,1,1,1,5,1,4,1,1,1,4,1,3,4,1,4,1,1,1,1,1,1,1,1,1,3,5,1,3,1,1,1,1,4,1,5,3,1,1,1,1,1,5,1,1,1,2,2};
|
||||
// vector<int> input_fishies = {3,4,3,1,2};
|
||||
array<ll, 9> reproduction_numbers;
|
||||
reproduction_numbers.fill(0);
|
||||
|
||||
ll result = 0;
|
||||
for(auto &fishie : input_fishies){
|
||||
if(reproduction_numbers[fishie] == 0){
|
||||
int rn = reproduction(fishie, 80);
|
||||
result += rn;
|
||||
reproduction_numbers[fishie] = rn;
|
||||
} else{
|
||||
result += reproduction_numbers[fishie];
|
||||
}
|
||||
}
|
||||
|
||||
cout << result << endl;
|
||||
|
||||
// Now do the exact same but with a bigger number of days
|
||||
|
||||
// Ok this doesn't really work out, it uses up too much memory, prolly because it's pretty exponential
|
||||
// So it's probably better to do this with a bottoms up approach
|
||||
|
||||
// Doing a problem analysis with example I came to the conclusion that you only need to calculate one
|
||||
// starting point, with 0 then being the obvious choice. The rest can be derived from that
|
||||
|
||||
// Also, I realized that instead of keeping track of all lanternfish numbers in a vector,
|
||||
// I can just use a map or array or vector tracking the number of fishes with the index indicating the stage
|
||||
// This reduces the memory size drastically, while still being just iterative
|
||||
|
||||
// First let's test the new method: works just dandy
|
||||
// Only problem now is overflows I think
|
||||
|
||||
array<ll, 9> start = {0, };
|
||||
for(auto &fish : input_fishies){
|
||||
start[fish]++;
|
||||
}
|
||||
|
||||
result = smart_reproduce(256, start);
|
||||
|
||||
cout << result << endl;
|
||||
|
||||
cout << flush;
|
||||
|
||||
return 0;
|
||||
}
|
||||
76
advent_of_code/2021/d7/main.cpp
Normal file
76
advent_of_code/2021/d7/main.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// vector<int> crab_pos = {16,1,2,0,4,2,7,1,2,14};
|
||||
vector<int> crab_pos = {1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,334,7,6,1172,110,184,1322,1248,516,0,259,331,1291,51,753,878,27,664,859,391,41,43,317,884,529,168,502,1236,908,1203,676,349,117,268,364,668,256,216,435,296,568,298,600,53,775,3,782,387,305,1648,254,1477,488,234,349,1410,55,456,202,394,29,578,400,188,1172,306,375,320,561,167,1078,643,279,339,12,1004,290,26,49,359,139,218,129,139,742,239,290,471,1899,1446,1046,657,895,90,196,467,1105,723,269,638,72,363,199,704,769,658,124,1001,1160,1150,806,154,407,238,155,499,1170,779,156,1041,728,124,349,397,916,24,1079,1275,222,1852,180,24,126,475,228,464,5,133,130,1119,108,895,396,1285,670,52,107,714,58,1143,714,144,1193,1754,157,802,1,1000,1001,1116,823,256,746,16,281,341,1363,233,26,46,474,91,1035,422,681,6,316,1564,31,162,194,116,904,1223,192,1552,74,1282,398,776,343,187,45,193,1311,111,161,27,660,1283,124,215,331,158,35,65,852,290,1199,285,555,630,309,375,8,301,32,706,74,722,303,474,857,12,211,54,391,171,66,1373,931,1272,294,619,470,75,195,40,386,98,127,332,183,741,1502,383,714,148,213,1053,130,491,66,1010,820,48,867,1462,901,866,1110,171,1095,147,206,1220,1242,839,1187,90,918,399,262,821,52,159,3,750,1464,1772,128,43,888,225,488,1257,281,597,321,281,1240,821,205,384,66,331,23,241,327,348,122,218,867,114,246,19,698,59,1218,28,61,238,135,773,64,54,401,297,1148,465,159,1339,45,650,702,861,0,340,5,129,80,171,55,135,1021,1420,22,330,199,0,288,1318,725,147,233,586,1530,268,512,19,94,497,389,530,1852,102,1175,38,1053,551,971,65,1254,32,409,756,531,2,758,678,351,9,788,590,176,1367,389,495,1608,489,75,341,198,68,27,3,109,635,457,381,571,978,16,359,1474,668,80,133,1337,608,1087,96,690,9,375,1012,1016,173,71,1482,64,1334,231,439,670,1092,1327,1637,1971,174,174,367,594,827,1497,296,128,13,348,780,1682,202,1060,191,1602,83,1437,91,519,601,242,179,70,158,84,220,869,1363,1119,648,185,162,961,281,479,621,13,1303,208,1117,373,141,995,584,82,994,1032,899,654,762,606,532,1108,277,89,515,422,696,275,1693,93,864,146,56,352,182,439,6,331,39,83,23,1880,724,87,59,129,889,9,1598,57,215,876,25,837,323,403,1229,761,34,1014,7,65,3,238,588,327,1307,1069,1272,166,162,1613,92,169,16,19,272,613,1069,861,452,1070,330,667,660,49,1178,183,600,1038,290,857,1505,1029,146,504,284,175,73,485,909,615,251,176,843,394,934,1056,71,290,199,258,67,4,404,912,10,1564,807,865,6,550,1022,506,988,195,480,13,981,18,220,156,341,491,220,326,480,1251,235,200,1398,179,196,187,316,1299,997,504,104,189,419,347,458,172,48,744,88,56,183,233,337,1107,475,337,54,753,49,33,612,301,157,73,261,1122,304,352,264,265,879,13,96,1153,262,40,653,196,326,1523,394,79,297,10,1326,21,411,789,553,4,712,123,392,286,671,141,28,168,544,9,537,490,396,1210,1800,354,228,35,1217,784,223,551,290,386,106,152,309,2,777,558,813,380,184,187,629,841,233,1027,823,1104,205,1528,981,197,674,517,1537,512,175,1117,730,397,803,1380,216,611,142,85,680,35,22,598,1530,44,236,265,358,1227,1189,1831,886,107,314,325,219,724,619,862,0,146,1358,172,94,53,225,648,152,583,666,927,99,394,150,113,959,528,1233,65,1408,181,114,38,550,282,742,936,1278,748,178,344,180,375,1003,97,623,98,309,248,277,902,466,1298,878,804,2,697,509,809,847,133,1215,573,1497,255,99,58,605,841,849,22,120,409,339,937,1333,943,823,1146,435,454,264,26,25,848,225,139,208,1125,12,242,241,2,382,258,579,5,1194,402,1069,874,1302,191,307,313,643,1080,505,1032,1099,267,160,821,520,55,30,362,77,270,648,177,805,977,464,872,326,1176,102,47,1254,92,217,399,184,283,770,282,1488,456,691,508,243,856,112,294,948,938,27,91,262,549,165,985,21,968,380,143,118,365,1562,318,610,1167,112,750,279,212,710,331,1022,1143,1566,17,357,672,1391,877,1051,105,21,219,1392,78,857,223,676,350,117,82,703,460,1332,129,235,25,133,195,474,791,223,170,1482,139,63,967,286,73,343,230,551,1035,44,1428,1589,542,592,7};
|
||||
|
||||
|
||||
int fuel_cost(int n){
|
||||
int result = 0;
|
||||
for(auto &crab : crab_pos){
|
||||
result += abs(crab - n);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int updated_fuel_cost(int n){
|
||||
int result = 0;
|
||||
|
||||
for(auto &crab : crab_pos){
|
||||
int dif = abs(crab - n);
|
||||
result += dif * (dif + 1) / 2;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(){
|
||||
ios::sync_with_stdio(0);
|
||||
cin.tie(0);
|
||||
|
||||
sort(crab_pos.begin(), crab_pos.end());
|
||||
|
||||
int upper = crab_pos.back(), lower = crab_pos.front();
|
||||
int m1 = (upper + lower) / 2, m2 = (upper + lower) / 2 + 1;
|
||||
|
||||
while(upper - lower > 1){
|
||||
if(fuel_cost(m1) < fuel_cost(m2)){
|
||||
upper = m1;
|
||||
} else{
|
||||
lower = m2;
|
||||
}
|
||||
|
||||
m1 = (upper + lower) / 2;
|
||||
m2 = (upper + lower) / 2 + 1;
|
||||
}
|
||||
|
||||
int target_coord = min(m2, m1);
|
||||
cout << fuel_cost(target_coord) << endl;
|
||||
|
||||
upper = crab_pos.back();
|
||||
lower = crab_pos.front();
|
||||
m1 = (upper + lower) / 2;
|
||||
m2 = (upper + lower) / 2 + 1;
|
||||
|
||||
while(upper - lower > 1){
|
||||
if(updated_fuel_cost(m1) < updated_fuel_cost(m2)){
|
||||
upper = m1;
|
||||
} else{
|
||||
lower = m2;
|
||||
}
|
||||
|
||||
m1 = (upper + lower) / 2;
|
||||
m2 = (upper + lower) / 2 + 1;
|
||||
|
||||
// cout << m1 << ", fuel: " << updated_fuel_cost(m1) << " " << m2 << ", fuel: " << updated_fuel_cost(m2) << endl;
|
||||
}
|
||||
|
||||
target_coord = min(m1, m2);
|
||||
cout << updated_fuel_cost(target_coord) << endl;
|
||||
|
||||
cout << flush;
|
||||
|
||||
return 0;
|
||||
}
|
||||
200
advent_of_code/2021/d8/8.txt
Normal file
200
advent_of_code/2021/d8/8.txt
Normal file
@@ -0,0 +1,200 @@
|
||||
ceb bgfdea febgc ec eadcgfb eagbcd fcdebg dcef gafbc egdbf | fdbgec fedbg gdabefc gefbd
|
||||
af cegdabf cfdge ecdbfg dcfga edafgc cfa cabedf gdbac afge | cgdab bcagd badecgf fa
|
||||
bgacfe cbedgf degcabf dfgcba gdcfb dfeca bgda cadbf bca ba | cab fbdeagc dcfbg cfgdb
|
||||
dg fadgceb dacbef agfeb gcdbef edcbf gdf ecgd cgbadf defbg | bedcf bgdfac cbfedg abfeg
|
||||
fedga gebfc cade gacef afc agdbfc degbcaf gdacfe gdefab ac | dacbgf gacfe faegcd decfag
|
||||
gecbd fbcdg ebgdcf dcfbag dec ebdgfac cebga de efdb acdefg | afbgdc edbf de ed
|
||||
cdegfb bgdafec gabfec cef afbe eagfc gecab agcdf gbadce fe | efc egacbf gecbfd cfage
|
||||
dac aedb fadcge gfebac cfeba abecfd bdafc dbcgf edgabfc ad | acgefd afbce bcfeag cad
|
||||
agbcfd acedf adcgeb fegb egdcfab agbec eagfc gaf fg bfeagc | acefbdg fag afbcgd fga
|
||||
ceagdb eabg cfadge dgfceb dgb aedcg gb gfeadbc bcdag dcfba | cfedag bg ebcagd ebcagd
|
||||
gacdbf cdfbg cb gfacd gbfde dagefc febcdag gbc eacbgd bcaf | egbdf cefadg adgcfb cbgdfa
|
||||
deacbg bfdgc efcbda fcgbed afbdg gcef cebgadf begdc cfb fc | cgfe gfec cfb bfc
|
||||
fe egbda fbaeg egf gbdcfae efbd agbdfe bgafc gdbcae gefcda | feagb gdbae debf acfdeg
|
||||
dfbace abd dagfbec abfcg cadegf begd degca bd dcabg gcabed | gbadc fgeacd gcabfed cfdeba
|
||||
bg fgb fcadgb cfdeabg ebcdgf gfdce gcfbe fcabe dgeacf dgbe | bg fcbeg cfgebd fcbaegd
|
||||
gefc cfgbade fg eagfd aecgd efdcag ebfad geacbd bagcdf fga | gf gefadc dgface bfaed
|
||||
cfabg dfcgea fgc cdbf dcabg fc cfdabg cbdgae fdcgeba gefba | dgaecf gfc acbdg fgc
|
||||
ad adc adcbge afbcd bcfae gdfcb cbdgfae fcedab cafbeg defa | adc bgfcd fabdc dcfbea
|
||||
fcaegdb agb efcbag bdcg bacfde afbcgd acfdb bgafd gafed bg | cbedgfa ebdfgca gfead cdbg
|
||||
gb egb gecfbd dgbc cbfge efbgadc efdcb ebfdac gdeafb fgcea | edfgcab bdcg gebfcd dcefb
|
||||
dca febdc bfaegc cgfdae agefcdb ebcdag ad adgf eagcf feadc | egcaf bgaefcd fcgade cdebf
|
||||
cegfd ecdb egafc fde bfdceg bgcfd dgeafcb cbadfg edfgab de | aegfc bcagdf eafbgd fbcdg
|
||||
gfdb gdafeb dceabg bd becfa dbfea degaf cfgeda gbeacdf abd | agdefb ceadgb fgaecd dfbage
|
||||
ba afcegb degac gaebc gedfbca cafb fgecdb bae feadgb gfbce | fbac bafdge ebdcgf aecgd
|
||||
cbefda gcbf efg bdfce dbefg bcfegad cefdga gbdea fbgedc gf | bfdcge ecadbf cdefb gf
|
||||
acbfd adefgcb adefbc dag bgfda gdcf egbaf dg acbdeg abcdfg | decbga cdafgb cedgafb dag
|
||||
fedabg cfgabe cedf adceb eca beadcf agbcefd afdeb adgcb ec | cegafb gecdabf ebcad dbgcfea
|
||||
cadefg dgb bd adbgef fdbcgea gfcdba fagde gbcef dabe degfb | agcdfe baed bgecf db
|
||||
gcdf efdcga afecg cgaefb bfagedc efgda def df cfaebd baegd | cdfaeg gfaecb cdaebfg fecag
|
||||
edfcg fgcea faed dge gdbaec bfgaec de fecgda dgecbfa bcdfg | defa gafdec acefg ged
|
||||
cgabe cedaf faecdb gcefda cfb bf fbcae gdeafbc gdfcab debf | gfdaec efbd efgcbda fb
|
||||
cda bgacedf bafdc bfdaec ecbd bfcae cfgdae fcgbea bfgda cd | adc becfa fagdbce egabdcf
|
||||
dageb agefd cdbga eb fgecabd baegcf bge dgbcae dcabgf cbde | edbga ebdc cabfegd bgecaf
|
||||
abfgce fbdagce egfc gc agfedb bdfca bagcf bcaged cgb faebg | gfcba ebcfagd dbefcga bcg
|
||||
agfec gdeacb fbgdca bfgacde faeb gaecb af edfcg egfcab afg | faeb gaf agdecb afg
|
||||
cfd cgdae egafdbc daefc debcga gacf cf fgdbec aefdb efagcd | dacebg gfac daceg gcdbae
|
||||
fbead ae cead fea gbafdec cgebfa bfecd eafcdb ecdgfb dgfab | ebacdf fea facebdg fbedca
|
||||
cbgfa gdc gd ceadb adfcgb cbaegf fagd ebgfdc acdgb aebfcdg | gcd dg bdagc badgcf
|
||||
cdbg gc bdfcea cfbedga ebfcg fgc agbef cbfged dcafge ecbfd | gbfedac edfcb cbdfea gefba
|
||||
badecfg egfabd fcdeb eadbf geafcd aedgcb abgf bdaeg afd fa | af beafd adf agdbe
|
||||
fgbedc aedbf bdfacge decbg acd ac ecag gcfbda bgadec dbeac | ecag ecga gdaecb ecdab
|
||||
bfagc fcgea fce acefbd fgedacb ce aegdf eafcbg cgbe acbfdg | cafbg fce fgade fgeda
|
||||
ebadc bcgad efdbag dbgcfa fcgb dgcfea dgacf bg gba bgaecfd | edagfb gb dcefabg gb
|
||||
dcf cdbgea gdfec dcafeg eacgd cf fbadegc gdfeb efdcab fcga | dacge acfgde cedgf egbdf
|
||||
afdcb febdag adfbe fedg dcgabe befdgac dbe de eabcgf baefg | bgdfeac dcbfaeg fcbad ed
|
||||
ecbd ebg agcebf be cfdbag gcbad eabgd ceabgdf gafde gbcdae | cbde bdcag gdaecb dbgca
|
||||
badgfce eabfgd cd dcfbea cda dceg fgcba gadbec adgbe adgbc | gdec bcagd dfabce bdacge
|
||||
adf afceb bdafe dgfbae ad dcfage bgad gfebdc fdbagce edbgf | bgaedf acfeb da becgfd
|
||||
bdg dgbcaf fdgeab bgdef bd fecbgad ecbafg dfegc gafeb abde | bfage baed bcfeag afdgecb
|
||||
bcdeag bagec cadfgb cgdab bge aegbfd eb bcde gbcadfe fecga | eb fdbcag bfdecag debagc
|
||||
bfegcd bgce fbdcga bc cfdeg fbc dbefc fcgade aefbgcd dbefa | dfgbca edgfac fcgadb gbafcd
|
||||
bfcgaed abfgc dfbgac ecfgd acbgde db feagcb dcb dfgbc adfb | bgfdc fcebga dabf fgacbe
|
||||
cebfg fgcbad fca ca faceg fadceg dbfaeg cade bcedagf dagef | cbfeg ac cebfg aegcf
|
||||
ega ebdgaf cadef acdefb agbcfed cgfa ecgda afedgc ag gcdeb | eadfbc dfeabg ega fgedab
|
||||
cdbeag fa adbec egbfda fabcd fda eadcfb aecf fbcgd gbadefc | af bacdgfe fa fda
|
||||
fegba cgafeb fcgdbae acfg bfeac gbcead edbgf afbdce agb ga | gaecdb gdfeb ebcfag gaebf
|
||||
cfega adceb bgc gdba bgadce fdaecbg cdebaf gb efcbdg cebga | gaefc edcbaf fcage eafdbgc
|
||||
adcb edgfc aedbgf daecf dbecaf dfa acfeb cgadefb aecgbf ad | dabc cfeab bdacfge caefbg
|
||||
bgdfeca dbacg gadefc bcegd cfbadg ca agdbf bcaf dca fadbge | eagfdb gfabd begcd ac
|
||||
bfdea bg aegcdb dgabf bag bcgf dbfcga gcdefa fagdc agbdcef | bfgc defacg efabd cdafg
|
||||
cgdfae dea geabcd cebad geba edcfabg ae adbcfg cabgd bcfed | gcfadb eda acdgb gfcaed
|
||||
ba fdeba fbca ebdgca bea deafcgb aedfc cadbef dceafg dgbfe | defac beacdf adbfce ecdbfga
|
||||
eabfg bgdeac bcfeda gdfabce befgda afgd af afb fbcge baged | cfabde fageb fegcbda afegb
|
||||
gdafcb gafed cfdbgea gcea edbcf cg fagdbe gdcef gcd gcefda | begcfad dfgaec cg gdfae
|
||||
gefad gecdaf gfd gaecdb cfed adcge df fbeag bfcaged bfadcg | fgbdaec ecgda fgbae dfg
|
||||
cdg ceda agbfc fdbega fbedcga gbdcef facgd cd faedgc fgead | facgdeb dcfag gdfceb gcd
|
||||
gcaed fdaeb bg gabedc dbcg bag agcefd edgab cgbfae fcdbega | afbegc fgabec gba gbdae
|
||||
ecbagd db ebcfg agedf dgfeacb efgdbc abecgf ebfgd bed bfdc | eagdbcf db bd bde
|
||||
dfecbga faebdg fgcd gd adg ebcad acefg acgbef ecafdg cedag | gd efbdga eafcg gfaebd
|
||||
bfgcea fegcbda cfgea adcfge dcea dgbecf gfdac dgbaf cgd cd | fecga cgd eadc fbdga
|
||||
gefbacd fgac abfce ebdacf bgfaed bceafg fg egbcf bgf gdbec | gbfec aegfcb faegbd gbdecaf
|
||||
dfbage adcbe be afdce ebfc adgbc edb abegcdf cfagde dfebca | cbagd edbafg adgcb ecfda
|
||||
edabc bedgafc fd adf begfad fcgd cdabf efcgab gcbfa cfdagb | df df df fecbag
|
||||
cfdg adfec daecb afc fc cadgfe fcedgab cagfeb fbagde dgeaf | acdeb cf afc dfgc
|
||||
efbg acgefd eb cfdgeba badef gedfa bcgeda deb dfcab fedgba | afbedg afdgbe abdfc afedcg
|
||||
gaedf ed gafce afegcd egbcfa deca bdfceg dfabecg fed gdabf | ed efd afdgb edf
|
||||
dcbaef gfdb dafgc efbcga bcfadg cgf bdcaf gf aedgc faecgbd | cfdeab cfg bgecaf cdgaf
|
||||
defbga aegb aefdb aebdfcg febdca ag gedaf abdgfc afg dfegc | ga acdfbge cadgfeb bdgafe
|
||||
dafeg cgdae efdgb dgbecf gfa bdfacg afbged bafe af efbgacd | bfcedg dagebf aefb acegd
|
||||
cbga cb fegac baedf faedcg facgdeb efacbg fcb bcfegd eabfc | bfade gcfbeda bc bcf
|
||||
dbfaecg aebdfg agbcef ca bfgdac bgace cag bgedc gebaf acef | agcfbe bgdce abcegf bgcea
|
||||
adfge bafe gcfaedb cabgdf gabedf gaf dageb efcgd cedgab af | eadgf baef ebgad dgfcaeb
|
||||
abfdcg fdebagc bdgae gadfec ed gfeadb afbgd gebca debf edg | fedb bgaedf ed aegbd
|
||||
aecbd ebcga abgfc eg gbefdca caedbf degb eabdcg cge fdcage | cfdegab gebd bgdcae ecg
|
||||
adebg fgcd cadef afg fdcegba caefdb gf feadg cfeagd gfcaeb | fga dfaec eafbcg fg
|
||||
eb fdcgab bfade ecfgadb aecdf gfdab bfeg dabegc begfda edb | ebd abcfgd bcedfga bgfe
|
||||
eac adfec gadecf faebcgd dacbf bgdcea gefdc ae egfa bcedfg | abgcdfe gbeacd cae gfea
|
||||
ba feabdc adeb dcfba geabcf gbcdeaf cdfbg edacf fgaedc bac | ba afdec bdae abcdgef
|
||||
bgad cgdaf bd bfceg agfbdc cadebf cdb fecdga bdcfg dcegbfa | bdfagc fdcbag cgfead agedcf
|
||||
dfgcba fgec cafegb afecb efbcdga gdeba bafcde ecbga gc cag | fcgabde adgbe debfac ecfg
|
||||
bcfea dabe defbac dbcfg cad efbagc ad adbgefc edacfg abdfc | aecbf dac da ad
|
||||
cbaf fbe agebf geacbf cgabde gbaec bcegfd fb fdeag cfbaegd | agcdefb gfeda ebgaf cagedb
|
||||
cd cebadg dbfc gcd cafbdg adfgb ecafg fcagd aegfbd efdcbga | fgdcba bfcd agcef dc
|
||||
afeb ba bgfade gacdf agdbf gba egdbf gbdecaf cbgdfe cbdage | dbfga fbedg gba agbdefc
|
||||
bgdafe gfced cbfagd fa bdecag cgdfa fcegadb abcf gbcad gfa | bgcad abecgd edgabc gecbfad
|
||||
cgfeb gbaef dfcg bgc cgaebd gc becafd dbcfe gefacbd cefgbd | gc gcb cbfde bcegad
|
||||
bg bega cfdeg cebda fdeabc dgebc cbdage dgfcab bdg fedgabc | edbgc ebcadg fgdce abcedf
|
||||
feadgc bgf febgcd gbcdfa faceb gbcfe gb dcfge gedb bacfdeg | bgf gbf dfegac fbg
|
||||
gbacf ab agcefd decabg bcdagfe begfac cba fdbgc agfec ebaf | agbfc ba cdbgf fcbdg
|
||||
adfe fa afb cdgeba agdebf fcgebad begad ceafbg bfgdc fdagb | baegfd adfgb dbega fba
|
||||
fdceba dbega ecb cb abcf gacfed cgfdeab fedca cedba gfdbce | bdeacf acdfe cb fecda
|
||||
dcegfa faebcg bafgd gfb bdfca gb fgabecd agdef begd beadgf | bfdga efcagd dgabcfe ebdg
|
||||
aecdgf beadgc geb dcefbg aegdb dbafgce gb dabfe cagb dceag | dgcae ecgbad deacg edcagf
|
||||
edga fbgedca gef becfd fbgda dfgcba ge dbegf abfdge cagefb | eg bgadfe eacgfb bdafgc
|
||||
afcbg acg dgafeb gefc afdcb cadebg befag defacbg cg abfgec | acg cg gbfcea dbfac
|
||||
fdbgcea cfgeb agdefb ecbfad abfde cd cadb debcf dcf cfegad | cgfeda cdfegba ecfgb edacfb
|
||||
gdaecb ecbdg dacg fgedbc caegbdf gfeab bfdeac dageb ad bad | dab gdaecb da gcfedb
|
||||
gec gcaeb ecdab fegb cfgaedb eacfbg bgdcaf fgaecd eg bfcga | bcegadf gaecb acbgdef gcfab
|
||||
df fbgdec fdg dgacb fabge afdbcge bdgace dbfga fcda bgdafc | cgbdfe bdfga fd df
|
||||
ebdaf gabfced abecf efgad db ebfcdg deb bacd fbgeca fedcba | abcdef acbd fcbadeg fdbae
|
||||
adfeg be dgbca cefgbd bedga deb dbgcfae adcgfb dacbge cabe | debfgca acbe dgafe ebd
|
||||
cdgbf gcbfda gd dbga eafcbd cdafb cegfb deagcf abcegfd cgd | aegfcdb gadb fcbad cbgfd
|
||||
deb bafcgd dafce cabed agdbec febdag be gebc dgbca fdacbge | gecb abdgc bed eb
|
||||
afc ac cebdaf becgdf agdfe cabegf bacd fdcbe dcefa cdfagbe | ca cfa bdca cbda
|
||||
aegfc edfac fabcde cfeabdg dagcfe fadg efcbg ga cbagde acg | bgecafd acg begcf dfcea
|
||||
fdgac fedabc cdge ebagf cgdfae ec eac gacfe bfcdga cfbdage | fbcdeag gabfe bdfagc caegf
|
||||
fagdbce fcbed fegca agdc cadfge dg fgdce egd aefdbg gebfca | cfegab gbefac deg gde
|
||||
eb bcegda efgdc ecfbda dbe fbae dfebc aegcfbd dagcfb dcfba | fdbgca dcagfb afbe be
|
||||
begacf fceagd fec ce defab cbge agcbdfe becfa acfgb bgadcf | ce afgbc cegb ce
|
||||
fbadg cbeafg fb eacfdg efbd decfgab fdaeg bdefga acgdb baf | fadeg bdfe bfdgae ecfbag
|
||||
ebfcgad ag afdecg gbafec egdbc dgcae aefdcb dcefa dfag aeg | acdfeb cafdbeg fcgdae cdgea
|
||||
da ead gbeda gbaec gcebdf gbfcdea ebfdg dgebfa adefcg badf | da fgcaed dfba afdcebg
|
||||
fgdaec fbegad cde ec efagcdb cgfbd dcbaef gadef dfecg agec | dce ced ecga fdaeg
|
||||
edfg de dagebc caefgb dfeca gfeca ead gdaecf fgeabdc dbfca | de befcga fceagb dcfae
|
||||
gda cdaef gbeacf gbafcd dfcaegb cdbg efadgb dfcag dg gafbc | afcbg gda fbegda fagbdc
|
||||
gfdcbe cadgf defag bfadce ac dfcagb fdbacge cbga afc bgcdf | begdcf dcfbg aedcbf degbcf
|
||||
ce cgbfed degbfac cde cagbdf cgfad debfa deacf cgfeda cega | ec bfadgc dfbcage gebcfd
|
||||
dfbceg dgeac acbf aebcg ebfga bcfgae adgfecb cb gbdeaf cbe | cbe cfba bc ebcfadg
|
||||
fbgdc acd cbeaf ad bcdfa fdacbeg efad agcdbe bcafeg fbcdae | cad afcbe da gdbaec
|
||||
bcgde cbadefg efgdab bfc aebfg fc bgfcda febcg gafbec eacf | bfc bcf bgcdfa dgaefb
|
||||
gc cdg gcfade dfcea abfdce gace cgadf aebdcgf gbafd cebgfd | cgfad ecga cg cg
|
||||
eadgbc ce dec afdeb ebdfc cfeabd cegbfda afec fbgdc baefgd | afec gfbead defbc bgfdc
|
||||
agcde acfegbd dbcgf dbcfge ecf fbaecg fe gdefc cfbagd bedf | cafbdg dagec cgefab gfcedba
|
||||
fbacg beadfc gcd gd cdfag fdeac ecbfgd ecfadg gdfcbae dgea | fdcbaeg gbecdf daefc fdgac
|
||||
gf degf bfdeag gbaef ebgfadc gaf caegb dfcaeb dbagcf fabed | afg ecdgafb cabdef adbfce
|
||||
adbgc dacbeg gecabf fcda cgefbda cf dbgfe cfb dgbfc dfgcab | afdc fcb cf dacf
|
||||
fdgace ba gdaeb egcbd fegbcda afdbge abg bfad efgda gcafbe | bag gab efdga fedagbc
|
||||
bgadc ba bga gdace bagdfce agcdeb degbaf ecab fgcade bfcgd | begdcfa ba ba ecgad
|
||||
bcegfa dafbc fbg fg dbgea adecbg bacgfed abfdeg gdabf fdge | fdcab gedf cbdfa gadebc
|
||||
gabdefc gdface edbag facgd gef acfe gdefcb efgad cbgfad ef | dafecg aecf gcdaef begad
|
||||
bdfcag adgfb ag efdcbga dbeacf bga egabcf cbdaf bdefg acdg | bga ceagdfb cegbaf egbcfa
|
||||
aegfdc dbace bcaefd geabcd ag debfg ega eagbd dfbgeac gcba | agebdc deabcg dbfecga bceafd
|
||||
ebad gfacbde ecfagb ea dcafg dfbge edfgbc fgdae eaf agdfbe | gebafd ae afbdeg dgfea
|
||||
gfc gedfa fdac cgbae ebafdg edfcag cfbdeg gcadbfe fc gcfea | adgfe eadfg egdfa caegdf
|
||||
fbdea eacbfg aegfb dgbf dcebgfa edgabf acfed edb gdaecb bd | deb bd dcfbeag dbgf
|
||||
cgedbfa dbgf gdfcba bgdeca acefd afcebg fcg agcdb fg gfdac | dgcba fbdg gf fg
|
||||
acbgdf gcedaf efgdc ecbdfga fb bdega bfce gdcfeb febgd fgb | eafcbdg defcg gfb gcadbf
|
||||
ebcgaf dgcaeb acebg cdfbea gbcef fagb gfcde fb efdbagc bef | bfe gdaebc fbaegc gcabedf
|
||||
fbgdace efcba bg gbc gcfbed gbad cbagfd fcagd gaefdc bcfga | gbad gb gcdfbe fabgc
|
||||
afecb dcbaf aedgcfb ce aedgbf ceb cefg fbega eacdbg acgfbe | agecbf cabgfed abecfg ebc
|
||||
fcdge ceabgf aebg aecfg cfbea edabcf ag befgadc acgbfd gca | bega acg cdbgfea agc
|
||||
gfcde db cbdge acgbe gbfecad bed ecfagb dgceab dfabec dbag | agdb bde bgceaf efacbd
|
||||
dace ae ecgfab degafc eadfg ecfgadb ebfdg aeg dgcaf bcgfad | gea age ea agcbfd
|
||||
eab cdabe bgcad bcef adcegfb bgafed be ebcafd acfgde dfcea | fbcead acbed be acedf
|
||||
cbgde cadeb ba fedabg ebcdagf abcf acefdb dacef dab degafc | fcagde dba ebdgaf eacdf
|
||||
ebd de caefgb bdeca bgdeaf afbcd gecab cegd bgcfdae ecbagd | adegbc bcfage agfbed eagfbd
|
||||
ecfdb acgdbfe gdb ebfacd bfcg gfdceb efgdb bg aegcbd gaedf | adegf bg ecfgbad bdfaec
|
||||
ecgabd fb bedca beacfd gfcbead fgaed dbf cfeb bedaf cbgadf | abedc dabfe bcfead cedfgba
|
||||
gfcae fdgab bafedg cdab dc bdgcaf gbcfead cgfda cdg gcbfde | bdefgc adfgb gdc bgecfd
|
||||
cdegf adegcf gdfebca fdg dagc ebcdf agcef dg gdfeba cabegf | gebafc gfd dg gdac
|
||||
faedcb gbecfa bf febg dgbcaef cdgab cgfae fgdeca bfcag fab | dfgacbe gbef fdbaec bfadce
|
||||
gcbeda cdegf dcbfe becdgfa fbca edabcf ebgafd efb acedb fb | gdbcae abcf fbac efb
|
||||
cdgfba defagb gadebc cd cdbfaeg edagb fbegc ceda cgd bdgce | dgbafc agbfde facdbg gedba
|
||||
cedgb egc afcgbd ec cbea gaedfc cbgda afegdbc ebdgac ebgfd | gfcbad gec aegcdf afecgbd
|
||||
gbc acdgeb egbfa egbcf cdefg bc efgdbca baefcg afcb fdaegb | ebdcga gecfd ecfgd gebdac
|
||||
gdeacb aecfg ebgca defbcag adebc beg gdabef bg acfbed dgcb | dbefga geb fbacde acbge
|
||||
ebgafd bc gafce cfb cdfagb dbce begfd efbcg gdfecb dacegbf | acegf fcgea cb edcbfag
|
||||
eagdb deafg fgdb gbdeaf dcgabe ecdgbfa afcge df fda acbfed | gdfb fd gbfeda fda
|
||||
faebg gfaecbd cfegb age gcadbf ebad fdbag bagdfe cgfeda ea | cbfge bafgcd fgabd gbdaf
|
||||
dgafecb ag fdebg fdgae fgba fbdage ecgfdb aebcdg fcaed eag | gafde ag egcbfd fbegd
|
||||
bdcgf dfceb afgdbc gd ebcgfa fdg gdecfab dbga afbgc adcegf | dg cfbagde bfcdg adcegbf
|
||||
cdbaf aefbcd abdefg bdgcaef dgcfb ca aebdf eacd acf ecagbf | dgcbf afc eacgfb egfcba
|
||||
bface dacbg dgface bgecaf efbd fbacde dabec cde ebadgcf ed | dce debac ced dagcef
|
||||
ebfdc efg bfgde dbegcf afdecb cebfag gcde dfacbeg eg afbdg | cdeg edcg gfadecb egcd
|
||||
efdgca agebf fcgbdae cdbfg bade dgfab edagfb dga da bafceg | dga adeb cafebg dag
|
||||
gbdec cb abcfged dcfeg fdacgb cbgdae agdeb acbe egbfda cbd | dcefg baec gdbcafe dbaeg
|
||||
gcdab cabfg ecgfa gfb efab dbegfc cabegdf cefagd fb gaefbc | egafdc baegfc feab aefb
|
||||
dacfeb dfc dgbac fd becdfag afcbge aebfc dcafb efad gebfcd | cdf bgacef efcgbd dabcfe
|
||||
dfcage bfdgec gcbfd gcfab agedbf edcfg fbd db gdceabf debc | acbfg dbf fdb bdce
|
||||
dbcge egf debagc gcedf cadfe bgcdfe bdfg gf cebgadf gbafec | gbdec dcegb gefdc gedbca
|
||||
ebdfcga bfecd feacd daeg cfgad ea dfagce cfgbae afbcgd aef | dcbgaf adbgefc cedbf dgfac
|
||||
ebfac dcgbf ecg degf cegbf cdgefb dabcge facdgb bfcdaeg ge | ge eg bfcae fegd
|
||||
bd fbacge edbc gfabed cegbf fcdgbea febgdc dcbgf dgfca dgb | gbefc efcgba db abefgc
|
||||
dacfgb dbacge bcf abcdfe bf fdabgec dcagb bagf gcbfd dcgef | agbfdc deacgb bdcag fb
|
||||
ecgfa bgdfeca fgdbec ebafdc gbdcfa bfacd dgf gd bgad gcdfa | fcagbd bgda fadgc gbfdec
|
||||
decab gcfbe faegdc gafb bgfeca ebcadgf ag ecgdbf gea eabcg | ga egdfac bcfeg ga
|
||||
ebfgd dbcgfe fgabd egad fecgab ga fagdeb cdbfa afg dgcbefa | gfa ag ag gfbad
|
||||
fgcade agbf fgcdabe ceafb gefca eagbfc bca fcedb ab edbgac | bgdeac ebcdf fagb bca
|
||||
eacbfg eabfcgd edfac ebfdc cgdeba bd dbc bfgedc bfdg ecbfg | gbdf bcd fdace bdfg
|
||||
bfdace cbfagd cbadf gedcabf adfbe faebg beagcd de dae cefd | fcde fabdgc aegfb aed
|
||||
deacb gfbeda dcbfa acbdge decfbg cagbedf ae eda bgedc gcea | dbefgc edgbfc ae ea
|
||||
ecfag bcafed bgdca ebagc cebagd abegcfd dagfcb be bdeg eab | gbeca acbdeg bagfdc bae
|
||||
cagef bdea dag egdaf agedbf egfbd bcfgad cgebadf ad egcbfd | dgeaf gad becdgf begcafd
|
||||
efcba egbfadc fgebd gbfaec bafedc efgcad ebfad da dabc ead | ebcagdf ead fecbag bcgadfe
|
||||
efa cadfb fgdcbe cegafd fcbeg abecgf cebaf aecdbfg beag ae | bdcfa bcagfe cbefg ea
|
||||
ceab aedbcf fdbce cb daefb dgacbfe gcdafb bafdeg dcegf cdb | bc abce afegbd edgfba
|
||||
afdegc acdfbg debcfag facebg bdgc dac dc acdbf gbcaf efdab | cd gadefbc cbfga abcgf
|
||||
ecf fc gdefc feabdg dcabef edagc bfdge fgdbec fcbaedg fbcg | efc bgfcde caedg gdfce
|
||||
gfeab aefdgc cdefgb bcfgd ad fad adbc acbfgd fgdba bgfedac | fgcdeba da gfdab cegdaf
|
||||
bea gebfd edfbacg egcdfa ab dagec dgeba dcab abcdge cgeabf | acdbge ba gecafd dfbge
|
||||
210
advent_of_code/2021/d8/main.cpp
Normal file
210
advent_of_code/2021/d8/main.cpp
Normal file
@@ -0,0 +1,210 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string file_name = "8.txt";
|
||||
// string file_name = "test_text.txt";
|
||||
|
||||
vector<pair<array<string, 10>, array<string, 4>>> input_text;
|
||||
|
||||
pair<array<string, 10>, array<string, 4>> parse_line(const string &s){
|
||||
istringstream ss(s);
|
||||
pair<array<string, 10>, array<string, 4>> result;
|
||||
string word;
|
||||
|
||||
for(size_t i = 0; i < 10; ++i){
|
||||
ss >> word;
|
||||
|
||||
result.first[i] = word;
|
||||
}
|
||||
|
||||
// There should be a pipe | here, which doesn't need to be put into the strings
|
||||
ss >> word;
|
||||
|
||||
for(size_t i = 0; i < 4; ++i){
|
||||
ss >> word;
|
||||
|
||||
result.second[i] = word;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
map<string, int> identify_digits(const array<string, 10> &digits){
|
||||
map<int, string> digit_strings;
|
||||
|
||||
vector<string> size_fives;
|
||||
vector<string> size_six;
|
||||
|
||||
for(size_t i = 0; i < 10; ++i){
|
||||
size_t ds = digits[i].size();
|
||||
|
||||
if(ds == 2){
|
||||
digit_strings[1] = digits[i];
|
||||
} else if(ds == 3){
|
||||
digit_strings[7] = digits[i];
|
||||
} else if(ds == 4){
|
||||
digit_strings[4] = digits[i];
|
||||
} else if(ds == 7){
|
||||
digit_strings[8] = digits[i];
|
||||
} else if(ds == 5){
|
||||
size_fives.push_back(digits[i]);
|
||||
} else if(ds == 6){
|
||||
size_six.push_back(digits[i]);
|
||||
}
|
||||
}
|
||||
|
||||
vector<set<char>> size_fives_sets(3);
|
||||
for(int i = 0; i < 3; ++i){
|
||||
for(char &c : size_fives[i]){
|
||||
size_fives_sets[i].insert(c);
|
||||
}
|
||||
|
||||
// We know that 2 is the only digit that combines with 4 to be 8
|
||||
for(char &c : digit_strings[4]){
|
||||
size_fives_sets[i].insert(c);
|
||||
}
|
||||
|
||||
if(size_fives_sets[i].size() == 7){
|
||||
digit_strings[2] = size_fives[i];
|
||||
size_fives.erase(size_fives.begin() + i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Now do kind of the same thing but for 5 and 3, combined with 7
|
||||
set<char> three_or_five;
|
||||
for(char &c : size_fives[0]){
|
||||
three_or_five.insert(c);
|
||||
}
|
||||
|
||||
for(char &c : digit_strings[7]){
|
||||
three_or_five.insert(c);
|
||||
}
|
||||
|
||||
// So in the case that it not of size 5, which means it is the number 5
|
||||
if(three_or_five.size() != 5){
|
||||
swap(size_fives[0], size_fives[1]);
|
||||
}
|
||||
|
||||
digit_strings[3] = size_fives[0];
|
||||
digit_strings[5] = size_fives[1];
|
||||
|
||||
vector<set<char>> size_six_sets(3);
|
||||
for(int i = 0; i < 3; ++i){
|
||||
for(char &c : size_six[i]){
|
||||
size_six_sets[i].insert(c);
|
||||
}
|
||||
|
||||
// Only 6 and 7 together form 8 from all the six sized digits
|
||||
for(char &c : digit_strings[7]){
|
||||
size_six_sets[i].insert(c);
|
||||
}
|
||||
|
||||
if(size_six_sets[i].size() == 7){
|
||||
digit_strings[6] = size_six[i];
|
||||
size_six.erase(size_six.begin() + i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Now we only need 9 and 0, which can be distinguished by combining with 4
|
||||
set<char> nine_or_zero;
|
||||
for(char &c : size_six[0]){
|
||||
nine_or_zero.insert(c);
|
||||
}
|
||||
|
||||
for(char &c : digit_strings[4]){
|
||||
nine_or_zero.insert(c);
|
||||
}
|
||||
|
||||
// In the case that the size is 7, which means it combines to an eight
|
||||
if(nine_or_zero.size() != 7){
|
||||
swap(size_six[0], size_six[1]);
|
||||
}
|
||||
|
||||
digit_strings[0] = size_six[0];
|
||||
digit_strings[9] = size_six[1];
|
||||
|
||||
for(auto &s : digit_strings){
|
||||
sort(s.second.begin(), s.second.end());
|
||||
}
|
||||
|
||||
map<string, int> result;
|
||||
for(auto &d : digit_strings){
|
||||
result[d.second] = d.first;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(){
|
||||
ios::sync_with_stdio(0);
|
||||
cin.tie(0);
|
||||
|
||||
|
||||
ifstream file_stream;
|
||||
file_stream.open(file_name);
|
||||
|
||||
if(file_stream.is_open()){
|
||||
string line;
|
||||
|
||||
while(getline(file_stream, line)){
|
||||
input_text.push_back(parse_line(line));
|
||||
}
|
||||
|
||||
file_stream.close();
|
||||
}
|
||||
|
||||
|
||||
// Testing the input parsing
|
||||
|
||||
// cout << input_text.size() << endl;
|
||||
// for(const auto &line : input_text){
|
||||
// for(auto word : line.first){
|
||||
// cout << word << " ";
|
||||
// }
|
||||
|
||||
// cout << "| ";
|
||||
|
||||
// for(auto word : line.second){
|
||||
// cout << word << " ";
|
||||
// }
|
||||
|
||||
// cout << endl;
|
||||
// }
|
||||
|
||||
int result = 0;
|
||||
for(const auto &line : input_text){
|
||||
for(size_t i = 0; i < 4; ++i){
|
||||
size_t w = line.second[i].size();
|
||||
if(w == 2 || w == 4 || w == 3 || w == 7){
|
||||
result++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << result << endl;
|
||||
|
||||
result = 0;
|
||||
for(auto &line : input_text){
|
||||
auto digit_map = identify_digits(line.first);
|
||||
|
||||
int acc = 0;
|
||||
for(int i = 0; i < 4; ++i){
|
||||
string s = line.second[i];
|
||||
sort(s.begin(), s.end());
|
||||
|
||||
acc *= 10;
|
||||
acc += digit_map[s];
|
||||
}
|
||||
|
||||
result += acc;
|
||||
}
|
||||
|
||||
cout << result << endl;
|
||||
|
||||
cout << flush;
|
||||
|
||||
return 0;
|
||||
}
|
||||
10
advent_of_code/2021/d8/test_text.txt
Normal file
10
advent_of_code/2021/d8/test_text.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe
|
||||
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc
|
||||
fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg
|
||||
fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb
|
||||
aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea
|
||||
fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb
|
||||
dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe
|
||||
bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef
|
||||
egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb
|
||||
gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce
|
||||
100
advent_of_code/2021/d9/9.txt
Normal file
100
advent_of_code/2021/d9/9.txt
Normal file
@@ -0,0 +1,100 @@
|
||||
8679876423456789989987898954321012987899875434989895795442356997901987896535698998998765457891256789
|
||||
6457987567899894679986567895433129876789976549876734984310129886899876789423987797899879378932349892
|
||||
5356798778979923498998678997654998765678987956985429875421298765679765698909876676789998989545998990
|
||||
3235679899767939567899989659769878654589999897899319986632987643456984567898765445678987997659897679
|
||||
4123799998547898978999896539878965433689989789998998996543498788767893456797654326799976899798756567
|
||||
3249999876335667899998764321989876521599875678997897987654679899879942345679763214898765678987645459
|
||||
4598899994213456989899895210195998632478934567896456798775989934989321234567985423989654346895532348
|
||||
5987778965365569875788986791234987543569123459965345679896799129994320123459876439876543235694321257
|
||||
9896567896878678964597897989545987656789034598954234567989898998998531234667987545987432126789452356
|
||||
8765436997989989753456789978956799867894145987892135679878956897897692345778998956798974235696543467
|
||||
9989324989999895642345898867897987998943239876793245789954345976898989659889989767899865345789675598
|
||||
9998217679998743210156987956789875459654998765689356798765586895899978998999879898934975486998786789
|
||||
9886504567897654434569896745899976598769876784878969899876678964698869987898965939123986567899997992
|
||||
8754323456789765567798765634568987899898765443567899910987989643987658976987894321094598998998798993
|
||||
9865454587899876789899654325999998987999765312476999321998996432198747895436789632989989989987659789
|
||||
9876575679958989896998543212789879996797887102345678939869987843249656789325896549879867879999545678
|
||||
9989989789347999945987654104698769865986543215456899995456998967398797993214589697657653468965431734
|
||||
8990399893276789434598653245789859879897654356768999876329879878979898943203478986543232457894320123
|
||||
7921234921045678923987654346999943997698867497878987989998765989767999854512567965432101666789434534
|
||||
6899399763234799101298765677899894989429878998989276799839974399956798765423689876543212345896598765
|
||||
5678987654365678912349878989956789871012989019894345976729865459969899899434799987675423469998999878
|
||||
6789098765458989434459989796545898765123499198765566795410976567898910987645678999796564598789891999
|
||||
7999239876567896545698997654323498764254568999987877989321989989967891298957789234987676987676790123
|
||||
9898999987678999656997997543215987654366789989098999679452397897654989979968999129998797899545679234
|
||||
8767988998789998969876889654634598765477899879129988568943986689769878969878978998989899998434569395
|
||||
7659867899995977899765678995745679889698999768934977456999875567998768954989457897875989986545678979
|
||||
8543456987654656997643435789899789998789098656899765349889543467899879943399568976564579897756799567
|
||||
7651568999743249876532124678987993219892197545699983298776532346789989892198679765442456798969895489
|
||||
8798678999654345997643245679786989398964987636989932109654321236789996793239789654321399899878943298
|
||||
9999789498765656798655456798675979987979898929879893298743210545678945679959899793210988989989654567
|
||||
5899893209986769899878567987434567896899759899769789987654332456789434567899998989334977878998767678
|
||||
6798995349899878968989879876323878935789898798754578998865443767897323456789987678999865467899898989
|
||||
8987689498778989654399998765218767899894987659865679339876554569965435579899896566987654389996949296
|
||||
9896569579667899543298997654103656999923496543986789219987665678978557679998765465698766578985432145
|
||||
8789458998549678954987986543212345689913987332299894398799876789987668989999764324569987678976543034
|
||||
9698967997434567895976799764323459899899876210123995987645997897898789793987653213498998989988743123
|
||||
6567899876523498999885679898549568998797965421234579998434598986799895692199866323567939497899984235
|
||||
7678999887214589998764323987678979987656986432346889989545679965987934889012965459879421345999876346
|
||||
9889698754323678999896434598899989499869997556577999878987899873296321679229876569989545457899997959
|
||||
9996529765434567899997576699999992349998998769679876767898968994985442578939987978998676567899879898
|
||||
9875419889556878998698678789998901298987899898998765658999754329876543467898798989129797698998765767
|
||||
8954323997667989896569899899987892987695778987999874549698942101987656789923689993239898799249654456
|
||||
7899934598798996789699999999976799875534569876899943234567893292998967993212598994399969895398932345
|
||||
6987895789899445678989889998865689754323998765698764345678989989899098977333457989989452976987890156
|
||||
5456789999973334569976767897653798965419879554459875456989878976789129765444569978979321989876789767
|
||||
4345678998762123469865456789794987894323965433369986787898956965678998976755998659867990198965678978
|
||||
6456989999854034599876345679989965789435986521298987899987545894578987897899876545745789987654589989
|
||||
7598997799543129986989256898877894689949898310397998999875636789699876989999997634634692198995694393
|
||||
8789345678954298765432123987956943567898765421986329298764125678987545678989876520125891019989989212
|
||||
9893256989879349877547434976245892468999976539765410198643034599975434345678987434566789198879878901
|
||||
8932167897998998987656549765126789567899987679887421985432123488954321236789498567879890987764967892
|
||||
6543019956987987698787697654246897679967898789999439876543434567896543357894339978989991996543456789
|
||||
7674998969876546549899898775356789989356999899876545998987689678997689458932129899599989875432365692
|
||||
8989867899995432134989939889768993490245699946988656799998789799298796569549099789439878989821234910
|
||||
9898756789987543235678923999899912391236789999799767897999999892109987678998987678998964298752355891
|
||||
8789645699997656349889019761967899989347899987659878966788942943212398789997986569897654129543456789
|
||||
7673234578998797956992129853459998578998999896542989454567893954336799898786597456789973298757767895
|
||||
7532145789239989898973439964578987699659498765431094313478999895445899959656454345897654569868878944
|
||||
8743256992139876799965598765679998789943249876532986324578998789656789743943201235789775699879989432
|
||||
9854367893298875689876789878789999897893101997679876534689545678967897659853212356899989989991096541
|
||||
2967478999987654799987897989997896956789212989789989776795434567898998789764323478968999878992297790
|
||||
1297678998699875698798956992166964349994329878999899897896565678999469898965454589457898765989999989
|
||||
3398789987545986789679349854345793298789498767989767999987776899878999987899565694346789974767899878
|
||||
4569893596536598993578998765456789097678997656678979789998897987767789876798976789234599543456789756
|
||||
9679932987623459219989549986578998986545789547569995698999999876345698765787898892123987654567998746
|
||||
8989921098434678998898767897679876563235679423459954567899889995467987654386949921012399876789986535
|
||||
7698792199549789876789989929989765432124795312367893456997768889567898565234939993233456987898994424
|
||||
4597679987698998954567899939899876554256789423458932349886657678978987432129898989346767898967943212
|
||||
3976567899987876443456789898789988764345697634569321298795443589989996559998757979958898969349854101
|
||||
2987788998986543212467898785678999975656896547679490999644312678993987898766646767899999954298765212
|
||||
0198999987697654401238789654589899876787987769789989898732101289432198998754325456789999832109654323
|
||||
3239329876559964316345696572476789987898999878999876789543232398991099987656212345678987643219866444
|
||||
4568939876449876425456789421245789998969643989012965699865353456789987999942101234567898964429977865
|
||||
7678949767320987434667897910134578989655432397929894989976967898899876798763212347678999896798798987
|
||||
9899899654321299545878956891549699876543201256898753979989878959978965679854323567899999799897659598
|
||||
2965698765453987656789345692998989998653213345697642367899989543567894598769434588923987678998543469
|
||||
1978799876764598997993236989887569998754324457799843456789197654679943459898665678999876567899765978
|
||||
9899890999865679439874345978765457899979434769987655667994398786999432345999778799789985434569879899
|
||||
5789921998998789598765469864312356789898945678998769789965459899898764597899899894678954423456998799
|
||||
4578939896789898679977698753203467999767896789879978899876569988799875989998954923789653210199879689
|
||||
3457898785878989789988789954365788998956789997965989934989698878679989878987643213568954521989767578
|
||||
2346798674569878999999896795456899567899898765434599323498987654567899767898642102399767439876543467
|
||||
3578997523458967899876964987678943467942909876765678999987654532348954457899773213989898998765432457
|
||||
4679876312567956899985453498789321237891912987876789987698963101237893248999964349878989219984321248
|
||||
5679765403458946799876312349895430146789899898998899956459854235456799867998765498766578923987432359
|
||||
6789876564599435679932103468989541236897678789329999843398765346667999878939878987655467899876543767
|
||||
7899997765789324598965213589978959345999578689210197652139985457788988989123989996544345699989656899
|
||||
8997999876993212987654325678967998957895436578922398721012496669899777799034992986432234889998789999
|
||||
9785789987895433498785448789554767898965323467899599432124987898954656678949891093210145678929898989
|
||||
7644678998999994569876759894323456789984212345678987643245698997653234599769789987623468989212987878
|
||||
5433567899787889678987898965412356894693203576789999854367789986542123678997649876544567894349896567
|
||||
4312345789656778989998997986501245793984212456894878965459991098421014569865432997658789965498765456
|
||||
5323456896547867899989986899312457892975323569923669876567892197532127689998321298979899989987654345
|
||||
5434677976432356789875465678923578931096467678912456997978989989643234899876432399989999899998543247
|
||||
7646789984321345679876324789437689542987568989101567898989877878967846789987643987694598799876543056
|
||||
8769899875432487798765455997659797653497679898913498999598765667899987895699656986533989689965432125
|
||||
9878949876546798939977686789768898767969898757895989996469654345678998934598799876421264579876654534
|
||||
9989432987657899424988797892979909879954998768999879875398943234579999323679987653210123499989878679
|
||||
9894321298767894212599998943989212999863239899398765421267892123589987454567899787332245589991989889
|
||||
8765210129898965343456789654694329876542145910239865430356942012399876565688921986543345678920199999
|
||||
87
advent_of_code/2021/d9/main.cpp
Normal file
87
advent_of_code/2021/d9/main.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string file_name = "9.txt";
|
||||
// string file_name = "test.txt";
|
||||
|
||||
int main(){
|
||||
ios::sync_with_stdio(0);
|
||||
cin.tie(0);
|
||||
|
||||
vector<vector<int>> heights;
|
||||
|
||||
ifstream file_stream;
|
||||
file_stream.open(file_name);
|
||||
|
||||
if(file_stream.is_open()){
|
||||
string line;
|
||||
|
||||
while(getline(file_stream, line)){
|
||||
vector<int> line_input;
|
||||
|
||||
for(const char &c : line){
|
||||
line_input.push_back(c - '0');
|
||||
}
|
||||
|
||||
heights.push_back(line_input);
|
||||
}
|
||||
}
|
||||
|
||||
// Test input parsing
|
||||
// for(auto v : heights){
|
||||
// for(auto i : v){
|
||||
// cout << i;
|
||||
// } cout << endl;
|
||||
// } cout << endl;
|
||||
|
||||
size_t HEIGHT = heights.size() - 1, WIDTH = heights[0].size() - 1;
|
||||
|
||||
int result = 0;
|
||||
|
||||
for(size_t i = 0; i <= HEIGHT; ++i){
|
||||
for(size_t j = 0; j <= WIDTH; ++j){
|
||||
bool lowest = true;
|
||||
int cur = heights[i][j];
|
||||
|
||||
// Check on corner cases (ha literally)
|
||||
if(i != 0){
|
||||
lowest &= cur < heights[i - 1][j];
|
||||
}
|
||||
if(i != HEIGHT){
|
||||
lowest &= cur < heights[i + 1][j];
|
||||
}
|
||||
|
||||
if(j != 0){
|
||||
lowest &= cur < heights[i][j - 1];
|
||||
}
|
||||
if(j != WIDTH){
|
||||
lowest &= cur < heights[i][j + 1];
|
||||
}
|
||||
|
||||
if(lowest){
|
||||
result += cur + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << result << endl;
|
||||
|
||||
// For the second part we need to do some kind of scanning of all points line by line
|
||||
// in which we need to make sure we don't scan points multiple times.
|
||||
// Im thinking of something akin to line segment intersection with a scan line
|
||||
// So keep track of which regions are currently present on the scan line
|
||||
// One region can occur multiple times on the scane line,
|
||||
// so we have to look into which events exist
|
||||
|
||||
// But now we have to keep track of the previous lines all the time as well,
|
||||
// so it is not exactly the same as a scanning line thing
|
||||
|
||||
// I think this doesn't work, in the worst case I think you can have situations
|
||||
// in which each points gets visited n times or n / 2 or something
|
||||
|
||||
|
||||
cout << flush;
|
||||
|
||||
return 0;
|
||||
}
|
||||
5
advent_of_code/2021/d9/test.txt
Normal file
5
advent_of_code/2021/d9/test.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
||||
Reference in New Issue
Block a user