-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay04.cs
More file actions
111 lines (94 loc) · 2.92 KB
/
Copy pathDay04.cs
File metadata and controls
111 lines (94 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace aoc2023;
public static class Day04
{
public static int step1()
{
var input = File.ReadAllLines("data\\aoc5.txt");
int total = 0;
foreach (var line in input)
{
var splits1 = line.Split(':');
var splits2 = splits1[1].Split('|');
var splitwin = splits2[0].Split(' ', StringSplitOptions.RemoveEmptyEntries);
var splitnums = splits2[1].Split(' ', StringSplitOptions.RemoveEmptyEntries);
int score = 0;
foreach(var num in splitwin)
{
int winnum = int.Parse(num);
foreach(var num2 in splitnums)
{
int thisnum = int.Parse(num2);
if(winnum == thisnum)
{
if(score == 0)
{
score = 1;
}
else
{
score *= 2;
}
}
}
}
total += score;
}
return total;
}
public static int step2()
{
var input = File.ReadAllLines("data\\aoc4.txt");
int total = 0;
int[] counts = new int[input.Length + 1000];
for (int x = 0; x < input.Length; x++)
{
counts[x] = 1;
}
int cp = 0;
foreach (var line in input)
{
int res = process(line); // moved here so solution is fast instead of taking like 20s.
for (int z = 0; z < counts[cp]; z++)
{
//int res = process(line); // original SLOW location :facepalm: but it worked.
for (int y = cp + 1; y < cp + res + 1; y++)
{
counts[y]++;
}
}
cp++;
}
for (int x = 0; x < input.Length; x++)
{
total += counts[x];
}
return total;
}
private static int process(string line)
{
var splits1 = line.Split(':');
var splits2 = splits1[1].Split('|');
var splitwin = splits2[0].Split(' ', StringSplitOptions.RemoveEmptyEntries);
var splitnums = splits2[1].Split(' ', StringSplitOptions.RemoveEmptyEntries);
int score = 0;
foreach (var num in splitwin)
{
int winnum = int.Parse(num);
foreach (var num2 in splitnums)
{
int thisnum = int.Parse(num2);
if (winnum == thisnum)
{
score++;
}
}
}
return score;
}
}