├── _config.yml ├── Bahn-Simulator ├── .gitignore ├── go.mod ├── test │ ├── stationCapacity │ │ ├── output.txt │ │ └── input.txt │ ├── testLineForthBack │ │ ├── input.txt │ │ └── output.txt │ ├── simple │ │ ├── output.txt │ │ └── input.txt │ ├── kapazität │ │ ├── output.txt │ │ └── input.txt │ ├── unusedWildcardTrain │ │ ├── input.txt │ │ └── output.txt │ └── large │ │ └── output.txt ├── Makefile ├── main_test.go ├── licenses-third-party │ └── go.LICENSE ├── main.go ├── parsePlan.go ├── passanger.go ├── world.go ├── train.go ├── parseInput.go └── LICENSE ├── cfp-InformatiCup_2022.pdf ├── logos ├── informaticup-logo.png ├── informaticup-logo-with-text.png └── README.md ├── informatiCup 2022 - Abfahrt!.pdf ├── README.md └── ICUPCI.md /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman 2 | -------------------------------------------------------------------------------- /Bahn-Simulator/.gitignore: -------------------------------------------------------------------------------- 1 | ICUP2022* 2 | Bahn-Simulator* 3 | build-* 4 | dist 5 | -------------------------------------------------------------------------------- /Bahn-Simulator/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/informatiCup/informatiCup2022/Bahn-Simulator 2 | 3 | go 1.16 4 | -------------------------------------------------------------------------------- /cfp-InformatiCup_2022.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/informatiCup/informatiCup2022/HEAD/cfp-InformatiCup_2022.pdf -------------------------------------------------------------------------------- /logos/informaticup-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/informatiCup/informatiCup2022/HEAD/logos/informaticup-logo.png -------------------------------------------------------------------------------- /informatiCup 2022 - Abfahrt!.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/informatiCup/informatiCup2022/HEAD/informatiCup 2022 - Abfahrt!.pdf -------------------------------------------------------------------------------- /logos/informaticup-logo-with-text.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/informatiCup/informatiCup2022/HEAD/logos/informaticup-logo-with-text.png -------------------------------------------------------------------------------- /Bahn-Simulator/test/stationCapacity/output.txt: -------------------------------------------------------------------------------- 1 | [Train:T1] 2 | 2 Depart L1 3 | 4 | [Train:T2] 5 | 1 Depart L2 6 | 7 | [Passenger:P1] 8 | 1 Board T1 9 | 5 Detrain 10 | 11 | -------------------------------------------------------------------------------- /logos/README.md: -------------------------------------------------------------------------------- 1 | # informatiCup-Logos 2 | 3 | Die Logos in diesem Verzeichnis dürfen in unveränderter Weise in Ausarbeitungen und Präsentationen für den informatiCup verwendet werden. 4 | -------------------------------------------------------------------------------- /Bahn-Simulator/test/testLineForthBack/input.txt: -------------------------------------------------------------------------------- 1 | [Stations] 2 | S1 1 3 | S2 1 4 | 5 | [Lines] 6 | L1 S1 S2 2 1 7 | 8 | [Trains] 9 | T1 * 1 2 10 | 11 | [Passengers] 12 | P1 S1 S2 1 10 13 | P2 S1 S2 2 20 14 | -------------------------------------------------------------------------------- /Bahn-Simulator/test/simple/output.txt: -------------------------------------------------------------------------------- 1 | [Train:T1] 2 | 2 Depart L2 3 | 4 | [Train:T2] 5 | 0 Start S2 6 | 2 Depart L1 7 | 8 | [Passenger:P1] 9 | 1 Board T2 10 | 6 Detrain 11 | 12 | [Passenger:P2] 13 | 1 Board T1 14 | 3 Detrain 15 | -------------------------------------------------------------------------------- /Bahn-Simulator/test/testLineForthBack/output.txt: -------------------------------------------------------------------------------- 1 | [Train:T1] 2 | 0 Start S1 3 | 2 Depart L1 4 | 7 Depart L1 5 | 12 Depart L1 6 | 7 | [Passenger:P1] 8 | 1 Board T1 9 | 6 Detrain 10 | 11 | [Passenger:P2] 12 | 11 Board T1 13 | 16 Detrain 14 | 15 | -------------------------------------------------------------------------------- /Bahn-Simulator/test/kapazität/output.txt: -------------------------------------------------------------------------------- 1 | [Train:T1] 2 | 2 Depart L1 3 | 4 | [Train:T2] 5 | 2 Depart L1 6 | 7 | [Train:T3] 8 | 2 Depart L1 9 | 10 | [Train:T4] 11 | 2 Depart L1 12 | 13 | [Train:T5] 14 | 2 Depart L1 15 | 16 | [Passenger:P1] 17 | 1 Board T1 18 | 4 Detrain 19 | 20 | [Passenger:P2] 21 | 1 Board T2 22 | 4 Detrain 23 | 24 | [Passenger:P3] 25 | 1 Board T3 26 | 4 Detrain -------------------------------------------------------------------------------- /Bahn-Simulator/test/stationCapacity/input.txt: -------------------------------------------------------------------------------- 1 | # Bahnhöfe: str(ID) 2 | [Stations] 3 | S1 1 4 | S2 1 5 | S3 1 6 | 7 | # Strecken: str(ID) str(Anfang) str(Ende) dec(Länge) int(Kapazität) 8 | [Lines] 9 | L1 S1 S2 4 1 10 | L2 S2 S3 4 1 11 | 12 | # Züge: str(ID) str(Startbahnhof)/* dec(Geschwindigkeit) int(Kapazität) 13 | [Trains] 14 | T1 S1 2 50 15 | T2 S2 2 50 16 | 17 | # Passagiere: str(ID) str(Startbahnhof) str(Zielbahnhof) int(Gruppengröße) int(Ankunftszeit) 18 | [Passengers] 19 | P1 S1 S2 50 4 20 | 21 | -------------------------------------------------------------------------------- /Bahn-Simulator/test/simple/input.txt: -------------------------------------------------------------------------------- 1 | # Bahnhöfe: str(ID) 2 | [Stations] 3 | S1 2 4 | S2 2 5 | S3 2 6 | 7 | # Strecken: str(ID) str(Anfang) str(Ende) dec(Länge) int(Kapazität) 8 | [Lines] 9 | L1 S2 S3 3.14 1 10 | L2 S2 S1 4 1 11 | 12 | # Züge: str(ID) str(Startbahnhof)/* dec(Geschwindigkeit) int(Kapazität) 13 | [Trains] 14 | T1 S2 5.5 30 15 | T2 * 0.9999999 50 16 | 17 | # Passagiere: str(ID) str(Startbahnhof) str(Zielbahnhof) int(Gruppengröße) int(Ankunftszeit) 18 | [Passengers] 19 | P1 S2 S3 3 3 20 | P2 S2 S1 10 3 21 | 22 | -------------------------------------------------------------------------------- /Bahn-Simulator/test/kapazität/input.txt: -------------------------------------------------------------------------------- 1 | # Bahnhöfe: str(ID) 2 | [Stations] 3 | S1 5 4 | S2 5 5 | 6 | # Strecken: str(ID) str(Anfang) str(Ende) dec(Länge) int(Kapazität) 7 | [Lines] 8 | L1 S1 S2 1 3 9 | 10 | # Züge: str(ID) str(Startbahnhof)/* dec(Geschwindigkeit) int(Kapazität) 11 | [Trains] 12 | T1 S1 1 1 13 | T2 S1 1 1 14 | T3 S1 1 1 15 | T4 S1 1 1 16 | T5 S1 1 1 17 | 18 | # Passagiere: str(ID) str(Startbahnhof) str(Zielbahnhof) int(Gruppengröße) int(Ankunftszeit) 19 | [Passengers] 20 | P1 S1 S2 1 4 21 | P2 S1 S2 1 4 22 | P3 S1 S2 1 4 23 | 24 | -------------------------------------------------------------------------------- /Bahn-Simulator/Makefile: -------------------------------------------------------------------------------- 1 | all: release 2 | 3 | clean: 4 | rm -rf build-win build-linux build-darwin dist 5 | 6 | dist: clean 7 | mkdir build-win 8 | mkdir build-linux 9 | mkdir build-darwin 10 | CGO_ENABLED=0 GOOS=windows go build -o build-win/bahn-simulator.exe 11 | CGO_ENABLED=0 GOOS=linux go build -o build-linux/bahn-simulator 12 | CGO_ENABLED=0 GOOS=darwin go build -o build-darwin/bahn-simulator 13 | 14 | release: dist 15 | mkdir dist 16 | cp -r licenses-third-party build-win 17 | cp -r licenses-third-party build-linux 18 | cp -r licenses-third-party build-darwin 19 | cp LICENSE build-win 20 | cp LICENSE build-linux 21 | cp LICENSE build-darwin 22 | zip dist/bahn-simulator-win.zip build-win/* build-win/*/* 23 | tar czvf dist/bahn-simulator-linux.tar.gz build-linux/* 24 | tar czvf dist/bahn-simulator-mac.tar.gz build-darwin/* 25 | -------------------------------------------------------------------------------- /Bahn-Simulator/main_test.go: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: Apache-2.0 2 | // Copyright 2021 Marcus Soll 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | 16 | package main 17 | 18 | import ( 19 | "fmt" 20 | "os" 21 | "path" 22 | "testing" 23 | ) 24 | 25 | func TestMain(t *testing.T) { 26 | dirs, err := os.ReadDir("test/") 27 | if err != nil { 28 | fmt.Println("can not read test dir:", err.Error()) 29 | t.FailNow() 30 | } 31 | for i := range dirs { 32 | if dirs[i].IsDir() { 33 | input := path.Join("test", dirs[i].Name(), "input.txt") 34 | output := path.Join("test", dirs[i].Name(), "output.txt") 35 | _, successful := runSimulation(input, output, false) 36 | if !successful { 37 | fmt.Println("Test", dirs[i].Name(), "failed") 38 | t.Fail() 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Bahn-Simulator/test/unusedWildcardTrain/input.txt: -------------------------------------------------------------------------------- 1 | [Stations] 2 | S1 3 3 | S2 3 4 | S3 2 5 | S4 1 6 | S5 3 7 | 8 | [Lines] 9 | L1 S1 S2 4.62768 2 10 | L2 S1 S3 4.94065 1 11 | L3 S1 S5 4.92471 2 12 | L4 S2 S3 4.15831 1 13 | L5 S2 S4 4.22590 1 14 | L6 S3 S4 6.30938 2 15 | L7 S3 S5 5.61106 2 16 | L8 S4 S5 4.85683 1 17 | 18 | [Trains] 19 | T1 S3 1.18571 7 20 | T2 S5 2.94952 6 21 | T3 S1 3.37228 9 22 | T4 S2 2.23773 8 23 | T5 * 1.16584 9 24 | T6 S2 3.50270 5 25 | T7 S5 1.46495 9 26 | T8 * 3.79408 6 27 | T9 S1 3.52201 6 28 | T10 * 2.90067 7 29 | 30 | [Passengers] 31 | P1 S3 S2 3 127 32 | P2 S2 S1 3 68 33 | P3 S4 S1 3 189 34 | P4 S4 S2 3 80 35 | P5 S3 S1 2 23 36 | P6 S4 S1 3 27 37 | P7 S1 S5 3 30 38 | P8 S3 S1 3 101 39 | P9 S5 S1 1 33 40 | P10 S4 S2 2 17 41 | P11 S3 S2 1 117 42 | P12 S4 S5 2 195 43 | P13 S2 S1 1 80 44 | P14 S2 S5 1 18 45 | P15 S4 S3 1 133 46 | P16 S5 S2 3 194 47 | P17 S3 S5 1 208 48 | P18 S2 S5 2 134 49 | P19 S4 S1 2 112 50 | P20 S5 S2 3 171 51 | P21 S4 S1 3 14 52 | P22 S2 S5 2 69 53 | P23 S4 S3 3 121 54 | P24 S5 S4 1 41 55 | P25 S2 S5 3 162 56 | P26 S2 S4 2 43 57 | P27 S3 S4 2 59 58 | P28 S2 S3 2 20 59 | P29 S4 S3 3 205 60 | P30 S3 S5 1 124 61 | P31 S1 S4 3 114 62 | P32 S4 S5 3 54 63 | P33 S2 S1 2 112 64 | P34 S4 S2 2 66 65 | P35 S5 S4 1 195 66 | P36 S1 S2 1 108 67 | P37 S5 S2 1 135 68 | P38 S3 S5 3 159 69 | P39 S1 S4 1 77 70 | P40 S1 S2 1 138 71 | P41 S4 S3 2 179 72 | 73 | -------------------------------------------------------------------------------- /Bahn-Simulator/licenses-third-party/go.LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009 The Go Authors. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are 5 | met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above 10 | copyright notice, this list of conditions and the following disclaimer 11 | in the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of Google Inc. nor the names of its 14 | contributors may be used to endorse or promote products derived from 15 | this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /Bahn-Simulator/test/unusedWildcardTrain/output.txt: -------------------------------------------------------------------------------- 1 | [Train:T3] 2 | 1 Depart L2 3 | 4 Depart L4 4 | 8 Depart L1 5 | 11 Depart L1 6 | 13 Depart L5 7 | 16 Depart L5 8 | 18 Depart L1 9 | 21 Depart L1 10 | 23 Depart L5 11 | 26 Depart L5 12 | 29 Depart L4 13 | 32 Depart L2 14 | 35 Depart L1 15 | 37 Depart L5 16 | 40 Depart L5 17 | 42 Depart L1 18 | 46 Depart L3 19 | 49 Depart L7 20 | 52 Depart L2 21 | 55 Depart L3 22 | 58 Depart L3 23 | 61 Depart L1 24 | 63 Depart L5 25 | 66 Depart L5 26 | 69 Depart L4 27 | 72 Depart L4 28 | 75 Depart L5 29 | 78 Depart L8 30 | 81 Depart L3 31 | 83 Depart L1 32 | 86 Depart L1 33 | 89 Depart L1 34 | 92 Depart L1 35 | 94 Depart L3 36 | 97 Depart L8 37 | 100 Depart L6 38 | 103 Depart L7 39 | 106 Depart L3 40 | 108 Depart L1 41 | 111 Depart L4 42 | 114 Depart L7 43 | 117 Depart L3 44 | 119 Depart L1 45 | 122 Depart L1 46 | 124 Depart L3 47 | 127 Depart L8 48 | 130 Depart L5 49 | 132 Depart L1 50 | 135 Depart L3 51 | 138 Depart L3 52 | 140 Depart L1 53 | 143 Depart L5 54 | 146 Depart L5 55 | 148 Depart L1 56 | 151 Depart L1 57 | 154 Depart L1 58 | 156 Depart L3 59 | 159 Depart L8 60 | 162 Depart L6 61 | 165 Depart L7 62 | 168 Depart L8 63 | 171 Depart L5 64 | 174 Depart L1 65 | 176 Depart L3 66 | 179 Depart L3 67 | 181 Depart L1 68 | 184 Depart L5 69 | 187 Depart L6 70 | 190 Depart L6 71 | 193 Depart L5 72 | 196 Depart L4 73 | 199 Depart L6 74 | 202 Depart L6 75 | 206 Depart L7 76 | 209 Depart L3 77 | 212 Depart L1 78 | 214 Depart L5 79 | 218 Depart L8 80 | 221 Depart L3 81 | 223 Depart L1 82 | 226 Depart L1 83 | 229 Depart L1 84 | 231 Depart L5 85 | 234 Depart L5 86 | 237 Depart L1 87 | 239 Depart L3 88 | 242 Depart L8 89 | 245 Depart L5 90 | 247 Depart L1 91 | 250 Depart L1 92 | 253 Depart L1 93 | 255 Depart L3 94 | 258 Depart L3 95 | 260 Depart L1 96 | 263 Depart L4 97 | 266 Depart L7 98 | 269 Depart L3 99 | 272 Depart L1 100 | 274 Depart L5 101 | 277 Depart L5 102 | 279 Depart L1 103 | 282 Depart L1 104 | 285 Depart L5 105 | 288 Depart L6 106 | 107 | [Passenger:P1] 108 | 3 Board T3 109 | 6 Detrain 110 | 111 | [Passenger:P2] 112 | 7 Board T3 113 | 10 Detrain 114 | 115 | [Passenger:P3] 116 | 15 Board T3 117 | 20 Detrain 118 | 119 | [Passenger:P4] 120 | 25 Board T3 121 | 28 Detrain 122 | 123 | [Passenger:P5] 124 | 31 Board T3 125 | 34 Detrain 126 | 127 | [Passenger:P6] 128 | 39 Board T3 129 | 44 Detrain 130 | 131 | [Passenger:P7] 132 | 45 Board T3 133 | 48 Detrain 134 | 135 | [Passenger:P8] 136 | 51 Board T3 137 | 54 Detrain 138 | 139 | [Passenger:P9] 140 | 57 Board T3 141 | 60 Detrain 142 | 143 | [Passenger:P10] 144 | 65 Board T3 145 | 68 Detrain 146 | 147 | [Passenger:P11] 148 | 71 Board T3 149 | 74 Detrain 150 | 151 | [Passenger:P12] 152 | 77 Board T3 153 | 80 Detrain 154 | 155 | [Passenger:P13] 156 | 85 Board T3 157 | 88 Detrain 158 | 159 | [Passenger:P14] 160 | 91 Board T3 161 | 96 Detrain 162 | 163 | [Passenger:P15] 164 | 99 Board T3 165 | 102 Detrain 166 | 167 | [Passenger:P16] 168 | 105 Board T3 169 | 110 Detrain 170 | 171 | [Passenger:P17] 172 | 113 Board T3 173 | 116 Detrain 174 | 175 | [Passenger:P18] 176 | 121 Board T3 177 | 126 Detrain 178 | 179 | [Passenger:P19] 180 | 129 Board T3 181 | 134 Detrain 182 | 183 | [Passenger:P20] 184 | 137 Board T3 185 | 142 Detrain 186 | 187 | [Passenger:P21] 188 | 145 Board T3 189 | 150 Detrain 190 | 191 | [Passenger:P22] 192 | 153 Board T3 193 | 158 Detrain 194 | 195 | [Passenger:P23] 196 | 161 Board T3 197 | 164 Detrain 198 | 199 | [Passenger:P24] 200 | 167 Board T3 201 | 170 Detrain 202 | 203 | [Passenger:P25] 204 | 173 Board T3 205 | 178 Detrain 206 | 207 | [Passenger:P26] 208 | 183 Board T3 209 | 186 Detrain 210 | 211 | [Passenger:P27] 212 | 189 Board T3 213 | 192 Detrain 214 | 215 | [Passenger:P28] 216 | 195 Board T3 217 | 198 Detrain 218 | 219 | [Passenger:P29] 220 | 201 Board T3 221 | 204 Detrain 222 | 223 | [Passenger:P30] 224 | 205 Board T3 225 | 208 Detrain 226 | 227 | [Passenger:P31] 228 | 211 Board T3 229 | 216 Detrain 230 | 231 | [Passenger:P32] 232 | 217 Board T3 233 | 220 Detrain 234 | 235 | [Passenger:P33] 236 | 225 Board T3 237 | 228 Detrain 238 | 239 | [Passenger:P34] 240 | 233 Board T3 241 | 236 Detrain 242 | 243 | [Passenger:P35] 244 | 241 Board T3 245 | 244 Detrain 246 | 247 | [Passenger:P36] 248 | 249 Board T3 249 | 252 Detrain 250 | 251 | [Passenger:P37] 252 | 257 Board T3 253 | 262 Detrain 254 | 255 | [Passenger:P38] 256 | 265 Board T3 257 | 268 Detrain 258 | 259 | [Passenger:P39] 260 | 271 Board T3 261 | 276 Detrain 262 | 263 | [Passenger:P40] 264 | 281 Board T3 265 | 284 Detrain 266 | 267 | [Passenger:P41] 268 | 287 Board T3 269 | 290 Detrain 270 | 271 | -------------------------------------------------------------------------------- /Bahn-Simulator/main.go: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: Apache-2.0 2 | // Copyright 2021 Marcus Soll 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | 16 | package main 17 | 18 | import ( 19 | "flag" 20 | "fmt" 21 | "log" 22 | "math/big" 23 | "os" 24 | "runtime/pprof" 25 | "sync" 26 | ) 27 | 28 | func main() { 29 | inputPath := flag.String("input", "input.txt", "path to input file") 30 | outputPath := flag.String("output", "output.txt", "path to input file") 31 | profile := flag.String("pprof", "", "if set to a path, a pprof profile will be written") 32 | verbose := flag.Bool("verbose", false, "verbose output") 33 | flag.Parse() 34 | 35 | if *profile != "" { 36 | f, err := os.Create(*profile) 37 | if err != nil { 38 | fmt.Println(err) 39 | os.Exit(2) 40 | } 41 | defer f.Close() 42 | err = pprof.StartCPUProfile(f) 43 | if err != nil { 44 | log.Panicln(err) 45 | } 46 | defer pprof.StopCPUProfile() 47 | } 48 | 49 | delay, successful := runSimulation(*inputPath, *outputPath, *verbose) 50 | if !successful { 51 | os.Exit(1) 52 | } 53 | 54 | if *verbose { 55 | fmt.Println("Printing score") 56 | } 57 | fmt.Println(delay.String()) 58 | } 59 | 60 | func runSimulation(input, output string, verbose bool) (*big.Int, bool) { 61 | world, err := ParseInput(input) 62 | if err != nil { 63 | fmt.Println("Can not read input file:", err) 64 | return big.NewInt(-1), false 65 | } 66 | 67 | if verbose { 68 | fmt.Println("Read output plans") 69 | } 70 | 71 | err = ParsePlan(world, output) 72 | if err != nil { 73 | fmt.Println("Can not read input file:", err) 74 | return big.NewInt(-1), false 75 | } 76 | 77 | if verbose { 78 | fmt.Println("Validating word begin") 79 | } 80 | errs := world.ValidateStart() 81 | if errs != nil { 82 | fmt.Println("initial validation failed:") 83 | for i := range errs { 84 | fmt.Println(errs[i].Error()) 85 | } 86 | return big.NewInt(-1), false 87 | } 88 | 89 | // Run simulation 90 | for world.CurrentTime.Cmp(&world.MaxTime) != +1 { 91 | world.CurrentTime.Add(&world.CurrentTime, big.NewInt(1)) 92 | if verbose { 93 | fmt.Println("Timestep", world.CurrentTime.String()) 94 | } 95 | var errFound bool 96 | 97 | e := make(chan error, 1) 98 | var wg sync.WaitGroup 99 | 100 | // Trains 101 | for k := range world.Trains { 102 | wg.Add(1) 103 | go world.Trains[k].Update(world, e, &wg) 104 | } 105 | 106 | go func() { 107 | wg.Wait() 108 | close(e) 109 | }() 110 | 111 | for err := range e { 112 | errFound = true 113 | fmt.Println("trains -", world.CurrentTime.String(), "-", err.Error()) 114 | } 115 | 116 | if errFound { 117 | return big.NewInt(-1), false 118 | } 119 | 120 | // Passengers 121 | e = make(chan error, 1) 122 | 123 | for k := range world.Passengers { 124 | wg.Add(1) 125 | go world.Passengers[k].Update(world, e, &wg) 126 | } 127 | 128 | go func() { 129 | wg.Wait() 130 | close(e) 131 | }() 132 | 133 | for err := range e { 134 | errFound = true 135 | fmt.Println("passengers -", world.CurrentTime.String(), "-", err.Error()) 136 | } 137 | 138 | if errFound { 139 | return big.NewInt(-1), false 140 | } 141 | 142 | // Validate 143 | if verbose { 144 | fmt.Println("Validate", world.CurrentTime.String()) 145 | } 146 | 147 | errs = world.Validate() 148 | if errs != nil { 149 | fmt.Println("validation", world.CurrentTime.String(), "failed:") 150 | for i := range errs { 151 | fmt.Println(errs[i].Error()) 152 | } 153 | return big.NewInt(-1), false 154 | } 155 | } 156 | 157 | // Check result and calculate delay 158 | if verbose { 159 | fmt.Println("Calculating score") 160 | } 161 | 162 | delay := big.NewInt(0) 163 | valid := true 164 | 165 | for k := range world.Passengers { 166 | d := world.Passengers[k].Delay() 167 | if d.Cmp(InvalidDelay) == 0 { 168 | valid = false 169 | fmt.Println("passenger", k, "does not reach goal") 170 | } 171 | delay.Add(delay, d) 172 | } 173 | 174 | if !valid { 175 | return big.NewInt(-1), false 176 | } 177 | 178 | return delay, true 179 | } 180 | -------------------------------------------------------------------------------- /Bahn-Simulator/parsePlan.go: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: Apache-2.0 2 | // Copyright 2021 Marcus Soll 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | 16 | package main 17 | 18 | import ( 19 | "bufio" 20 | "fmt" 21 | "math/big" 22 | "os" 23 | "strings" 24 | ) 25 | 26 | type PlanState int 27 | 28 | const ( 29 | PlanUnknown PlanState = iota 30 | PlanPassenger 31 | PlanTrain 32 | ) 33 | 34 | func ParsePlan(w *World, path string) error { 35 | currentState := PlanUnknown 36 | currentID := "" 37 | 38 | f, err := os.Open(path) 39 | if err != nil { 40 | return err 41 | } 42 | defer f.Close() 43 | 44 | scanner := bufio.NewScanner(f) 45 | for scanner.Scan() { 46 | s := scanner.Text() 47 | if strings.HasPrefix(s, "#") { 48 | // Comment 49 | continue 50 | } 51 | if s == "" { 52 | // Empty line 53 | continue 54 | } 55 | if strings.HasPrefix(s, "[") { 56 | s = strings.TrimSpace(s) 57 | s = strings.TrimPrefix(s, "[") 58 | s = strings.TrimSuffix(s, "]") 59 | split := strings.Split(s, ":") 60 | if len(split) != 2 { 61 | return fmt.Errorf("can not parse %s", s) 62 | } 63 | currentID = split[1] 64 | if split[0] == "Train" { 65 | currentState = PlanTrain 66 | } else if split[0] == "Passenger" { 67 | currentState = PlanPassenger 68 | } else { 69 | return fmt.Errorf("unknown type '%s'", split[0]) 70 | } 71 | continue 72 | } 73 | 74 | switch currentState { 75 | case PlanUnknown: 76 | return fmt.Errorf("can not parse '%s': no prior definition found", s) 77 | case PlanPassenger: 78 | matches := passengerPlanRegexp.FindStringSubmatch(s) 79 | if matches == nil { 80 | return fmt.Errorf("can not parse '%s': not matching definition for line", s) 81 | } 82 | time, ok := new(big.Int).SetString(matches[passengerPlanRegexpTime], 10) 83 | if !ok { 84 | return fmt.Errorf("can not parse time '%s'", matches[passengerPlanRegexpTime]) 85 | } 86 | if time.Cmp(big.NewInt(0)) != +1 { 87 | return fmt.Errorf("can not parse '%s': time '%s' must be positive", s, matches[passengerPlanRegexpTime]) 88 | } 89 | p, ok := w.Passengers[currentID] 90 | if !ok { 91 | return fmt.Errorf("can not parse '%s': no valid passenger id (%s)", s, currentID) 92 | } 93 | _, ok = p.Plan[time.String()] 94 | if ok { 95 | return fmt.Errorf("can not parse '%s': time %s already in plan", s, time.String()) 96 | } 97 | p.Plan[time.String()] = s 98 | 99 | if time.Cmp(&w.MaxTime) == +1 { 100 | maxtime := new(big.Int).Set(time) 101 | maxtime.Add(maxtime, big.NewInt(1)) 102 | w.MaxTime = *maxtime 103 | } 104 | case PlanTrain: 105 | matches := trainPlanRegexp.FindStringSubmatch(s) 106 | if matches == nil { 107 | return fmt.Errorf("can not parse '%s': not matching definition for line", s) 108 | } 109 | time, ok := new(big.Int).SetString(matches[trainPlanRegexpTime], 10) 110 | if !ok { 111 | return fmt.Errorf("can not parse time '%s'", matches[trainPlanRegexpTime]) 112 | } 113 | t, ok := w.Trains[currentID] 114 | if !ok { 115 | return fmt.Errorf("can not parse '%s': no valid train id (%s)", s, currentID) 116 | } 117 | switch time.Cmp(big.NewInt(0)) { 118 | case +1: 119 | _, ok = t.Plan[time.String()] 120 | if ok { 121 | return fmt.Errorf("can not parse '%s': time %s already in plan", s, time.String()) 122 | } 123 | t.Plan[time.String()] = s 124 | 125 | if time.Cmp(&w.MaxTime) == +1 { 126 | maxtime := new(big.Int).Set(time) 127 | maxtime.Add(maxtime, big.NewInt(1)) 128 | w.MaxTime = *maxtime 129 | } 130 | case 0: 131 | if matches[trainPlanRegexpAction] != "Start" { 132 | return fmt.Errorf("can not parse '%s': time %s must be 'Start'", s, time.String()) 133 | } 134 | if t.PositionType != TrainPositionWildcard || len(t.Position) != 1 || t.Position[0] != "*" { 135 | return fmt.Errorf("can not parse '%s': train must be at '*' for 'Start'", s) 136 | } 137 | st, ok := w.Stations[matches[trainPlanRegexpID]] 138 | if !ok { 139 | return fmt.Errorf("can not parse '%s': station '%s' does not exist'", s, matches[trainPlanRegexpID]) 140 | } 141 | t.Position = []string{st.ID} 142 | t.PositionType = TrainPositionStation 143 | st.CurrenTrains.Add(&st.CurrenTrains, big.NewInt(1)) 144 | case -1: 145 | return fmt.Errorf("can not parse '%s': time '%s' must be positive", s, matches[trainPlanRegexpTime]) 146 | } 147 | default: 148 | return fmt.Errorf("[internal] unknown current state") 149 | } 150 | } 151 | if err := scanner.Err(); err != nil { 152 | return err 153 | } 154 | return nil 155 | } 156 | -------------------------------------------------------------------------------- /Bahn-Simulator/passanger.go: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: Apache-2.0 2 | // Copyright 2021 Marcus Soll 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | 16 | package main 17 | 18 | import ( 19 | "fmt" 20 | "math/big" 21 | "regexp" 22 | "sync" 23 | ) 24 | 25 | type PassengerPosition int 26 | 27 | var InvalidDelay = big.NewInt(-1) 28 | 29 | const ( 30 | PassengerPositionUnknown PassengerPosition = iota 31 | PassengerPositionStation 32 | PassengerPositionTrain 33 | ) 34 | 35 | type Passenger struct { 36 | ID string 37 | Start string 38 | Target string 39 | Size big.Int 40 | TargetTime big.Int 41 | TargetReached big.Int 42 | PositionType PassengerPosition 43 | Position string 44 | Plan map[string]string 45 | } 46 | 47 | var passengerPlanRegexp = regexp.MustCompile(`\A(?P