Compare commits

...

2 Commits

Author SHA1 Message Date
Akshay Mankar 58ebc70747
README 2023-12-01 11:28:09 +01:00
Akshay Mankar 181946dd33
[bash] Solve Day 1 2023-12-01 11:28:06 +01:00
2 changed files with 121 additions and 0 deletions

26
README.org Normal file
View File

@ -0,0 +1,26 @@
#+TITLE: Advent of Code 2023
#+OPTIONS: toc:nil
All programs read input from ~stdin~ and print output to ~stdout~. The output will be printed in this format:
#+BEGIN_SRC
Part 1 Answer: <answer1>
Part 2 Answer: <answer2>
#+END_SRC
* Haskell
Run it like this:
#+BEGIN_SRC bash
cabal run aoc2023 -- day<n> < ./input/day<n>
#+END_SRC
* Bash
Run it like this:
#+BEGIN_SRC bash
./bash/day<n>.sh < ./input/day<n>
#+END_SRC

95
bash/day1.sh Executable file
View File

@ -0,0 +1,95 @@
#!/usr/bin/env bash
set -euo pipefail
firstDigits() {
sed 's|^[^0-9]*\([0-9]\).*|\1|'
}
lastDigits() {
sed 's|^.*\([0-9]\)[^0-9]*$|\1|'
}
sumTensDigits() {
echo $(("$(xargs -I '{}' echo '{} * 10 +') 0"))
}
sumOnesDigits() {
echo $(("$(xargs -I '{}' echo '{} +') 0"))
}
detectDigits() {
sed '
s|zero|0|g;
s|twone|2|g;
s|one|1|g;
s|eightwo|8|g;
s|two|2|g;
s|eighthree|8|g;
s|three|3|g;
s|four|4|g;
s|five|5|g;
s|six|6|g;
s|seven|7|g;
s|nineight|9|g;
s|eight|8|g;
s|nine|9|g
'
}
detectReverseDigits() {
sed '
s|enorez|1|g
s|orez|0|g;
s|thgieno|8|g;
s|eno|1|g;
s|owt|2|g;
s|thgieerht|8|g;
s|eerht|3|g;
s|ruof|4|g;
s|thgievif|8|g;
s|evif|5|g;
s|xis|6|g;
s|enineves|9|g
s|neves|7|g;
s|thgie|8|g;
s|enin|9|g
'
}
part1 () {
local input=$(cat)
local tensSum=$(<<< "$input" firstDigits | sumTensDigits)
local onesSum=$(<<< "$input" lastDigits | sumOnesDigits)
echo $(("$tensSum + $onesSum"))
}
part2 () {
local input=$(cat)
local tensSum=$(<<< "$input" detectDigits | firstDigits | sumTensDigits)
local onesSum=$(<<< "$input" rev | detectReverseDigits | firstDigits | sumOnesDigits)
echo $(("$tensSum + $onesSum"))
}
main () {
local input=$(cat)
echo "Part 1 Answer: $(part1 <<< "$input")"
echo "Part 2 Answer: $(part2 <<< "$input")"
}
main