Skip to content

Commit

Permalink
12-2023 Part 2 + Optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent-seguin committed Dec 18, 2023
1 parent 7c1bfdf commit 5ba3bdd
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 86 deletions.
111 changes: 62 additions & 49 deletions 2023/Day12/Solution.cs
Original file line number Diff line number Diff line change
@@ -1,93 +1,106 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using AngleSharp.Media.Dom;

namespace AdventOfCode.Y2023.Day12;

[ProblemName("Hot Springs")]
class Solution : Solver
{
private readonly Dictionary<(long, long, long), long> dp = new();

public object PartOne(string input)
{
var lines = ParseLines(input);
var answer = 0;

foreach (var line in lines)
long answer = 0;
foreach (var line in ParseLines(input))
{
var splits = line.Split(" ", StringSplitOptions.RemoveEmptyEntries);
var dots = splits[0];
var numbers = splits[1];

var blocks = numbers.Split(",")
.Select(int.Parse);

var score = Resolve(dots, blocks, 0);
.Select(int.Parse)
.ToList();

dp.Clear();

var score = Resolve(dots, blocks, 0, 0, 0);
answer += score;
}

return answer;
}

public object PartTwo(string input)
{
var lines = ParseLines(input);
return 0;
long answer = 0;
foreach (var line in ParseLines(input))
{
var splits = line.Split(" ", StringSplitOptions.RemoveEmptyEntries);
var dots = string.Join("?", Enumerable.Repeat(splits[0], 5));
var numbers = string.Join(",", Enumerable.Repeat(splits[1], 5));
var blocks = numbers.Split(",")
.Select(int.Parse)
.ToList();

dp.Clear();

var score = Resolve(dots, blocks, 0, 0, 0);
answer += score;
}

return answer;
}

private static string[] ParseLines(string input) =>
input.Split("\n", StringSplitOptions.RemoveEmptyEntries);
private static int Resolve(string dots, IEnumerable<int> blocks, int index)

private long Resolve(string dots, IReadOnlyList<int> blocks, int index, int blockIndex, long current)
{
if (index == dots.Length)
{
return IsValid(dots, blocks) ? 1 : 0;
}
var key = (index, blockIndex, current);

if (dots[index] == '?')
if (dp.TryGetValue(key, out long value))
{
return Resolve($"{dots[..index]}#{dots[(index+1)..]}", blocks, index+1) +
Resolve($"{dots[..index]}.{dots[(index+1)..]}", blocks, index+1);
return value;
}

return Resolve(dots, blocks, index+1);
}

private static bool IsValid(string dots, IEnumerable<int> blocks)
{
var current = 0;
List<int> seen = [];

foreach (var dot in dots)
if (index == dots.Length)
{
if (dot == '.')
{
if (current > 0)
{
seen.Add(current);
}

current = 0;
}
else if (dot == '#')
if (blockIndex == blocks.Count && current == 0)
{
current++;
return 1;
}
else

if (blockIndex == blocks.Count - 1 && blocks[(int)blockIndex] == current)
{
throw new Exception("Invalid dot");
return 1;
}

return 0;
}

if (current > 0)
long answer = 0;

foreach (var c in new[] { '.', '#' })
{
seen.Add(current);
if (dots[(int)index] == c || dots[(int)index] == '?')
{
if (c == '.' && current == 0)
{
answer += Resolve(dots, blocks, index + 1, blockIndex, 0);
}
else if (c == '.' && current > 0 && blockIndex < blocks.Count() && blocks[(int)blockIndex] == current)
{
answer += Resolve(dots, blocks, index + 1, blockIndex + 1, 0);
}
else if (c == '#')
{
answer += Resolve(dots, blocks, index + 1, blockIndex, current + 1);
}
}
}

return seen.SequenceEqual(blocks);
dp.Add(key, answer);

return answer;
}
}
2 changes: 2 additions & 0 deletions 2023/Day12/input.refout
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
7939
850504257483930
53 changes: 26 additions & 27 deletions 2023/SplashScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,54 @@ public void Show() {

var color = Console.ForegroundColor;
Write(0xcc00, false, " ▄█▄ ▄▄█ ▄ ▄ ▄▄▄ ▄▄ ▄█▄ ▄▄▄ ▄█ ▄▄ ▄▄▄ ▄▄█ ▄▄▄\n █▄█ █ █ █ █ █▄█ █ █ █ █ █ █▄ ");
Write(0xcc00, false, " █ █ █ █ █ █▄█\n █ █ █▄█ ▀▄▀ █▄▄ █ █ █▄ █▄█ █ █▄ █▄█ █▄█ █▄▄ int y = 2023;\n ");
Write(0xcc00, false, " \n ");
Write(0xcc00, false, " █ █ █ █ █ █▄█\n █ █ █▄█ ▀▄▀ █▄▄ █ █ █▄ █▄█ █ █▄ █▄█ █▄█ █▄▄ // 2023\n \n ");
Write(0xcc00, false, " ");
Write(0x666666, false, " * ");
Write(0xcccccc, false, "14 ");
Write(0x666666, false, "**\n * ");
Write(0xcccccc, false, "15 ");
Write(0x666666, false, "**\n .");
Write(0xd4dde4, false, "..");
Write(0x666666, false, "**\n ");
Write(0xd4dde4, false, "...");
Write(0x666666, false, " * ");
Write(0xcccccc, false, "16 ");
Write(0x666666, false, "**\n ");
Write(0xd4dde4, false, ".'");
Write(0x666666, false, "'....' '.. * ");
Write(0xd4dde4, false, ".''....' '..");
Write(0x666666, false, " * ");
Write(0xcccccc, false, "13 ");
Write(0x666666, false, "**\n '.ZZ * ");
Write(0x666666, false, "**\n ");
Write(0xd4dde4, false, "'.ZZ");
Write(0x666666, false, " * ");
Write(0xcccccc, false, "17 ");
Write(0x666666, false, "**\n ");
Write(0xd4dde4, false, ".'");
Write(0x666666, false, "''' ZZ* ");
Write(0xd4dde4, false, ".");
Write(0x666666, false, "'''. ");
Write(0xd4dde4, false, "' ");
Write(0xd4dde4, false, ".'''' ZZ");
Write(0xffff66, true, "* ");
Write(0xd4dde4, false, ".'''. ' ");
Write(0xcccccc, false, "12 ");
Write(0xffff66, false, "*");
Write(0x666666, false, "*\n ");
Write(0xd4dde4, false, "'");
Write(0x333333, false, "...");
Write(0xd4dde4, false, ". '.");
Write(0x333333, false, "..' * ....");
Write(0xd4dde4, false, "' ");
Write(0x666666, false, "18\n ");
Write(0xffff66, false, "**\n ");
Write(0xd4dde4, false, "'.... '...'");
Write(0x666666, false, " * ");
Write(0xd4dde4, false, "....' ");
Write(0xcccccc, false, "18 ");
Write(0x666666, false, "**\n ");
Write(0xd4dde4, false, ".'");
Write(0xffff66, true, "*");
Write(0x666666, false, " '. ");
Write(0xffff66, true, "* ");
Write(0xd4dde4, false, "'. ");
Write(0xcccccc, false, "11 ");
Write(0xffff66, false, "**\n ");
Write(0xd4dde4, false, "'..''''.");
Write(0xffff66, true, "*");
Write(0xd4dde4, false, ".''");
Write(0x666666, false, "''");
Write(0xd4dde4, false, "..'");
Write(0xd4dde4, false, ".''''..'");
Write(0xe3b585, false, "' ''... ");
Write(0xcccccc, false, "10 ");
Write(0xffff66, false, "**\n ");
Write(0xe3b585, false, ".");
Write(0xd4dde4, false, "'''");
Write(0xe3b585, false, "~ ~ ~ ~ ");
Write(0xe3b585, false, "~ ~ ~ ~");
Write(0x333333, false, " * ");
Write(0x6b4d3b, false, "### ");
Write(0xe3b585, false, "''. \n .' ~ ");
Write(0xe3b585, false, "''. ");
Write(0x666666, false, "19\n ");
Write(0xe3b585, false, ".' ~ ");
Write(0xcc00, false, ",");
Write(0xffff66, true, "* ");
Write(0xe3b585, false, "~ ~ ~ ~ ");
Expand Down
Loading

0 comments on commit 5ba3bdd

Please sign in to comment.