Skip to content

Commit 368fad5

Browse files
01-2023
1 parent 95f537a commit 368fad5

File tree

8 files changed

+1229
-2
lines changed

8 files changed

+1229
-2
lines changed

2023/Day01/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
original source: [https://adventofcode.com/2023/day/1](https://adventofcode.com/2023/day/1)
2+
## --- Day 1: Trebuchet?! ---
3+
Something is wrong with global snow production, and you've been selected to take a look. The Elves have even given you a map; on it, they've used stars to mark the top fifty locations that are likely to be having problems.
4+
5+
You've been doing this long enough to know that to restore snow operations, you need to check all <em>fifty stars</em> by December 25th.
6+
7+
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants <em>one star</em>. Good luck!
8+
9+
You try to ask why they can't just use a [weather machine](/2015/day/1) ("not powerful enough") and where they're even sending you ("the sky") and why your map looks mostly blank ("you sure ask a lot of questions") and hang on did you just say the sky ("of course, where do you think snow comes from") when you realize that the Elves are already loading you into a [trebuchet](https://en.wikipedia.org/wiki/Trebuchet) ("please hold still, we need to strap you in").
10+
11+
As they're making the final adjustments, they discover that their calibration document (your puzzle input) has been <em>amended</em> by a very young Elf who was apparently just excited to show off her art skills. Consequently, the Elves are having trouble reading the values on the document.
12+
13+
The newly-improved calibration document consists of lines of text; each line originally contained a specific <em>calibration value</em> that the Elves now need to recover. On each line, the calibration value can be found by combining the <em>first digit</em> and the <em>last digit</em> (in that order) to form a single <em>two-digit number</em>.
14+
15+
For example:
16+
17+
<pre>
18+
<code>1abc2
19+
pqr3stu8vwx
20+
a1b2c3d4e5f
21+
treb7uchet
22+
</code>
23+
</pre>
24+
25+
In this example, the calibration values of these four lines are <code>12</code>, <code>38</code>, <code>15</code>, and <code>77</code>. Adding these together produces <code><em>142</em></code>.
26+
27+
Consider your entire calibration document. <em>What is the sum of all of the calibration values?</em>
28+
29+
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: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.Immutable;
4+
using System.Diagnostics;
5+
using System.Linq;
6+
using System.Text.RegularExpressions;
7+
using System.Text;
8+
using Microsoft.CodeAnalysis.Text;
9+
10+
namespace AdventOfCode.Y2023.Day01;
11+
12+
[ProblemName("Trebuchet?!")]
13+
class Solution : Solver {
14+
15+
public object PartOne(string input)
16+
{
17+
var data = input.Split("\n");
18+
var results = new List<int>();
19+
20+
foreach (var d in data)
21+
{
22+
var numbers = string.Concat(d.Where(char.IsNumber));
23+
if (numbers.Length == 1)
24+
{
25+
results.Add(int.Parse($"{numbers}{numbers}"));
26+
continue;
27+
}
28+
29+
results.Add(int.Parse($"{numbers[0]}{numbers[^1]}"));
30+
}
31+
32+
return results.Sum();
33+
}
34+
35+
public object PartTwo(string input)
36+
{
37+
var result = 0;
38+
var data = input.Split("\n");
39+
40+
foreach (var d in data)
41+
{
42+
List<int> numbers = new();
43+
for (int i = 0; i < d.Length; i++)
44+
{
45+
if (char.IsNumber(d[i]))
46+
{
47+
numbers.Add(int.Parse(d[i].ToString()));
48+
continue;
49+
}
50+
51+
foreach (var (text, number) in textNumbers)
52+
{
53+
// Si on trouve un des nombres en littéral, on ajoute la valeur correspondante
54+
if (i + text.Length - 1 < d.Length && d[i..(i + text.Length)] == text)
55+
{
56+
numbers.Add(number);
57+
break;
58+
}
59+
}
60+
}
61+
62+
result += int.Parse($"{numbers[0]}{numbers[^1]}");
63+
}
64+
65+
return result;
66+
}
67+
68+
private readonly Dictionary<string, int> textNumbers = new()
69+
{
70+
{"one", 1},
71+
{"two", 2},
72+
{"three", 3},
73+
{"four", 4},
74+
{"five", 5},
75+
{"six", 6},
76+
{"seven", 7},
77+
{"eight", 8},
78+
{"nine", 9}
79+
};
80+
}

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