move to 2020 fodler

This commit is contained in:
2021-12-01 11:43:46 +01:00
parent eb251ac1ea
commit c651a48895
33 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
# 2
For the first we can simply count the occurrences and see if they are between the accepted values.
Just some simple parsing.
The second one is similar, but we can be more efficient if we XOR the first and second position.
<details>
<summary>Solutions</summary>
<ol>
<li>548</li>
<li>502</li>
</ol>
</details>

View File

@@ -0,0 +1,77 @@
package main
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
)
type sRow struct {
min, max int
char, password string
}
func parse(data []byte) []sRow {
parsed := []sRow{}
for _, row := range strings.Split(strings.TrimSpace(string(data)), "\n") {
s0 := strings.Split(row, ":")
rule := strings.TrimSpace(s0[0])
password := strings.TrimSpace(s0[1])
s1 := strings.Split(rule, " ")
minMax := strings.TrimSpace(s1[0])
char := strings.TrimSpace(s1[1])
s2 := strings.Split(minMax, "-")
min, _ := strconv.Atoi(strings.TrimSpace(s2[0]))
max, _ := strconv.Atoi(strings.TrimSpace(s2[1]))
r := sRow{
min: min,
max: max,
char: char,
password: password,
}
parsed = append(parsed, r)
}
return parsed
}
func validSimple(rows []sRow) int {
valid := 0
for _, row := range rows {
count := strings.Count(row.password, row.char)
if row.min <= count && count <= row.max {
valid++
}
}
return valid
}
func validComplex(rows []sRow) int {
valid := 0
for _, row := range rows {
l := len(row.password)
min := row.min - 1
max := row.max - 1
if min >= l || max >= l {
continue
}
r := []rune(row.password)
a := string(r[min]) == row.char
b := string(r[max]) == row.char
if a != b {
valid++
}
}
return valid
}
func main() {
data, _ := ioutil.ReadFile("./solutions/2/data.txt")
rows := parse(data)
simple := validSimple(rows)
fmt.Println(simple)
complex := validComplex(rows)
fmt.Println(complex)
}

View File

@@ -0,0 +1,27 @@
from os.path import join, dirname
def checkRow(row: str, alternative=False) -> bool:
rule, password = map(lambda s: s.strip(), row.split(':'))
amount, char = rule.split(' ')
minimum, maximum = map(int, amount.split('-'))
if alternative:
return (password[minimum - 1] == char) ^ (password[maximum - 1] == char)
else:
occurrences = password.count(char)
return minimum <= occurrences <= maximum
data = join(dirname(__file__), '../data.txt')
with open(data) as f:
valid = 0
valid_alt = 0
rows = list(f.read().strip().split('\n'))
for row in rows:
if(checkRow(row)):
valid += 1
if(checkRow(row, alternative=True)):
valid_alt += 1
print(f'Found {valid} valid passwords.')
print('Policy changed...')
print(f'Found {valid_alt} valid passwords.')