Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
adventofcode.com
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirabilos
adventofcode.com
Commits
16664664
Verified
Commit
16664664
authored
6 months ago
by
mirabilos
Committed by
mirabilos
6 months ago
Browse files
Options
Downloads
Patches
Plain Diff
slightly better abort condition in s2 than s1c though
parent
e074b301
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
2024/13/s1
+102
-0
102 additions, 0 deletions
2024/13/s1
2024/13/s1b
+110
-0
110 additions, 0 deletions
2024/13/s1b
2024/13/s1c
+110
-0
110 additions, 0 deletions
2024/13/s1c
2024/13/s2
+110
-0
110 additions, 0 deletions
2024/13/s2
with
432 additions
and
0 deletions
2024/13/s1
0 → 100755
+
102
−
0
View file @
16664664
#!/bin/mksh
die() {
print -ru2 -- "E: $*"
exit 1
}
bc |&
function tobc {
print -pr -- "$@" || die coprocess died trying to write
}
function frombc {
read -p "$@" || die coprocess died trying to read
}
function quitbc {
trap - HUP INT QUIT TRAP PIPE TERM
exec 3>&p
print -ru3 -- quit
exec 3>&-
:
}
function trapbc {
local i=$1
exec 3>&p
exec 3>&-
wait
print -u2
print -ru2 -- "E: killed by signal $i"
exit $((128 + i))
}
trap 'trapbc 1' HUP
trap 'trapbc 2' INT
trap 'trapbc 3' QUIT
trap 'trapbc 5' TRAP
trap 'trapbc 13' PIPE
trap 'trapbc 15' TERM
tobc 'scale = 30'
tobc 'define r(x) {'
tobc ' auto o'
tobc ' o = scale'
tobc ' scale = 0'
tobc ' x /= 1'
tobc ' scale = o'
tobc ' return (x)'
tobc '}'
tobc 'define i(x) {'
tobc ' if (x == r(x)) return (1)'
tobc ' return (0)'
tobc '}'
tobc 'define s() {'
tobc ' auto a, b, i, j, l;'
tobc ' l = 9999999'
tobc ' for (b = 0; b < 101; b++) {'
tobc ' a = (g - b * e) / c'
tobc ' i = (h - b * f) / d'
tobc ' if (a != i) continue'
tobc ' if (i(a) != 1) continue'
tobc ' j = r(3 * a + b)'
tobc ' if (j < l) l = j'
tobc ' }'
tobc ' if (l < 9999999) {'
tobc ' print "winner: ", l, "\n";'
tobc ' return (l)'
tobc ' }'
tobc ' print "no winner\n";'
tobc ' return (0)'
tobc '}'
tobc 'z = 0'
while :; do
read -r b a x y || die 'cannot read line 1'
[[ $b$a = ButtonA: ]] || die 'wrong line 1'
x=${x#X+}
tobc "c = ${x%,}"
tobc "d = ${y#Y+}"
read -r b a x y || die 'cannot read line 2'
[[ $b$a = ButtonB: ]] || die 'wrong line 2'
x=${x#X+}
tobc "e = ${x%,}"
tobc "f = ${y#Y+}"
read -r b x y || die 'cannot read line 3'
[[ $b = Prize: ]] || die 'wrong line 3'
x=${x#X=}
tobc "g = ${x%,}"
tobc "h = ${y#Y=}"
tobc "z += s()"
frombc r
print -r -- "round $((++rnd)): $r"
read -r r || break
[[ -z $r ]] || die 'line 4 not empty'
done <inputfile
tobc 'print "z = ", z, "\n"'
frombc r
print -r -- ":: $r"
quitbc
This diff is collapsed.
Click to expand it.
2024/13/s1b
0 → 100755
+
110
−
0
View file @
16664664
#!/bin/mksh
# s2 without the 10000000000000 factor, to validate
die() {
print -ru2 -- "E: $*"
exit 1
}
bc |&
function tobc {
print -pr -- "$@" || die coprocess died trying to write
}
function frombc {
read -p "$@" || die coprocess died trying to read
}
function quitbc {
trap - HUP INT QUIT TRAP PIPE TERM
exec 3>&p
print -ru3 -- quit
exec 3>&-
:
}
function trapbc {
local i=$1
exec 3>&p
exec 3>&-
wait
print -u2
print -ru2 -- "E: killed by signal $i"
exit $((128 + i))
}
trap 'trapbc 1' HUP
trap 'trapbc 2' INT
trap 'trapbc 3' QUIT
trap 'trapbc 5' TRAP
trap 'trapbc 13' PIPE
trap 'trapbc 15' TERM
tobc 'scale = 30'
tobc 'define r(x) {'
tobc ' auto o'
tobc ' o = scale'
tobc ' scale = 0'
tobc ' x /= 1'
tobc ' scale = o'
tobc ' return (x)'
tobc '}'
tobc 'define i(x) {'
tobc ' if (x == r(x)) return (1)'
tobc ' return (0)'
tobc '}'
tobc 'define s() {'
tobc ' auto a, b, i, j, l, q, r, s;'
tobc ' l = 9999999999999999999999999999999999'
tobc ' b = -1'
tobc ' while (1) {'
tobc ' b = b + 1'
tobc ' if (((b * e) > g) || ((b * f) > h)) break'
tobc ' a = (g - b * e) / c'
tobc ' i = (h - b * f) / d'
tobc ' if (a != i) continue'
tobc ' if (i(a) != 1) continue'
tobc ' j = r(3 * a + b)'
tobc ' if (j < l) l = j'
tobc ' }'
tobc ' if (l < 9999999999999999999999999999999999) {'
tobc ' print "winner: ", l, "\n";'
tobc ' return (l)'
tobc ' }'
tobc ' print "no winner\n";'
tobc ' return (0)'
tobc '}'
tobc 'z = 0'
while :; do
read -r b a x y || die 'cannot read line 1'
[[ $b$a = ButtonA: ]] || die 'wrong line 1'
x=${x#X+}
tobc "c = ${x%,}"
tobc "d = ${y#Y+}"
read -r b a x y || die 'cannot read line 2'
[[ $b$a = ButtonB: ]] || die 'wrong line 2'
x=${x#X+}
tobc "e = ${x%,}"
tobc "f = ${y#Y+}"
read -r b x y || die 'cannot read line 3'
[[ $b = Prize: ]] || die 'wrong line 3'
x=${x#X=}
tobc "g = ${x%,}"
tobc "h = ${y#Y=}"
tobc "z += s()"
frombc r
# while [[ $r != *winner* ]]; do
# print -r -- "D: $r"
# frombc r
# done
print -r -- "round $((++rnd)): $r"
read -r r || break
[[ -z $r ]] || die 'line 4 not empty'
done <inputfile
tobc 'print "z = ", z, "\n"'
frombc r
print -r -- ":: $r"
quitbc
This diff is collapsed.
Click to expand it.
2024/13/s1c
0 → 100755
+
110
−
0
View file @
16664664
#!/bin/mksh
# s2 without the 10000000000000 factor, to validate
die() {
print -ru2 -- "E: $*"
exit 1
}
bc |&
function tobc {
print -pr -- "$@" || die coprocess died trying to write
}
function frombc {
read -p "$@" || die coprocess died trying to read
}
function quitbc {
trap - HUP INT QUIT TRAP PIPE TERM
exec 3>&p
print -ru3 -- quit
exec 3>&-
:
}
function trapbc {
local i=$1
exec 3>&p
exec 3>&-
wait
print -u2
print -ru2 -- "E: killed by signal $i"
exit $((128 + i))
}
trap 'trapbc 1' HUP
trap 'trapbc 2' INT
trap 'trapbc 3' QUIT
trap 'trapbc 5' TRAP
trap 'trapbc 13' PIPE
trap 'trapbc 15' TERM
tobc 'scale = 30'
tobc 'define r(x) {'
tobc ' auto o'
tobc ' o = scale'
tobc ' scale = 0'
tobc ' x /= 1'
tobc ' scale = o'
tobc ' return (x)'
tobc '}'
tobc 'define i(x) {'
tobc ' if (x == r(x)) return (1)'
tobc ' return (0)'
tobc '}'
tobc 'define s() {'
tobc ' auto a, b, i, j, l, q, r, s;'
tobc ' l = 9999999999999999999999999999999999'
tobc ' b = (3*h*c - 3*g*d) / ((c - 3*e)*d - (d - 3*f)*c) - 20'
tobc ' while (1) {'
tobc ' b = b + 1'
tobc ' if (((b * e) > g) || ((b * f) > h)) break'
tobc ' a = (g - b * e) / c'
tobc ' i = (h - b * f) / d'
tobc ' if (a != i) continue'
tobc ' if (i(a) != 1) continue'
tobc ' j = r(3 * a + b)'
tobc ' if (j < l) l = j'
tobc ' }'
tobc ' if (l < 9999999999999999999999999999999999) {'
tobc ' print "winner: ", l, "\n";'
tobc ' return (l)'
tobc ' }'
tobc ' print "no winner\n";'
tobc ' return (0)'
tobc '}'
tobc 'z = 0'
while :; do
read -r b a x y || die 'cannot read line 1'
[[ $b$a = ButtonA: ]] || die 'wrong line 1'
x=${x#X+}
tobc "c = ${x%,}"
tobc "d = ${y#Y+}"
read -r b a x y || die 'cannot read line 2'
[[ $b$a = ButtonB: ]] || die 'wrong line 2'
x=${x#X+}
tobc "e = ${x%,}"
tobc "f = ${y#Y+}"
read -r b x y || die 'cannot read line 3'
[[ $b = Prize: ]] || die 'wrong line 3'
x=${x#X=}
tobc "g = ${x%,}"
tobc "h = ${y#Y=}"
tobc "z += s()"
frombc r
# while [[ $r != *winner* ]]; do
# print -r -- "D: $r"
# frombc r
# done
print -r -- "round $((++rnd)): $r"
read -r r || break
[[ -z $r ]] || die 'line 4 not empty'
done <inputfile
tobc 'print "z = ", z, "\n"'
frombc r
print -r -- ":: $r"
quitbc
This diff is collapsed.
Click to expand it.
2024/13/s2
0 → 100755
+
110
−
0
View file @
16664664
#!/bin/mksh
# s2 without the 10000000000000 factor, to validate
die() {
print -ru2 -- "E: $*"
exit 1
}
bc |&
function tobc {
print -pr -- "$@" || die coprocess died trying to write
}
function frombc {
read -p "$@" || die coprocess died trying to read
}
function quitbc {
trap - HUP INT QUIT TRAP PIPE TERM
exec 3>&p
print -ru3 -- quit
exec 3>&-
:
}
function trapbc {
local i=$1
exec 3>&p
exec 3>&-
wait
print -u2
print -ru2 -- "E: killed by signal $i"
exit $((128 + i))
}
trap 'trapbc 1' HUP
trap 'trapbc 2' INT
trap 'trapbc 3' QUIT
trap 'trapbc 5' TRAP
trap 'trapbc 13' PIPE
trap 'trapbc 15' TERM
tobc 'scale = 30'
tobc 'define r(x) {'
tobc ' auto o'
tobc ' o = scale'
tobc ' scale = 0'
tobc ' x /= 1'
tobc ' scale = o'
tobc ' return (x)'
tobc '}'
tobc 'define i(x) {'
tobc ' if (x == r(x)) return (1)'
tobc ' return (0)'
tobc '}'
tobc 'define s() {'
tobc ' auto a, b, i, j, l, n, q, r, s;'
tobc ' l = 9999999999999999999999999999999999'
tobc ' b = (3*h*c - 3*g*d) / ((c - 3*e)*d - (d - 3*f)*c) - 100'
tobc ' for (n = 0; n < 250; ++n) {'
tobc ' b = b + 1'
tobc ' if (((b * e) > g) || ((b * f) > h)) break'
tobc ' a = (g - b * e) / c'
tobc ' i = (h - b * f) / d'
tobc ' if (a != i) continue'
tobc ' if (i(a) != 1) continue'
tobc ' j = r(3 * a + b)'
tobc ' if (j < l) l = j'
tobc ' }'
tobc ' if (l < 9999999999999999999999999999999999) {'
tobc ' print "winner: ", l, "\n";'
tobc ' return (l)'
tobc ' }'
tobc ' print "no winner\n";'
tobc ' return (0)'
tobc '}'
tobc 'z = 0'
while :; do
read -r b a x y || die 'cannot read line 1'
[[ $b$a = ButtonA: ]] || die 'wrong line 1'
x=${x#X+}
tobc "c = ${x%,}"
tobc "d = ${y#Y+}"
read -r b a x y || die 'cannot read line 2'
[[ $b$a = ButtonB: ]] || die 'wrong line 2'
x=${x#X+}
tobc "e = ${x%,}"
tobc "f = ${y#Y+}"
read -r b x y || die 'cannot read line 3'
[[ $b = Prize: ]] || die 'wrong line 3'
x=${x#X=}
tobc "g = ${x%,} + 10000000000000"
tobc "h = ${y#Y=} + 10000000000000"
tobc "z += s()"
frombc r
# while [[ $r != *winner* ]]; do
# print -r -- "D: $r"
# frombc r
# done
print -r -- "round $((++rnd)): $r"
read -r r || break
[[ -z $r ]] || die 'line 4 not empty'
done <inputfile
tobc 'print "z = ", z, "\n"'
frombc r
print -r -- ":: $r"
quitbc
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment