Browse Source

Solved part1

Piotr Czajkowski 2 years ago
parent
commit
963a6ff6e7
2 changed files with 161 additions and 1 deletions
  1. 63 1
      10/code.go
  2. 98 0
      10/input

+ 63 - 1
10/code.go

@@ -18,11 +18,73 @@ func readInput(file string) []string {
 	return lines
 }
 
+var points map[rune]int
+
+func init() {
+	points = make(map[rune]int)
+	points[')'] = 3
+	points[']'] = 57
+	points['}'] = 1197
+	points['>'] = 25137
+}
+
+func parseLine(line string) rune {
+	opened := make(map[rune]int)
+	var lastOpened []rune
+
+	for _, char := range line {
+		if char == '(' || char == '[' || char == '{' || char == '<' {
+			opened[char]++
+			lastOpened = append(lastOpened, char)
+			continue
+		}
+
+		if len(lastOpened) == 0 {
+			return char
+		}
+
+		switch char {
+		case ')':
+			if lastOpened[len(lastOpened)-1] != '(' {
+				return char
+			}
+		case ']':
+			if lastOpened[len(lastOpened)-1] != '[' {
+				return char
+			}
+		case '}':
+			if lastOpened[len(lastOpened)-1] != '{' {
+				return char
+			}
+		case '>':
+			if lastOpened[len(lastOpened)-1] != '<' {
+				return char
+			}
+		}
+
+		lastOpened = lastOpened[:len(lastOpened)-1]
+	}
+
+	return ' '
+}
+
+func part1(input []string) int {
+	var total int
+	for _, line := range input {
+		illegal := parseLine(line)
+		if illegal != ' ' {
+			total += points[illegal]
+		}
+	}
+
+	return total
+}
+
 func main() {
 	if len(os.Args) < 2 {
 		log.Fatal("Please provide a file name as argument")
 	}
 
 	input := readInput(os.Args[1])
-	fmt.Println(input)
+	fmt.Println("Part 1:", part1(input))
 }

+ 98 - 0
10/input

@@ -0,0 +1,98 @@
+([(<<<{({([{{{{}[]}<[]<>>}<{()()}{{}()}>}{((()()))([[]<>]<{}()>)}]({((<>{})[<>()])<{()<>}<<>(
+{<{{<[[[(<<<([{}{}](()()))(([][])<()<>>)>>({([[]]<{}<>>)[{[]{}}{[]()}]}[(([]{})){([]<>)}])>((({{<>[]}
+[[{<<[{{<[(<[<()<>}[{}[]]]((()()))>{[([]<>)([]())]<([])[{}[]]>})<<<[{}[]]((){})>(<<>[]>[[]<>])>
+({[{[<([{((((<{}()><<>()>))[<{[]<>}<{}()>>({<>[]}(<><>>)])([[<()<>>({}<>)]({[]()}[[]{}])]))}
+(<<({{{{({[[(<{}{}>{{}<>}){<<>>}]<(<{}()>{(){}}){[{}()]([]<>}}>][([((){})<[]<>>]<<<>{}>{[]}>)(
+{(((([{([<[[<[(){}]<()()>>{[[]{}]{{}[]}}]([<<><>>{[]{}}]<{[]()}[()()]>)]((({[]}[<><>]))<<[{}[]][<>()]>([{}[]
+([({[[<{[{{[{<{}{}>[<>[]]}(([]()){<><>})]{<<<>()>(()())>{{<>[]}[<>{}]}}}(([(<><>){[][]}][{[]}[[][]]])<([{
+<[<{[(([<[([<<()()>{{}{}}>[[<>][{}()]]]({(<>{})}<(<>{}){<><>}>))(<{[{}<>][()()]}<([]())<<>[]>>>)]><
+<[([<<{{[(((<[(){}}{(){}}>{[()[]]<[]{}>}))){[[[[<>()](<>())]]]}]{<{(<<{}[]>[<>]><[[]()]<{}[]>>)({[()<>]
+(({[<([<<([{{{()[]}{[]{}}}{{<>[]}{()[]}}}<<(()){<>[]}>[(<><>]{(){}}]>]([[({}{})((){})][[<><>]((){})]][{(
+{{((<<<{[({[{{()<>}{[][]}}](<([][])[{}{}]>[{(){}}<[]{}>])}<[{{{}{}}(()<>)}[(()<>)[<><>]]]>)(<<[[[][]][{}<>
+[({[[{[{[{<((([])){<{}()>{[]{}}})>{<[{()<>>({})][{(){}}<()[]>]>{[{<>{}}(<>{})][<()[]><<>()>]}}
+<<{[(<<{{{(<{{[]()}<<>[]>}>{<<{}{}>{[]{}}><[{}{}]{{}[]}>})}{<{[<{}{}>({}())]}{[([]())[[]<>]]<{<><>}
+((<[[[([{(({({()()}[{}()])(<{}()>[[][]])}((<[]{}>(<>()))([<><>][<>{}]]))<{([[]{}][[][]])}>
+<[<<<<{(((<{[{(){}}{<>()}]}{{{{}()}}<<<>()>>}>{({{<>()}}{<[]<>>[()()]}]}){<(<({}<>){[]<>}>({{}()}(<>())))>
+(([[[{{{{<<{<[[]()]{()[]}><[<>[]]({}<>)>}<<{[]{}}<<>>><[{}[]](<><>)>>>[({<{}[]>[(){}]}[{(){}
+{[{[({{({<<([({}[])][{()<>}{<>()}])<{<[]()>{()<>}}>>[(<<[]{}>{()()}>)]>})<{[{({<<>[]>{{}()}
+<([{[[<[([<(<<<>()><[][]>>)[[[<>()]<<>{}>]]>{<(<{}<>><[]()>)<(<>)[[][]]>>}])]{[[((<({}())[[]{}]><<{}
+[([[([{[<({(<({}{})[<><>]>){{({}{})(()())}[([]<>)<[][]>]}}}[[([[[][]]]<({}{})(<>[])>)(([[]()]{<><>})[[[]()
+{[[<[<(({<[{<[()[]>{<><>}>([[]<>]{[][]})}([{<><>}<<>{}>][{(){}}[{}<>]])][<(<()<>>(<>()))[{()<>}
+(<<<<({[{<[[({()<>}(()()))][({()}[<><>])]]<<[(<>[]){{}()}][([]{})({}{})]>{<[[]<>]<<>()>><[{}()][<>
+[({<<{{(<{<<{<()()>(()<>)}<{<><>}>>(([()<>][()<>])[(()<>)(()<>)])>[<(<()<>>{{}()}){[[]{}]{<>{}}}>]}[(<[({}(
+({([([[{{[{{{<<><>>{()()}}[[<>[]][()()]]}({{<><>}{<>()}}<({}())>)}<[{[{}()](<>[])}(<(){}>)]
+(<{[[[{((<([[<[]{}>([]<>)]{{()()}}]({[{}[]](()<>)}{{{}}[<>()]}))>))}([{(([(<{}[]>[<>()])]({<
+([{{<([<{{<[{(()]([][])}][{([])<{}{}>}<([]<>)<{}[]>>]>{<[[[]{}]<{}[]>]<{()}({}<>)>>}}}{<{<<[(){}](()())
+{{<<<[{<([<<([{}[]])([()()][()<>>)><({[]()}[()<>])>>]<{[([{}{}]({}[]))]{(({}{}))(<<>()>({}[]))}}{{[[{}{}]<[]<
+{[([({[[<{{[{[[][]][<>()]}<<[]()>>]{<{[][]}(<>{})>}}{<[[<><>](<>())](<(){}>({}<>))>[<<[]{}><[]()>>[{[]{}
+[{{{([{([[[<{{{}()}{<>[]}}[{<>{}}[[]<>]]>]<([{{}{}]<[]()>]){{{{}()}[<>[]]}(<{}[]>{[]<>})}>][{{[<<>{}><()[]>]
+[<[(<(<([(({({{}<>})[{{}<>}{{}[]}]}{[<[]<>><{}[]>][[{}<>)<()<>>]}){{<<(){}>[{}()]><<()>([]<>)>}})
+<(([<(([([{([({})][<{}[]><[]()>]){{{[]{}}[[]<>]}<(<>())[{}()]>}}])](<[({(({})(<>()))}{{<[]>}{<<>{}>[()()]
+[<{[[{([<{[<(({}{})(<>()))><{{{}<>}<()()>}(<<>()>(<><>))>][[[[()<>]<[]{}>]]{({{}[]}<()<>>)}]}([<<[()()]
+([<{{(<(([<{<(<>{})<<>[]>><{{}[]}<[]{}}>}{(((){}){<>()})[<(){}>{<><>}]}>{<([[]()](()<>)){<()>[<><
+<(<([({<(([[[[<><>]([]{})]({[]()})](({[]}({}[]))[({}<>)(()())])]<{({[]{}}([]())){<()<>>({}<>)}}
+[<(<{(<<<([{<[()<>][<>{}]>{[<>{}][()<>]}}{{<<>{}>[<>{}]}[{(){}}[[][]]]}][{[<[]()>(<>())][<<>[]
+[<[[([[[([([[[<><>]({}())][[<>()]]](([[]]({}))[{()[]}({}<>)]))(([{()[]}{{}}][{()[]}[<>[]]])({{<>{}}{
+[<[([<<{{{[[[({}{})<()[]>]]]<(({(){}}{(){}})<[{}[]][[][]]>)>}({{([[]{}][[][]])<[<>[]][(){}]>}<[
+<[<[({{<(<<[{{<>[]}}[(()())<[]<>>]][{{{}<>}<<>[]]}]>>)<{<(((<>[]))[([])]){(<[]()>({}()))[{{}<
+({([<<<<[<{(((<>[])<()()>))<<[<>[]][<>[]]][{[][]}]>}((([[][]])({{}[]}[<><>]))({[[]<>](<>[])
+{(<{((({[{({<<()[]>(<>())>{<{}{}><<><>>}}(([[]<>][()[]]){[{}()]}))[<<<<>()}[{}[]]>(<{}{}>{<>[]
+(<<[{[([<[<[{(<>{})[{}[]]}([<>[]]<{}[]>)]>](({<{(){}}{{}{}}>}<<{[][]}<{}()>>[{[]{}}{[][]}]>}[{<([]()){<>
+{[{{<[[([<{[{([][]){<><>}}[[{}}<<>()>]](<[<><>]({}<>)>[<{}<>><{}{}>])}[<<{()[]}><{{}<>}<<><>>>>([[[]{}]<()<>>
+{[<{{([[[[{({{()<>}(<>[])})}(((<{}[]>))<[(()[])[{}{}]><<{}()><(){}>>>)][<(<(()[])((){})><[{}()]<[]<>>>
+{<{[[{<[(({[{<()()><{}<>>}{[[]{}]([]<>)}]([({}[])<[][]>]<[[]{}][()()]>)})<(((<<>{}>[<>()])(
+[[<{{{(<[{{[[({}<>)<()<>>]]>}{(<[{<>()}]<{{}()}<<>[]>>>{{(<><>){()<>}}[{(){}}<<>{}>]})[{<<()<>>
+{[{(<({[[{[{{<[]{}>{[]()}}([[][]][{}{}])}[<{<>{}}<[]<>>>({()()}{[]()})]]<(<{()()}([][])>[{<>
+[[{[[<{{({(<{([]()){()<>}}([()()]{<>})>[[{<>{}}({}<>)][{{}{}}<()[]>]])<<[<[]{}>[{}[]]]{[{}[
+({({<<{<([[<({[]()}[<>{}]){[{}()][{}<>]}>][<<<{}[]>[{}<>]><<<>>[()()]>><([[]{}]<<>()>>({[]<>}<{}[]>)>]])
+{<[{[[{<<([({{<>{}}[{}{}]}(({}{})(<>())))<[([]{}){{}()}]>]([[<[][]>]{<{}[]>{{}()}}]{{[()[]
+{[([[[({([<<(<<><>>[[][]])>([<{}[]>])>({{{<>{}}{{}{}}}{<<>{}>({})}}{[{()<>}]})])>([<[{{([]{}){()()}}[{{
+<{<(<<{[<([(<<[][]><<>[]>>(<[]{}>(<>[])))])[({(((){}){[]{}})({()()}<{}>)}<<([]())<()<>>>[{<><
+<[([{{<<[{([[{<>[]}([][])]]([(<><>){{}}]<<<>[]>[{}[]]>))([[[<>{}]{[]()}][({}())>][[[<><>]]{({}())[<>{}]}]
+{<[[[<[[{[[{((<>{})[()()])[<<>>{()<>}]}]]{{{<(<>{})<[][]>>[{<>{}}{{}[]}]}({{[]<>}(<>())}<<[]{}>>)
+{<{<<({<<(([([(){}][()<>])[{[][]}(()[])]]{((()())[[]<>])})][[[[<[]{}>[<><>]][[()()][<>]]]({[{}{}]([]()
+{[<([(({<<[<{<()><[]{}>}><((()[]))<[()][{}{}]>>](([[()[]][<><>]][({}[])[()[]]])<<{{}{}}<[]<>>>{[(
+{[([{<{[<<[({{[]{}}<<><>>}{{()<>}<[]{}>}){{{[]<>]<{}[]>}[(()[])]}][(<[{}]>{([]<>)[[]{}]}){{[[]<>]}<{()
+<({{{({[(<<<{<()[]>({}[])}{[()()]{[]<>}}>(<[[]()][<><>]>{([])<<>()>})>({{<{}()>(())}(({}){()<>})}[<<{}{}>{()
+<({[({[(<[(<[(()())][([])[{}]]>{{{<><>}(<>{})}(([]{})<()()>)})]>[(([{((){})(<>()}}[([][]){[]}]](([{}[]]<
+[<({([{{(<<<<({}())>>{{<<><>><<><>>}[({}<>){<><>}]}>[<{<[]{}>{[]()}}<[[]][[]()]>><[{{}<>}<()
+{<<<([<<[[{([<()[]>{[]()}](<<>{}>([]<>))){<<{}{}><{}{}>><<[][]>({}{})>}}{<[<[][]>[<>()]]{[()[]]{[][]}}><<{<>}
+<({[[<[{<(({{(()<>){{}<>}}[<()<>>({}[])]}<(<<>[]>{()()})[<[]{}>(()<>)]>)[<<{[]<>}(()<>)>[{(){}}<{}()>]>{({
+(<([{{{<{({[<{()[]}{{}()}>]})}](<{(<<<[]{}>({}{})>[(()<>){[]{}}]>{({{}()}{{}<>})(<()()>[<>{}])})[([[{}<
+((<(<<[((<{{[<<><>>{()<>}]([<>[]]<()<>>)}}><[(<{[]{}}{<><>}>{<{}{}>(()())})][[{[{}()][<>()]}((<><>)[<>[
+([[[([<<{((<<({}[])(<>{})><({}())[<>[]]>>){((<[][]>([]<>)){{(){}]<<>[]>}){[{<><>}[{}{}]]}}){{{[{<>()}({}[])]<
+<{{[{[<<[<{<<(()())(<>())>((<>[]))>[{[()()](<>()}}{({}<>)(<>{})}]}>]>{[<[{[{<>{}}{{}[]}]}{[<<><>>({}())](((
+{<{[(<[(({<<{[{}[]](<><>)}<<()[]><{}<>>>><(((){})[{}])<[<>()]<()<>>>>>[{<[{}<>]<<>[]>>(<[]()>(<
+[<<[[{[({(<((({}{})(<>())))[{{[]<>}<(){}>}]>([({{}<>}[[]{}])]((({}())(<>[]))([[]<>](()[])))))}[<(((({}()){<>[
+{<((((({((<[{[[][]](<><>)}{{{}{}}(()())}]{((<><>)<[]()>)<<()<>>(()<>)>}>[{([{}()]<[]<>>){<[]<>><(){}
+([{[<[<((<[<<{{}()}<<>{}>>([<><>]([]{}))>}(<{([]()){<>()}}[<[]<>>(<><>)]><(<()[]><()<>>)<[{}[]]
+<<{[[<{{[({[{{{}{}}(()[])}]})]}([{[{([()]({}{}))[([][])<()[]>]}({{[][]}{[][]>}([()()]))][<(
+<<{{[[<([{{({[()())(<><>)})}}{(<([<>()][[]<>])[{()[]}{{}{}}]>({{<>()}{{}<>}}))[({<[]><<>>}((<>()
+{<<<({[<<[{((<{}[]>([][])))[([{}{}]<{}{}>){[()()](()<>)}]}{<{(<>())<{}<>>}([[]()]({}))>{[(<>[]
+(<<[{[({{{<{[<[][]><[]()>][{()[]}[<>[]]]}><<[(<><>)<[][]>]<<<>{}><[]<>>>>({<{}()>([]<>)}({()[]}
+[<<[<[[(<{[{[[{}[]]{()<>}]<[{}{}][{}()]>}{(<{}<>>{()[]})<{()[]}[[]()]>}]}{{<((<>{})[{}()])[{<><>}<[][]>]>[<{
+{<<{([({<(<{(<[]<>>)}[<{[][]}>((<><>)((){}))]>)[(<{([]())[{}()]}<({})>>(<[()<>][[][]]>{([][])<()[]>})){[{<
+{<(([{<<[[[({{[]{}}({}<>)}[(<>{})[<>[]]])({[()[]]{[]}]<[<><>]<[]{}>>)][(<[[]()]([]<>)>(([]())({}
+(<[{([<([(<{{{<><>}<<>[]>}{[<><>][()<>]}}[(<[][]><()<>>)]>[{((()<>)<[]()))[(<><>)<()[]>]}])]<([<<{<>()
+<[(<({{({[<{({<><>}[[][]]){((){})<()[]>}}{[{[]{}}{(){}}][{{}()}{()<>}]}><{[<{}[]>((){})]({<>
+({<({(([{<{{<[<><>]{[][]}>[{()()}(()[])}}{<<<>{}>[{}{}]><{[]<>}[[][]]>}}[[{{<>{}}}<{<>[]}{(){}}>][{[(
+<{[<[([<({[[{<<>{}>{<><>}}((()<>)<()()>)]<{[[]()]({}[])}<(<>())<()[]>>>>{{{{[]{}}[{}[]]}}([
+<[(([{[[({{{{(()())<()()>}(<<>{}>{[]{}})}<{[<><>][<>()]}<{{}{}}>>}{[(<<>{}>((){}))[<[]()>{
+{<<(<(({[<[{[((){})([][])]}{<(<>{}}(<>{})>[([]<>)([][])]}]{[<((){})<()<>>><<[]<>><{}()>>]({(()<>)}{<{}<
+[[[(([([[[[[{[[]]}{({})(<>{})}){({{}}[{}[]]){<(){}><[]<>>}}]]]]){(([{(<<{}[]>(<><>)>)<<{{}[]}[<><
+([(<(((({{[<({[]<>})(([])[{}[]])>[[<<>()>[<>()]][[[][]][[][]]]]]{<<([][]){()[])>(<{}{}>[[]
+([(([(<<([[{{<{}<>>(<>[])}[[[]<>]<()()>]}[(({}<>){<>{}})[<[][]>[[][]]]]]{[{({}[])}<(<><>)[()<>]
+{(<<(<[({(<[{[(){}]<<>()>}[[{}<>]{[]{}}]]><<[(()<>){[]{}}]{{[]}{<>}}>[[(<><>)]<(<>{}){{}{}}]]>)}([<{({(
+[{<<<[{(<{(<<<(){}>[<>[]]>[{[][]}<<>{}>]>)<<[{(){}}{()<>}]<<()[]>]>([[[]()]<{}[]>](<<>()><<><>>))>}>)({[<<[
+{<[([[([({([[(<>{})][([]()){[][]}]]{(<<><>>[()<>])<<{}<>>[()]>})})]{<[<((({}<>){{}{}}){<[]()>[<
+<[<<([(((<<<([{}{}][<>()])>>{[[<()()>([]<>)]({<>{}}<{}{}>)]([(<><>)([]<>}]{{()<>}{{}[]}})}>{[[{{[]<>}<
+<[{{{[<([[{[{[()[]]}(({}()){[]()})](({()}{{}()}){([]())<[]<>>}}}<{<([]())[()<>]>([[]()][{}{}]
+<(<<<<{[{(({[{{}<>}[(){}]}}([(<>())]{[[]](<>{})}))(<[{{}<>}{<>()}][[[]{}]({}[])]>[(([]())<[]()>)[[()]{[
+[<{[<<{[{{{[<{()[]}([]<>)>((<>{}))][[((){})[<>{}]]<(<>{})>]}}<({<{{}}[{}()])<(()<>)<{}{}>>}<<
+<({<{{<{<[[(((()<>)[()()])[{[][]}<<>{}>])(<({}<>)(()<>)>[<<>{}>([]<>)])][{{[{}[]]((){})}([[][
+<<<{{<<<<({(<{{}<>}<[]>>{<<><>>{[]{}}})<(<{}>({}<>)){{<>()}[()[]]}>]<{[<()<>>([]{})]{[(){}
+(({<({[[{[{{{{{}{}}{<>{}}}[<<>{}><{}()>)}}]}(([{(<()()><{}()>)[{<>()}(<>())]}][[<{{}{}}({})>[<[]<>>
+<{<<{[{[{(<([{<>[]}<()[]>]{{[]()}<{}[]>})<[[<>()]{()[]}](([][])[{}{}])>>)<({<({}[])[{}[]]>({[]{}}
+(<([{{{(({({<{<><>}[{}<>]>})<{[(<>())[<>[]]]}{<(<>{}){{}()}>}>})(([((<[]()><[]()>))(<<()[]>{<>{}
+<[[[{{[{<<[(({{}<>}<(){}>)[<{}{}>({}())])]<{<{{}[]}>}{([()]{<>{}})}>>>(<([(((){})[(){}])])(((<(){
+[[[{{[{[([(<[{()<>}((){})]{[{}()][<><>]}>)([{<(){}>}({<>}[<>[]])]<{{{}()}(<>[])}<(()())([]