mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 12:50:32 +01:00
Implement 2025 day 3 in bash
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
## Day 02: Terraform
|
# Day 02: Terraform
|
||||||
|
|
||||||
The code assumes an input file at `../inputs/02.txt`. Other than that, simply try to run as follows:
|
The code assumes an input file at `../inputs/02.txt`. Other than that, simply try to run as follows:
|
||||||
|
|
||||||
|
|||||||
4
2025/day03/README.md
Normal file
4
2025/day03/README.md
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
# Day 03: Bash
|
||||||
|
|
||||||
|
Simple as can be, simply run `./solve.sh <input file>` and get both answers. While I allowed myself
|
||||||
|
to use `coreutils` with bash, they turned out to be entirely unnecessary. Pure bash just works.
|
||||||
4
2025/day03/sample.txt
Normal file
4
2025/day03/sample.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
987654321111111
|
||||||
|
811111111111119
|
||||||
|
234234234234278
|
||||||
|
818181911112111
|
||||||
64
2025/day03/solve.sh
Executable file
64
2025/day03/solve.sh
Executable file
@@ -0,0 +1,64 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
if [[ $# -lt 1 ]]; then
|
||||||
|
echo "Usage: $0 <input file>" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
joltage() {
|
||||||
|
local first second c i
|
||||||
|
|
||||||
|
first=0
|
||||||
|
second=0
|
||||||
|
|
||||||
|
for ((i = 0; i < ${#1}; i++)); do
|
||||||
|
c="${1:$i:1}"
|
||||||
|
|
||||||
|
if [[ $second -gt $first ]]; then
|
||||||
|
first="$second"
|
||||||
|
second="$c"
|
||||||
|
elif [[ $c -gt $second ]]; then
|
||||||
|
second="$c"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "$first$second"
|
||||||
|
}
|
||||||
|
|
||||||
|
joltage2() {
|
||||||
|
local c i d e n digits
|
||||||
|
|
||||||
|
digits=(0 0 0 0 0 0 0 0 0 0 0 0)
|
||||||
|
|
||||||
|
for ((i = 0; i < ${#1}; i++)); do
|
||||||
|
c="${1:$i:1}"
|
||||||
|
|
||||||
|
for ((d = 0; d < 11; d++)); do
|
||||||
|
if [[ ${digits[((d + 1))]} -gt ${digits[$d]} ]]; then
|
||||||
|
for ((e = d; e < 11; e++)); do
|
||||||
|
n=$((e + 1))
|
||||||
|
digits[e]=${digits[n]}
|
||||||
|
done
|
||||||
|
digits[11]="0"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
if [[ $c -gt ${digits[11]} ]]; then
|
||||||
|
digits[11]="$c"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
printf "%s" "${digits[@]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
total=0
|
||||||
|
total2=0
|
||||||
|
|
||||||
|
while IFS="" read -r line || [[ -n "$p" ]]; do
|
||||||
|
((total += $(joltage "$line")))
|
||||||
|
((total2 += $(joltage2 "$line")))
|
||||||
|
done <"$1"
|
||||||
|
|
||||||
|
echo "$total"
|
||||||
|
echo "$total2"
|
||||||
Reference in New Issue
Block a user