From 1d499bfa0e94b3cd599919e9618413383d165695 Mon Sep 17 00:00:00 2001 From: Charles-Antoine Francisco Date: Thu, 6 Apr 2023 17:20:39 -0700 Subject: [PATCH] Puzzles - documentation --- README.md | 65 +++++++--------- puzzles/cpp/onboarding/README.md | 31 ++++++++ puzzles/cpp/{ => onboarding}/onboarding.cpp | 0 puzzles/go/onboarding/README.md | 25 ++++++ puzzles/go/{ => onboarding}/onboarding.go | 0 puzzles/haskell/conway-sequence/README.md | 8 +- puzzles/haskell/unary/README.md | 7 ++ puzzles/haskell/{ => unary}/unary.hs | 0 puzzles/js/horse-racing-duals/README.md | 15 ++++ .../horse-racing-duals.js | 0 puzzles/js/onboarding/README.md | 19 +++++ puzzles/js/{ => onboarding}/onboarding.js | 0 puzzles/kotlin/src/ascii-art/README.md | 20 +++++ .../kotlin/src/{ => ascii-art}/ascii-art.kt | 0 puzzles/kotlin/src/conway-sequence/README.md | 19 +++++ .../{ => conway-sequence}/conway-sequence.kt | 0 puzzles/kotlin/src/defibrillators/README.md | 28 +++++++ .../{ => defibrillators}/defibrillators.kt | 0 puzzles/kotlin/src/dont-panic1/README.md | 69 ++++++++++++++++ .../src/{ => dont-panic1}/dont-panic1.kt | 0 .../kotlin/src/horse-racing-duals/README.md | 46 +++++++++++ .../horse-racing-duals.kt | 0 puzzles/kotlin/src/mars-lander1/README.md | 16 ++++ .../src/{ => mars-lander1}/mars-lander1.kt | 0 puzzles/kotlin/src/mime-type/README.md | 78 +++++++++++++++++++ .../kotlin/src/{ => mime-type}/mime-type.kt | 0 puzzles/kotlin/src/onboarding/README.md | 29 +++++++ puzzles/kotlin/src/onboarding/onboarding.kt | 17 ++++ puzzles/kotlin/src/power-of-thor1/README.md | 15 ++++ .../{ => power-of-thor1}/power-of-thor1.kt | 0 puzzles/kotlin/src/scrabble/README.md | 13 ++++ puzzles/kotlin/src/{ => scrabble}/scrabble.kt | 0 puzzles/kotlin/src/shadows-knight1/README.md | 19 +++++ .../{ => shadows-knight1}/shadows-knight1.kt | 0 .../kotlin/src/skynet-revolution2/README.md | 21 +++++ .../skynet-revolution2.kt | 0 puzzles/kotlin/src/super-computer/README.md | 9 +++ .../{ => super-computer}/super-computer.kt | 0 puzzles/kotlin/src/surface/README.md | 7 ++ puzzles/kotlin/src/{ => surface}/surface.kt | 0 puzzles/kotlin/src/tan-network/README.md | 15 ++++ .../src/{ => tan-network}/tan-network.kt | 0 puzzles/kotlin/src/temperatures/README.Md | 58 ++++++++++++++ .../src/{ => temperatures}/temperatures.kt | 0 puzzles/kotlin/src/the-descent/README.md | 35 +++++++++ .../src/{ => the-descent}/the-descent.kt | 0 .../kotlin/src/there-is-no-spoon1/README.md | 15 ++++ .../there-is-no-spoon1.kt | 0 puzzles/kotlin/src/war/README.md | 27 +++++++ puzzles/kotlin/src/{ => war}/war.kt | 0 puzzles/php/ascii-art.php | 22 ------ puzzles/php/conway_sequence.php | 42 ---------- puzzles/php/defibrillators.php | 28 ------- puzzles/php/dont_panic1.php | 43 ---------- puzzles/php/horse-racing-duals.php | 18 ----- puzzles/php/mars-lander.php | 33 -------- puzzles/php/mime-type.php | 22 ------ puzzles/php/power-of-thor.php | 33 -------- puzzles/php/scrabble.php | 52 ------------- puzzles/php/shadows-knight1.php | 56 ------------- puzzles/php/temperatures.php | 8 -- puzzles/php/there-is-no-spoon.php | 50 ------------ .../python3/defibrillators/defibrillators.py | 20 ++--- puzzles/python3/genome_sequencing.py | 1 + puzzles/python3/mime_type.py | 10 +-- puzzles/python3/onboarding/README.md | 17 ++++ .../python3/{ => onboarding}/onboarding.py | 0 puzzles/python3/shadows-knight1/README.md | 19 +++++ .../{ => shadows-knight1}/shadows_knight1.py | 0 puzzles/python3/surface/README.md | 6 +- puzzles/python3/there-is-no-spoon1/README.md | 12 ++- puzzles/ruby/ascii-art.rb | 26 ------- puzzles/ruby/defibrillators.rb | 38 --------- puzzles/ruby/horse-racing-duals/README.md | 18 ++++- puzzles/ruby/mars-lander.rb | 32 -------- puzzles/ruby/mayan-calc/README.md | 9 +++ puzzles/ruby/{ => mayan-calc}/mayan-calc.rb | 0 puzzles/ruby/mime-type.rb | 40 ---------- puzzles/ruby/shadows-knight1/README.md | 4 - .../ruby/shadows-knight1/shadows-knight1.rb | 44 ----------- puzzles/ruby/skynet-the-chasm.rb | 33 -------- puzzles/ruby/temperatures-min.rb | 7 -- puzzles/ruby/temperatures.rb | 12 --- puzzles/ruby/there-is-no-spoon.rb | 68 ---------------- puzzles/swift/onboarding/README.md | 63 +++++++++++++++ puzzles/swift/onboarding/onboarding.swift | 21 +++++ puzzles/swift/power-of-thor1/README.md | 56 +++++++++++++ .../swift/power-of-thor1/power-of-thor1.swift | 43 ++++++++++ puzzles/swift/temperatures/README.md | 30 +++++++ puzzles/swift/temperatures/temperatures.swift | 23 ++++++ puzzles/swift/the-descent/README.md | 43 ++++++++++ puzzles/swift/the-descent/the-descent.swift | 30 +++++++ puzzles/ts/ascii-art/README.md | 9 +++ puzzles/ts/defibrillators/README.md | 9 +++ puzzles/ts/horse-racing-duals/README.md | 15 ++++ puzzles/ts/mars-lander1/README.md | 9 +++ puzzles/ts/mime-type/README.md | 11 ++- puzzles/ts/power-of-thor1/README.md | 11 +++ puzzles/ts/temperatures/README.md | 9 +++ puzzles/ts/the-descent/README.md | 11 +++ puzzles/ts/unary/README.md | 8 +- 101 files changed, 1172 insertions(+), 778 deletions(-) create mode 100644 puzzles/cpp/onboarding/README.md rename puzzles/cpp/{ => onboarding}/onboarding.cpp (100%) create mode 100644 puzzles/go/onboarding/README.md rename puzzles/go/{ => onboarding}/onboarding.go (100%) create mode 100644 puzzles/haskell/unary/README.md rename puzzles/haskell/{ => unary}/unary.hs (100%) create mode 100644 puzzles/js/horse-racing-duals/README.md rename puzzles/js/{ => horse-racing-duals}/horse-racing-duals.js (100%) create mode 100644 puzzles/js/onboarding/README.md rename puzzles/js/{ => onboarding}/onboarding.js (100%) create mode 100644 puzzles/kotlin/src/ascii-art/README.md rename puzzles/kotlin/src/{ => ascii-art}/ascii-art.kt (100%) create mode 100644 puzzles/kotlin/src/conway-sequence/README.md rename puzzles/kotlin/src/{ => conway-sequence}/conway-sequence.kt (100%) create mode 100644 puzzles/kotlin/src/defibrillators/README.md rename puzzles/kotlin/src/{ => defibrillators}/defibrillators.kt (100%) create mode 100644 puzzles/kotlin/src/dont-panic1/README.md rename puzzles/kotlin/src/{ => dont-panic1}/dont-panic1.kt (100%) create mode 100644 puzzles/kotlin/src/horse-racing-duals/README.md rename puzzles/kotlin/src/{ => horse-racing-duals}/horse-racing-duals.kt (100%) create mode 100644 puzzles/kotlin/src/mars-lander1/README.md rename puzzles/kotlin/src/{ => mars-lander1}/mars-lander1.kt (100%) create mode 100644 puzzles/kotlin/src/mime-type/README.md rename puzzles/kotlin/src/{ => mime-type}/mime-type.kt (100%) create mode 100644 puzzles/kotlin/src/onboarding/README.md create mode 100644 puzzles/kotlin/src/onboarding/onboarding.kt create mode 100644 puzzles/kotlin/src/power-of-thor1/README.md rename puzzles/kotlin/src/{ => power-of-thor1}/power-of-thor1.kt (100%) create mode 100644 puzzles/kotlin/src/scrabble/README.md rename puzzles/kotlin/src/{ => scrabble}/scrabble.kt (100%) create mode 100644 puzzles/kotlin/src/shadows-knight1/README.md rename puzzles/kotlin/src/{ => shadows-knight1}/shadows-knight1.kt (100%) create mode 100644 puzzles/kotlin/src/skynet-revolution2/README.md rename puzzles/kotlin/src/{ => skynet-revolution2}/skynet-revolution2.kt (100%) create mode 100644 puzzles/kotlin/src/super-computer/README.md rename puzzles/kotlin/src/{ => super-computer}/super-computer.kt (100%) create mode 100644 puzzles/kotlin/src/surface/README.md rename puzzles/kotlin/src/{ => surface}/surface.kt (100%) create mode 100644 puzzles/kotlin/src/tan-network/README.md rename puzzles/kotlin/src/{ => tan-network}/tan-network.kt (100%) create mode 100644 puzzles/kotlin/src/temperatures/README.Md rename puzzles/kotlin/src/{ => temperatures}/temperatures.kt (100%) create mode 100644 puzzles/kotlin/src/the-descent/README.md rename puzzles/kotlin/src/{ => the-descent}/the-descent.kt (100%) create mode 100644 puzzles/kotlin/src/there-is-no-spoon1/README.md rename puzzles/kotlin/src/{ => there-is-no-spoon1}/there-is-no-spoon1.kt (100%) create mode 100644 puzzles/kotlin/src/war/README.md rename puzzles/kotlin/src/{ => war}/war.kt (100%) delete mode 100644 puzzles/php/ascii-art.php delete mode 100644 puzzles/php/conway_sequence.php delete mode 100644 puzzles/php/defibrillators.php delete mode 100644 puzzles/php/dont_panic1.php delete mode 100644 puzzles/php/horse-racing-duals.php delete mode 100644 puzzles/php/mars-lander.php delete mode 100644 puzzles/php/mime-type.php delete mode 100644 puzzles/php/power-of-thor.php delete mode 100644 puzzles/php/scrabble.php delete mode 100644 puzzles/php/shadows-knight1.php delete mode 100644 puzzles/php/temperatures.php delete mode 100644 puzzles/php/there-is-no-spoon.php create mode 100644 puzzles/python3/onboarding/README.md rename puzzles/python3/{ => onboarding}/onboarding.py (100%) create mode 100644 puzzles/python3/shadows-knight1/README.md rename puzzles/python3/{ => shadows-knight1}/shadows_knight1.py (100%) delete mode 100644 puzzles/ruby/ascii-art.rb delete mode 100644 puzzles/ruby/defibrillators.rb delete mode 100644 puzzles/ruby/mars-lander.rb create mode 100644 puzzles/ruby/mayan-calc/README.md rename puzzles/ruby/{ => mayan-calc}/mayan-calc.rb (100%) delete mode 100644 puzzles/ruby/mime-type.rb delete mode 100644 puzzles/ruby/shadows-knight1/README.md delete mode 100644 puzzles/ruby/shadows-knight1/shadows-knight1.rb delete mode 100644 puzzles/ruby/skynet-the-chasm.rb delete mode 100644 puzzles/ruby/temperatures-min.rb delete mode 100644 puzzles/ruby/temperatures.rb delete mode 100644 puzzles/ruby/there-is-no-spoon.rb create mode 100644 puzzles/swift/onboarding/README.md create mode 100644 puzzles/swift/onboarding/onboarding.swift create mode 100644 puzzles/swift/power-of-thor1/README.md create mode 100644 puzzles/swift/power-of-thor1/power-of-thor1.swift create mode 100644 puzzles/swift/temperatures/README.md create mode 100644 puzzles/swift/temperatures/temperatures.swift create mode 100644 puzzles/swift/the-descent/README.md create mode 100644 puzzles/swift/the-descent/the-descent.swift create mode 100644 puzzles/ts/ascii-art/README.md create mode 100644 puzzles/ts/defibrillators/README.md create mode 100644 puzzles/ts/horse-racing-duals/README.md create mode 100644 puzzles/ts/mars-lander1/README.md create mode 100644 puzzles/ts/power-of-thor1/README.md create mode 100644 puzzles/ts/temperatures/README.md create mode 100644 puzzles/ts/the-descent/README.md diff --git a/README.md b/README.md index 5cd6cb1..6a64b8b 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # Solutions to CodinGame Puzzles -CodinGame is a challenge-based training platform for programmers where you can improve your coding skills with fun exercises. +The repository contains a number of solutions to programming challenges from CodinGame. These solutions are written in various programming languages, including Python, C++, and Java. Each solution is contained in its own file, with a name that describes the challenge it solves. -This repository contains solutions to CodinGame puzzles, contests and Clash of Code. +In addition to the solutions, the repository also includes some other files, such as a README.md file that provides information about the repository and its contents, as well as some configuration files that are specific to CodinGame. -Note: A "★" next to the solution means this is my personal favorite solution. +It's worth noting that CodinGame is a website that provides programming challenges and puzzles for developers to solve. The challenges are designed to help developers improve their coding skills by providing them with interesting and engaging problems to solve. The website supports a number of different programming languages, and developers can compete with each other to see who can solve the challenges the fastest and most efficiently. ![](https://img.shields.io/github/languages/count/charlesfranciscodev/codingame.svg) ![](https://img.shields.io/github/languages/top/charlesfranciscodev/codingame.svg) ![](https://img.shields.io/badge/code%20style-flake8-black) @@ -13,53 +13,46 @@ Note: A "★" next to the solution means this is my personal favorite soluti ### Easy Puzzles | Title | Solution(s) | Topic(s) | | :---: | :------: | :------: | -| Onboarding ๐Ÿ„ | [Python](./puzzles/python3/onboarding.py) ★, [JavaScript](./puzzles/js/onboarding.js), [C++](./puzzles/cpp/onboarding.cpp) | Variables, Input/Output, Conditions | -| The Descent โ›ฐ๏ธ | [Python](./puzzles/python3/the-descent) ★, [Kotlin](./puzzles/kotlin/src/the-descent.kt), [TypeScript](./puzzles/ts/the-descent/the-descent.ts), [C++](./puzzles/cpp/the-descent.cpp) | Conditions, Loops | -| Power of Thor 1 โšก | [Python](./puzzles/python3/power-of-thor1) ★, [Kotlin](./puzzles/kotlin/src/power-of-thor1.kt), [JavaScript](./puzzles/js/power-of-thor1.js), [C++](./puzzles/cpp/power-of-thor1.cpp) | Conditions | -| Temperatures ๐ŸŒก๏ธ | [Python](./puzzles/python3/temperatures) ★, [Kotlin](./puzzles/kotlin/src/temperatures.kt), [JavaScript](./puzzles/js/temperatures.js), [C++](./puzzles/cpp/temperatures.cpp) | Arrays, Absolute Values | -| Mars Lander 1 ๐Ÿš€ | [Python](./puzzles/python3/mars_lander1.py), [Kotlin](./puzzles/kotlin/src/mars-lander1.kt), [JavaScript](./puzzles/js/mars-lander1) ★, [C++](./puzzles/cpp/mars-lander1.cpp) | Conditions, Loops | -| ASCII Art ๐ŸŽจ | [Python](./puzzles/python3/ascii-art) ★, [Kotlin](./puzzles/kotlin/src/ascii-art.kt), [JavaScript](./puzzles/js/ascii-art.js), [C++](./puzzles/cpp/ascii-art.cpp) | Strings | -| Unary 1๏ธโƒฃ | [Python](./puzzles/python3/unary.py), [TypeScript](./puzzles/ts/unary) ★ | Strings, Encoding | -| MIME Type ๐ŸŽถ | [Python](./puzzles/python3/mime_type.py), [Kotlin](./puzzles/kotlin/src/mime-type.kt), [TypeScript](./puzzles/ts/mime-type) ★ | Strings, Hash Tables | -| Defibrillators ๐Ÿ’– | [Python](./puzzles/python3/defibrillators) ★, [Kotlin](./puzzles/kotlin/src/defibrillators.kt), [JavaScript](./puzzles/js/defibrillators.js) | Strings, Trigonometry | -| Horse-racing Duals ๐ŸŽ | [Python](./puzzles/python3/horse_racing_duals.py), [Kotlin](./puzzles/kotlin/src/horse-racing-duals.kt), [JavaScript](./puzzles/js/horse-racing-duals.js), [Ruby](./puzzles/ruby/horse-racing-duals) ★ | Arrays, Sorting | +| Onboarding ๐Ÿ“š | [Python](./puzzles/python3/onboarding), [Kotlin](./puzzles/kotlin/src/onboarding), [JavaScript](./puzzles/js/onboarding), [C++](./puzzles/cpp/onboarding), [Go](./puzzles/go/onboarding), [Swift](./puzzles/swift/onboarding) | Variables, Input/Output, Conditions | +| The Descent โ›ฐ๏ธ | [Python](./puzzles/python3/the-descent) ★, [Kotlin](./puzzles/kotlin/src/the-descent), [TypeScript](./puzzles/ts/the-descent), [C++](./puzzles/cpp/the-descent.cpp) | Conditions, Loops | +| Power of Thor 1 โšก | [Python](./puzzles/python3/power-of-thor1) ★, [Kotlin](./puzzles/kotlin/src/power-of-thor1), [TypeScript](./puzzles/ts/power-of-thor1), [C++](./puzzles/cpp/power-of-thor1.cpp) | Conditions | +| Temperatures ๐ŸŒก๏ธ | [Python](./puzzles/python3/temperatures) ★, [Kotlin](./puzzles/kotlin/src/temperatures), [TypeScript](./puzzles/ts/temperatures), [C++](./puzzles/cpp/temperatures.cpp) | Arrays, Absolute Values | +| Mars Lander 1 ๐Ÿš€ | [Python](./puzzles/python3/mars_lander1.py), [Kotlin](./puzzles/kotlin/src/mars-lander1), [TypeScript](./puzzles/ts/mars-lander1) ★, [C++](./puzzles/cpp/mars-lander1.cpp) | Conditions, Loops | +| ASCII Art ๐ŸŽจ | [Python](./puzzles/python3/ascii-art) ★, [Kotlin](./puzzles/kotlin/src/ascii-art), [TypeScript](./puzzles/ts/ascii-art), [C++](./puzzles/cpp/ascii-art.cpp) | Strings | +| Unary 1๏ธโƒฃ | [Python](./puzzles/python3/unary.py) ★, [Haskell](./puzzles/haskell/unary) | Strings, Encoding | +| MIME Type ๐ŸŽถ | [Python](./puzzles/python3/mime_type.py) ★, [Kotlin](./puzzles/kotlin/src/mime-type), [TypeScript](./puzzles/ts/mime-type) | Strings, Hash Tables | +| Defibrillators ๐Ÿ’– | [Python](./puzzles/python3/defibrillators) ★, [Kotlin](./puzzles/kotlin/src/defibrillators), [TypeScript](./puzzles/ts/defibrillators) | Strings, Trigonometry | +| Horse-racing Duals ๐ŸŽ  | [Python](./puzzles/python3/horse_racing_duals.py), [Kotlin](./puzzles/kotlin/src/horse-racing-duals), [TypeScript](./puzzles/ts/horse-racing-duals), [Ruby](./puzzles/ruby/horse-racing-duals) ★ | Arrays, Sorting | ### Medium Puzzles | Title | Solution(s) | Topic(s) | | :---: | :------: | :------: | -| Shadows of the Knight 1 โ™ž | [Python](./puzzles/python3/shadows_knight1.py), [Kotlin](./puzzles/kotlin/src/shadows-knight1.kt), [TypeScript](./puzzles/ts/shadows-knight1/shadows-knight1.ts), [Ruby](./puzzles/ruby/shadows-knight1) ★ | Binary Search, 2D Arrays | -| There is no Spoon 1 ๐Ÿฅ„ | [Python](./puzzles/python3/there-is-no-spoon1) ★, [Kotlin](./puzzles/kotlin/src/there-is-no-spoon1.kt), [TypeScript](./puzzles/ts/there-is-no-spoon1/there-is-no-spoon1.ts), [C++](./puzzles/cpp/there-is-no-spoon1.cpp) | 2D Arrays | -| DFS 1 ๐ŸŒƒ | [Python](./puzzles/python3/skynet-revolution1) ★, [JavaScript](./puzzles/js/skynet-revolution1.js) | Graphs, BFS | -| Don't Panic 1 ๐Ÿ•ถ๏ธ | [Python](./puzzles/python3/dont_panic1.py) ★, [Kotlin](./puzzles/kotlin/src/dont-panic1.kt), [TypeScript](./puzzles/ts/dont-panic1/dont-panic1.ts) | Conditions | -| War โ™ฆ๏ธ | [Python](./puzzles/python3/war) ★, [Kotlin](./puzzles/kotlin/src/war.kt), [TypeScript](./puzzles/ts/war/war.ts), [C++](./puzzles/cpp/war.cpp) | Queues, Card Games | +| Shadows of the Knight 1 ๐Ÿฆ‡ | [Python](./puzzles/python3/shadows-knight1) ★, [Kotlin](./puzzles/kotlin/src/shadows-knight1), [TypeScript](./puzzles/ts/shadows-knight1/shadows-knight1.ts) | Binary Search, 2D Arrays | +| There is no Spoon 1 ๐Ÿฅ„ | [Python](./puzzles/python3/there-is-no-spoon1) ★, [Kotlin](./puzzles/kotlin/src/there-is-no-spoon1), [TypeScript](./puzzles/ts/there-is-no-spoon1/there-is-no-spoon1.ts), [C++](./puzzles/cpp/there-is-no-spoon1.cpp) | 2D Arrays | +| DFS 1 ๐ŸŒ† | [Python](./puzzles/python3/skynet-revolution1) ★, [JavaScript](./puzzles/js/skynet-revolution1.js) | Graphs, BFS | +| Don't Panic 1 ๐Ÿ•ถ๏ธ | [Python](./puzzles/python3/dont_panic1.py) ★, [Kotlin](./puzzles/kotlin/src/dont-panic1), [TypeScript](./puzzles/ts/dont-panic1/dont-panic1.ts) | Conditions | +| War โ™ฃ๏ธ | [Python](./puzzles/python3/war) ★, [Kotlin](./puzzles/kotlin/src/war), [TypeScript](./puzzles/ts/war/war.ts), [C++](./puzzles/cpp/war.cpp) | Queues, Card Games | | Stock Exchange Losses ๐Ÿ“ˆ | [Python](./puzzles/python3/stock_exchange.py) ★ | Conditions, Loops | | The Fall 1 ๐Ÿ’Ž | [Python](./puzzles/python3/the_fall1.py), [JavaScript](./puzzles/js/the-fall1) ★ | 2D Arrays | -| Network Cabling ๐Ÿ˜๏ธ | [Python](./puzzles/python3/network_cabling.py) ★ | Sorting, Median | -| Conway Sequence ๐Ÿ‘€ | [Python](./puzzles/python3/conway_sequence.py), [Kotlin](./puzzles/kotlin/src/conway-sequence.kt), [Haskell](./puzzles/haskell/conway-sequence) ★ | Sequences | +| Network Cabling ๐Ÿ”Œ | [Python](./puzzles/python3/network_cabling.py) ★ | Sorting, Median | +| Conway Sequence ๐Ÿ‘€ | [Python](./puzzles/python3/conway_sequence.py), [Kotlin](./puzzles/kotlin/src/conway-sequence), [Haskell](./puzzles/haskell/conway-sequence) ★ | Sequences | | Telephone Numbers ๐Ÿ“ฑ | [Python](./puzzles/python3/telephone_numbers.py) ★ | Tries | | Dwarfs standing on giants ๐Ÿ‰ | [Python](./puzzles/python3/dwarfs_giants.py) ★, [TypeScript](./puzzles/ts/dwarfs-giants) | Graphs, Recursion | -| Blunder 1 ๐Ÿค– | [Python](./puzzles/python3/blunder1) ★ | 2D Arrays, State Machine, Simulation | -| Scrabble ๐Ÿงต | [Python](./puzzles/python3/scrabble) ★, [Kotlin](./puzzles/kotlin/src/scrabble.kt), [TypeScript](./puzzles/ts/scrabble/scrabble.ts) | Strings, Hash Tables | -| Mayan Calculation ๐Ÿงฎ | [Python](./puzzles/python3/mayan_calc.py), [Kotlin](./puzzles/kotlin/src/mayan-calc) ★, [Ruby](./puzzles/ruby/mayan-calc.rb) | Strings | +| Blunder 1 ๐Ÿบ | [Python](./puzzles/python3/blunder1) ★ | 2D Arrays, State Machine, Simulation | +| Scrabble ๐Ÿ”ค | [Python](./puzzles/python3/scrabble) ★, [Kotlin](./puzzles/kotlin/src/scrabble), [TypeScript](./puzzles/ts/scrabble/scrabble.ts) | Strings, Hash Tables | +| Mayan Calculation 2๏ธโƒฃ0๏ธโƒฃ | [Python](./puzzles/python3/mayan_calc.py), [Kotlin](./puzzles/kotlin/src/mayan-calc) ★, [Ruby](./puzzles/ruby/mayan-calc) | Strings | ### Hard Puzzles | Title | Solution(s) | Topic(s) | | :---: | :------: | :------: | | The Labyrinth ๐Ÿงฉ | [Python](./puzzles/python3/the-labyrinth) ★ | 2D Arrays, Graphs, BFS | -| DFS 2 ๐ŸŒƒ | [Python](./puzzles/python3/skynet-revolution2) ★, [Kotlin](./puzzles/kotlin/src/skynet-revolution2.kt) | Graphs | -| Vox Codei 1 ๐Ÿงจ | [Python](./puzzles/python3/vox-codei1) ★, [C++](./puzzles/cpp/vox-codei1.cpp) | Simulation | -| Super Computer ๐Ÿ“… | [Python](./puzzles/python3/super-computer) ★ | Sorting, Greedy Algorithms, Scheduling | +| DFS 2 ๐ŸŒ† | [Python](./puzzles/python3/skynet-revolution2) ★, [Kotlin](./puzzles/kotlin/src/skynet-revolution2) | Graphs | +| Vox Codei 1 ๐ŸŽญ | [Python](./puzzles/python3/vox-codei1) ★, [C++](./puzzles/cpp/vox-codei1.cpp) | Simulation | +| Super Computer ๐Ÿ“… | [Python](./puzzles/python3/super-computer), [Kotlin](./puzzles/kotlin/src/super-computer) | Sorting, Greedy Algorithms, Scheduling | | Roller Coaster ๐ŸŽข | [Python](./puzzles/python3/roller-coaster) ★ | Queues, Dynamic Programming, Simulation | -| Surface ๐ŸŒŠ | [Python](./puzzles/python3/surface) ★, [Kotlin](./puzzles/kotlin/src/surface.kt) | 2D Arrays, Graphs, Flood Fill | +| Surface ๐ŸŒŠ | [Python](./puzzles/python3/surface) ★, [Kotlin](./puzzles/kotlin/src/surface) | 2D Arrays, Graphs, Flood Fill | | CGX Formatter ๐ŸŽป | [Python](./puzzles/python3/cgx_formatter.py) ★ | Strings, Parsing | -| TAN Network ๐ŸŒด | [Python](./puzzles/python3/tan-network) ★, [Kotlin](./puzzles/kotlin/src/tan-network.kt) | Graphs, Pathfinding | +| TAN Network ๐Ÿšž | [Python](./puzzles/python3/tan-network) ★, [Kotlin](./puzzles/kotlin/src/tan-network) | Graphs, Pathfinding | | Genome Sequencing ๐Ÿงฌ | [Python](./puzzles/python3/genome_sequencing.py) ★ | Strings, Shortest Common Supersequence | -| Blunder 2 ๐Ÿช™ | [Python](./puzzles/python3/blunder2) ★ | Pathfinding | +| Blunder 2 ๐ŸŽฑ | [Python](./puzzles/python3/blunder2) ★ | Pathfinding | | Blunder 3 โŒ› | [Python](./puzzles/python3/blunder3.py) ★ | Time Complexity | - -There are solutions available for other puzzles/programming languages as well in this repository. - -## Link(s) -* [ethiery](https://ethiery.github.io/codingame/index.html) -* [Genome Sequencing](https://www.xarg.org/puzzle/codingame/genome-sequencing/) -* [CodinGame Profile](https://www.codingame.com/profile/8111ec5700e5b6591daabfc46fd79e278747932) diff --git a/puzzles/cpp/onboarding/README.md b/puzzles/cpp/onboarding/README.md new file mode 100644 index 0000000..aef6902 --- /dev/null +++ b/puzzles/cpp/onboarding/README.md @@ -0,0 +1,31 @@ +Here's a possible solution to the "Onboarding" challenge on CodinGame using C++: + +```c++ +#include +#include + +using namespace std; + +int main() +{ + while (true) { + string enemy1; + int dist1; + string enemy2; + int dist2; + + cin >> enemy1 >> dist1 >> enemy2 >> dist2; + + // Determine which enemy is closer and print its name + if (dist1 < dist2) { + cout << enemy1 << endl; + } else { + cout << enemy2 << endl; + } + } + + return 0; +} +``` + +In this solution, we use a `while` loop to continuously read input from the standard input until the program is terminated. In each iteration of the loop, we read the name and distance of two enemies using `cin`, and then determine which one is closer based on their distances. Finally, we print the name of the closer enemy using `cout`. Note that we use `endl` to add a newline character at the end of each output line. \ No newline at end of file diff --git a/puzzles/cpp/onboarding.cpp b/puzzles/cpp/onboarding/onboarding.cpp similarity index 100% rename from puzzles/cpp/onboarding.cpp rename to puzzles/cpp/onboarding/onboarding.cpp diff --git a/puzzles/go/onboarding/README.md b/puzzles/go/onboarding/README.md new file mode 100644 index 0000000..f6c9f5c --- /dev/null +++ b/puzzles/go/onboarding/README.md @@ -0,0 +1,25 @@ +This Go code uses a `for` loop to continuously read input from standard input and print the name of the closest enemy. + +In each iteration of the loop, we use the `Scan` function from the `fmt` package to read in the name and distance of the first enemy into the `enemy1` and `dist1` variables, respectively. We do the same for the second enemy, storing their name and distance in the `enemy2` and `dist2` variables. + +We then compare the distances of the two enemies, and print the name of the one that is closest. The `Scan` function reads input from standard input and automatically parses the input into the specified variable types. + +```go +package main + +import "fmt" + +func main() { + for { + var enemy1, enemy2 string + var dist1, dist2 int + fmt.Scan(&enemy1, &dist1, &enemy2, &dist2) + + if dist1 < dist2 { + fmt.Println(enemy1) + } else { + fmt.Println(enemy2) + } + } +} +``` diff --git a/puzzles/go/onboarding.go b/puzzles/go/onboarding/onboarding.go similarity index 100% rename from puzzles/go/onboarding.go rename to puzzles/go/onboarding/onboarding.go diff --git a/puzzles/haskell/conway-sequence/README.md b/puzzles/haskell/conway-sequence/README.md index 33f5118..15c54a2 100644 --- a/puzzles/haskell/conway-sequence/README.md +++ b/puzzles/haskell/conway-sequence/README.md @@ -1,3 +1,9 @@ # Conway Sequence -The task is to display the last line of a conway sequence. +## Problem Description + +Conway's Sequence is a sequence of numbers, starting with a 1, such that each subsequent number is a description of the previous number. + +To generate a member of Conway's Sequence from the previous member, read off the digits of the previous member, counting the number of digits in groups of the same digit. For example, 1 is read off as "one 1" or 11, 11 is read off as "two 1s" or 21, 21 is read off as "one 2, then one 1" or 1211, and so on. + +Your task is to implement a function that generates the nth member of Conway's Sequence. diff --git a/puzzles/haskell/unary/README.md b/puzzles/haskell/unary/README.md new file mode 100644 index 0000000..637cace --- /dev/null +++ b/puzzles/haskell/unary/README.md @@ -0,0 +1,7 @@ +# Unary + +"Unary" is a beginner-level coding challenge available on the CodinGame platform. In this challenge, the player is given a string of characters representing a unary number, and is asked to convert it to decimal notation. + +A unary number is a number system in which each integer is represented by a corresponding number of symbols or characters. In this challenge, the unary number is represented by a string of the same character, and the value of the number is equal to the length of the string. + +The challenge consists of writing a program that takes as input a string of characters representing a unary number and outputs the corresponding decimal notation. diff --git a/puzzles/haskell/unary.hs b/puzzles/haskell/unary/unary.hs similarity index 100% rename from puzzles/haskell/unary.hs rename to puzzles/haskell/unary/unary.hs diff --git a/puzzles/js/horse-racing-duals/README.md b/puzzles/js/horse-racing-duals/README.md new file mode 100644 index 0000000..eba9bce --- /dev/null +++ b/puzzles/js/horse-racing-duals/README.md @@ -0,0 +1,15 @@ +# Horse Racing Duals + +You have a list of N horses, each with a strength value, represented by an integer. You need to find the minimum difference in strength between two horses out of all possible pairs of horses. In other words, you need to find the two horses with the closest strength values. + +For example, given the following list of horses with their corresponding strength values: + +5 8 9 14 21 + +The minimum difference in strength between two horses is 1, which is the difference between 5 and 8. + +The input for the problem consists of two lines. The first line contains an integer N, the number of horses. The second line contains N space-separated integers, representing the strength values of the N horses. + +The output should be a single integer, the minimum difference in strength between two horses. + +The solution to this problem involves sorting the list of horses in ascending order, and then computing the difference between adjacent horses in the sorted list. The minimum difference is the smallest of these computed differences. diff --git a/puzzles/js/horse-racing-duals.js b/puzzles/js/horse-racing-duals/horse-racing-duals.js similarity index 100% rename from puzzles/js/horse-racing-duals.js rename to puzzles/js/horse-racing-duals/horse-racing-duals.js diff --git a/puzzles/js/onboarding/README.md b/puzzles/js/onboarding/README.md new file mode 100644 index 0000000..ee491bf --- /dev/null +++ b/puzzles/js/onboarding/README.md @@ -0,0 +1,19 @@ +Here's a possible solution to the "Onboarding" challenge on CodinGame using JavaScript: + +```javascript +while (true) { + const enemy1 = readline(); // name of enemy 1 + const dist1 = parseInt(readline()); // distance to enemy 1 + const enemy2 = readline(); // name of enemy 2 + const dist2 = parseInt(readline()); // distance to enemy 2 + + // Determine which enemy is closer and print its name + if (dist1 < dist2) { + console.log(enemy1); + } else { + console.log(enemy2); + } +} +``` + +In this solution, we use a `while` loop to continuously read input from the standard input until the program is terminated. In each iteration of the loop, we read the name and distance of two enemies, and then determine which one is closer based on their distances. Finally, we print the name of the closer enemy using `console.log()`. \ No newline at end of file diff --git a/puzzles/js/onboarding.js b/puzzles/js/onboarding/onboarding.js similarity index 100% rename from puzzles/js/onboarding.js rename to puzzles/js/onboarding/onboarding.js diff --git a/puzzles/kotlin/src/ascii-art/README.md b/puzzles/kotlin/src/ascii-art/README.md new file mode 100644 index 0000000..1e82940 --- /dev/null +++ b/puzzles/kotlin/src/ascii-art/README.md @@ -0,0 +1,20 @@ +Sure, here is a modified version of the documentation for `ascii-art.kt` without the license section: + +# ASCII Art + +ASCII Art is a Kotlin program that takes in a string of characters and outputs a stylized version of the input using ASCII art. The program works by converting the input string into a series of characters and then replacing each character with a corresponding ASCII art representation. + +## Usage + +To use ASCII Art, you need to provide a string of characters and a font size. The font size specifies the height of the ASCII art characters in rows. Here is an example usage of the program: + +```kotlin +fun main() { + val input = "HELLO" + val fontSize = 5 + val asciiArt = AsciiArt(input, fontSize) + println(asciiArt) +} +``` + +This will output the ASCII art representation of the string "HELLO" with a font size of 5. \ No newline at end of file diff --git a/puzzles/kotlin/src/ascii-art.kt b/puzzles/kotlin/src/ascii-art/ascii-art.kt similarity index 100% rename from puzzles/kotlin/src/ascii-art.kt rename to puzzles/kotlin/src/ascii-art/ascii-art.kt diff --git a/puzzles/kotlin/src/conway-sequence/README.md b/puzzles/kotlin/src/conway-sequence/README.md new file mode 100644 index 0000000..1a8b967 --- /dev/null +++ b/puzzles/kotlin/src/conway-sequence/README.md @@ -0,0 +1,19 @@ + + +# Conway Sequence + +This is the Kotlin implementation for the "Conway Sequence" puzzle on CodinGame. + +## Problem Description + +Conway's Sequence is a sequence of numbers, starting with a 1, such that each subsequent number is a description of the previous number. + +To generate a member of Conway's Sequence from the previous member, read off the digits of the previous member, counting the number of digits in groups of the same digit. For example, 1 is read off as "one 1" or 11, 11 is read off as "two 1s" or 21, 21 is read off as "one 2, then one 1" or 1211, and so on. + +Your task is to implement a function that generates the nth member of Conway's Sequence. + +## Solution + +The solution to this problem is to generate each member of the sequence, starting from 1, until we reach the nth member. To generate each member, we need to read off the digits of the previous member, counting the number of digits in groups of the same digit. + +We can do this using two nested loops. The outer loop generates each member of the sequence, while the inner loop reads off the digits of the previous member and counts the number of digits in groups of the same digit. diff --git a/puzzles/kotlin/src/conway-sequence.kt b/puzzles/kotlin/src/conway-sequence/conway-sequence.kt similarity index 100% rename from puzzles/kotlin/src/conway-sequence.kt rename to puzzles/kotlin/src/conway-sequence/conway-sequence.kt diff --git a/puzzles/kotlin/src/defibrillators/README.md b/puzzles/kotlin/src/defibrillators/README.md new file mode 100644 index 0000000..1293600 --- /dev/null +++ b/puzzles/kotlin/src/defibrillators/README.md @@ -0,0 +1,28 @@ +Sure, here is the updated markdown documentation for `defibrillators.kt` without the credits: + +# Defibrillators Puzzle + +This is the solution for the Defibrillators puzzle from CodinGame, implemented in Kotlin. + +The puzzle can be found at: [https://www.codingame.com/training/easy/defibrillators](https://www.codingame.com/training/easy/defibrillators) + +## Problem Description + +The goal of this puzzle is to find the closest defibrillator to a given location. The input contains a number of defibrillators, each with a name, a longitude, and a latitude. The user's location is also given as a longitude and latitude. The distance between two points can be calculated using the haversine formula. + +## Solution Description + +The solution reads in the user's location, the number of defibrillators, and the details of each defibrillator. For each defibrillator, the solution calculates the distance to the user's location using the haversine formula. The solution then keeps track of the closest defibrillator and returns its name. + +## How to Run + +The solution is implemented in Kotlin and can be run using the Kotlin compiler or an IDE such as IntelliJ IDEA. The code can be found in the `defibrillators.kt` file in the `src` folder. + +The program can be run by providing the user's location and the details of the defibrillators as command line arguments. For example: + +``` +kotlin defibrillators.kt -3.879483,48.879678 3 +"Defibrillator 1;6.145;49.986" +"Defibrillator 2;6.153;49.988" +"Defibrillator 3;6.155;49.990" +``` \ No newline at end of file diff --git a/puzzles/kotlin/src/defibrillators.kt b/puzzles/kotlin/src/defibrillators/defibrillators.kt similarity index 100% rename from puzzles/kotlin/src/defibrillators.kt rename to puzzles/kotlin/src/defibrillators/defibrillators.kt diff --git a/puzzles/kotlin/src/dont-panic1/README.md b/puzzles/kotlin/src/dont-panic1/README.md new file mode 100644 index 0000000..fd89365 --- /dev/null +++ b/puzzles/kotlin/src/dont-panic1/README.md @@ -0,0 +1,69 @@ + + +# Dont-Panic1 Puzzle Solution + +This is a markdown documentation for the Kotlin solution of the Dont-Panic1 puzzle on CodinGame. The solution can be found in the following file: + +- https://github.com/charlesfranciscodev/codingame/blob/master/puzzles/kotlin/src/dont-panic1.kt + +## Problem Description + +The puzzle is about a worker in a building where the floors are numbered from 0 (ground floor) to N. The worker is trying to reach a certain floor F, but the elevator he is in only stops on certain floors. + +The worker is given a set of instructions to follow in order to reach his desired floor. The instructions are in the form of pairs (floor, direction), where `floor` is the floor number that the worker must get off on, and `direction` is either "LEFT" or "RIGHT", indicating the direction the worker must walk from the elevator. + +If the worker reaches the desired floor F, the puzzle is solved. Otherwise, the worker must follow the instructions until he reaches the desired floor or falls off the building. + +## Solution Overview + +The solution to this puzzle involves simulating the movement of the worker in the building. We start by reading in the input data and parsing it into a more convenient format. + +Next, we initialize the state of the worker by setting his current floor and direction. We then iterate through the instructions, simulating the worker's movement on each floor. + +On each floor, we check if the worker needs to get off on that floor. If so, we check if he is going in the correct direction. If not, we change his direction and add an additional turn to his total turn count. + +Finally, we output the total turn count, which represents the minimum number of turns required for the worker to reach his desired floor. + +## Parsing the Input + +The input for this puzzle is given in the following format: + +``` +N P F +elevators +num_of_instructions +instruction_1 +instruction_2 +... +instruction_N +``` + +- `N` is the number of floors in the building. +- `P` is the position of the elevator on the starting floor. +- `F` is the desired floor that the worker wants to reach. +- `elevators` is a string of length N indicating which floors have elevators. A character of `0` means there is no elevator on that floor, and a character of `1` means there is an elevator. +- `num_of_instructions` is the number of instructions that the worker must follow. +- `instruction_i` is a pair of integers and a string representing the floor number, the direction (LEFT or RIGHT), and a space-separated pair of integers representing the floor number and the direction (LEFT or RIGHT). + +We start by reading in the first line of input and parsing `N`, `P`, and `F`: + +```kotlin +val (numFloors, elevatorPos, targetFloor) = readLine()!!.split(" ").map { it.toInt() } +``` + +Next, we read in the string `elevators` and convert it to a list of booleans using the `map` function: + +```kotlin +val elevators = readLine()!!.map { it == '1' } +``` + +We then read in the number of instructions and parse each instruction into a pair of integers and a string: + +```kotlin +val numInstructions = readLine()!!.toInt() +val instructions = List(numInstructions) { + val (floor, direction) = readLine()!!.split(" ") + val (floorNum, directionStr) = direction.split("-") + floorNum.toInt() to directionStr +} +``` diff --git a/puzzles/kotlin/src/dont-panic1.kt b/puzzles/kotlin/src/dont-panic1/dont-panic1.kt similarity index 100% rename from puzzles/kotlin/src/dont-panic1.kt rename to puzzles/kotlin/src/dont-panic1/dont-panic1.kt diff --git a/puzzles/kotlin/src/horse-racing-duals/README.md b/puzzles/kotlin/src/horse-racing-duals/README.md new file mode 100644 index 0000000..0a34e79 --- /dev/null +++ b/puzzles/kotlin/src/horse-racing-duals/README.md @@ -0,0 +1,46 @@ +# Horse Racing Duals + +This is a solution to the Horse Racing Duals puzzle on [CodinGame](https://www.codingame.com/ide/puzzle/horse-racing-duals). + +## Problem Description + +Given `N` integers representing the strengths of `N` horses, your goal is to find the minimum absolute difference between the strengths of any two horses in a pair. You must output this minimum difference. + +## Solution + +The approach to solve this problem is to sort the list of horse strengths in ascending order and then compute the absolute difference between adjacent horses. The smallest difference found is the minimum absolute difference between the strengths of any two horses. + +## Code Explanation + +The code begins with reading the integer `N` from the input. This is followed by reading `N` integers representing the strengths of the `N` horses. These strengths are stored in a list. + +The list of horse strengths is then sorted in ascending order using the `sorted()` function. A new list is created by computing the absolute difference between adjacent strengths in the sorted list. The minimum absolute difference found in this new list is the minimum absolute difference between the strengths of any two horses. + +Finally, the minimum absolute difference is printed to the console. + +```kotlin +import java.util.* + +fun main(args : Array) { + + val input = Scanner(System.`in`) + val n = input.nextInt() + + val strengths = mutableListOf() + for (i in 0 until n) { + strengths.add(input.nextInt()) + } + + strengths.sort() + val differences = mutableListOf() + for (i in 0 until n-1) { + differences.add(strengths[i+1] - strengths[i]) + } + + println(differences.min()) +} +``` + +## Conclusion + +This solution shows that sorting a list and then computing differences between adjacent elements is an effective way to solve this problem. The code is concise and easy to understand. \ No newline at end of file diff --git a/puzzles/kotlin/src/horse-racing-duals.kt b/puzzles/kotlin/src/horse-racing-duals/horse-racing-duals.kt similarity index 100% rename from puzzles/kotlin/src/horse-racing-duals.kt rename to puzzles/kotlin/src/horse-racing-duals/horse-racing-duals.kt diff --git a/puzzles/kotlin/src/mars-lander1/README.md b/puzzles/kotlin/src/mars-lander1/README.md new file mode 100644 index 0000000..883dffe --- /dev/null +++ b/puzzles/kotlin/src/mars-lander1/README.md @@ -0,0 +1,16 @@ +# Mars Lander 1 Puzzle + +This repository contains a Kotlin solution for the Mars Lander 1 puzzle on Codingame. + +## Problem Statement + +The Mars Lander 1 puzzle involves guiding a lander to a safe landing on the surface of Mars. The lander is subject to gravity and has a limited amount of fuel to control its descent. The goal is to land the lander at a specific landing zone without crashing or running out of fuel. + +The puzzle consists of a series of test cases, each with its own landing zone and obstacles. The player must guide the lander through each test case, optimizing their fuel usage and trajectory to achieve a safe landing. + +## Solution + +The solution to the Mars Lander 1 puzzle is implemented in the `MarsLander1.kt` file. The solution uses a simple physics-based approach to simulate the lander's descent and control its trajectory. + +The solution implements a loop that iterates over each time step of the simulation. At each time step, the solution calculates the lander's current state (position, velocity, fuel level) and determines the appropriate action to take (rotate the lander or fire the thrusters). + diff --git a/puzzles/kotlin/src/mars-lander1.kt b/puzzles/kotlin/src/mars-lander1/mars-lander1.kt similarity index 100% rename from puzzles/kotlin/src/mars-lander1.kt rename to puzzles/kotlin/src/mars-lander1/mars-lander1.kt diff --git a/puzzles/kotlin/src/mime-type/README.md b/puzzles/kotlin/src/mime-type/README.md new file mode 100644 index 0000000..c512111 --- /dev/null +++ b/puzzles/kotlin/src/mime-type/README.md @@ -0,0 +1,78 @@ +# Mime Type + +This is a Kotlin solution to the "Mime Type" problem on [Codingame](https://www.codingame.com/training/easy/mime-type). + +## Problem Description + +You are given a list of file names and their corresponding MIME types. You need to read a list of file names and determine the corresponding MIME type for each file name. If the MIME type cannot be determined, the program should output `UNKNOWN`. + +The MIME type is determined by the file extension. The table of file extensions and their corresponding MIME types is given. + +| Extension | MIME Type | +|-----------|-----------| +|html |text/html | +|htm |text/html | +|png |image/png | +|jpeg |image/jpeg | +|jpg |image/jpeg | +|gif |image/gif | +|bmp |image/bmp | +|txt |text/plain| +|pdf |application/pdf| + +The file name may have multiple periods (.) in it, but the extension is always the substring that follows the last period. + +## Solution + +The solution reads the number of file names, the number of MIME types and the list of file name and MIME type pairs. It stores the MIME types in a map with the extension as the key. Then it reads a list of file names and determines the corresponding MIME type by looking up the extension in the map. If the extension is not found, it outputs `UNKNOWN`. + +## Usage + +To use the solution, copy the code in the [mime-type.kt](https://github.com/charlesfranciscodev/codingame/blob/master/puzzles/kotlin/src/mime-type.kt) file and run it in a Kotlin environment. The input is read from the standard input and the output is written to the standard output. + +## Example + +Input: + +``` +2 +4 +html text/html +png image/png +gif image/gif +txt text/plain +file.html +file.txt +``` + +Output: + +``` +text/html +text/plain +``` + +## Code + +```kotlin +import java.util.* + +fun main(args : Array) { + val scanner = Scanner(System.`in`) + val n = scanner.nextInt() + val q = scanner.nextInt() + val map = mutableMapOf() + for (i in 0 until q) { + val ext = scanner.next() + val mime = scanner.next() + map[ext.toLowerCase()] = mime + } + for (i in 0 until n) { + val fileName = scanner.next() + val extension = fileName.substringAfterLast('.').toLowerCase() + val mime = map[extension] + println(mime ?: "UNKNOWN") + } +} +``` + diff --git a/puzzles/kotlin/src/mime-type.kt b/puzzles/kotlin/src/mime-type/mime-type.kt similarity index 100% rename from puzzles/kotlin/src/mime-type.kt rename to puzzles/kotlin/src/mime-type/mime-type.kt diff --git a/puzzles/kotlin/src/onboarding/README.md b/puzzles/kotlin/src/onboarding/README.md new file mode 100644 index 0000000..ad93e57 --- /dev/null +++ b/puzzles/kotlin/src/onboarding/README.md @@ -0,0 +1,29 @@ +This Kotlin code solves the "Onboarding" problem on CodinGame. The problem presents the player with two enemies and their respective distances, and the player must output the name of the enemy that is closer. + +The code starts by importing the `Scanner` class from the `java.util` package. The `Scanner` class allows the program to read user input from the standard input stream. + +The `main` function sets up an infinite loop that continuously reads input from the standard input stream. Within the loop, the program reads in the name of the first enemy and its distance using the `scanner.next()` and `scanner.nextInt()` methods, respectively. It then reads in the name of the second enemy and its distance in a similar way. + +The program then compares the distances of the two enemies using an `if` statement. If the distance of the first enemy is less than the distance of the second enemy, the program outputs the name of the first enemy using the `println` method. Otherwise, it outputs the name of the second enemy. + +The loop continues indefinitely, waiting for new input from the standard input stream. + +```kotlin +import java.util.Scanner + +fun main(args: Array) { + val scanner = Scanner(System.`in`) + while (true) { + val enemy1 = scanner.next() // name of enemy 1 + val dist1 = scanner.nextInt() // distance to enemy 1 + val enemy2 = scanner.next() // name of enemy 2 + val dist2 = scanner.nextInt() // distance to enemy 2 + + if (dist1 < dist2) { + println(enemy1) + } else { + println(enemy2) + } + } +} +``` diff --git a/puzzles/kotlin/src/onboarding/onboarding.kt b/puzzles/kotlin/src/onboarding/onboarding.kt new file mode 100644 index 0000000..ec5f6eb --- /dev/null +++ b/puzzles/kotlin/src/onboarding/onboarding.kt @@ -0,0 +1,17 @@ +import java.util.Scanner + +fun main(args: Array) { + val scanner = Scanner(System.`in`) + while (true) { + val enemy1 = scanner.next() // name of enemy 1 + val dist1 = scanner.nextInt() // distance to enemy 1 + val enemy2 = scanner.next() // name of enemy 2 + val dist2 = scanner.nextInt() // distance to enemy 2 + + if (dist1 < dist2) { + println(enemy1) + } else { + println(enemy2) + } + } +} diff --git a/puzzles/kotlin/src/power-of-thor1/README.md b/puzzles/kotlin/src/power-of-thor1/README.md new file mode 100644 index 0000000..55f836e --- /dev/null +++ b/puzzles/kotlin/src/power-of-thor1/README.md @@ -0,0 +1,15 @@ +# Power of Thor Puzzle Solution in Kotlin + +## Problem Description + +In the Power of Thor puzzle on [CodinGame](https://www.codingame.com/training/easy/power-of-thor-episode-1), you control Thor, who has to reach the light of power located at a certain (x, y) coordinate. Thor starts at a different (x, y) coordinate on the map and has a limited amount of energy. + +On each turn, you must print the direction in which Thor should move towards the light of power: North, South, East, or West. + +## Code Explanation + +The solution is implemented in the `main` function of the `PowerOfThor1.kt` file. The main function reads the initial values of Thor's position (`x` and `y`) and the position of the light of power (`lightX` and `lightY`) from the standard input. + +The function then enters a loop that runs until Thor reaches the light of power. In each iteration of the loop, the function calculates the direction in which Thor should move by comparing Thor's current position to the position of the light of power. + +The direction is printed to the console, and Thor's position is updated accordingly. The loop continues until Thor reaches the light of power. \ No newline at end of file diff --git a/puzzles/kotlin/src/power-of-thor1.kt b/puzzles/kotlin/src/power-of-thor1/power-of-thor1.kt similarity index 100% rename from puzzles/kotlin/src/power-of-thor1.kt rename to puzzles/kotlin/src/power-of-thor1/power-of-thor1.kt diff --git a/puzzles/kotlin/src/scrabble/README.md b/puzzles/kotlin/src/scrabble/README.md new file mode 100644 index 0000000..96e2e25 --- /dev/null +++ b/puzzles/kotlin/src/scrabble/README.md @@ -0,0 +1,13 @@ +# Scrabble Solver + +This is a Kotlin program that solves the popular word game Scrabble. Given a set of tiles with letters on them, the program finds all valid words that can be created using those tiles. + +## Usage + +To use this program, simply run it from the command line with a list of tiles as arguments. For example: + +``` +$ kotlin ScrabbleSolver.kt A B C D E F G +``` + +This will find all valid words that can be created using the letters A, B, C, D, E, F, and G. diff --git a/puzzles/kotlin/src/scrabble.kt b/puzzles/kotlin/src/scrabble/scrabble.kt similarity index 100% rename from puzzles/kotlin/src/scrabble.kt rename to puzzles/kotlin/src/scrabble/scrabble.kt diff --git a/puzzles/kotlin/src/shadows-knight1/README.md b/puzzles/kotlin/src/shadows-knight1/README.md new file mode 100644 index 0000000..faacd5c --- /dev/null +++ b/puzzles/kotlin/src/shadows-knight1/README.md @@ -0,0 +1,19 @@ + + +# Shadows of the Knight - Episode 1 + +This is a solution to the "Shadows of the Knight - Episode 1" puzzle on CodinGame (https://www.codingame.com/training/easy/shadows-of-the-knight-episode-1). + +## Problem Description + +The problem requires us to simulate a scenario where Batman is trying to locate the Joker in a building with N floors and M rooms per floor. Batman starts at a certain position in the building and has a limited number of moves. In each move, he can choose to move up, down, left or right to a new position in the building. The building is described by a 2D grid of characters, where '.' represents an empty room, '#' represents a wall, and 'X' represents the location of the Joker. + +The objective is to determine the location of the Joker using the fewest possible number of moves. + +## Solution Overview + +To solve the problem, we will use a binary search algorithm to determine the location of the Joker on each turn. The basic idea is to divide the building into smaller and smaller sub-regions by guessing the position of the Joker on each turn. We will start with a search space that includes the entire building, and then refine our search space on each turn based on the feedback provided by Batman's movements. + +## Conclusion + +The "Shadows of the Knight - Episode 1" puzzle is a simple example of a search problem that can be solved using a binary search algorithm. The solution presented here demonstrates the basic concepts involved in solving such problems and can be used as a starting point for more complex search problems. diff --git a/puzzles/kotlin/src/shadows-knight1.kt b/puzzles/kotlin/src/shadows-knight1/shadows-knight1.kt similarity index 100% rename from puzzles/kotlin/src/shadows-knight1.kt rename to puzzles/kotlin/src/shadows-knight1/shadows-knight1.kt diff --git a/puzzles/kotlin/src/skynet-revolution2/README.md b/puzzles/kotlin/src/skynet-revolution2/README.md new file mode 100644 index 0000000..7ada2a6 --- /dev/null +++ b/puzzles/kotlin/src/skynet-revolution2/README.md @@ -0,0 +1,21 @@ +# Skynet Revolution - Episode 2 + +## Alternative Solution + +The solution to this problem is based on the idea of finding the articulation points of the graph. An articulation point is a vertex whose removal disconnects the graph into two or more disconnected components. + +To find the articulation points, we use a modified version of the depth-first search algorithm. We maintain the depth of each node and the lowest depth reachable from the node using back edges. If a child node has a back edge to one of its ancestors, then the ancestor is not an articulation point. Otherwise, if the lowest depth reachable from the child node is greater than or equal to the depth of the current node, then the current node is an articulation point. + +Once we have identified the articulation points, we can find the critical links by removing each link and checking if any of the articulation points is no longer reachable from the other side of the link. + +## General Tips + +1. Use a graph data structure to represent the network of nodes and links. You can use an adjacency list or an adjacency matrix to store the connections between nodes. + +2. To implement the depth-first search algorithm, you can use a recursive function that visits each node and updates its depth and lowest reachable depth. + +3. To check if a link is critical, you can remove the link from the graph and check if any of the articulation points is no longer reachable from the other side of the link. To do this, you can use a modified version of the depth-first search algorithm that starts from the other side of the link and checks if any of the articulation points is reachable. + +4. To optimize the algorithm, you can use memoization to avoid recalculating the depth and lowest reachable depth of each node multiple times. You can also use a set or a hash table to store the list of articulation points and the list of critical links, to avoid duplicates. + +5. Use good coding practices, such as using meaningful variable names, writing comments to explain the code, and testing the code with different inputs and edge cases. \ No newline at end of file diff --git a/puzzles/kotlin/src/skynet-revolution2.kt b/puzzles/kotlin/src/skynet-revolution2/skynet-revolution2.kt similarity index 100% rename from puzzles/kotlin/src/skynet-revolution2.kt rename to puzzles/kotlin/src/skynet-revolution2/skynet-revolution2.kt diff --git a/puzzles/kotlin/src/super-computer/README.md b/puzzles/kotlin/src/super-computer/README.md new file mode 100644 index 0000000..897fcf6 --- /dev/null +++ b/puzzles/kotlin/src/super-computer/README.md @@ -0,0 +1,9 @@ +# Super Computer + +The Super Computer puzzle on Codingame is a programming challenge where the goal is to schedule a set of tasks on a number of available processors, maximizing the number of tasks that can be completed within a given timeframe. + +The input for the puzzle consists of the number of tasks and the amount of time required to complete each task. The output is the maximum number of tasks that can be scheduled within a given timeframe. + +To solve this puzzle, you need to implement an efficient algorithm to schedule the tasks. One approach is to use a greedy algorithm that sorts the tasks in descending order based on their duration, and then assigns each task to the processor with the earliest available finish time. + +As you progress through the puzzle, you will encounter various edge cases that will test the robustness of your algorithm. Overall, the Super Computer puzzle is a great way to practice your programming skills and algorithmic thinking. diff --git a/puzzles/kotlin/src/super-computer.kt b/puzzles/kotlin/src/super-computer/super-computer.kt similarity index 100% rename from puzzles/kotlin/src/super-computer.kt rename to puzzles/kotlin/src/super-computer/super-computer.kt diff --git a/puzzles/kotlin/src/surface/README.md b/puzzles/kotlin/src/surface/README.md new file mode 100644 index 0000000..9b7c0b3 --- /dev/null +++ b/puzzles/kotlin/src/surface/README.md @@ -0,0 +1,7 @@ +# Surface + +The coding challenge on the website https://www.codingame.com/training/hard/surface asks the user to write a program that calculates the total surface area of a group of connected cells in a two-dimensional grid. The grid consists of cells that are either black or white, and the cells are connected horizontally or vertically. The program must read the input which contains the dimensions of the grid and the color of each cell, and then output the total surface area of the largest group of connected cells of the same color. + +To solve this challenge, the program must use a graph traversal algorithm, such as depth-first search or breadth-first search, to find the connected components of cells that have the same color. Once the program has identified all of the connected components, it can calculate the surface area of each component and return the maximum surface area. + +The program needs to handle different edge cases, such as a grid that has only black cells or only white cells, a grid with only one cell, or a grid with disconnected components of the same color. The challenge also provides multiple test cases to ensure that the program correctly handles various inputs. diff --git a/puzzles/kotlin/src/surface.kt b/puzzles/kotlin/src/surface/surface.kt similarity index 100% rename from puzzles/kotlin/src/surface.kt rename to puzzles/kotlin/src/surface/surface.kt diff --git a/puzzles/kotlin/src/tan-network/README.md b/puzzles/kotlin/src/tan-network/README.md new file mode 100644 index 0000000..9d04602 --- /dev/null +++ b/puzzles/kotlin/src/tan-network/README.md @@ -0,0 +1,15 @@ +# Tan Network + +This is a solution for the "Tan Network" puzzle on Codingame platform, written in Kotlin. + +## Problem Description + +The problem statement can be found on the following link: https://www.codingame.com/training/medium/tan-network + +In summary, the problem is about finding the shortest path between two nodes in a weighted graph. + +## Solution + +The solution to this problem is based on Dijkstra's algorithm. The basic idea is to start with a source node and then explore its neighboring nodes. We keep track of the distance from the source node to each neighboring node, and we update it if we find a shorter path. We then select the unexplored node with the shortest distance and repeat the process until we reach the target node. + +The implementation involves a priority queue to select the next node to explore. The distance from the source node to each node is stored in an array. We also keep track of the visited nodes to avoid revisiting them. diff --git a/puzzles/kotlin/src/tan-network.kt b/puzzles/kotlin/src/tan-network/tan-network.kt similarity index 100% rename from puzzles/kotlin/src/tan-network.kt rename to puzzles/kotlin/src/tan-network/tan-network.kt diff --git a/puzzles/kotlin/src/temperatures/README.Md b/puzzles/kotlin/src/temperatures/README.Md new file mode 100644 index 0000000..8bb3e10 --- /dev/null +++ b/puzzles/kotlin/src/temperatures/README.Md @@ -0,0 +1,58 @@ + + +# temperatures.kt + +This is a Kotlin solution for the "Temperatures" puzzle on the Codingame platform. + +## Problem Description + +Given a list of temperatures, find the temperature closest to zero. If two temperatures are equally close to zero, return the positive temperature. + +### Input + +- `n`: an integer, the number of temperatures in the list (0 โ‰ค `n` โ‰ค 1,000,000) +- `temperatures`: a space-separated list of `n` integers representing the temperatures (-273 โ‰ค `temperatures[i]` โ‰ค 5526) + +### Output + +- The temperature closest to zero. If two temperatures are equally close to zero, return the positive temperature. + +## Solution Overview + +To solve this problem, we need to find the temperature with the smallest absolute value that is greater than or equal to zero. We can do this by iterating over the list of temperatures and keeping track of the temperature with the smallest absolute value seen so far. If two temperatures have the same absolute value, we choose the positive one. + +## Solution Details + +Here's the code for the solution: + +```kotlin +fun main(args : Array) { + val n = readLine()!!.toInt() + val temperatures = readLine()!!.split(" ").map{ it.toInt() } + + var closestTemp = 5526 // The highest possible temperature + for (temperature in temperatures) { + val absTemp = kotlin.math.abs(temperature) + val absClosestTemp = kotlin.math.abs(closestTemp) + if (absTemp < absClosestTemp || (absTemp == absClosestTemp && temperature > 0)) { + closestTemp = temperature + } + } + + println(closestTemp) +} +``` + +Here's how the solution works: + +1. We read in the input values of `n` and `temperatures` using `readLine()!!`. +2. We set `closestTemp` to the highest possible temperature, `5526`. +3. We iterate over the list of temperatures using a for loop, and for each temperature: + - We calculate its absolute value using `kotlin.math.abs(temperature)`. + - We calculate the absolute value of `closestTemp` using `kotlin.math.abs(closestTemp)`. + - If the absolute value of the current temperature is smaller than the absolute value of `closestTemp`, or if the absolute values are equal and the current temperature is positive, we update `closestTemp` to the current temperature. +4. We print the final value of `closestTemp`. + +## Conclusion + +This Kotlin solution uses a simple algorithm to find the temperature closest to zero in a given list of temperatures. By keeping track of the temperature with the smallest absolute value seen so far, we can efficiently find the desired temperature. \ No newline at end of file diff --git a/puzzles/kotlin/src/temperatures.kt b/puzzles/kotlin/src/temperatures/temperatures.kt similarity index 100% rename from puzzles/kotlin/src/temperatures.kt rename to puzzles/kotlin/src/temperatures/temperatures.kt diff --git a/puzzles/kotlin/src/the-descent/README.md b/puzzles/kotlin/src/the-descent/README.md new file mode 100644 index 0000000..392a907 --- /dev/null +++ b/puzzles/kotlin/src/the-descent/README.md @@ -0,0 +1,35 @@ +Sure, here's the same documentation without the conclusion section: + +# The Descent + +This is a Kotlin solution for the "The Descent" puzzle on [Codingame](https://www.codingame.com/ide/puzzle/the-descent). + +## Problem Description + +The goal of the puzzle is to destroy mountains, one by one, by shooting them with a cannon. You have a limited number of shots, and each shot destroys a single point on the mountain at the height of the shot. The objective is to destroy the highest mountain first and then move on to the next highest mountain, until all the mountains are destroyed. + +The input for the puzzle is an array of integers representing the height of each mountain. The program should output the index of the highest mountain in the array, and then loop until all the mountains are destroyed. + +## Solution + +The solution uses a loop to keep firing shots until all the mountains are destroyed. At each iteration, the program finds the index of the highest mountain and fires a shot at that index. The program then updates the array to remove the destroyed point, and repeats the process until all the mountains are destroyed. + +```kotlin +fun main() { + // read in the mountain heights + val mountainHeights = IntArray(8) { readLine()!!.toInt() } + + while (true) { + // find the highest mountain + val highestMountainIndex = mountainHeights.indexOf(mountainHeights.max()) + + // fire a shot at the highest mountain + println(highestMountainIndex) + + // update the mountain heights array + mountainHeights[highestMountainIndex] = 0 + } +} +``` + +The program starts by reading in the mountain heights using the `readLine()` function. It then enters a loop that repeats until all the mountains have been destroyed. In each iteration of the loop, the program finds the index of the highest mountain using the `indexOf()` function, which returns the index of the first occurrence of the given element in the array. The program then fires a shot at the highest mountain by printing the index of the highest mountain to the console. Finally, the program updates the mountain heights array by setting the value of the highest mountain to 0. \ No newline at end of file diff --git a/puzzles/kotlin/src/the-descent.kt b/puzzles/kotlin/src/the-descent/the-descent.kt similarity index 100% rename from puzzles/kotlin/src/the-descent.kt rename to puzzles/kotlin/src/the-descent/the-descent.kt diff --git a/puzzles/kotlin/src/there-is-no-spoon1/README.md b/puzzles/kotlin/src/there-is-no-spoon1/README.md new file mode 100644 index 0000000..76de8e9 --- /dev/null +++ b/puzzles/kotlin/src/there-is-no-spoon1/README.md @@ -0,0 +1,15 @@ +Sure, here's the Markdown document for the "There Is No Spoon 1" coding puzzle in Kotlin without the code section: + +# There Is No Spoon 1 - Kotlin Solution + +This is a solution in Kotlin to the "There Is No Spoon 1" coding puzzle on [CodinGame](https://www.codingame.com/training/easy/there-is-no-spoon-episode-1). + +## Problem Description + +The problem requires finding the neighboring cells of each node in a 2D grid with no space between cells. The cells are represented by "." in the input string, and the node indices are represented by "0". The output is the indices of the neighboring nodes for each node. + +## Solution + +The solution reads the input grid into a 2D array of characters, and then iterates over the cells to find the neighboring cells. The neighboring cells are found by searching in each direction from the current cell until the edge of the grid is reached or another node is found. + +The solution uses a nested loop to iterate over the cells, and then a nested loop to search in each direction for neighboring cells. The indices of neighboring nodes are stored in a list, and then output at the end of the search for each node. \ No newline at end of file diff --git a/puzzles/kotlin/src/there-is-no-spoon1.kt b/puzzles/kotlin/src/there-is-no-spoon1/there-is-no-spoon1.kt similarity index 100% rename from puzzles/kotlin/src/there-is-no-spoon1.kt rename to puzzles/kotlin/src/there-is-no-spoon1/there-is-no-spoon1.kt diff --git a/puzzles/kotlin/src/war/README.md b/puzzles/kotlin/src/war/README.md new file mode 100644 index 0000000..16dd5aa --- /dev/null +++ b/puzzles/kotlin/src/war/README.md @@ -0,0 +1,27 @@ +Here is the Markdown documentation for the Kotlin file `war.kt` located in the `puzzles/kotlin/src` directory of the `charlesfranciscodev/codingame` GitHub repository: + +--- + +# War Game Simulation + +This is a Kotlin implementation of the War card game simulation. The War card game is a simple two-player card game where each player gets half of the deck and then they take turns playing the top card from their hand. The player with the highest card wins the round and takes both cards. If both players play cards of the same value, then a "war" is declared and each player plays three additional cards. The winner of the "war" round takes all the cards played in that round. + +## Code Structure + +The code is divided into the following classes: + +### Card + +This class represents a single playing card in the game. It has two properties: `value` and `suit`. The `value` property is an integer value from 2 to 14 representing the card value, and the `suit` property is a string representing the card suit. + +### Deck + +This class represents a deck of playing cards. It has a list of `Card` objects and functions for shuffling the deck and dealing cards to players. + +### Player + +This class represents a player in the game. It has a list of `Card` objects representing the player's hand and functions for playing cards, adding cards to the player's hand, and checking if the player has any cards left. + +### Game + +This class represents the game itself. It has two `Player` objects, a `Deck` object, and functions for playing the game and determining the winner. diff --git a/puzzles/kotlin/src/war.kt b/puzzles/kotlin/src/war/war.kt similarity index 100% rename from puzzles/kotlin/src/war.kt rename to puzzles/kotlin/src/war/war.kt diff --git a/puzzles/php/ascii-art.php b/puzzles/php/ascii-art.php deleted file mode 100644 index 7598987..0000000 --- a/puzzles/php/ascii-art.php +++ /dev/null @@ -1,22 +0,0 @@ - 25) - $position = 26; - $output .= substr($row, $position * $width, $width); - } - echo "$output\n"; -} -?> diff --git a/puzzles/php/conway_sequence.php b/puzzles/php/conway_sequence.php deleted file mode 100644 index b419584..0000000 --- a/puzzles/php/conway_sequence.php +++ /dev/null @@ -1,42 +0,0 @@ - diff --git a/puzzles/php/defibrillators.php b/puzzles/php/defibrillators.php deleted file mode 100644 index 88b4e11..0000000 --- a/puzzles/php/defibrillators.php +++ /dev/null @@ -1,28 +0,0 @@ - diff --git a/puzzles/php/dont_panic1.php b/puzzles/php/dont_panic1.php deleted file mode 100644 index 1b514fb..0000000 --- a/puzzles/php/dont_panic1.php +++ /dev/null @@ -1,43 +0,0 @@ - $elevators[$cloneFloor]) { - echo("BLOCK\n"); - } else { - echo("WAIT\n"); - } -} -?> \ No newline at end of file diff --git a/puzzles/php/horse-racing-duals.php b/puzzles/php/horse-racing-duals.php deleted file mode 100644 index ded9492..0000000 --- a/puzzles/php/horse-racing-duals.php +++ /dev/null @@ -1,18 +0,0 @@ - \ No newline at end of file diff --git a/puzzles/php/mars-lander.php b/puzzles/php/mars-lander.php deleted file mode 100644 index 4cfb5ed..0000000 --- a/puzzles/php/mars-lander.php +++ /dev/null @@ -1,33 +0,0 @@ - diff --git a/puzzles/php/mime-type.php b/puzzles/php/mime-type.php deleted file mode 100644 index 124522c..0000000 --- a/puzzles/php/mime-type.php +++ /dev/null @@ -1,22 +0,0 @@ - diff --git a/puzzles/php/power-of-thor.php b/puzzles/php/power-of-thor.php deleted file mode 100644 index 91697ca..0000000 --- a/puzzles/php/power-of-thor.php +++ /dev/null @@ -1,33 +0,0 @@ - $lightY) { - $direction .= "N"; - --$thorY; - } else if ($thorY < $lightY) { - $direction .= "S"; - ++$thorY; - } - - if ($thorX > $lightX) { - $direction .= "W"; - --$thorX; - } else if ($thorX < $lightX) { - $direction .= "E"; - ++$thorX; - } - - // A single line providing the move to be made: N NE E SE S SW W or NW - echo("$direction" . "\n"); -} -?> diff --git a/puzzles/php/scrabble.php b/puzzles/php/scrabble.php deleted file mode 100644 index 8a1539d..0000000 --- a/puzzles/php/scrabble.php +++ /dev/null @@ -1,52 +0,0 @@ - 1, 'a' => 1, 'i' => 1, 'o' => 1, 'n' => 1, 'r' => 1, 't' => 1, 'l' => 1, 's' => 1,'u' => 1, - 'd' => 2, 'g' => 2, - 'b' => 3, 'c' => 3, 'm' => 3, 'p' => 3, - 'f' => 4, 'h' => 4, 'v' => 4, 'w' => 4, 'y' => 4, - 'k' => 5, - 'j' => 8, 'x' => 8, - 'q' => 10, 'z' => 10 - ); - - return $arr[$letter]; -} - -$bestWord = ""; -$highScore = 0; -$dictionary = []; - -fscanf(STDIN, "%d", $nbWords); -for ($i = 0; $i < $nbWords; ++$i) { - $dictionary[] = stream_get_line(STDIN, 31, "\n"); -} -$letters = stream_get_line(STDIN, 9, "\n"); - -for ($i = 0; $i < $nbWords; ++$i) { - $letters2 = $letters; - $word = $dictionary[$i]; - $isValid = true; - $currentScore = 0; - - // calculate the total score for one word - for ($j = 0; $j < strlen($word); ++$j) { - $character = $word[$j]; - $pos = strpos($letters2, $character); - if ($pos !== false) { - $letters2 = substr_replace($letters2, "", $pos, strlen($character)); // remove the letter - $currentScore += getScore($character); - } else { - $isValid = false; // unavailable letter - break; - } - } - - if ($isValid && ($currentScore > $highScore)) { - $highScore = $currentScore; - $bestWord = $word; - } -} - -echo("$bestWord\n"); -?> diff --git a/puzzles/php/shadows-knight1.php b/puzzles/php/shadows-knight1.php deleted file mode 100644 index 5828b09..0000000 --- a/puzzles/php/shadows-knight1.php +++ /dev/null @@ -1,56 +0,0 @@ - \ No newline at end of file diff --git a/puzzles/php/temperatures.php b/puzzles/php/temperatures.php deleted file mode 100644 index e844dc8..0000000 --- a/puzzles/php/temperatures.php +++ /dev/null @@ -1,8 +0,0 @@ - $x ? -1 : 1) : (abs($w) < abs($x) ? -1 : 1); -}); -echo $n == 0 ? "0" : "$t[0]\n"; -?> \ No newline at end of file diff --git a/puzzles/php/there-is-no-spoon.php b/puzzles/php/there-is-no-spoon.php deleted file mode 100644 index 1408af6..0000000 --- a/puzzles/php/there-is-no-spoon.php +++ /dev/null @@ -1,50 +0,0 @@ - \ No newline at end of file diff --git a/puzzles/python3/defibrillators/defibrillators.py b/puzzles/python3/defibrillators/defibrillators.py index 9adf9b0..1a41976 100644 --- a/puzzles/python3/defibrillators/defibrillators.py +++ b/puzzles/python3/defibrillators/defibrillators.py @@ -8,20 +8,20 @@ def to_radians(angle_in_degrees: str) -> float: if __name__ == "__main__": - min_distance = math.inf - closest_defib_name = "" - longitude = to_radians(input()) - latitude = to_radians(input()) - nb_defib = int(input()) + min_distance: float = math.inf + closest_defib_name: str = "" + longitude: float = to_radians(input()) + latitude: float = to_radians(input()) + nb_defib: int = int(input()) for _ in range(nb_defib): defib: List[str] = input().split(";") - defib_longitude = to_radians(defib[4]) - defib_latitude = to_radians(defib[5]) - x = (defib_longitude - longitude) * math.cos((latitude + defib_latitude) / 2) - y = defib_latitude - latitude + defib_longitude: float = to_radians(defib[4]) + defib_latitude: float = to_radians(defib[5]) + x: float = (defib_longitude - longitude) * math.cos((latitude + defib_latitude) / 2) + y: float = defib_latitude - latitude EARTH_RADIUS: int = 6371 - distance = math.hypot(x, y) * EARTH_RADIUS + distance: float = math.hypot(x, y) * EARTH_RADIUS if distance < min_distance: min_distance = distance closest_defib_name = defib[1] diff --git a/puzzles/python3/genome_sequencing.py b/puzzles/python3/genome_sequencing.py index 8ae1c3f..91891fd 100644 --- a/puzzles/python3/genome_sequencing.py +++ b/puzzles/python3/genome_sequencing.py @@ -1,3 +1,4 @@ +# Based on https://www.xarg.org/puzzle/codingame/genome-sequencing/ import itertools from typing import List, Tuple diff --git a/puzzles/python3/mime_type.py b/puzzles/python3/mime_type.py index d5e3806..b327a1f 100644 --- a/puzzles/python3/mime_type.py +++ b/puzzles/python3/mime_type.py @@ -3,20 +3,20 @@ if __name__ == "__main__": table: Dict[str, str] = {} # file extension => mime type - nb_elements = int(input()) - nb_names = int(input()) + nb_elements: int = int(input()) + nb_names: int = int(input()) for _ in range(nb_elements): extension, mime_type = input().split() table[extension.lower()] = mime_type for _ in range(nb_names): - name = input().lower() - dot_index = name.rfind(".") + name: str = input().lower() + dot_index: int = name.rfind(".") if dot_index == -1 or dot_index == len(name) - 1: print("UNKNOWN") else: - extension = name[dot_index + 1:] + extension: str = name[dot_index + 1:] if extension in table: print(table[extension]) else: diff --git a/puzzles/python3/onboarding/README.md b/puzzles/python3/onboarding/README.md new file mode 100644 index 0000000..0ac7fea --- /dev/null +++ b/puzzles/python3/onboarding/README.md @@ -0,0 +1,17 @@ +Here's a possible solution to the "Onboarding" challenge on CodinGame using Python: + +```python +while True: + enemy_1 = input() # name of enemy 1 + dist_1 = int(input()) # distance to enemy 1 + enemy_2 = input() # name of enemy 2 + dist_2 = int(input()) # distance to enemy 2 + + # Determine which enemy is closer and print its name + if dist_1 < dist_2: + print(enemy_1) + else: + print(enemy_2) +``` + +In this solution, we use a `while` loop to continuously read input from the standard input until the program is terminated. In each iteration of the loop, we read the name and distance of two enemies, and then determine which one is closer based on their distances. Finally, we print the name of the closer enemy using `print()`. diff --git a/puzzles/python3/onboarding.py b/puzzles/python3/onboarding/onboarding.py similarity index 100% rename from puzzles/python3/onboarding.py rename to puzzles/python3/onboarding/onboarding.py diff --git a/puzzles/python3/shadows-knight1/README.md b/puzzles/python3/shadows-knight1/README.md new file mode 100644 index 0000000..faacd5c --- /dev/null +++ b/puzzles/python3/shadows-knight1/README.md @@ -0,0 +1,19 @@ + + +# Shadows of the Knight - Episode 1 + +This is a solution to the "Shadows of the Knight - Episode 1" puzzle on CodinGame (https://www.codingame.com/training/easy/shadows-of-the-knight-episode-1). + +## Problem Description + +The problem requires us to simulate a scenario where Batman is trying to locate the Joker in a building with N floors and M rooms per floor. Batman starts at a certain position in the building and has a limited number of moves. In each move, he can choose to move up, down, left or right to a new position in the building. The building is described by a 2D grid of characters, where '.' represents an empty room, '#' represents a wall, and 'X' represents the location of the Joker. + +The objective is to determine the location of the Joker using the fewest possible number of moves. + +## Solution Overview + +To solve the problem, we will use a binary search algorithm to determine the location of the Joker on each turn. The basic idea is to divide the building into smaller and smaller sub-regions by guessing the position of the Joker on each turn. We will start with a search space that includes the entire building, and then refine our search space on each turn based on the feedback provided by Batman's movements. + +## Conclusion + +The "Shadows of the Knight - Episode 1" puzzle is a simple example of a search problem that can be solved using a binary search algorithm. The solution presented here demonstrates the basic concepts involved in solving such problems and can be used as a starting point for more complex search problems. diff --git a/puzzles/python3/shadows_knight1.py b/puzzles/python3/shadows-knight1/shadows_knight1.py similarity index 100% rename from puzzles/python3/shadows_knight1.py rename to puzzles/python3/shadows-knight1/shadows_knight1.py diff --git a/puzzles/python3/surface/README.md b/puzzles/python3/surface/README.md index 3fc0aa9..fe8ac2f 100644 --- a/puzzles/python3/surface/README.md +++ b/puzzles/python3/surface/README.md @@ -1,6 +1,10 @@ # Surface -The goal of this puzzle is to find in a grid groups of adjacents water cells and compute the size of the surface or lake. You can use a flood fill algorithm to traverse the grid and memoization to store the state of each cell in the grid for reusing it later. +The coding challenge on the website https://www.codingame.com/training/hard/surface asks the user to write a program that calculates the total surface area of a group of connected cells in a two-dimensional grid. The grid consists of cells that are either black or white, and the cells are connected horizontally or vertically. The program must read the input which contains the dimensions of the grid and the color of each cell, and then output the total surface area of the largest group of connected cells of the same color. + +To solve this challenge, the program must use a graph traversal algorithm, such as depth-first search or breadth-first search, to find the connected components of cells that have the same color. Once the program has identified all of the connected components, it can calculate the surface area of each component and return the maximum surface area. + +The program needs to handle different edge cases, such as a grid that has only black cells or only white cells, a grid with only one cell, or a grid with disconnected components of the same color. The challenge also provides multiple test cases to ensure that the program correctly handles various inputs. ## Links * [Wikipedia](https://en.wikipedia.org/wiki/Flood_fill) diff --git a/puzzles/python3/there-is-no-spoon1/README.md b/puzzles/python3/there-is-no-spoon1/README.md index 4f8d372..5f19150 100644 --- a/puzzles/python3/there-is-no-spoon1/README.md +++ b/puzzles/python3/there-is-no-spoon1/README.md @@ -1,5 +1,11 @@ -# There is no Spoon - Episode 1 +# There Is No Spoon 1 -The goal of this puzzle is to learn about multidimensional arrays. In this case, a 2D array. +## Problem Description -In this puzzle, you also have to store input values into a grid to explore it. You have to go through all elements from a grid (certainly using a double loop) and from those points, iterate again on some elements of the grid. Solving this puzzle make you learn the concept of nested loops. +The problem requires finding the neighboring cells of each node in a 2D grid with no space between cells. The cells are represented by "." in the input string, and the node indices are represented by "0". The output is the indices of the neighboring nodes for each node. + +## Solution + +The solution reads the input grid into a 2D array of characters, and then iterates over the cells to find the neighboring cells. The neighboring cells are found by searching in each direction from the current cell until the edge of the grid is reached or another node is found. + +The solution uses a nested loop to iterate over the cells, and then a nested loop to search in each direction for neighboring cells. The indices of neighboring nodes are stored in a list, and then output at the end of the search for each node. \ No newline at end of file diff --git a/puzzles/ruby/ascii-art.rb b/puzzles/ruby/ascii-art.rb deleted file mode 100644 index d38f62d..0000000 --- a/puzzles/ruby/ascii-art.rb +++ /dev/null @@ -1,26 +0,0 @@ -@width = gets.to_i # the width of a letter represented in ASCII art -@height = gets.to_i # the height of a letter represented in ASCII art. -@text = gets.chomp.upcase - -asciiArt = Array.new # the characters A to Z and ? represented in ASCII art. - -@height.times do - row = gets.chomp - asciiArt.push(row) -end - -# Prints the letter for every height level -asciiArt.each do |row| - @text.each_char { |c| - ascii = c.ord - position = ascii - 'A'.ord - unless position.between?(0, 25) - position = 26 - end - column = position * @width - for i in column..(column + @width - 1) do - print row[i] - end - } - print "\n" -end diff --git a/puzzles/ruby/defibrillators.rb b/puzzles/ruby/defibrillators.rb deleted file mode 100644 index 0d17891..0000000 --- a/puzzles/ruby/defibrillators.rb +++ /dev/null @@ -1,38 +0,0 @@ -# Convert degrees to radians -def to_radians(degrees) - degrees.to_f * Math::PI / 180 -end - -def format_degrees(distance) - to_radians(distance.chomp.gsub(',', '.')) -end - -EARTH_RADIUS = 6371 -NAME = 1 -LONGITUDE = 4 -LATITUDE = 5 - -min = Float::MAX -name = '' - -longitude = format_degrees(gets) -latitude = format_degrees(gets) -defib_count = gets.to_i - -# Determine the name of the defibrillator -# located the closest to the userโ€™s position. -defib_count.times do - defib = gets.chomp - defibArray = defib.split(';') - defibLon = format_degrees(defibArray[LONGITUDE]) - defibLat = format_degrees(defibArray[LATITUDE]) - x = (defibLon - longitude) * (Math.cos((latitude + defibLat) / 2)) - y = defibLat - latitude - distance = Math.hypot(x, y) * EARTH_RADIUS - if distance < min - min = distance - name = defibArray[NAME] - end -end - -puts name diff --git a/puzzles/ruby/horse-racing-duals/README.md b/puzzles/ruby/horse-racing-duals/README.md index 055840a..eba9bce 100644 --- a/puzzles/ruby/horse-racing-duals/README.md +++ b/puzzles/ruby/horse-racing-duals/README.md @@ -1,5 +1,15 @@ -# Horse-racing Duals +# Horse Racing Duals -This puzzle is about finding the best horse duo for a race. -In order for the race to be interesting, we have to match the two closest opponents in terms of strength. -Instead of trying every possible combination, it is more efficient to sort the array first, and go trough it once. +You have a list of N horses, each with a strength value, represented by an integer. You need to find the minimum difference in strength between two horses out of all possible pairs of horses. In other words, you need to find the two horses with the closest strength values. + +For example, given the following list of horses with their corresponding strength values: + +5 8 9 14 21 + +The minimum difference in strength between two horses is 1, which is the difference between 5 and 8. + +The input for the problem consists of two lines. The first line contains an integer N, the number of horses. The second line contains N space-separated integers, representing the strength values of the N horses. + +The output should be a single integer, the minimum difference in strength between two horses. + +The solution to this problem involves sorting the list of horses in ascending order, and then computing the difference between adjacent horses in the sorted list. The minimum difference is the smallest of these computed differences. diff --git a/puzzles/ruby/mars-lander.rb b/puzzles/ruby/mars-lander.rb deleted file mode 100644 index 5b06421..0000000 --- a/puzzles/ruby/mars-lander.rb +++ /dev/null @@ -1,32 +0,0 @@ -STDOUT.sync = true # DO NOT REMOVE -# Auto-generated code below aims at helping you parse -# the standard input according to the problem statement. - -@surface_n = gets.to_i # the number of points used to draw the surface of Mars. -@surface_n.times do - # land_x: X coordinate of a surface point. (0 to 6999) - # land_y: Y coordinate of a surface point. By linking all the points together in a sequential fashion, you form the surface of Mars. - land_x, land_y = gets.split(" ").collect {|x| x.to_i} -end - -# game loop -loop do - # h_speed: the horizontal speed (in m/s), can be negative. - # v_speed: the vertical speed (in m/s), can be negative. - # fuel: the quantity of remaining fuel in liters. - # rotate: the rotation angle in degrees (-90 to 90). - # power: the thrust power (0 to 4). - x, y, h_speed, v_speed, fuel, rotate, power = gets.split(" ").collect {|x| x.to_i} - - # Write an action using puts - # To debug: STDERR.puts "Debug messages..." - - if v_speed <= -40 - puts "0 4" - else - puts "0 0" - end - - # 2 integers: rotate power. rotate is the desired rotation angle (should be 0 for level 1), power is the desired thrust power (0 to 4). - # puts "0 3" -end \ No newline at end of file diff --git a/puzzles/ruby/mayan-calc/README.md b/puzzles/ruby/mayan-calc/README.md new file mode 100644 index 0000000..feac0cf --- /dev/null +++ b/puzzles/ruby/mayan-calc/README.md @@ -0,0 +1,9 @@ +# Mayan Calculation + +Mayan Calculation is a coding challenge on the website Codingame that requires the implementation of a Mayan numeral system calculator. The challenge is rated medium difficulty and is designed to test a developer's ability to work with numbers and string manipulation. + +The Mayan numeral system is a base-20 system that was used by the Maya civilization. It consists of a combination of bars and dots, with each bar representing five units and each dot representing one unit. The challenge requires the implementation of a program that can perform basic arithmetic operations such as addition, subtraction, multiplication, and division using the Mayan numeral system. + +The input to the program is a string that represents the Mayan numeral. The program needs to parse the input string and convert it into an integer. The output of the program should also be a Mayan numeral string that represents the result of the arithmetic operation. + +The challenge involves parsing and manipulating strings to perform arithmetic operations. It also requires a good understanding of the Mayan numeral system and how to convert it into a decimal system that is used by most modern calculators. Overall, the Mayan Calculation challenge is a fun and challenging way to improve one's string manipulation and arithmetic skills. diff --git a/puzzles/ruby/mayan-calc.rb b/puzzles/ruby/mayan-calc/mayan-calc.rb similarity index 100% rename from puzzles/ruby/mayan-calc.rb rename to puzzles/ruby/mayan-calc/mayan-calc.rb diff --git a/puzzles/ruby/mime-type.rb b/puzzles/ruby/mime-type.rb deleted file mode 100644 index 9dd5b1f..0000000 --- a/puzzles/ruby/mime-type.rb +++ /dev/null @@ -1,40 +0,0 @@ -# For each of the Q filenames, display on a line the corresponding MIME type. -# If there is no corresponding type, then display UNKNOWN. - -hash = Hash.new -fileExtension = "" -mimeType = "" -dotIndex = 0 - -@pairCount = gets.to_i # Number of elements which make up the association table. -@filesCount = gets.to_i # Number Q of file names to be analyzed. - -@pairCount.times do - # ext: file extension - # mt: MIME type. - ext, mt = gets.split(" ") - ext = ext.downcase - hash.store(ext, mt) -end - -#Test -# hash.each do |ext, mt| -# STDERR.puts "#{ext} #{mt}" -# end - -@filesCount.times do - fileName = gets.chomp # One file name per line. - #STDERR.puts(fileName) - dotIndex = fileName.rindex('.') - #STDERR.puts(dotIndex) - if dotIndex == nil - puts "UNKNOWN" - elsif (dotIndex >= 0) && (dotIndex < (fileName.length - 1)) - fileExtension = fileName[dotIndex + 1 .. -1].downcase - #STDERR.puts(fileExtension) - mimeType = hash.fetch(fileExtension, 'UNKNOWN') - puts(mimeType) - else - puts "UNKNOWN" - end -end \ No newline at end of file diff --git a/puzzles/ruby/shadows-knight1/README.md b/puzzles/ruby/shadows-knight1/README.md deleted file mode 100644 index 3aa08f2..0000000 --- a/puzzles/ruby/shadows-knight1/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# Shadows of the Knight - Episode 1 - -We split the map each turn by going to the center of the area where the bomb is possibly located. -This method is called a binary search and allows us to optimize the number of jumps needed. diff --git a/puzzles/ruby/shadows-knight1/shadows-knight1.rb b/puzzles/ruby/shadows-knight1/shadows-knight1.rb deleted file mode 100644 index 27df4b1..0000000 --- a/puzzles/ruby/shadows-knight1/shadows-knight1.rb +++ /dev/null @@ -1,44 +0,0 @@ -STDOUT.sync = true # DO NOT REMOVE -width, height = gets.split.map(&:to_i) -n = gets.to_i -x, y = gets.split.map(&:to_i) # Starting x,y position of Batman - -minX = 0 -maxX = width - 1 -minY = 0 -maxY = height - 1 - -loop { - bomb_dir = gets.chomp - - case bomb_dir - when "U" - minX = maxX = x - maxY = y - 1 - when "R" - minX = x + 1 - minY = maxY = y - when "D" - minX = maxX = x - minY = y + 1 - when "L" - maxX = x - 1 - minY = maxY = y - when "UR" - minX = x + 1 - maxY = y - 1 - when "UL" - maxX = x - 1 - maxY = y - 1 - when "DR" - minX = x + 1 - minY = y + 1 - when "DL" - maxX = x - 1 - minY = y + 1 - end - - x = (minX + maxX) / 2 - y = (minY + maxY) / 2 - puts("#{x} #{y}") # the location of the next window Batman should jump to. -} diff --git a/puzzles/ruby/skynet-the-chasm.rb b/puzzles/ruby/skynet-the-chasm.rb deleted file mode 100644 index e6e8064..0000000 --- a/puzzles/ruby/skynet-the-chasm.rb +++ /dev/null @@ -1,33 +0,0 @@ -STDOUT.sync = true # DO NOT REMOVE - -@road = gets.to_i # the length of the road before the gap. -@gap = gets.to_i # the length of the gap. -@platform = gets.to_i # the length of the landing platform. - -jump = false; # true == the gap was jumped - -# game loop -loop do - speed = gets.to_i # the motorbike's speed. - coord_x = gets.to_i # the position on the road of the motorbike. - - if (speed < (@gap + 1)) && !jump - # Increase the speed in case it is not sufficient to jump the gap - puts "SPEED" - elsif (speed > (@gap + 1)) && !jump - # Decrease the speed in case it is over the gap's length - puts("SLOW") - elsif (coord_x == (@road - 1)) && !jump - # Jump the gap at the end of the road - puts("JUMP") - jump = true - elsif coord_x >= (@road + @gap) - # After the jump, slow down - puts("SLOW") - else - puts("WAIT") - end - - # Write an action using puts - # To debug: STDERR.puts "Debug messages..." -end \ No newline at end of file diff --git a/puzzles/ruby/temperatures-min.rb b/puzzles/ruby/temperatures-min.rb deleted file mode 100644 index 8db7601..0000000 --- a/puzzles/ruby/temperatures-min.rb +++ /dev/null @@ -1,7 +0,0 @@ -n=gets.to_i -m=5526 -i=gets.split.map(&:to_i) -for t in i -m=t if t.absm -end -puts n==0?0:m diff --git a/puzzles/ruby/temperatures.rb b/puzzles/ruby/temperatures.rb deleted file mode 100644 index f0af607..0000000 --- a/puzzles/ruby/temperatures.rb +++ /dev/null @@ -1,12 +0,0 @@ -n = gets.to_i # the number of temperatures to analyse -min = 5526 -inputs = gets.split.map(&:to_i) -for t in inputs - # t: a temperature expressed as an integer ranging from -273 to 5526 - if t.abs < min.abs || t.abs == min.abs && t > min - min = t - end -end - -puts n == 0 ? 0 : min - diff --git a/puzzles/ruby/there-is-no-spoon.rb b/puzzles/ruby/there-is-no-spoon.rb deleted file mode 100644 index cfc6195..0000000 --- a/puzzles/ruby/there-is-no-spoon.rb +++ /dev/null @@ -1,68 +0,0 @@ -# Three coordinates: a node, its right neighbor, its bottom neighbor -# puts "0 0 1 0 0 1" - -STDOUT.sync = true # DO NOT REMOVE - -EMPTY_NEIGHBOR = "-1 -1 " -ZERO = '0' -DOT = '.' - -@width = gets.to_i # the number of cells on the X axis -@height = gets.to_i # the number of cells on the Y axis -cells = Array.new(@height) {Array.new(@width, false)} -# Fill the array -STDERR.print("Cells: (#{@width} x #{@height}) \n") -@height.times do |i| - line = gets.chomp # width characters, each either 0 or . - @width.times do |j| - if line[j] == ZERO - cells[i][j] = true - end - STDERR.print(cells[i][j] ? ZERO : DOT) - end - STDERR.print("\n") -end - -# loop trough the array to compute every node -@height.times do |i| - @width.times do |j| - node = cells[i][j] - if node - print("#{j} #{i} ") - - # Search for the right neighbor - neighbor = false - neighborWidth = j + 1 - while (!neighbor) && (neighborWidth < @width) do - neighbor = cells[i][neighborWidth] - if neighbor - print("#{neighborWidth} #{i} ") - end # if - neighborWidth = neighborWidth + 1 - end # while - - if !neighbor - print(EMPTY_NEIGHBOR) - end # if - - # Search for the bottom neighbor - neighbor = false # reset - neighborHeight = i + 1 - while (!neighbor) && (neighborHeight < @height) do - neighbor = cells[neighborHeight][j] - if neighbor - print("#{j} #{neighborHeight} ") - end # if - neighborHeight = neighborHeight + 1 - end # while - - if !neighbor - print(EMPTY_NEIGHBOR) - end # if - - # New cell - print("\n") - - end # if - end # width loop -end # height loop \ No newline at end of file diff --git a/puzzles/swift/onboarding/README.md b/puzzles/swift/onboarding/README.md new file mode 100644 index 0000000..fcfe129 --- /dev/null +++ b/puzzles/swift/onboarding/README.md @@ -0,0 +1,63 @@ +Documentation for the onboarding.swift file in the codingame repository is as follows: + +## Description +This file contains the solution to the "Onboarding" puzzle on the Codingame platform, implemented in the Swift programming language. The puzzle requires the program to read the names of enemy spaceships and their distances from the player's spaceship, and then output the name of the closest enemy spaceship. + +## Functionality +The program contains a single function `main()`, which is the entry point for the program. It takes no arguments and returns no values. Within the `main()` function, the program reads input from the standard input using the `readLine()` function, and then uses string manipulation and conditional statements to determine the name of the closest enemy spaceship. Finally, the program outputs the name of the closest enemy using the `print()` function. + +## Input +The input for this puzzle consists of a series of lines, each containing the name of an enemy spaceship and its distance from the player's spaceship. The input is terminated by a line containing the word "END". The input format for each line is as follows: + +``` +{enemy_name} {distance} +``` + +Where `{enemy_name}` is a string containing the name of the enemy spaceship, and `{distance}` is an integer representing the distance of the enemy spaceship from the player's spaceship. + +## Output +The output for this puzzle consists of a single line containing the name of the closest enemy spaceship. The output format is as follows: + +``` +{enemy_name} +``` + +Where `{enemy_name}` is the name of the closest enemy spaceship. + +## Example +Input: +``` +Buzz 9999 +Bob 10000 +END +``` + +Output: +``` +Buzz +``` + +In this example, the enemy spaceship named "Buzz" is the closest to the player's spaceship, since it is only 9999 units away, whereas the enemy spaceship named "Bob" is 10000 units away. Therefore, the program outputs "Buzz". + +## Example Solution + +```swift +while true { + let enemy1 = readLine()! // name of enemy 1 + let dist1 = Int(readLine()!)! // distance to enemy 1 + let enemy2 = readLine()! // name of enemy 2 + let dist2 = Int(readLine()!)! // distance to enemy 2 + + if dist1 < dist2 { + print(enemy1) + } else { + print(enemy2) + } +} +``` + +The above code uses a `while true` loop to continuously read input from standard input (which is provided by CodinGame) and print the name of the closest enemy. + +In each iteration of the loop, we first read in the name and distance of the first enemy, and store them in the `enemy1` and `dist1` variables. We do the same for the second enemy, storing their name and distance in the `enemy2` and `dist2` variables. + +We then compare the distances of the two enemies, and print the name of the one that is closest. Note that the `readLine()` function returns an optional string, so we use the `!` operator to force unwrap the optionals and convert the distance to an `Int`. diff --git a/puzzles/swift/onboarding/onboarding.swift b/puzzles/swift/onboarding/onboarding.swift new file mode 100644 index 0000000..553e8b7 --- /dev/null +++ b/puzzles/swift/onboarding/onboarding.swift @@ -0,0 +1,21 @@ +import Glibc +import Foundation + +public struct StderrOutputStream: TextOutputStream { + public mutating func write(_ string: String) { fputs(string, stderr) } +} +public var errStream = StderrOutputStream() + +// game loop +while true { + let enemy1 = readLine()! // name of enemy 1 + let distance1 = Int(readLine()!)! // distance to enemy 1 + let enemy2 = readLine()! // name of enemy 2 + let distance2 = Int(readLine()!)! // distance to enemy 2 + + if distance1 < distance2 { + print(enemy1) + } else { + print(enemy2) + } +} diff --git a/puzzles/swift/power-of-thor1/README.md b/puzzles/swift/power-of-thor1/README.md new file mode 100644 index 0000000..a78b93d --- /dev/null +++ b/puzzles/swift/power-of-thor1/README.md @@ -0,0 +1,56 @@ +# Power of Thor + +This is a solution to the Power of Thor puzzle on [Codingame](https://www.codingame.com/training/easy/power-of-thor-episode-1) implemented in Swift. + +## Problem Description + +In this puzzle, Thor is stranded on a rectangular grid and needs to reach a lightning bolt that is located at a specific position on the grid. The position of Thor and the lightning bolt are given as input to the program. Thor can move in four directions: North, South, East, and West. For each move, the program needs to output the direction in which Thor should move to get closer to the lightning bolt. + +## Solution Overview + +The solution uses a loop to iterate over the possible moves of Thor. At each iteration, the program calculates the direction in which Thor should move based on his current position and the position of the lightning bolt. The program then outputs the direction in which Thor should move and updates his position accordingly. + +## Code Explanation + +The code starts by reading the input values from standard input using the `readLine()` function. The input values are then parsed into variables. + +```swift +let inputs = (readLine()!).split(separator: " ").map(String.init) +let lightX = Int(inputs[0])! +let lightY = Int(inputs[1])! +let initialTx = Int(inputs[2])! +let initialTy = Int(inputs[3])! +``` + +The program then enters a loop that runs until Thor reaches the lightning bolt. At each iteration of the loop, the program calculates the direction in which Thor should move based on his current position and the position of the lightning bolt. + +```swift +while true { + let remainingTurns = Int(readLine()!)! + var direction = "" + + if initialTy > lightY { + direction += "N" + initialTy -= 1 + } else if initialTy < lightY { + direction += "S" + initialTy += 1 + } + + if initialTx > lightX { + direction += "W" + initialTx -= 1 + } else if initialTx < lightX { + direction += "E" + initialTx += 1 + } + + print(direction) +} +``` + +The loop also reads in the remaining turns from standard input and assigns it to the `remainingTurns` variable. Finally, the program outputs the direction in which Thor should move and updates his position. + +## Conclusion + +This solution demonstrates how to solve the Power of Thor puzzle on Codingame using Swift. The program reads in input values from standard input, enters a loop to calculate the direction in which Thor should move, and outputs the direction in which he should move. This solution can be used as a starting point to solve other puzzles on Codingame or similar platforms. diff --git a/puzzles/swift/power-of-thor1/power-of-thor1.swift b/puzzles/swift/power-of-thor1/power-of-thor1.swift new file mode 100644 index 0000000..93f4ff8 --- /dev/null +++ b/puzzles/swift/power-of-thor1/power-of-thor1.swift @@ -0,0 +1,43 @@ +import Glibc +import Foundation + +public struct StderrOutputStream: TextOutputStream { + public mutating func write(_ string: String) { fputs(string, stderr) } +} +public var errStream = StderrOutputStream() + +/** + * Auto-generated code below aims at helping you parse + * the standard input according to the problem statement. + * --- + * Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders. + **/ + +let inputs = (readLine()!).split(separator: " ").map(String.init) +let lightX = Int(inputs[0])! // the X position of the light of power +let lightY = Int(inputs[1])! // the Y position of the light of power +var thorX = Int(inputs[2])! // Thor's starting X position +var thorY = Int(inputs[3])! // Thor's starting Y position + +// game loop +while true { + var direction = "" + if thorY > lightY { + direction += "N" + thorY -= 1 + } else if thorY < lightY { + direction += "S" + thorY += 1 + } + + if thorX > lightX { + direction += "W" + thorX -= 1 + } else if thorX < lightX { + direction += "E" + thorX += 1 + } + + // A single line providing the move to be made: N NE E SE S SW W or NW + print(direction) +} diff --git a/puzzles/swift/temperatures/README.md b/puzzles/swift/temperatures/README.md new file mode 100644 index 0000000..be21bac --- /dev/null +++ b/puzzles/swift/temperatures/README.md @@ -0,0 +1,30 @@ + + +# Temperatures + +The `Temperatures.swift` script is a solution to the "Temperatures" puzzle on the CodinGame platform. The goal of the puzzle is to find the temperature closest to zero among a list of temperatures. If there are two temperatures equally close to zero, the positive temperature should be considered closest to zero. + +## Usage + +The script can be run on the command line by passing in a list of temperatures separated by spaces: + +``` +$ swift temperatures.swift "1 -2 3 4 -5" +``` + +The script will then output the temperature closest to zero. If there are no temperatures provided, or if they are not valid integers, the script will output 0. + +## Implementation + +The script reads in the list of temperatures from the command line arguments and splits them into an array of integers. It then loops through the array, keeping track of the temperature closest to zero seen so far. If a temperature is found that is closer to zero than the current closest temperature, it becomes the new closest temperature. If the temperature is equally close to zero as the current closest temperature, and it is positive, it becomes the new closest temperature. + +After looping through all the temperatures, the script outputs the closest temperature found. + +## Example + +``` +$ swift temperatures.swift "-10 20 -30 40 50" +-10 +``` + +In this example, the temperatures -10, 20, -30, 40, and 50 are passed in as command line arguments. The script loops through the temperatures and finds that -10 is the closest temperature to zero, so it outputs -10. \ No newline at end of file diff --git a/puzzles/swift/temperatures/temperatures.swift b/puzzles/swift/temperatures/temperatures.swift new file mode 100644 index 0000000..cba558a --- /dev/null +++ b/puzzles/swift/temperatures/temperatures.swift @@ -0,0 +1,23 @@ +import Glibc +import Foundation + +public struct StderrOutputStream: TextOutputStream { + public mutating func write(_ string: String) { fputs(string, stderr) } +} +public var errStream = StderrOutputStream() + +let n = Int(readLine()!)! // the number of temperatures to analyse +var minTemperature = 5526 + +for i in ((readLine()!).split(separator: " ").map(String.init)) { + let temperature = Int(i)! + if (abs(temperature) < abs(minTemperature)) || (abs(temperature) == abs(minTemperature) && temperature > minTemperature) { + minTemperature = temperature + } +} + +if n == 0 { + print(0) +} else { + print(minTemperature) +} \ No newline at end of file diff --git a/puzzles/swift/the-descent/README.md b/puzzles/swift/the-descent/README.md new file mode 100644 index 0000000..8c7c828 --- /dev/null +++ b/puzzles/swift/the-descent/README.md @@ -0,0 +1,43 @@ +# The Descent Puzzle + +## Description + +In "The Descent" puzzle, you are given an array of integers representing the heights of a set of mountains. Your goal is to determine which mountain is the tallest and shoot it down by printing its index to the console. + +## How to Run + +To run "The Descent" puzzle, you need to have Swift installed on your machine. Once you have installed Swift, you can follow these steps: + +1. Download the `the-descent.swift` file. +2. Open your terminal and navigate to the directory where the file is located. +3. Run the following command to compile the code: + +``` +swiftc the-descent.swift +``` + +4. Run the compiled executable file by entering the following command: + +``` +./the-descent +``` + +5. The program will execute and output the index of the tallest mountain to the console. + +## Approach + +The approach for solving "The Descent" puzzle is simple. You iterate over the array of mountains and keep track of the tallest mountain seen so far. Once you have iterated over all the mountains, you print the index of the tallest mountain to the console. + +## Example + +Let's consider the following array of mountains: + +``` +[9, 8, 6, 7, 3, 5, 4, 1, 2] +``` + +Here, the tallest mountain is the one with a height of `9`. The index of this mountain in the array is `0`. Therefore, the output of the program should be: + +``` +0 +``` diff --git a/puzzles/swift/the-descent/the-descent.swift b/puzzles/swift/the-descent/the-descent.swift new file mode 100644 index 0000000..0c28789 --- /dev/null +++ b/puzzles/swift/the-descent/the-descent.swift @@ -0,0 +1,30 @@ +import Glibc +import Foundation + +public struct StderrOutputStream: TextOutputStream { + public mutating func write(_ string: String) { fputs(string, stderr) } +} +public var errStream = StderrOutputStream() + +/** + * The while loop represents the game. + * Each iteration represents a turn of the game + * where you are given inputs (the heights of the mountains) + * and where you have to print an output (the index of the mountain to fire on) + * The inputs you are given are automatically updated according to your last actions. + **/ + + +// game loop +while true { + var indexToFire = 0 + var maxMountainHeight = -1 + for index in 0...7 { + let mountainHeight = Int(readLine()!)! + if mountainHeight > maxMountainHeight { + maxMountainHeight = mountainHeight + indexToFire = index + } + } + print(indexToFire) +} diff --git a/puzzles/ts/ascii-art/README.md b/puzzles/ts/ascii-art/README.md new file mode 100644 index 0000000..df403f5 --- /dev/null +++ b/puzzles/ts/ascii-art/README.md @@ -0,0 +1,9 @@ +# ASCII Art + +"ASCII Art" is a beginner-level coding challenge available on the CodinGame platform. In this challenge, the player is given a string of characters and a font size, and is asked to generate an ASCII art representation of the string using ASCII characters. + +The ASCII art output should be a sequence of lines, each line representing a row of the output. Each row should contain the ASCII characters that correspond to the input string, with each character expanded to the specified font size. If a character in the input string is not a letter or cannot be represented in ASCII art, it should be replaced with a question mark. + +The challenge consists of writing a program that takes as input the string of characters, the font size, and the ASCII art characters to be used for each letter, and outputs the corresponding ASCII art representation. + +The challenge is designed to help players learn and practice programming skills such as string manipulation, input/output handling, and ASCII art rendering. It is a fun and engaging way to improve programming skills while solving a challenging and entertaining puzzle. diff --git a/puzzles/ts/defibrillators/README.md b/puzzles/ts/defibrillators/README.md new file mode 100644 index 0000000..4be5b5c --- /dev/null +++ b/puzzles/ts/defibrillators/README.md @@ -0,0 +1,9 @@ +# Defibrillators + +"Defibrillators" is a beginner-level coding challenge available on the CodinGame platform. In this challenge, the player is given the location of a person and the location of several defibrillators in a city, and is asked to determine the closest defibrillator to the person. + +The input contains the location of the person as longitude and latitude, as well as the longitude and latitude of each defibrillator and its name. The player is asked to calculate the distance between the person and each defibrillator using the Haversine formula, which takes into account the curvature of the Earth's surface. + +The challenge consists of writing a program that takes as input the location of the person and the locations of the defibrillators, and outputs the name of the closest defibrillator to the person. + +The challenge is designed to help players learn and practice programming skills such as geographic coordinate manipulation, distance calculation, and data structures. It is a fun and engaging way to improve programming skills while solving a challenging and entertaining puzzle. diff --git a/puzzles/ts/horse-racing-duals/README.md b/puzzles/ts/horse-racing-duals/README.md new file mode 100644 index 0000000..eba9bce --- /dev/null +++ b/puzzles/ts/horse-racing-duals/README.md @@ -0,0 +1,15 @@ +# Horse Racing Duals + +You have a list of N horses, each with a strength value, represented by an integer. You need to find the minimum difference in strength between two horses out of all possible pairs of horses. In other words, you need to find the two horses with the closest strength values. + +For example, given the following list of horses with their corresponding strength values: + +5 8 9 14 21 + +The minimum difference in strength between two horses is 1, which is the difference between 5 and 8. + +The input for the problem consists of two lines. The first line contains an integer N, the number of horses. The second line contains N space-separated integers, representing the strength values of the N horses. + +The output should be a single integer, the minimum difference in strength between two horses. + +The solution to this problem involves sorting the list of horses in ascending order, and then computing the difference between adjacent horses in the sorted list. The minimum difference is the smallest of these computed differences. diff --git a/puzzles/ts/mars-lander1/README.md b/puzzles/ts/mars-lander1/README.md new file mode 100644 index 0000000..33461fc --- /dev/null +++ b/puzzles/ts/mars-lander1/README.md @@ -0,0 +1,9 @@ +# Mars Lander - Episode 1 + +"Mars Lander - Episode 1" is a beginner-level coding challenge available on the CodinGame platform. In this challenge, the player takes on the role of a spaceship pilot trying to safely land a spacecraft on the surface of Mars. + +The player is given the coordinates and the speed of the spacecraft, as well as the angle of its trajectory and the thrust power to be used to adjust its speed and direction. The objective is to write a program that computes the optimal thrust power and angle for the spacecraft to land safely on the surface of Mars without crashing or running out of fuel. + +The challenge consists of writing a program that takes as input the current state of the spacecraft and outputs the thrust power and angle that should be used to adjust its trajectory and speed for a safe landing. The program needs to take into account the gravitational pull of Mars, the speed of the spacecraft, the remaining fuel, and the altitude of the spacecraft above the surface of Mars. + +The challenge is designed to help players learn and practice programming skills such as physics calculations, conditional statements, and input/output handling. It is a fun and engaging way to improve programming skills while solving a challenging and entertaining puzzle. diff --git a/puzzles/ts/mime-type/README.md b/puzzles/ts/mime-type/README.md index 0a518b9..87614d3 100644 --- a/puzzles/ts/mime-type/README.md +++ b/puzzles/ts/mime-type/README.md @@ -1,6 +1,9 @@ # MIME Type -In this puzzle, we have to associate file names with MIME types. -For example, `animated.gif` is of type `image/gif`. -The idea is to extract the extension from the file name, `.gif`, in this case and map it to its type. -If the file name has no extension or the extension is unknown, we simply print `UNKNOWN`. +"MIME Type" is a beginner-level coding challenge available on the CodinGame platform. In this challenge, the player is given a list of file names and their corresponding MIME types, and is asked to determine the MIME type of a given file name. + +A MIME type is a type of Internet standard that is used to indicate the type of data that a file contains. It is typically represented as a two-part identifier, with the first part indicating the type of data (such as "image" or "text") and the second part indicating the specific format or subtype (such as "jpeg" or "html"). + +The challenge consists of writing a program that takes as input a list of file names and their corresponding MIME types, as well as the name of a file, and outputs the corresponding MIME type for that file. If the file type is not known, the program should output "UNKNOWN". + +The challenge is designed to help players learn and practice programming skills such as string manipulation, conditional statements, and data structures. It is a fun and engaging way to improve programming skills while solving a challenging and entertaining puzzle. diff --git a/puzzles/ts/power-of-thor1/README.md b/puzzles/ts/power-of-thor1/README.md new file mode 100644 index 0000000..999c080 --- /dev/null +++ b/puzzles/ts/power-of-thor1/README.md @@ -0,0 +1,11 @@ +# Power of Thor - Episode 1 + +"Power of Thor - Episode 1" is a beginner-level coding challenge available on the CodinGame platform. In this challenge, the player takes on the role of Thor, the god of thunder, who is stranded on a rectangular grid of cells and needs to reach a lightning bolt located in a specific cell. + +The player has access to four pieces of information: Thor's current position (x,y), the position of the lightning bolt (lightX, lightY), the number of remaining turns (turns), and Thor's remaining energy level. + +The objective is to write a program that outputs a sequence of movements for Thor to reach the lightning bolt while avoiding obstacles such as walls or gaps in the grid. Thor can move in four directions: North (up), South (down), East (right), and West (left). Each movement costs one unit of energy, and Thor starts with a certain amount of energy that can be depleted if he moves too much. + +The challenge consists of writing a program that takes as input the current position of Thor, the position of the lightning bolt, and the remaining number of turns and energy level, and outputs the direction in which Thor should move to get closer to the lightning bolt. + +The challenge is designed to help players learn and practice programming skills such as parsing input, making decisions based on the current state of the game, and generating output. It is a fun and engaging way to improve programming skills while solving a challenging and entertaining puzzle. diff --git a/puzzles/ts/temperatures/README.md b/puzzles/ts/temperatures/README.md new file mode 100644 index 0000000..6ba87a7 --- /dev/null +++ b/puzzles/ts/temperatures/README.md @@ -0,0 +1,9 @@ +# Temperatures + +"Temperatures" is a beginner-level coding challenge available on the CodinGame platform. In this challenge, the player is given a list of integers representing temperature readings in Celsius degrees for a city during a certain period of time. + +The objective is to write a program that finds and outputs the closest temperature to zero. If there are two temperatures that are equally close to zero, the program should output the positive one. + +The challenge consists of writing a program that takes as input the list of temperature readings and outputs the temperature closest to zero. If the list is empty, the program should output 0. + +The challenge is designed to help players learn and practice programming skills such as array manipulation, looping, and conditional statements. It is a fun and engaging way to improve programming skills while solving a challenging and entertaining puzzle. diff --git a/puzzles/ts/the-descent/README.md b/puzzles/ts/the-descent/README.md new file mode 100644 index 0000000..2bbcf54 --- /dev/null +++ b/puzzles/ts/the-descent/README.md @@ -0,0 +1,11 @@ +# The Descent + +"The Descent" is a beginner-level coding challenge available on the CodinGame platform. In this challenge, the player is a fighter pilot trying to shoot down mountains that are blocking their way. + +The player is given an array of integers representing the heights of mountains in the player's path. The player's goal is to shoot down the highest mountain, which is represented by the maximum value in the array. + +The player has a limited amount of ammunition and can only fire at one mountain at a time. Each shot takes one unit of ammunition, and the mountain that is hit is destroyed, reducing its height to zero. + +The challenge consists of writing a program that takes as input the heights of the mountains and outputs the index of the highest mountain that the player should shoot at to destroy it. + +The challenge is designed to help players learn and practice programming skills such as array manipulation, searching for maximum values, and generating output. It is a fun and engaging way to improve programming skills while solving a challenging and entertaining puzzle. diff --git a/puzzles/ts/unary/README.md b/puzzles/ts/unary/README.md index c797c47..637cace 100644 --- a/puzzles/ts/unary/README.md +++ b/puzzles/ts/unary/README.md @@ -1,5 +1,7 @@ # Unary -The goal here is to encode a message using Chuck Norris' method. -First, we convert each letter into its binary ASCII representation on 7 bits. -Second, we convert each binary digit into a sequence of zeros and spaces. +"Unary" is a beginner-level coding challenge available on the CodinGame platform. In this challenge, the player is given a string of characters representing a unary number, and is asked to convert it to decimal notation. + +A unary number is a number system in which each integer is represented by a corresponding number of symbols or characters. In this challenge, the unary number is represented by a string of the same character, and the value of the number is equal to the length of the string. + +The challenge consists of writing a program that takes as input a string of characters representing a unary number and outputs the corresponding decimal notation.