aboutsummaryrefslogtreecommitdiffstats
path: root/day2/solution.sh
blob: 1b145381e7ac3d7841eef1ad7276b7edd55ca17e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env bash

field='roygbv'

ops=$(cat input.txt)

sorted_ops=$(
    cat input.txt |
    tr 'rvbgyo' '123456' |
    grep -o . |
    sort |
    tr -d '\n' |
    tr '123456' 'rvbgyo'
)

calc() {
    local ops=$1
    local pos=0
    for (( i=0; i<${#ops}; i++ )); do
        ((pos++))
        while [ "${field:pos%${#field}:1}" != "${ops:i:1}" ]; do
            ((pos++))
        done
    done
    echo $pos
}

calc "$ops"
calc "$sorted_ops"