FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
adventofcode/src/main/python/AoC2024_05.py at main · pareronia/adventofcode · GitHub
pareronia
/
adventofcode
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
0
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
adventofcode
/
src
/
main
/
python
/
AoC2024_05.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
109 lines (87 loc) · 2.02 KB
Breadcrumbs
adventofcode
/
src
/
main
/
python
/
AoC2024_05.py
Copy path
File metadata and controls
109 lines (87 loc) · 2.02 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#! /usr/bin/env python3
#
# Advent of Code 2024 Day 5
#
import
sys
from
collections
import
defaultdict
from
enum
import
Enum
from
enum
import
auto
from
enum
import
unique
from
functools
import
cmp_to_key
from
aoc
import
my_aocd
from
aoc
.
common
import
InputData
from
aoc
.
common
import
SolutionBase
from
aoc
.
common
import
aoc_samples
Input
=
tuple
[
dict
[
int
,
list
[
int
]],
list
[
list
[
int
]]]
Output1
=
int
Output2
=
int
TEST
=
"""
\
47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13
75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47
"""
@
unique
class
Mode
(
Enum
):
USE_CORRECT
=
auto
()
USE_INCORRECT
=
auto
()
class
Solution
(
SolutionBase
[
Input
,
Output1
,
Output2
]):
def
parse_input
(
self
,
input_data
:
InputData
)
->
Input
:
blocks
=
my_aocd
.
to_blocks
(
input_data
)
order
=
defaultdict
[
int
,
list
[
int
]](
list
)
for
line
in
blocks
[
0
]:
a
,
b
=
map
(
int
,
line
.
split
(
"|"
))
order
[
a
].
append
(
b
)
updates
=
[
list
(
map
(
int
,
line
.
split
(
","
)))
for
line
in
blocks
[
1
]]
return
order
,
updates
def
solve
(
self
,
inputs
:
Input
,
mode
:
Mode
)
->
int
:
order
,
updates
=
inputs
def
cmp
(
a
:
int
,
b
:
int
)
->
int
:
return
-
1
if
b
in
order
[
a
]
else
1
ans
=
0
for
update
in
updates
:
correct
=
update
[:]
correct
.
sort
(
key
=
cmp_to_key
(
cmp
))
if
not
((
mode
==
Mode
.
USE_CORRECT
)
^
(
update
==
correct
)):
ans
+=
correct
[
len
(
correct
)
//
2
]
return
ans
def
part_1
(
self
,
inputs
:
Input
)
->
Output1
:
return
self
.
solve
(
inputs
,
Mode
.
USE_CORRECT
)
def
part_2
(
self
,
inputs
:
Input
)
->
Output2
:
return
self
.
solve
(
inputs
,
Mode
.
USE_INCORRECT
)
@
aoc_samples
(
(
(
"part_1"
,
TEST
,
143
),
(
"part_2"
,
TEST
,
123
),
)
)
def
samples
(
self
)
->
None
:
pass
solution
=
Solution
(
2024
,
5
)
def
main
()
->
None
:
solution
.
run
(
sys
.
argv
)
if
__name__
==
"__main__"
:
main
()
Back
|
FazBrowse Home
|
New Git URL