mirror of
https://github.com/seigler/aoc2023
synced 2025-07-25 13:56:10 +00:00
day 5
This commit is contained in:
parent
e038b96e6f
commit
fb96c0bcc4
4 changed files with 199 additions and 13 deletions
|
@ -63,16 +63,18 @@
|
|||
},
|
||||
{
|
||||
"part1": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "600279879",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 0.546875
|
||||
},
|
||||
"part2": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"solved": true,
|
||||
"result": "20191102",
|
||||
"attempts": [
|
||||
"20191103"
|
||||
],
|
||||
"time": 2.305333
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
12
README.md
12
README.md
|
@ -15,7 +15,7 @@
|
|||
[](src/day02)
|
||||
[](src/day03)
|
||||
[](src/day04)
|
||||

|
||||
[](src/day05)
|
||||

|
||||

|
||||

|
||||
|
@ -97,9 +97,9 @@ Both parts: 1.773ms
|
|||
|
||||
```
|
||||
Day 05
|
||||
Time part 1: -
|
||||
Time part 2: -
|
||||
Both parts: -
|
||||
Time part 1: 0.547ms
|
||||
Time part 2: 2.305ms
|
||||
Both parts: 2.852ms
|
||||
```
|
||||
|
||||
```
|
||||
|
@ -243,8 +243,8 @@ Both parts: -
|
|||
```
|
||||
|
||||
```
|
||||
Total stars: 7/50
|
||||
Total time: 7.478ms
|
||||
Total stars: 9/50
|
||||
Total time: 10.331ms
|
||||
```
|
||||
|
||||
<!--/RESULTS-->
|
||||
|
|
9
src/day05/README.md
Normal file
9
src/day05/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2023 - day 5 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2023/day/5)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
175
src/day05/index.ts
Normal file
175
src/day05/index.ts
Normal file
|
@ -0,0 +1,175 @@
|
|||
import run from "aocrunner"
|
||||
|
||||
const parseInput = (rawInput: string) => {
|
||||
const [seeds, ...maps] = rawInput.split("\n\n")
|
||||
return {
|
||||
seeds: seeds.split(": ")[1].split(" ").map(Number),
|
||||
maps: maps.map((map) =>
|
||||
map
|
||||
.split("map:\n")[1]
|
||||
.split("\n")
|
||||
.map((line) => line.split(" ").map(Number)),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
const part1 = (rawInput: string) => {
|
||||
const { seeds, maps } = parseInput(rawInput)
|
||||
let locations = seeds.map((seed) =>
|
||||
maps.reduce((id, map) => {
|
||||
for (const [to, from, span] of map) {
|
||||
if (id >= from && id < from + span) {
|
||||
return id - from + to
|
||||
}
|
||||
}
|
||||
return id
|
||||
}, seed),
|
||||
)
|
||||
return locations.sort((a, b) => a - b)[0]
|
||||
}
|
||||
|
||||
/** [start, end) */
|
||||
type Range = [number, number]
|
||||
|
||||
const getMapper = ([mapDestStart, mapSourceStart, mapLength]: number[]) => {
|
||||
return (range: Range) => {
|
||||
const [a, b] = range
|
||||
const c = mapSourceStart
|
||||
const d = mapSourceStart + mapLength
|
||||
/*
|
||||
a b
|
||||
==========
|
||||
==============
|
||||
c d
|
||||
*/
|
||||
const affected: Range = [
|
||||
Math.max(a, c) - mapSourceStart + mapDestStart,
|
||||
Math.min(b, d) - mapSourceStart + mapDestStart,
|
||||
]
|
||||
if (affected[1] - affected[0] <= 0) {
|
||||
return { affected: [], unaffected: [range] }
|
||||
}
|
||||
const unaffected: Range[] = []
|
||||
if (a < c) {
|
||||
unaffected.push([a, c])
|
||||
}
|
||||
if (b > d) {
|
||||
unaffected.push([d, b])
|
||||
}
|
||||
return {
|
||||
affected: [affected],
|
||||
unaffected,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const part2 = (rawInput: string) => {
|
||||
const { seeds, maps } = parseInput(rawInput)
|
||||
let seedRanges: Range[] = []
|
||||
for (let i = 0; i < seeds.length; i += 2) {
|
||||
seedRanges.push([seeds[i], seeds[i] + seeds[i + 1]])
|
||||
}
|
||||
let queue: Range[] = seedRanges
|
||||
for (const map of maps) {
|
||||
const mappers = map.map(getMapper)
|
||||
const affected: Range[] = []
|
||||
for (const mapper of mappers) {
|
||||
const unaffected: Range[] = []
|
||||
while (queue.length > 0) {
|
||||
const x = mapper(queue.shift()!)
|
||||
unaffected.push(...x.unaffected)
|
||||
affected.push(...x.affected)
|
||||
}
|
||||
queue.push(...unaffected)
|
||||
}
|
||||
queue.push(...affected)
|
||||
}
|
||||
return queue.sort((a, b) => a[0] - b[0])[0][0]
|
||||
}
|
||||
|
||||
run({
|
||||
part1: {
|
||||
tests: [
|
||||
{
|
||||
input: `seeds: 79 14 55 13
|
||||
|
||||
seed-to-soil map:
|
||||
50 98 2
|
||||
52 50 48
|
||||
|
||||
soil-to-fertilizer map:
|
||||
0 15 37
|
||||
37 52 2
|
||||
39 0 15
|
||||
|
||||
fertilizer-to-water map:
|
||||
49 53 8
|
||||
0 11 42
|
||||
42 0 7
|
||||
57 7 4
|
||||
|
||||
water-to-light map:
|
||||
88 18 7
|
||||
18 25 70
|
||||
|
||||
light-to-temperature map:
|
||||
45 77 23
|
||||
81 45 19
|
||||
68 64 13
|
||||
|
||||
temperature-to-humidity map:
|
||||
0 69 1
|
||||
1 0 69
|
||||
|
||||
humidity-to-location map:
|
||||
60 56 37
|
||||
56 93 4`,
|
||||
expected: 35,
|
||||
},
|
||||
],
|
||||
solution: part1,
|
||||
},
|
||||
part2: {
|
||||
tests: [
|
||||
{
|
||||
input: `seeds: 79 14 55 13
|
||||
|
||||
seed-to-soil map:
|
||||
50 98 2
|
||||
52 50 48
|
||||
|
||||
soil-to-fertilizer map:
|
||||
0 15 37
|
||||
37 52 2
|
||||
39 0 15
|
||||
|
||||
fertilizer-to-water map:
|
||||
49 53 8
|
||||
0 11 42
|
||||
42 0 7
|
||||
57 7 4
|
||||
|
||||
water-to-light map:
|
||||
88 18 7
|
||||
18 25 70
|
||||
|
||||
light-to-temperature map:
|
||||
45 77 23
|
||||
81 45 19
|
||||
68 64 13
|
||||
|
||||
temperature-to-humidity map:
|
||||
0 69 1
|
||||
1 0 69
|
||||
|
||||
humidity-to-location map:
|
||||
60 56 37
|
||||
56 93 4`,
|
||||
expected: 46,
|
||||
},
|
||||
],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
onlyTests: false,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue