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
3fbeb3bc
Verified
Commit
3fbeb3bc
authored
6 months ago
by
mirabilos
Committed by
mirabilos
6 months ago
Browse files
Options
Downloads
Patches
Plain Diff
cleaner Dijkstra
parent
a0e73aa5
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
2024/18/i2c
+40
-0
40 additions, 0 deletions
2024/18/i2c
2024/18/s1.c
+128
-0
128 additions, 0 deletions
2024/18/s1.c
with
168 additions
and
0 deletions
2024/18/i2c
0 → 100755
+
40
−
0
View file @
3fbeb3bc
#!/bin/mksh
set -e
function doone {
print -r -- 'static const struct ipt I[] = {'
while IFS= read -r line; do
print -r -- " {$line},"
done <"$1"
print '};'
}
exec >inputs.h
print 'struct ipt {'
print ' unsigned x;'
print ' unsigned y;'
print '};'
print
print '#ifndef PROD'
print
if [[ -s c2 ]]; then
print '#if defined(EX2)'
doone c2
for t in 3 4 5 6 7 8 9; do
if [[ -s c$t ]]; then
print "#elif defined(EX$t)"
doone c$t
fi
done
print '#else'
doone c
print '#endif'
else
doone c
fi
print
print '#else'
print
doone i
print
print '#endif'
exec >/dev/null
exit 0
This diff is collapsed.
Click to expand it.
2024/18/s1.c
0 → 100644
+
128
−
0
View file @
3fbeb3bc
#include
<err.h>
#include
<limits.h>
#include
<stdbool.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
"inputs.h"
#ifndef PROD
#define W 7
#define Z 12
#define SX 0
#define SY 0
#define EX 6
#define EY 6
#else
#define W 71
#define Z 1024
#define SX 0
#define SY 0
#define EX 70
#define EY 70
#endif
struct
node
{
struct
node
*
next
;
struct
node
*
neigh
[
4
];
unsigned
long
long
cost
;
unsigned
char
ok
:
1
;
};
#define INFCOST ((unsigned long long)-1)
static
struct
node
NN
[
W
][
W
];
#define N(y,x) (&(NN[y][x]))
static
struct
node
*
start
;
static
struct
node
*
finish
;
int
main
(
void
)
{
unsigned
int
x
,
y
,
d
;
struct
node
*
cur
,
*
unvis
,
*
prev
;
unsigned
long
long
newcost
;
for
(
y
=
0
;
y
<
W
;
++
y
)
for
(
x
=
0
;
x
<
W
;
++
x
)
{
cur
=
N
(
y
,
x
);
cur
->
neigh
[
0
]
=
y
==
0
?
NULL
:
N
(
y
-
1
,
x
);
cur
->
neigh
[
1
]
=
x
==
W
-
1
?
NULL
:
N
(
y
,
x
+
1
);
cur
->
neigh
[
2
]
=
y
==
W
-
1
?
NULL
:
N
(
y
+
1
,
x
);
cur
->
neigh
[
3
]
=
x
==
0
?
NULL
:
N
(
y
,
x
-
1
);
cur
->
cost
=
INFCOST
;
cur
->
ok
=
1
;
}
for
(
d
=
0
;
d
<
Z
;
++
d
)
{
cur
=
N
(
I
[
d
].
y
,
I
[
d
].
x
);
cur
->
ok
=
0
;
for
(
x
=
0
;
x
<
4
;
++
x
)
if
(
cur
->
neigh
[
x
])
cur
->
neigh
[
x
]
->
neigh
[
x
^
2U
]
=
NULL
;
}
if
(
!
((
start
=
N
(
SY
,
SX
))
->
ok
))
errx
(
1
,
"no start position"
);
if
(
!
((
finish
=
N
(
EY
,
EX
))
->
ok
))
errx
(
1
,
"no end position"
);
/* Dijkstra 1 */
unvis
=
start
;
prev
=
start
;
for
(
y
=
0
;
y
<
W
;
++
y
)
for
(
x
=
0
;
x
<
W
;
++
x
)
{
cur
=
N
(
y
,
x
);
if
(
cur
->
ok
)
{
prev
->
next
=
cur
;
prev
=
cur
;
}
}
/* Dijkstra 2 */
start
->
cost
=
0
;
/* Dijkstra 3 initial */
cur
=
start
;
prev
=
NULL
;
/* Dijkstra 4 */
step4:
for
(
d
=
0
;
d
<
4
;
++
d
)
if
(
cur
->
neigh
[
d
])
{
newcost
=
cur
->
cost
+
1
;
if
(
newcost
<
cur
->
neigh
[
d
]
->
cost
)
cur
->
neigh
[
d
]
->
cost
=
newcost
;
}
/* Dijkstra 5 */
if
(
prev
==
NULL
)
unvis
=
cur
->
next
;
else
prev
->
next
=
cur
->
next
;
/* Dijkstra 3 */
if
(
unvis
==
NULL
)
goto
step6
;
newcost
=
unvis
->
cost
;
prev
=
NULL
;
cur
=
unvis
;
while
(
cur
->
next
)
{
if
(
cur
->
next
->
cost
<
newcost
)
{
newcost
=
cur
->
next
->
cost
;
prev
=
cur
;
}
cur
=
cur
->
next
;
}
cur
=
prev
?
prev
->
next
:
unvis
;
if
(
newcost
<
INFCOST
)
goto
step4
;
/* Dijkstra 6 */
step6:
if
(
finish
->
cost
==
INFCOST
)
errx
(
1
,
"no solution"
);
printf
(
"%llu
\n
"
,
finish
->
cost
);
return
(
0
);
}
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