Add solution for "Day 9: Disk Fragmenter", part 1
This commit is contained in:
parent
e1cd0867cb
commit
54204766ec
|
@ -0,0 +1,31 @@
|
|||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2025 Stefan Müller
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify it under
|
||||
// the terms of the GNU General Public License as published by the Free Software
|
||||
// Foundation, either version 3 of the License, or (at your option) any later
|
||||
// version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along with
|
||||
// this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <aoc/Solver.hpp>
|
||||
|
||||
class DiskFragmenter
|
||||
: public Solver
|
||||
{
|
||||
public:
|
||||
virtual const std::string getPuzzleName() const override;
|
||||
virtual const std::string getInputFileName() const override;
|
||||
virtual void processDataLine(const std::string& line) override;
|
||||
virtual void finish() override;
|
||||
private:
|
||||
int getDigit(const std::string& line, const size_t index) const;
|
||||
long long int calcChecksumPart(const size_t idNumber, const int nBlocks, size_t& position) const;
|
||||
};
|
|
@ -0,0 +1,103 @@
|
|||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2025 Stefan Müller
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify it under
|
||||
// the terms of the GNU General Public License as published by the Free Software
|
||||
// Foundation, either version 3 of the License, or (at your option) any later
|
||||
// version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along with
|
||||
// this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include <aoc/DiskFragmenter.hpp>
|
||||
|
||||
const std::string DiskFragmenter::getPuzzleName() const
|
||||
{
|
||||
return "Day 9: Disk Fragmenter";
|
||||
}
|
||||
|
||||
const std::string DiskFragmenter::getInputFileName() const
|
||||
{
|
||||
return "disk_fragmenter.txt";
|
||||
}
|
||||
|
||||
void DiskFragmenter::processDataLine(const std::string& line)
|
||||
{
|
||||
//size_t maxIdNumber{ line.size() / 2 };
|
||||
// Index of the first unprocessed digit in the disk map.
|
||||
size_t front{ 0 };
|
||||
// ID number of the file 'front' refers to (when 'isFile == true'). Equivalent to 'front / 2', but calculated
|
||||
// incrementally.
|
||||
size_t frontIdNumber{ 0 };
|
||||
// Index of the last unprocessed digit in the disk map.
|
||||
size_t back{ line.size() - 1 };
|
||||
// Number of remaining fragmented blocks in the file that 'back' refers to (when 'isFile == true').
|
||||
int nRemainingBackBlocks{ getDigit(line, back) };
|
||||
// ID number of the file 'back' refers to (when 'isFile == true'). Equivalent to 'back / 2', but calculated
|
||||
// incrementally.
|
||||
size_t backIdNumber{ line.size() / 2 };
|
||||
// Current block position.
|
||||
size_t position{ 0 };
|
||||
|
||||
bool isFile{ true };
|
||||
|
||||
while (front < back)
|
||||
{
|
||||
if (isFile)
|
||||
{
|
||||
// Adds the checksum for the file at 'front'.
|
||||
int nFileBlocks = getDigit(line, front);
|
||||
part1 += calcChecksumPart(frontIdNumber, nFileBlocks, position);
|
||||
frontIdNumber++;
|
||||
}
|
||||
else
|
||||
{
|
||||
int nFreeBlocks = getDigit(line, front);
|
||||
while (nFreeBlocks > 0)
|
||||
{
|
||||
if (nFreeBlocks >= nRemainingBackBlocks)
|
||||
{
|
||||
// Adds the checksum for all the blocks of the file at 'back'.
|
||||
part1 += calcChecksumPart(backIdNumber, nRemainingBackBlocks, position);
|
||||
backIdNumber--;
|
||||
|
||||
nFreeBlocks -= nRemainingBackBlocks;
|
||||
back -= 2;
|
||||
nRemainingBackBlocks = getDigit(line, back);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Adds the checksum for the blocks of the file at 'back' that fit into the empty blocks at 'front'.
|
||||
part1 += calcChecksumPart(backIdNumber, nFreeBlocks, position);
|
||||
|
||||
nRemainingBackBlocks -= nFreeBlocks;
|
||||
nFreeBlocks = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
front++;
|
||||
isFile = !isFile;
|
||||
}
|
||||
|
||||
// Adds the checksum for the remaining blocks of the file at 'back'.
|
||||
part1 += calcChecksumPart(backIdNumber, nRemainingBackBlocks, position);
|
||||
}
|
||||
|
||||
void DiskFragmenter::finish()
|
||||
{
|
||||
}
|
||||
|
||||
int DiskFragmenter::getDigit(const std::string& line, const size_t index) const
|
||||
{
|
||||
return line[index] - '0';
|
||||
}
|
||||
|
||||
long long int DiskFragmenter::calcChecksumPart(const size_t idNumber, const int nBlocks, size_t& position) const
|
||||
{
|
||||
position += nBlocks;
|
||||
return idNumber * ((nBlocks * (2 * position - nBlocks - 1)) / 2);
|
||||
}
|
|
@ -29,6 +29,7 @@
|
|||
#include <aoc/GuardGallivant.hpp>
|
||||
#include <aoc/BridgeRepair.hpp>
|
||||
#include <aoc/ResonantCollinearity.hpp>
|
||||
#include <aoc/DiskFragmenter.hpp>
|
||||
#include <aoc/HoofIt.hpp>
|
||||
#include <aoc/PlutonianPebbles.hpp>
|
||||
#include <aoc/LanParty.hpp>
|
||||
|
@ -57,6 +58,7 @@ void Program::runSolvers()
|
|||
runSolver<GuardGallivant>(solverEngine);
|
||||
runSolver<BridgeRepair>(solverEngine);
|
||||
runSolver<ResonantCollinearity>(solverEngine);
|
||||
runSolver<DiskFragmenter>(solverEngine);
|
||||
runSolver<HoofIt>(solverEngine);
|
||||
runSolver<PlutonianPebbles>(solverEngine);
|
||||
runSolver<LanParty>(solverEngine);
|
||||
|
|
|
@ -135,6 +135,19 @@ TEST_CASE("[ResonantCollinearityTests]")
|
|||
}
|
||||
}
|
||||
|
||||
TEST_CASE("[DiskFragmenterTests]")
|
||||
{
|
||||
TestContext test;
|
||||
SECTION("FullData")
|
||||
{
|
||||
test.run(std::make_unique<DiskFragmenter>(), 6401092019345, 0, test.getInputPaths());
|
||||
}
|
||||
SECTION("ExampleData")
|
||||
{
|
||||
test.run(std::make_unique<DiskFragmenter>(), 1928, 0, test.getExampleInputPaths());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("[HoofItTests]")
|
||||
{
|
||||
TestContext test;
|
||||
|
|
Loading…
Reference in New Issue