2023: Day 1

This commit is contained in:
Wesley Moore 2023-12-01 21:02:25 +10:00
parent 132f85bcd8
commit 140ec1f33c
No known key found for this signature in database
6 changed files with 1063 additions and 0 deletions

9
2023/day1/README.md Normal file
View file

@ -0,0 +1,9 @@
I completed this one with just shell tools.
Day 1:
zsh day1.zsh ../input/day1.txt
Day 2:
zsh day1.2.zsh ../input/day1.txt

22
2023/day1/day1.2.awk Normal file
View file

@ -0,0 +1,22 @@
BEGIN {
map["one"] = 1
map["two"] = 2
map["three"] = 3
map["four"] = 4
map["five"] = 5
map["six"] = 6
map["seven"] = 7
map["eight"] = 8
map["nine"] = 9
}
{
pos = match($0, "one|two|three|four|five|six|seven|eight|nine")
if (pos != 0) {
numeral = substr($0, RSTART, RLENGTH)
digit = map[numeral]
sub(numeral, digit)
}
print
}

22
2023/day1/day1.2.rev.awk Normal file
View file

@ -0,0 +1,22 @@
BEGIN {
map["eno"] = 1
map["owt"] = 2
map["eerht"] = 3
map["ruof"] = 4
map["evif"] = 5
map["xis"] = 6
map["neves"] = 7
map["thgie"] = 8
map["enin"] = 9
}
{
pos = match($0, "enin|thgie|neves|xis|evif|ruof|eerht|owt|eno")
if (pos != 0) {
numeral = substr($0, RSTART, RLENGTH)
digit = map[numeral]
sub(numeral, digit)
}
print
}

6
2023/day1/day1.2.zsh Normal file
View file

@ -0,0 +1,6 @@
#!/bin/zsh
paste -d '\0' \
<(awk -f day1.2.awk $1 | tr -d a-z | rg -o '^.') \
<(rev $1 | awk -f day1.2.rev.awk | rev | tr -d a-z | rg -o '.$') | \
awk 'BEGIN { sum = 0 } { sum += $0 } END { print sum }'

4
2023/day1/day1.zsh Normal file
View file

@ -0,0 +1,4 @@
#!/bin/zsh
paste -d '\0' <(tr -d a-z < $1 | rg -o ^.) <(tr -d a-z < $1 | rg -o .$) | \
awk 'BEGIN { sum = 0 } { sum += $0 } END { print sum }'

1000
2023/input/day1.txt Normal file

File diff suppressed because it is too large Load diff