Add solutions for "Day 1: Historian Hysteria"
This commit is contained in:
parent
5f08565799
commit
e30905c3ab
|
@ -129,13 +129,13 @@
|
|||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="AdventOfCode2024.cpp" />
|
||||
<ClCompile Include="Demo.cpp" />
|
||||
<ClCompile Include="HistorianHysteria.cpp" />
|
||||
<ClCompile Include="Program.cpp" />
|
||||
<ClCompile Include="Solver.cpp" />
|
||||
<ClCompile Include="SolverEngine.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Demo.h" />
|
||||
<ClInclude Include="HistorianHysteria.h" />
|
||||
<ClInclude Include="Program.h" />
|
||||
<ClInclude Include="Solver.h" />
|
||||
<ClInclude Include="SolverEngine.h" />
|
||||
|
|
|
@ -18,9 +18,6 @@
|
|||
<ClCompile Include="AdventOfCode2024.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Demo.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Solver.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
|
@ -30,6 +27,9 @@
|
|||
<ClCompile Include="Program.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="HistorianHysteria.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="SolverEngine.h">
|
||||
|
@ -38,10 +38,10 @@
|
|||
<ClInclude Include="Solver.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Demo.h">
|
||||
<ClInclude Include="Program.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Program.h">
|
||||
<ClInclude Include="HistorianHysteria.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2024 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 "Demo.h"
|
||||
|
||||
std::string Demo::getPuzzleName() const
|
||||
{
|
||||
return "Demo";
|
||||
}
|
||||
|
||||
std::string Demo::getInputFileName() const
|
||||
{
|
||||
return "demo.txt";
|
||||
}
|
||||
|
||||
void Demo::processDataLine(const std::string& line)
|
||||
{
|
||||
part1 += std::stoi(line);
|
||||
if (part2 == 0)
|
||||
{
|
||||
part2 = std::stoi(line);
|
||||
}
|
||||
else
|
||||
{
|
||||
part2 *= std::stoi(line);
|
||||
}
|
||||
}
|
||||
|
||||
void Demo::finish()
|
||||
{
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2024 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 <iostream>
|
||||
|
||||
#include "HistorianHysteria.h"
|
||||
|
||||
std::string HistorianHysteria::getPuzzleName() const
|
||||
{
|
||||
return "Day 1: Historian Hysteria";
|
||||
}
|
||||
|
||||
std::string HistorianHysteria::getInputFileName() const
|
||||
{
|
||||
return "historian_hysteria.txt";
|
||||
}
|
||||
|
||||
void HistorianHysteria::processDataLine(const std::string& line)
|
||||
{
|
||||
auto pos = line.find(" ");
|
||||
left.insert(std::stoi(line.substr(0, pos)));
|
||||
right.insert(std::stoi(line.substr(pos + 3)));
|
||||
}
|
||||
|
||||
void HistorianHysteria::finish()
|
||||
{
|
||||
int prev{ 0 };
|
||||
auto nSame{ 0 };
|
||||
auto leftIterator = left.begin();
|
||||
auto rightIterator = right.begin();
|
||||
auto rightSameIterator = right.begin();
|
||||
|
||||
while (leftIterator != left.end())
|
||||
{
|
||||
part1 += abs(*leftIterator - *rightIterator);
|
||||
|
||||
if (prev != *leftIterator)
|
||||
{
|
||||
nSame = 0;
|
||||
// Skips over numbers in the right list that are smaller than the current left number.
|
||||
while (rightSameIterator != right.end() && *rightSameIterator < *leftIterator)
|
||||
{
|
||||
rightSameIterator++;
|
||||
}
|
||||
// Counts the occurrences of the current left number in the right list.
|
||||
while (rightSameIterator != right.end() && *rightSameIterator == *leftIterator)
|
||||
{
|
||||
rightSameIterator++;
|
||||
nSame++;
|
||||
}
|
||||
prev = *leftIterator;
|
||||
}
|
||||
part2 += *leftIterator * nSame;
|
||||
|
||||
leftIterator++;
|
||||
rightIterator++;
|
||||
}
|
||||
}
|
|
@ -15,9 +15,11 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <set>
|
||||
|
||||
#include "Solver.h"
|
||||
|
||||
class Demo :
|
||||
class HistorianHysteria :
|
||||
public Solver
|
||||
{
|
||||
public:
|
||||
|
@ -25,4 +27,7 @@ class Demo :
|
|||
virtual std::string getInputFileName() const override;
|
||||
virtual void processDataLine(const std::string& line) override;
|
||||
virtual void finish() override;
|
||||
private:
|
||||
std::multiset<int> left;
|
||||
std::multiset<int> right;
|
||||
};
|
|
@ -16,10 +16,11 @@
|
|||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include "Demo.h"
|
||||
#include "Program.h"
|
||||
#include "SolverEngine.h"
|
||||
|
||||
#include "HistorianHysteria.h"
|
||||
|
||||
void Program::run()
|
||||
{
|
||||
std::cout << "### Advent of Code 2024 ###\n";
|
||||
|
@ -29,7 +30,7 @@ void Program::run()
|
|||
void Program::runSolvers()
|
||||
{
|
||||
SolverEngine solverEngine{ getInputPaths() };
|
||||
solverEngine.run(*std::make_unique<Demo>());
|
||||
solverEngine.run(*std::make_unique<HistorianHysteria>());
|
||||
}
|
||||
|
||||
std::vector<std::string> Program::getInputPaths() const
|
||||
|
|
|
@ -18,35 +18,36 @@
|
|||
|
||||
#include "Part1TestContext.h"
|
||||
#include "Part2TestContext.h"
|
||||
#include "../Demo.h"
|
||||
#include "../Solver.h"
|
||||
#include "../SolverEngine.h"
|
||||
|
||||
#include "../HistorianHysteria.h"
|
||||
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
|
||||
namespace UnitTests
|
||||
{
|
||||
TEST_CLASS(DemoTests)
|
||||
TEST_CLASS(HistorianHysteriaTests)
|
||||
{
|
||||
public:
|
||||
TEST_METHOD(FullData1)
|
||||
{
|
||||
runTest(*std::make_unique<Demo>(), 66, part1TestContext_);
|
||||
runTest(*std::make_unique<HistorianHysteria>(), 2176849, part1TestContext_);
|
||||
}
|
||||
|
||||
TEST_METHOD(FullData2)
|
||||
{
|
||||
runTest(*std::make_unique<Demo>(), 39916800, part2TestContext_);
|
||||
runTest(*std::make_unique<HistorianHysteria>(), 23384288, part2TestContext_);
|
||||
}
|
||||
|
||||
TEST_METHOD(ExampleData1)
|
||||
{
|
||||
runTest(*std::make_unique<Demo>(), 10, part1ExampleTestContext_);
|
||||
runTest(*std::make_unique<HistorianHysteria>(), 11, part1ExampleTestContext_);
|
||||
}
|
||||
|
||||
TEST_METHOD(ExampleData2)
|
||||
{
|
||||
runTest(*std::make_unique<Demo>(), 24, part2ExampleTestContext_);
|
||||
runTest(*std::make_unique<HistorianHysteria>(), 31, part2ExampleTestContext_);
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
|
@ -102,7 +102,7 @@
|
|||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;..\$(Platform)\$(Configuration);D:\Projects\VS\AdventOfCode2024Full\code\AdventOfCode2024\x64\Debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>Demo.obj;Solver.obj;SolverEngine.obj;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>Solver.obj;SolverEngine.obj;HistorianHysteria.obj;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
|
|
Loading…
Reference in New Issue