Skip to content

Commit a6df90b

Browse files
Finished day 1
1 parent 4142c4f commit a6df90b

20 files changed

+250
-651
lines changed

2023/Day01/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,24 @@ In this example, the calibration values of these four lines are <code>12</code>,
2727
Consider your entire calibration document. <em>What is the sum of all of the calibration values?</em>
2828

2929

30+
## --- Part Two ---
31+
Your calculation isn't quite right. It looks like some of the digits are actually <em>spelled out with letters</em>: <code>one</code>, <code>two</code>, <code>three</code>, <code>four</code>, <code>five</code>, <code>six</code>, <code>seven</code>, <code>eight</code>, and <code>nine</code> <em>also</em> count as valid "digits".
32+
33+
Equipped with this new information, you now need to find the real first and last digit on each line. For example:
34+
35+
<pre>
36+
<code>two1nine
37+
eightwothree
38+
abcone2threexyz
39+
xtwone3four
40+
4nineeightseven2
41+
zoneight234
42+
7pqrstsixteen
43+
</code>
44+
</pre>
45+
46+
In this example, the calibration values are <code>29</code>, <code>83</code>, <code>13</code>, <code>24</code>, <code>42</code>, <code>14</code>, and <code>76</code>. Adding these together produces <code><em>281</em></code>.
47+
48+
<em>What is the sum of all of the calibration values?</em>
49+
50+

2023/Day01/Solution.cs

Lines changed: 164 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,172 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Collections.Immutable;
4-
using System.Linq;
5-
using System.Text.RegularExpressions;
6-
using System.Text;
7-
81
namespace AdventOfCode.Y2023.Day01;
92

103
[ProblemName("Trebuchet?!")]
11-
class Solution : Solver {
4+
class Solution : Solver
5+
{
6+
public object PartOne(string[] input)
7+
{
8+
int sum = 0;
9+
10+
foreach (string line in input)
11+
{
12+
string firstNumber = "";
13+
string lastNumber = "";
14+
15+
for (int i = 0; i < line.Length; i++)
16+
{
17+
if (firstNumber == "" && char.IsNumber(line[i]))
18+
{
19+
firstNumber = line[i].ToString();
20+
}
21+
int reverseIndex = line.Length - 1 - i;
22+
if (lastNumber == "" && char.IsNumber(line[reverseIndex]))
23+
{
24+
lastNumber = line[reverseIndex].ToString();
25+
}
26+
}
27+
28+
sum += int.TryParse(firstNumber + lastNumber, out int number) ? number : 0;
29+
}
30+
31+
return sum;
32+
}
33+
34+
public object PartTwo(string[] input)
35+
{
36+
int sum = 0;
37+
38+
foreach (string line in input)
39+
{
40+
string firstNumber = "";
41+
string lastNumber = "";
42+
43+
for (int i = 0; i < line.Length; i++)
44+
{
45+
if (firstNumber == "")
46+
{
47+
TryParseWrittenNumber(line, i, false, out firstNumber);
48+
49+
if (char.IsNumber(line[i]))
50+
{
51+
firstNumber = line[i].ToString();
52+
}
53+
else
54+
{
55+
TryParseWrittenNumber(line, i, false, out firstNumber);
56+
}
57+
}
58+
int reverseIndex = line.Length - 1 - i;
59+
if (lastNumber == "")
60+
{
61+
if (char.IsNumber(line[reverseIndex]))
62+
{
63+
lastNumber = line[reverseIndex].ToString();
64+
}
65+
else
66+
{
67+
TryParseWrittenNumber(line, reverseIndex, true, out lastNumber);
68+
}
69+
}
70+
}
71+
72+
sum += int.TryParse(firstNumber + lastNumber, out int number) ? number : 0;
73+
}
74+
75+
return sum;
76+
}
77+
78+
private bool TryParseWrittenNumber(string line, int startIndex, bool reversed, out string number)
79+
{
80+
number = "";
81+
bool hasRoomFor3 = line.Length >= startIndex + 3;
82+
bool hasReversedRoomFor3 = reversed && startIndex - 3 >= -1;
83+
bool hasRoomFor4 = line.Length >= startIndex + 4;
84+
bool hasReversedRoomFor4 = reversed && startIndex - 4 >= -1;
85+
bool hasRoomFor5 = line.Length >= startIndex + 5;
86+
bool hasReversedRoomFor5 = reversed && startIndex - 5 >= -1;
87+
88+
if (hasRoomFor3)
89+
{
90+
number = ParseWritten3LetterNumber(line.Substring(startIndex, 3));
91+
if (number != "")
92+
{
93+
return true;
94+
}
95+
}
96+
if (hasReversedRoomFor3)
97+
{
98+
number = ParseWritten3LetterNumber(line.Substring(startIndex - (3 - 1), 3));
99+
if (number != "")
100+
{
101+
return true;
102+
}
103+
}
104+
if (hasRoomFor4)
105+
{
106+
number = ParseWritten4LetterNumber(line.Substring(startIndex, 4));
107+
if (number != "")
108+
{
109+
return true;
110+
}
111+
}
112+
if (hasReversedRoomFor4)
113+
{
114+
number = ParseWritten4LetterNumber(line.Substring(startIndex - (4 - 1), 4));
115+
if (number != "")
116+
{
117+
return true;
118+
}
119+
}
120+
if (hasRoomFor5)
121+
{
122+
number = ParseWritten5LetterNumber(line.Substring(startIndex, 5));
123+
if (number != "")
124+
{
125+
return true;
126+
}
127+
}
128+
if (hasReversedRoomFor5)
129+
{
130+
number = ParseWritten5LetterNumber(line.Substring(startIndex - (5 - 1), 5));
131+
if (number != "")
132+
{
133+
return true;
134+
}
135+
}
136+
137+
return false;
138+
}
139+
140+
private string ParseWritten3LetterNumber(string line)
141+
{
142+
return line switch
143+
{
144+
"one" => "1",
145+
"two" => "2",
146+
"six" => "6",
147+
_ => ""
148+
};
149+
}
12150

13-
public object PartOne(string input) {
14-
return 0;
151+
private string ParseWritten4LetterNumber(string line)
152+
{
153+
return line switch
154+
{
155+
"four" => "4",
156+
"five" => "5",
157+
"nine" => "9",
158+
_ => ""
159+
};
15160
}
16161

17-
public object PartTwo(string input) {
18-
return 0;
162+
private string ParseWritten5LetterNumber(string line)
163+
{
164+
return line switch
165+
{
166+
"three" => "3",
167+
"seven" => "7",
168+
"eight" => "8",
169+
_ => ""
170+
};
19171
}
20172
}

2023/Day01/input.refout

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
55029
2+
55686

2023/Day01/input.refouts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
55029

2023/Day02/README.md

Lines changed: 0 additions & 32 deletions
This file was deleted.

2023/Day02/Solution.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

2023/Day02/input.in

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy