2020-12-02 17:47:40 +01:00
package main
import (
"AoC2020/helper"
"fmt"
"os"
"strconv"
"strings"
)
func main ( ) {
args := os . Args [ 1 : ]
input , err := helper . GetInput ( args [ 0 ] )
if err != nil {
fmt . Println ( err )
}
count := 0
countPart2 := 0
for _ , row := range input {
if checkPassword ( disassembleRow ( row ) ) {
2020-12-05 14:43:34 +01:00
count ++
2020-12-02 17:47:40 +01:00
}
if checkPasswordPart2 ( disassembleRow ( row ) ) {
2020-12-05 14:43:34 +01:00
countPart2 ++
2020-12-02 17:47:40 +01:00
}
}
fmt . Println ( count )
fmt . Println ( countPart2 )
}
func disassembleRow ( row string ) ( int , int , string , string ) {
fields := strings . Split ( row , " " )
scope := strings . Split ( fields [ 0 ] , "-" )
min , _ := strconv . Atoi ( scope [ 0 ] )
max , _ := strconv . Atoi ( scope [ 1 ] )
return min , max , string ( fields [ 1 ] [ 0 ] ) , fields [ 2 ]
}
func checkPassword ( min int , max int , char string , password string ) bool {
occurence := strings . Count ( password , char )
return ( min <= occurence && occurence <= max )
}
func checkPasswordPart2 ( p1 int , p2 int , char string , password string ) bool {
count := 0
2020-12-05 14:43:34 +01:00
if string ( password [ p1 - 1 ] ) == char {
2020-12-02 17:47:40 +01:00
count ++
}
2020-12-05 14:43:34 +01:00
if string ( password [ p2 - 1 ] ) == char {
count ++
2020-12-02 17:47:40 +01:00
}
return count == 1
}