-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
91 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
## --- Day 7: Bridge Repair --- | ||
The Historians take you to a familiar [rope bridge](/2022/day/9) over a river in the middle of a jungle. The Chief isn't on this side of the bridge, though; maybe he's on the other side? | ||
|
||
When you go to cross the bridge, you notice a group of engineers trying to repair it. (Apparently, it breaks pretty frequently.) You won't be able to cross until it's fixed. | ||
|
||
You ask how long it'll take; the engineers tell you that it only needs final calibrations, but some young elephants were playing nearby and <em>stole all the operators</em> from their calibration equations! They could finish the calibrations if only someone could determine which test values could possibly be produced by placing any combination of operators into their calibration equations (your puzzle input). | ||
|
||
Read the [full puzzle](https://adventofcode.com/2024/day/7). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
namespace AdventOfCode.Y2024.Day07; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
|
||
[ProblemName("Bridge Repair")] | ||
class Solution : Solver { | ||
|
||
public object PartOne(string input) => Filter(input, Check1).Sum(); | ||
public object PartTwo(string input) => Filter(input, Check2).Sum(); | ||
|
||
private IEnumerable<long> Filter(string input, Func<long,long,List<long>, bool> check) => | ||
from line in input.Split("\n") | ||
let parts = Regex.Matches(line, @"\d+").Select(m=>long.Parse(m.Value)) | ||
let target = parts.First() | ||
let nums = parts.Skip(1).ToList() | ||
where check(target, 0, nums) | ||
select target; | ||
|
||
private bool Check1(long target, long acc, List<long> nums) => | ||
nums switch { | ||
[] => target == acc, | ||
_ => Check1(target, acc * nums[0], nums[1..]) || | ||
Check1(target, acc + nums[0], nums[1..]) | ||
}; | ||
|
||
private bool Check2(long target, long acc, List<long> nums) => | ||
nums switch { | ||
[] => target == acc, | ||
_ => Check2(target, acc * nums[0], nums[1..]) || | ||
Check2(target, acc + nums[0], nums[1..]) || | ||
Check2(target, long.Parse($"{acc}{nums[0]}"), nums[1..]) | ||
}; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
6231007345478 | ||
333027885676693 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.